blob: d5d95fb85a6ad6abdb6981a0289f8d6d4f8a107a [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;
Griff Hazen61a9e862014-05-22 16:05:19 -070071import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080072import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080073import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070074import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070075import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076import android.widget.RemoteViews;
77
Griff Hazen959591e2014-05-15 22:26:18 -070078import com.android.internal.R;
Selim Cinek389edcd2017-05-11 19:16:44 -070079import com.android.internal.annotations.VisibleForTesting;
Svet Ganovddb94882016-06-23 19:55:24 -070080import com.android.internal.util.ArrayUtils;
Griff Hazenc091ba82014-05-16 10:13:26 -070081import com.android.internal.util.NotificationColorUtil;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -080082import com.android.internal.util.Preconditions;
Griff Hazen959591e2014-05-15 22:26:18 -070083
Tor Norbyed9273d62013-05-30 15:59:53 -070084import java.lang.annotation.Retention;
85import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020086import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050087import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070088import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070089import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070090import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050091import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080092
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093/**
94 * A class that represents how a persistent notification is to be presented to
95 * the user using the {@link android.app.NotificationManager}.
96 *
Joe Onoratocb109a02011-01-18 17:57:41 -080097 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
98 * easier to construct Notifications.</p>
99 *
Joe Fernandez558459f2011-10-13 16:47:36 -0700100 * <div class="special reference">
101 * <h3>Developer Guides</h3>
102 * <p>For a guide to creating notifications, read the
103 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
104 * developer guide.</p>
105 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800106 */
107public class Notification implements Parcelable
108{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400109 private static final String TAG = "Notification";
110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400112 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400113 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400114 */
115 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
116 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
117 = "android.intent.category.NOTIFICATION_PREFERENCES";
118
119 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500120 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
121 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400122 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500123 */
124 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
125
126 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400127 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds005c8b92017-08-24 10:35:53 -0400128 * contain a {@link NotificationChannelGroup#getId() group id} that can be used to narrow down
129 * what settings should be shown in the target app.
130 */
131 public static final String EXTRA_CHANNEL_GROUP_ID = "android.intent.extra.CHANNEL_GROUP_ID";
132
133 /**
134 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
Julia Reynolds3aedded2017-03-31 14:42:09 -0400135 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
136 * that can be used to narrow down what settings should be shown in the target app.
137 */
138 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
139
140 /**
141 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
142 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
143 * that can be used to narrow down what settings should be shown in the target app.
144 */
145 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
146
147 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 * Use all default values (where applicable).
149 */
150 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500151
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 /**
153 * Use the default notification sound. This will ignore any given
154 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500155 *
Chris Wren47c20a12014-06-18 17:27:29 -0400156 * <p>
157 * A notification that is noisy is more likely to be presented as a heads-up notification.
158 * </p>
159 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500161 */
162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 public static final int DEFAULT_SOUND = 1;
164
165 /**
166 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500167 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700168 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500169 *
Chris Wren47c20a12014-06-18 17:27:29 -0400170 * <p>
171 * A notification that vibrates is more likely to be presented as a heads-up notification.
172 * </p>
173 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500175 */
176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 /**
180 * Use the default notification lights. This will ignore the
181 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
182 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500183 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500185 */
186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500188
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200190 * Maximum length of CharSequences accepted by Builder and friends.
191 *
192 * <p>
193 * Avoids spamming the system with overly large strings such as full e-mails.
194 */
195 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
196
197 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800198 * Maximum entries of reply text that are accepted by Builder and friends.
199 */
200 private static final int MAX_REPLY_HISTORY = 5;
201
202 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500203 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800204 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500205 * Default value: {@link System#currentTimeMillis() Now}.
206 *
207 * Choose a timestamp that will be most relevant to the user. For most finite events, this
208 * corresponds to the time the event happened (or will happen, in the case of events that have
209 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800210 * timestamped according to when the activity began.
211 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500212 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800213 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500214 * <ul>
215 * <li>Notification of a new chat message should be stamped when the message was received.</li>
216 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
217 * <li>Notification of a completed file download should be stamped when the download finished.</li>
218 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
219 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
220 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800221 * </ul>
222 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700223 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
224 * anymore by default and must be opted into by using
225 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 */
227 public long when;
228
229 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700230 * The creation time of the notification
231 */
232 private long creationTime;
233
234 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400236 *
237 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 */
Dan Sandler86647982015-05-13 23:41:13 -0400239 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700240 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800241 public int icon;
242
243 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800244 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
245 * leave it at its default value of 0.
246 *
247 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700248 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800249 */
250 public int iconLevel;
251
252 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500253 * The number of events that this notification represents. For example, in a new mail
254 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800255 *
Julia Reynolds30331982017-04-27 10:12:50 -0400256 * The system may or may not use this field to modify the appearance of the notification.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500257 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
258 * badge icon in Launchers that support badging.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 */
Julia Reynolds30331982017-04-27 10:12:50 -0400260 public int number = 0;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800261
262 /**
263 * The intent to execute when the expanded status entry is clicked. If
264 * this is an activity, it must include the
265 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800266 * that you take care of task management as described in the
267 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800268 * Stack</a> document. In particular, make sure to read the notification section
269 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
270 * Notifications</a> for the correct ways to launch an application from a
271 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 */
273 public PendingIntent contentIntent;
274
275 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500276 * The intent to execute when the notification is explicitly dismissed by the user, either with
277 * the "Clear All" button or by swiping it away individually.
278 *
279 * This probably shouldn't be launching an activity since several of those will be sent
280 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 */
282 public PendingIntent deleteIntent;
283
284 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700285 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800286 *
Chris Wren47c20a12014-06-18 17:27:29 -0400287 * <p>
288 * The system UI may choose to display a heads-up notification, instead of
289 * launching this intent, while the user is using the device.
290 * </p>
291 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800292 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400293 */
294 public PendingIntent fullScreenIntent;
295
296 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400297 * Text that summarizes this notification for accessibility services.
298 *
299 * As of the L release, this text is no longer shown on screen, but it is still useful to
300 * accessibility services (where it serves as an audible announcement of the notification's
301 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400302 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800303 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 */
305 public CharSequence tickerText;
306
307 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400308 * Formerly, a view showing the {@link #tickerText}.
309 *
310 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400311 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400312 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800313 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400314
315 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400316 * The view that will represent this notification in the notification list (which is pulled
317 * down from the status bar).
318 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500319 * As of N, this field may be null. The notification view is determined by the inputs
320 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400321 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400323 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 public RemoteViews contentView;
325
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400326 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400327 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400328 * opportunity to show more detail. The system UI may choose to show this
329 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400330 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500331 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400332 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
333 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400334 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400335 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400336 public RemoteViews bigContentView;
337
Chris Wren8fd39ec2014-02-27 17:43:26 -0500338
339 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400340 * A medium-format version of {@link #contentView}, providing the Notification an
341 * opportunity to add action buttons to contentView. At its discretion, the system UI may
342 * choose to show this as a heads-up notification, which will pop up so the user can see
343 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400344 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500345 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400346 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
347 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500348 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400349 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500350 public RemoteViews headsUpContentView;
351
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400352 /**
Dan Sandler86647982015-05-13 23:41:13 -0400353 * A large bitmap to be shown in the notification content area.
354 *
355 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 */
Dan Sandler86647982015-05-13 23:41:13 -0400357 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800358 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359
360 /**
361 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500362 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400364 * A notification that is noisy is more likely to be presented as a heads-up notification.
365 * </p>
366 *
367 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500368 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800369 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500370 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800371 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500372 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800373 public Uri sound;
374
375 /**
376 * Use this constant as the value for audioStreamType to request that
377 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700378 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400379 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500380 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800381 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700382 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800383 public static final int STREAM_DEFAULT = -1;
384
385 /**
386 * The audio stream type to use when playing the sound.
387 * Should be one of the STREAM_ constants from
388 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400389 *
390 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700392 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 public int audioStreamType = STREAM_DEFAULT;
394
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400396 * The default value of {@link #audioAttributes}.
397 */
398 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
399 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
400 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
401 .build();
402
403 /**
404 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500405 *
406 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400407 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500408 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400409 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
410
411 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500412 * The pattern with which to vibrate.
413 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 * <p>
415 * To vibrate the default pattern, see {@link #defaults}.
416 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500417 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800418 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500419 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800420 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500421 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 public long[] vibrate;
423
424 /**
425 * The color of the led. The hardware will do its best approximation.
426 *
427 * @see #FLAG_SHOW_LIGHTS
428 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500429 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800430 */
Tor Norbye80756e32015-03-02 09:39:27 -0800431 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500432 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 public int ledARGB;
434
435 /**
436 * The number of milliseconds for the LED to be on while it's flashing.
437 * The hardware will do its best approximation.
438 *
439 * @see #FLAG_SHOW_LIGHTS
440 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500441 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500443 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 public int ledOnMS;
445
446 /**
447 * The number of milliseconds for the LED to be off 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 *
453 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500455 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800456 public int ledOffMS;
457
458 /**
459 * Specifies which values should be taken from the defaults.
460 * <p>
461 * To set, OR the desired from {@link #DEFAULT_SOUND},
462 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
463 * values, use {@link #DEFAULT_ALL}.
464 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500465 *
466 * @deprecated use {@link NotificationChannel#getSound()} and
467 * {@link NotificationChannel#shouldShowLights()} and
468 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500470 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800471 public int defaults;
472
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 /**
474 * Bit to be bitwise-ored into the {@link #flags} field that should be
475 * set if you want the LED on for this notification.
476 * <ul>
477 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
478 * or 0 for both ledOnMS and ledOffMS.</li>
479 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
480 * <li>To flash the LED, pass the number of milliseconds that it should
481 * be on and off to ledOnMS and ledOffMS.</li>
482 * </ul>
483 * <p>
484 * Since hardware varies, you are not guaranteed that any of the values
485 * you pass are honored exactly. Use the system defaults (TODO) if possible
486 * because they will be set to values that work on any given hardware.
487 * <p>
488 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500489 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500490 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500492 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
494
495 /**
496 * Bit to be bitwise-ored into the {@link #flags} field that should be
497 * set if this notification is in reference to something that is ongoing,
498 * like a phone call. It should not be set if this notification is in
499 * reference to something that happened at a particular point in time,
500 * like a missed phone call.
501 */
502 public static final int FLAG_ONGOING_EVENT = 0x00000002;
503
504 /**
505 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700506 * the audio will be repeated until the notification is
507 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 */
509 public static final int FLAG_INSISTENT = 0x00000004;
510
511 /**
512 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700513 * set if you would only like the sound, vibrate and ticker to be played
514 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800515 */
516 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
517
518 /**
519 * Bit to be bitwise-ored into the {@link #flags} field that should be
520 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500521 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800522 */
523 public static final int FLAG_AUTO_CANCEL = 0x00000010;
524
525 /**
526 * Bit to be bitwise-ored into the {@link #flags} field that should be
527 * set if the notification should not be canceled when the user clicks
528 * the Clear all button.
529 */
530 public static final int FLAG_NO_CLEAR = 0x00000020;
531
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700532 /**
533 * Bit to be bitwise-ored into the {@link #flags} field that should be
534 * set if this notification represents a currently running service. This
535 * will normally be set for you by {@link Service#startForeground}.
536 */
537 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
538
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400539 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500540 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800541 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500542 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400543 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700544 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500545 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400546
Griff Hazendfcb0802014-02-11 12:00:00 -0800547 /**
548 * Bit to be bitswise-ored into the {@link #flags} field that should be
549 * set if this notification is relevant to the current device only
550 * and it is not recommended that it bridge to other devices.
551 */
552 public static final int FLAG_LOCAL_ONLY = 0x00000100;
553
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700554 /**
555 * Bit to be bitswise-ored into the {@link #flags} field that should be
556 * set if this notification is the group summary for a group of notifications.
557 * Grouped notifications may display in a cluster or stack on devices which
558 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
559 */
560 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
561
Julia Reynoldse46bb372016-03-17 11:05:58 -0400562 /**
563 * Bit to be bitswise-ored into the {@link #flags} field that should be
564 * set if this notification is the group summary for an auto-group of notifications.
565 *
566 * @hide
567 */
568 @SystemApi
569 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
570
Julia Reynolds4db59552017-06-30 13:34:01 -0400571 /**
572 * @hide
573 */
574 public static final int FLAG_CAN_COLORIZE = 0x00000800;
575
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 public int flags;
577
Tor Norbyed9273d62013-05-30 15:59:53 -0700578 /** @hide */
579 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
580 @Retention(RetentionPolicy.SOURCE)
581 public @interface Priority {}
582
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500584 * Default notification {@link #priority}. If your application does not prioritize its own
585 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500586 *
587 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500588 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500589 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500590 public static final int PRIORITY_DEFAULT = 0;
591
592 /**
593 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
594 * items smaller, or at a different position in the list, compared with your app's
595 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500596 *
597 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500598 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500599 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500600 public static final int PRIORITY_LOW = -1;
601
602 /**
603 * Lowest {@link #priority}; these items might not be shown to the user except under special
604 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500605 *
606 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500607 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500608 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500609 public static final int PRIORITY_MIN = -2;
610
611 /**
612 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
613 * show these items larger, or at a different position in notification lists, compared with
614 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500615 *
616 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500617 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500618 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500619 public static final int PRIORITY_HIGH = 1;
620
621 /**
622 * Highest {@link #priority}, for your application's most important items that require the
623 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500624 *
625 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500626 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500627 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500628 public static final int PRIORITY_MAX = 2;
629
630 /**
631 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800632 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500633 * Priority is an indication of how much of the user's valuable attention should be consumed by
634 * this notification. Low-priority notifications may be hidden from the user in certain
635 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500636 * system will make a determination about how to interpret this priority when presenting
637 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400638 *
639 * <p>
640 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
641 * as a heads-up notification.
642 * </p>
643 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500644 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500645 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700646 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500647 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500648 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800649
Dan Sandler26e81cf2014-05-06 10:01:27 -0400650 /**
651 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
652 * to be applied by the standard Style templates when presenting this notification.
653 *
654 * The current template design constructs a colorful header image by overlaying the
655 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
656 * ignored.
657 */
Tor Norbye80756e32015-03-02 09:39:27 -0800658 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400659 public int color = COLOR_DEFAULT;
660
661 /**
662 * Special value of {@link #color} telling the system not to decorate this notification with
663 * any special color but instead use default colors when presenting this notification.
664 */
Tor Norbye80756e32015-03-02 09:39:27 -0800665 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400666 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600667
668 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800669 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800670 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800671 */
672 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800673 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800674
675 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700676 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
677 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600678 * lockscreen).
679 *
680 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
681 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
682 * shown in all situations, but the contents are only available if the device is unlocked for
683 * the appropriate user.
684 *
685 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
686 * can be read even in an "insecure" context (that is, above a secure lockscreen).
687 * To modify the public version of this notification—for example, to redact some portions—see
688 * {@link Builder#setPublicVersion(Notification)}.
689 *
690 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
691 * and ticker until the user has bypassed the lockscreen.
692 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600693 public @Visibility int visibility;
694
695 /** @hide */
696 @IntDef(prefix = { "VISIBILITY_" }, value = {
697 VISIBILITY_PUBLIC,
698 VISIBILITY_PRIVATE,
699 VISIBILITY_SECRET,
700 })
701 @Retention(RetentionPolicy.SOURCE)
702 public @interface Visibility {}
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600703
Griff Hazenfc3922d2014-08-20 11:56:44 -0700704 /**
705 * Notification visibility: Show this notification in its entirety on all lockscreens.
706 *
707 * {@see #visibility}
708 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600709 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700710
711 /**
712 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
713 * private information on secure lockscreens.
714 *
715 * {@see #visibility}
716 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600717 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700718
719 /**
720 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
721 *
722 * {@see #visibility}
723 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600724 public static final int VISIBILITY_SECRET = -1;
725
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500726 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400727 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500728 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400729 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500730
731 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400732 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500733 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400734 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500735
736 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400737 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500738 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400739 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500740
741 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400742 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500743 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400744 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500745
746 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400747 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500748 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400749 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500750
751 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400752 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500753 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400754 public static final String CATEGORY_ALARM = "alarm";
755
756 /**
757 * Notification category: progress of a long-running background operation.
758 */
759 public static final String CATEGORY_PROGRESS = "progress";
760
761 /**
762 * Notification category: social network or sharing update.
763 */
764 public static final String CATEGORY_SOCIAL = "social";
765
766 /**
767 * Notification category: error in background operation or authentication status.
768 */
769 public static final String CATEGORY_ERROR = "err";
770
771 /**
772 * Notification category: media transport control for playback.
773 */
774 public static final String CATEGORY_TRANSPORT = "transport";
775
776 /**
777 * Notification category: system or device status update. Reserved for system use.
778 */
779 public static final String CATEGORY_SYSTEM = "sys";
780
781 /**
782 * Notification category: indication of running background service.
783 */
784 public static final String CATEGORY_SERVICE = "service";
785
786 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400787 * Notification category: a specific, timely recommendation for a single thing.
788 * For example, a news app might want to recommend a news story it believes the user will
789 * want to read next.
790 */
791 public static final String CATEGORY_RECOMMENDATION = "recommendation";
792
793 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400794 * Notification category: ongoing information about device or contextual status.
795 */
796 public static final String CATEGORY_STATUS = "status";
797
798 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400799 * Notification category: user-scheduled reminder.
800 */
801 public static final String CATEGORY_REMINDER = "reminder";
802
803 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400804 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
805 * that best describes this Notification. May be used by the system for ranking and filtering.
806 */
807 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500808
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700809 private String mGroupKey;
810
811 /**
812 * Get the key used to group this notification into a cluster or stack
813 * with other notifications on devices which support such rendering.
814 */
815 public String getGroup() {
816 return mGroupKey;
817 }
818
819 private String mSortKey;
820
821 /**
822 * Get a sort key that orders this notification among other notifications from the
823 * same package. This can be useful if an external sort was already applied and an app
824 * would like to preserve this. Notifications will be sorted lexicographically using this
825 * value, although providing different priorities in addition to providing sort key may
826 * cause this value to be ignored.
827 *
828 * <p>This sort key can also be used to order members of a notification group. See
829 * {@link Builder#setGroup}.
830 *
831 * @see String#compareTo(String)
832 */
833 public String getSortKey() {
834 return mSortKey;
835 }
836
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500837 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400838 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400839 * <p>
840 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
841 * APIs, and are intended to be used by
842 * {@link android.service.notification.NotificationListenerService} implementations to extract
843 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500844 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400845 public Bundle extras = new Bundle();
846
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400847 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700848 * All pending intents in the notification as the system needs to be able to access them but
849 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700850 * custom parcelable objects.
851 *
852 * @hide
853 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700854 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700855
856 /**
Dianne Hackborn98305522017-05-05 17:53:53 -0700857 * Token identifying the notification that is applying doze/bgcheck whitelisting to the
858 * pending intents inside of it, so only those will get the behavior.
859 *
860 * @hide
861 */
Adrian Roosfb921842017-10-26 14:49:56 +0200862 private IBinder mWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -0700863
864 /**
865 * Must be set by a process to start associating tokens with Notification objects
866 * coming in to it. This is set by NotificationManagerService.
867 *
868 * @hide
869 */
870 static public IBinder processWhitelistToken;
871
872 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400873 * {@link #extras} key: this is the title of the notification,
874 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
875 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500876 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400877
878 /**
879 * {@link #extras} key: this is the title of the notification when shown in expanded form,
880 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
881 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400882 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400883
884 /**
885 * {@link #extras} key: this is the main text payload, as supplied to
886 * {@link Builder#setContentText(CharSequence)}.
887 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500888 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400889
890 /**
891 * {@link #extras} key: this is a third line of text, as supplied to
892 * {@link Builder#setSubText(CharSequence)}.
893 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400894 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400895
896 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800897 * {@link #extras} key: this is the remote input history, as supplied to
898 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700899 *
900 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
901 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
902 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
903 * notifications once the other party has responded).
904 *
905 * The extra with this key is of type CharSequence[] and contains the most recent entry at
906 * the 0 index, the second most recent at the 1 index, etc.
907 *
908 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800909 */
910 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
911
912 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400913 * {@link #extras} key: this is a small piece of additional text as supplied to
914 * {@link Builder#setContentInfo(CharSequence)}.
915 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400916 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400917
918 /**
919 * {@link #extras} key: this is a line of summary information intended to be shown
920 * alongside expanded notifications, as supplied to (e.g.)
921 * {@link BigTextStyle#setSummaryText(CharSequence)}.
922 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400923 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400924
925 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200926 * {@link #extras} key: this is the longer text shown in the big form of a
927 * {@link BigTextStyle} notification, as supplied to
928 * {@link BigTextStyle#bigText(CharSequence)}.
929 */
930 public static final String EXTRA_BIG_TEXT = "android.bigText";
931
932 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400933 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
934 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400935 *
936 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400937 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400938 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500939 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400940
941 /**
942 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
943 * notification payload, as
944 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400945 *
946 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400947 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400948 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400949 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400950
951 /**
952 * {@link #extras} key: this is a bitmap to be used instead of the one from
953 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
954 * shown in its expanded form, as supplied to
955 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
956 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400957 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400958
959 /**
960 * {@link #extras} key: this is the progress value supplied to
961 * {@link Builder#setProgress(int, int, boolean)}.
962 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400963 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400964
965 /**
966 * {@link #extras} key: this is the maximum value supplied to
967 * {@link Builder#setProgress(int, int, boolean)}.
968 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400969 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400970
971 /**
972 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
973 * {@link Builder#setProgress(int, int, boolean)}.
974 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400975 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400976
977 /**
978 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
979 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
980 * {@link Builder#setUsesChronometer(boolean)}.
981 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400982 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400983
984 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800985 * {@link #extras} key: whether the chronometer set on the notification should count down
986 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -0700987 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -0800988 */
Adrian Roos96b7e202016-05-17 13:50:38 -0700989 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -0800990
991 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400992 * {@link #extras} key: whether {@link #when} should be shown,
993 * as supplied to {@link Builder#setShowWhen(boolean)}.
994 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400995 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400996
997 /**
998 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
999 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
1000 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001001 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001002
1003 /**
1004 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
1005 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
1006 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001007 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -04001008
1009 /**
1010 * {@link #extras} key: A string representing the name of the specific
1011 * {@link android.app.Notification.Style} used to create this notification.
1012 */
Chris Wren91ad5632013-06-05 15:05:57 -04001013 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001014
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001015 /**
Chris Wrene6c48932014-09-29 17:19:27 -04001016 * {@link #extras} key: A String array containing the people that this notification relates to,
1017 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001018 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001019 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001020
1021 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04001022 * Allow certain system-generated notifications to appear before the device is provisioned.
1023 * Only available to notifications coming from the android package.
1024 * @hide
1025 */
Maurice Lam96c10032017-03-29 15:42:38 -07001026 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001027 @RequiresPermission(android.Manifest.permission.NOTIFICATION_DURING_SETUP)
John Spurlockfd7f1e02014-03-18 16:41:57 -04001028 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
1029
1030 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -07001031 * {@link #extras} key: A
1032 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
1033 * in the background when the notification is selected. The URI must point to an image stream
1034 * suitable for passing into
1035 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
1036 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
1037 * URI used for this purpose must require no permissions to read the image data.
1038 */
1039 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1040
1041 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001042 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001043 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001044 * {@link android.app.Notification.MediaStyle} notification.
1045 */
1046 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1047
1048 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001049 * {@link #extras} key: the indices of actions to be shown in the compact view,
1050 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1051 */
1052 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1053
Christoph Studer943aa672014-08-03 20:31:16 +02001054 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001055 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1056 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001057 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1058 * {@link CharSequence}
Alex Hillsfc737de2016-03-23 17:33:02 -04001059 */
1060 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1061
1062 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001063 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001064 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001065 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001066 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001067
1068 /**
1069 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1070 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001071 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1072 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001073 */
1074 public static final String EXTRA_MESSAGES = "android.messages";
1075
1076 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001077 * {@link #extras} key: an array of
1078 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1079 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1080 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1081 * array of bundles.
1082 */
1083 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1084
1085 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001086 * {@link #extras} key: whether the notification should be colorized as
1087 * supplied to {@link Builder#setColorized(boolean)}}.
1088 */
1089 public static final String EXTRA_COLORIZED = "android.colorized";
1090
1091 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001092 * @hide
1093 */
1094 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1095
Selim Cinek247fa012016-02-18 09:50:48 -08001096 /**
1097 * @hide
1098 */
1099 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1100
Shane Brennan472a3b32016-12-12 15:28:10 -08001101 /**
Selim Cinekd0426622017-07-11 13:19:59 +02001102 * @hide
1103 */
1104 public static final String EXTRA_REDUCED_IMAGES = "android.reduced.images";
1105
1106 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001107 * {@link #extras} key: the audio contents of this notification.
1108 *
1109 * This is for use when rendering the notification on an audio-focused interface;
1110 * the audio contents are a complete sound sample that contains the contents/body of the
1111 * notification. This may be used in substitute of a Text-to-Speech reading of the
1112 * notification. For example if the notification represents a voice message this should point
1113 * to the audio of that message.
1114 *
1115 * The data stored under this key should be a String representation of a Uri that contains the
1116 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1117 *
1118 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1119 * has a field for holding data URI. That field can be used for audio.
1120 * See {@code Message#setData}.
1121 *
1122 * Example usage:
1123 * <pre>
1124 * {@code
1125 * Notification.Builder myBuilder = (build your Notification as normal);
1126 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1127 * }
1128 * </pre>
1129 */
1130 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1131
Dan Sandler80eaa592016-04-14 23:34:54 -04001132 /** @hide */
1133 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06001134 @RequiresPermission(android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME)
Dan Sandler732bd6c2016-04-12 14:20:32 -04001135 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1136
Dianne Hackbornfb5d4b52017-05-16 17:03:44 -07001137 /**
1138 * This is set on the notification shown by the activity manager about all apps
1139 * running in the background. It indicates that the notification should be shown
1140 * only if any of the given apps do not already have a {@link #FLAG_FOREGROUND_SERVICE}
1141 * notification currently visible to the user. This is a string array of all
1142 * package names of the apps.
1143 * @hide
1144 */
1145 public static final String EXTRA_FOREGROUND_APPS = "android.foregroundApps";
1146
Dan Sandlerd63f9322015-05-06 15:18:49 -04001147 private Icon mSmallIcon;
1148 private Icon mLargeIcon;
1149
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001150 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001151 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001152
Julia Reynolds13d898c2017-02-02 12:22:05 -05001153 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001154 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001155
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001156 /** @hide */
1157 @IntDef(prefix = { "GROUP_ALERT_" }, value = {
1158 GROUP_ALERT_ALL, GROUP_ALERT_CHILDREN, GROUP_ALERT_SUMMARY
1159 })
1160 @Retention(RetentionPolicy.SOURCE)
1161 public @interface GroupAlertBehavior {}
1162
1163 /**
1164 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all notifications in a
1165 * group with sound or vibration ought to make sound or vibrate (respectively), so this
1166 * notification will not be muted when it is in a group.
1167 */
1168 public static final int GROUP_ALERT_ALL = 0;
1169
1170 /**
1171 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that all children
1172 * notification in a group should be silenced (no sound or vibration) even if they are posted
1173 * to a {@link NotificationChannel} that has sound and/or vibration. Use this constant to
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001174 * mute this notification if this notification is a group child. This must be applied to all
1175 * children notifications you want to mute.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001176 *
1177 * <p> For example, you might want to use this constant if you post a number of children
1178 * notifications at once (say, after a periodic sync), and only need to notify the user
1179 * audibly once.
1180 */
1181 public static final int GROUP_ALERT_SUMMARY = 1;
1182
1183 /**
1184 * Constant for {@link Builder#setGroupAlertBehavior(int)}, meaning that the summary
1185 * notification in a group should be silenced (no sound or vibration) even if they are
1186 * posted to a {@link NotificationChannel} that has sound and/or vibration. Use this constant
1187 * to mute this notification if this notification is a group summary.
1188 *
1189 * <p>For example, you might want to use this constant if only the children notifications
Julia Reynolds399d9bf2017-08-11 12:52:14 -04001190 * in your group have content and the summary is only used to visually group notifications
1191 * rather than to alert the user that new information is available.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001192 */
1193 public static final int GROUP_ALERT_CHILDREN = 2;
1194
Julia Reynolds2f431e22017-06-07 14:12:09 +00001195 private int mGroupAlertBehavior = GROUP_ALERT_ALL;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001196
Julia Reynolds13d898c2017-02-02 12:22:05 -05001197 /**
1198 * If this notification is being shown as a badge, always show as a number.
1199 */
1200 public static final int BADGE_ICON_NONE = 0;
1201
1202 /**
1203 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1204 * represent this notification.
1205 */
1206 public static final int BADGE_ICON_SMALL = 1;
1207
1208 /**
1209 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1210 * represent this notification.
1211 */
1212 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001213 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001214
Chris Wren51c75102013-07-16 20:49:17 -04001215 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001216 * Structure to encapsulate a named action that can be shown as part of this notification.
1217 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1218 * selected by the user.
1219 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001220 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1221 * or {@link Notification.Builder#addAction(Notification.Action)}
1222 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001223 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001224 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001225 /**
1226 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1227 * {@link RemoteInput}s.
1228 *
1229 * This is intended for {@link RemoteInput}s that only accept data, meaning
1230 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1231 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1232 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1233 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1234 *
1235 * You can test if a RemoteInput matches these constraints using
1236 * {@link RemoteInput#isDataOnly}.
1237 */
1238 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1239
Griff Hazen959591e2014-05-15 22:26:18 -07001240 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001241 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001242 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001243 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001244
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001245 /**
1246 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001247 *
1248 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001249 */
Dan Sandler86647982015-05-13 23:41:13 -04001250 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001251 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001252
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001253 /**
1254 * Title of the action.
1255 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001256 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001257
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001258 /**
1259 * Intent to send when the user invokes this action. May be null, in which case the action
1260 * may be rendered in a disabled presentation by the system UI.
1261 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001262 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001263
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001264 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001265 if (in.readInt() != 0) {
1266 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001267 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1268 icon = mIcon.getResId();
1269 }
Dan Sandler86647982015-05-13 23:41:13 -04001270 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001271 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1272 if (in.readInt() == 1) {
1273 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1274 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001275 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001276 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001277 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001278 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001279
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001280 /**
Dan Sandler86647982015-05-13 23:41:13 -04001281 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001282 */
Dan Sandler86647982015-05-13 23:41:13 -04001283 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001284 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001285 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001286 }
1287
Adrian Roos7af53622016-10-12 13:44:05 -07001288 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001289 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001290 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001291 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001292 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1293 this.icon = icon.getResId();
1294 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001295 this.title = title;
1296 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001297 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001298 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001299 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001300 }
1301
1302 /**
Dan Sandler86647982015-05-13 23:41:13 -04001303 * Return an icon representing the action.
1304 */
1305 public Icon getIcon() {
1306 if (mIcon == null && icon != 0) {
1307 // you snuck an icon in here without using the builder; let's try to keep it
1308 mIcon = Icon.createWithResource("", icon);
1309 }
1310 return mIcon;
1311 }
1312
1313 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001314 * Get additional metadata carried around with this Action.
1315 */
1316 public Bundle getExtras() {
1317 return mExtras;
1318 }
1319
1320 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001321 * Return whether the platform should automatically generate possible replies for this
1322 * {@link Action}
1323 */
1324 public boolean getAllowGeneratedReplies() {
1325 return mAllowGeneratedReplies;
1326 }
1327
1328 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001329 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001330 * May return null if no remote inputs were added. Only returns inputs which accept
1331 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001332 */
1333 public RemoteInput[] getRemoteInputs() {
1334 return mRemoteInputs;
1335 }
1336
1337 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001338 * Get the list of inputs to be collected from the user that ONLY accept data when this
1339 * action is sent. These remote inputs are guaranteed to return true on a call to
1340 * {@link RemoteInput#isDataOnly}.
1341 *
Shane Brennanf670d6c2017-04-25 13:05:45 -07001342 * Returns null if there are no data-only remote inputs.
Shane Brennan472a3b32016-12-12 15:28:10 -08001343 *
1344 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1345 * of non-textual RemoteInputs do not access these remote inputs.
1346 */
1347 public RemoteInput[] getDataOnlyRemoteInputs() {
1348 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1349 }
1350
1351 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001352 * Builder class for {@link Action} objects.
1353 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001354 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001355 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001356 private final CharSequence mTitle;
1357 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001358 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001359 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001360 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001361
1362 /**
1363 * Construct a new builder for {@link Action} object.
1364 * @param icon icon to show for this action
1365 * @param title the title of the action
1366 * @param intent the {@link PendingIntent} to fire when users trigger this action
1367 */
Dan Sandler86647982015-05-13 23:41:13 -04001368 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001369 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001370 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001371 }
1372
1373 /**
1374 * Construct a new builder for {@link Action} object.
1375 * @param icon icon to show for this action
1376 * @param title the title of the action
1377 * @param intent the {@link PendingIntent} to fire when users trigger this action
1378 */
1379 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001380 this(icon, title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001381 }
1382
1383 /**
1384 * Construct a new builder for {@link Action} object using the fields from an
1385 * {@link Action}.
1386 * @param action the action to read fields from.
1387 */
1388 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001389 this(action.getIcon(), action.title, action.actionIntent,
1390 new Bundle(action.mExtras), action.getRemoteInputs(),
1391 action.getAllowGeneratedReplies());
Griff Hazen959591e2014-05-15 22:26:18 -07001392 }
1393
Dan Sandler86647982015-05-13 23:41:13 -04001394 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Adrian Roos7af53622016-10-12 13:44:05 -07001395 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Griff Hazen959591e2014-05-15 22:26:18 -07001396 mIcon = icon;
1397 mTitle = title;
1398 mIntent = intent;
1399 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001400 if (remoteInputs != null) {
1401 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1402 Collections.addAll(mRemoteInputs, remoteInputs);
1403 }
Adrian Roos7af53622016-10-12 13:44:05 -07001404 mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001405 }
1406
1407 /**
1408 * Merge additional metadata into this builder.
1409 *
1410 * <p>Values within the Bundle will replace existing extras values in this Builder.
1411 *
1412 * @see Notification.Action#extras
1413 */
1414 public Builder addExtras(Bundle extras) {
1415 if (extras != null) {
1416 mExtras.putAll(extras);
1417 }
1418 return this;
1419 }
1420
1421 /**
1422 * Get the metadata Bundle used by this Builder.
1423 *
1424 * <p>The returned Bundle is shared with this Builder.
1425 */
1426 public Bundle getExtras() {
1427 return mExtras;
1428 }
1429
1430 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001431 * Add an input to be collected from the user when this action is sent.
1432 * Response values can be retrieved from the fired intent by using the
1433 * {@link RemoteInput#getResultsFromIntent} function.
1434 * @param remoteInput a {@link RemoteInput} to add to the action
1435 * @return this object for method chaining
1436 */
1437 public Builder addRemoteInput(RemoteInput remoteInput) {
1438 if (mRemoteInputs == null) {
1439 mRemoteInputs = new ArrayList<RemoteInput>();
1440 }
1441 mRemoteInputs.add(remoteInput);
1442 return this;
1443 }
1444
1445 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001446 * Set whether the platform should automatically generate possible replies to add to
1447 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1448 * {@link RemoteInput}, this has no effect.
1449 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1450 * otherwise
1451 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001452 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001453 */
1454 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1455 mAllowGeneratedReplies = allowGeneratedReplies;
1456 return this;
1457 }
1458
1459 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001460 * Apply an extender to this action builder. Extenders may be used to add
1461 * metadata or change options on this builder.
1462 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001463 public Builder extend(Extender extender) {
1464 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001465 return this;
1466 }
1467
1468 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001469 * Combine all of the options that have been set and return a new {@link Action}
1470 * object.
1471 * @return the built action
1472 */
1473 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001474 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1475 RemoteInput[] previousDataInputs =
1476 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001477 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001478 for (RemoteInput input : previousDataInputs) {
1479 dataOnlyInputs.add(input);
1480 }
1481 }
1482 List<RemoteInput> textInputs = new ArrayList<>();
1483 if (mRemoteInputs != null) {
1484 for (RemoteInput input : mRemoteInputs) {
1485 if (input.isDataOnly()) {
1486 dataOnlyInputs.add(input);
1487 } else {
1488 textInputs.add(input);
1489 }
1490 }
1491 }
1492 if (!dataOnlyInputs.isEmpty()) {
1493 RemoteInput[] dataInputsArr =
1494 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1495 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1496 }
1497 RemoteInput[] textInputsArr = textInputs.isEmpty()
1498 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1499 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001500 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001501 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001502 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001503
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001504 @Override
1505 public Action clone() {
1506 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001507 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001508 title,
1509 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001510 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001511 getRemoteInputs(),
1512 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001513 }
1514 @Override
1515 public int describeContents() {
1516 return 0;
1517 }
1518 @Override
1519 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001520 final Icon ic = getIcon();
1521 if (ic != null) {
1522 out.writeInt(1);
1523 ic.writeToParcel(out, 0);
1524 } else {
1525 out.writeInt(0);
1526 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001527 TextUtils.writeToParcel(title, out, flags);
1528 if (actionIntent != null) {
1529 out.writeInt(1);
1530 actionIntent.writeToParcel(out, flags);
1531 } else {
1532 out.writeInt(0);
1533 }
Griff Hazen959591e2014-05-15 22:26:18 -07001534 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001535 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001536 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001537 }
Griff Hazen959591e2014-05-15 22:26:18 -07001538 public static final Parcelable.Creator<Action> CREATOR =
1539 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001540 public Action createFromParcel(Parcel in) {
1541 return new Action(in);
1542 }
1543 public Action[] newArray(int size) {
1544 return new Action[size];
1545 }
1546 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001547
1548 /**
1549 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1550 * metadata or change options on an action builder.
1551 */
1552 public interface Extender {
1553 /**
1554 * Apply this extender to a notification action builder.
1555 * @param builder the builder to be modified.
1556 * @return the build object for chaining.
1557 */
1558 public Builder extend(Builder builder);
1559 }
1560
1561 /**
1562 * Wearable extender for notification actions. To add extensions to an action,
1563 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1564 * the {@code WearableExtender()} constructor and apply it to a
1565 * {@link android.app.Notification.Action.Builder} using
1566 * {@link android.app.Notification.Action.Builder#extend}.
1567 *
1568 * <pre class="prettyprint">
1569 * Notification.Action action = new Notification.Action.Builder(
1570 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001571 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001572 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001573 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001574 */
1575 public static final class WearableExtender implements Extender {
1576 /** Notification action extra which contains wearable extensions */
1577 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1578
Pete Gastaf6781d2014-10-07 15:17:05 -04001579 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001580 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001581 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1582 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1583 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001584
1585 // Flags bitwise-ored to mFlags
1586 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001587 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001588 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001589
1590 // Default value for flags integer
1591 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1592
1593 private int mFlags = DEFAULT_FLAGS;
1594
Pete Gastaf6781d2014-10-07 15:17:05 -04001595 private CharSequence mInProgressLabel;
1596 private CharSequence mConfirmLabel;
1597 private CharSequence mCancelLabel;
1598
Griff Hazen61a9e862014-05-22 16:05:19 -07001599 /**
1600 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1601 * options.
1602 */
1603 public WearableExtender() {
1604 }
1605
1606 /**
1607 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1608 * wearable options present in an existing notification action.
1609 * @param action the notification action to inspect.
1610 */
1611 public WearableExtender(Action action) {
1612 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1613 if (wearableBundle != null) {
1614 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001615 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1616 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1617 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001618 }
1619 }
1620
1621 /**
1622 * Apply wearable extensions to a notification action that is being built. This is
1623 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1624 * method of {@link android.app.Notification.Action.Builder}.
1625 */
1626 @Override
1627 public Action.Builder extend(Action.Builder builder) {
1628 Bundle wearableBundle = new Bundle();
1629
1630 if (mFlags != DEFAULT_FLAGS) {
1631 wearableBundle.putInt(KEY_FLAGS, mFlags);
1632 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001633 if (mInProgressLabel != null) {
1634 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1635 }
1636 if (mConfirmLabel != null) {
1637 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1638 }
1639 if (mCancelLabel != null) {
1640 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1641 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001642
1643 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1644 return builder;
1645 }
1646
1647 @Override
1648 public WearableExtender clone() {
1649 WearableExtender that = new WearableExtender();
1650 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001651 that.mInProgressLabel = this.mInProgressLabel;
1652 that.mConfirmLabel = this.mConfirmLabel;
1653 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001654 return that;
1655 }
1656
1657 /**
1658 * Set whether this action is available when the wearable device is not connected to
1659 * a companion device. The user can still trigger this action when the wearable device is
1660 * offline, but a visual hint will indicate that the action may not be available.
1661 * Defaults to true.
1662 */
1663 public WearableExtender setAvailableOffline(boolean availableOffline) {
1664 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1665 return this;
1666 }
1667
1668 /**
1669 * Get whether this action is available when the wearable device is not connected to
1670 * a companion device. The user can still trigger this action when the wearable device is
1671 * offline, but a visual hint will indicate that the action may not be available.
1672 * Defaults to true.
1673 */
1674 public boolean isAvailableOffline() {
1675 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1676 }
1677
1678 private void setFlag(int mask, boolean value) {
1679 if (value) {
1680 mFlags |= mask;
1681 } else {
1682 mFlags &= ~mask;
1683 }
1684 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001685
1686 /**
1687 * Set a label to display while the wearable is preparing to automatically execute the
1688 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1689 *
1690 * @param label the label to display while the action is being prepared to execute
1691 * @return this object for method chaining
1692 */
1693 public WearableExtender setInProgressLabel(CharSequence label) {
1694 mInProgressLabel = label;
1695 return this;
1696 }
1697
1698 /**
1699 * Get the label to display while the wearable is preparing to automatically execute
1700 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1701 *
1702 * @return the label to display while the action is being prepared to execute
1703 */
1704 public CharSequence getInProgressLabel() {
1705 return mInProgressLabel;
1706 }
1707
1708 /**
1709 * Set a label to display to confirm that the action should be executed.
1710 * This is usually an imperative verb like "Send".
1711 *
1712 * @param label the label to confirm the action should be executed
1713 * @return this object for method chaining
1714 */
1715 public WearableExtender setConfirmLabel(CharSequence label) {
1716 mConfirmLabel = label;
1717 return this;
1718 }
1719
1720 /**
1721 * Get the label to display to confirm that the action should be executed.
1722 * This is usually an imperative verb like "Send".
1723 *
1724 * @return the label to confirm the action should be executed
1725 */
1726 public CharSequence getConfirmLabel() {
1727 return mConfirmLabel;
1728 }
1729
1730 /**
1731 * Set a label to display to cancel the action.
1732 * This is usually an imperative verb, like "Cancel".
1733 *
1734 * @param label the label to display to cancel the action
1735 * @return this object for method chaining
1736 */
1737 public WearableExtender setCancelLabel(CharSequence label) {
1738 mCancelLabel = label;
1739 return this;
1740 }
1741
1742 /**
1743 * Get the label to display to cancel the action.
1744 * This is usually an imperative verb like "Cancel".
1745 *
1746 * @return the label to display to cancel the action
1747 */
1748 public CharSequence getCancelLabel() {
1749 return mCancelLabel;
1750 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001751
1752 /**
1753 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1754 * platform that it can generate the appropriate transitions.
1755 * @param hintLaunchesActivity {@code true} if the content intent will launch
1756 * an activity and transitions should be generated, false otherwise.
1757 * @return this object for method chaining
1758 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001759 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001760 boolean hintLaunchesActivity) {
1761 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1762 return this;
1763 }
1764
1765 /**
1766 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1767 * platform that it can generate the appropriate transitions
1768 * @return {@code true} if the content intent will launch an activity and transitions
1769 * should be generated, false otherwise. The default value is {@code false} if this was
1770 * never set.
1771 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001772 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001773 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1774 }
Alex Hills9f087612016-06-07 09:08:59 -04001775
1776 /**
1777 * Set a hint that this Action should be displayed inline.
1778 *
1779 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1780 * otherwise
1781 * @return this object for method chaining
1782 */
1783 public WearableExtender setHintDisplayActionInline(
1784 boolean hintDisplayInline) {
1785 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1786 return this;
1787 }
1788
1789 /**
1790 * Get a hint that this Action should be displayed inline.
1791 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08001792 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04001793 * otherwise. The default value is {@code false} if this was never set.
1794 */
1795 public boolean getHintDisplayActionInline() {
1796 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1797 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001798 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001799 }
1800
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001801 /**
1802 * Array of all {@link Action} structures attached to this notification by
1803 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1804 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1805 * interface for invoking actions.
1806 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001807 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001808
1809 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001810 * Replacement version of this notification whose content will be shown
1811 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1812 * and {@link #VISIBILITY_PUBLIC}.
1813 */
1814 public Notification publicVersion;
1815
1816 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001817 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001818 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001819 */
1820 public Notification()
1821 {
1822 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001823 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001824 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 }
1826
1827 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 * @hide
1829 */
1830 public Notification(Context context, int icon, CharSequence tickerText, long when,
1831 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1832 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001833 new Builder(context)
1834 .setWhen(when)
1835 .setSmallIcon(icon)
1836 .setTicker(tickerText)
1837 .setContentTitle(contentTitle)
1838 .setContentText(contentText)
1839 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1840 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 }
1842
1843 /**
1844 * Constructs a Notification object with the information needed to
1845 * have a status bar icon without the standard expanded view.
1846 *
1847 * @param icon The resource id of the icon to put in the status bar.
1848 * @param tickerText The text that flows by in the status bar when the notification first
1849 * activates.
1850 * @param when The time to show in the time field. In the System.currentTimeMillis
1851 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001852 *
1853 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001854 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001855 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 public Notification(int icon, CharSequence tickerText, long when)
1857 {
1858 this.icon = icon;
1859 this.tickerText = tickerText;
1860 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001861 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 }
1863
1864 /**
1865 * Unflatten the notification from a parcel.
1866 */
Svet Ganovddb94882016-06-23 19:55:24 -07001867 @SuppressWarnings("unchecked")
1868 public Notification(Parcel parcel) {
1869 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
1870 // intents in extras are always written as the last entry.
1871 readFromParcelImpl(parcel);
1872 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001873 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07001874 }
1875
1876 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 {
1878 int version = parcel.readInt();
1879
Adrian Roosfb921842017-10-26 14:49:56 +02001880 mWhitelistToken = parcel.readStrongBinder();
1881 if (mWhitelistToken == null) {
1882 mWhitelistToken = processWhitelistToken;
Dianne Hackborn98305522017-05-05 17:53:53 -07001883 }
1884 // Propagate this token to all pending intents that are unmarshalled from the parcel.
Adrian Roosfb921842017-10-26 14:49:56 +02001885 parcel.setClassCookie(PendingIntent.class, mWhitelistToken);
Dianne Hackborn98305522017-05-05 17:53:53 -07001886
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001888 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001889 if (parcel.readInt() != 0) {
1890 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001891 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1892 icon = mSmallIcon.getResId();
1893 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001894 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 number = parcel.readInt();
1896 if (parcel.readInt() != 0) {
1897 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1898 }
1899 if (parcel.readInt() != 0) {
1900 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1901 }
1902 if (parcel.readInt() != 0) {
1903 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1904 }
1905 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001906 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001907 }
1908 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1910 }
Joe Onorato561d3852010-11-20 18:09:34 -08001911 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001912 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001913 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001914 defaults = parcel.readInt();
1915 flags = parcel.readInt();
1916 if (parcel.readInt() != 0) {
1917 sound = Uri.CREATOR.createFromParcel(parcel);
1918 }
1919
1920 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001921 if (parcel.readInt() != 0) {
1922 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1923 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 vibrate = parcel.createLongArray();
1925 ledARGB = parcel.readInt();
1926 ledOnMS = parcel.readInt();
1927 ledOffMS = parcel.readInt();
1928 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001929
1930 if (parcel.readInt() != 0) {
1931 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1932 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001933
1934 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001935
John Spurlockfd7f1e02014-03-18 16:41:57 -04001936 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001937
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001938 mGroupKey = parcel.readString();
1939
1940 mSortKey = parcel.readString();
1941
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001942 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001943
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001944 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1945
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001946 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001947 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1948 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001949
Chris Wren8fd39ec2014-02-27 17:43:26 -05001950 if (parcel.readInt() != 0) {
1951 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1952 }
1953
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001954 visibility = parcel.readInt();
1955
1956 if (parcel.readInt() != 0) {
1957 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1958 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001959
1960 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001961
1962 if (parcel.readInt() != 0) {
1963 mChannelId = parcel.readString();
1964 }
Julia Reynolds2a128742016-11-28 14:29:25 -05001965 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05001966
1967 if (parcel.readInt() != 0) {
1968 mShortcutId = parcel.readString();
1969 }
1970
1971 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04001972
1973 if (parcel.readInt() != 0) {
1974 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1975 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04001976
1977 mGroupAlertBehavior = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001978 }
1979
Andy Stadler110988c2010-12-03 14:29:16 -08001980 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001981 public Notification clone() {
1982 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001983 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001984 return that;
1985 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001986
Daniel Sandler1a497d32013-04-18 14:52:45 -04001987 /**
1988 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1989 * of this into that.
1990 * @hide
1991 */
1992 public void cloneInto(Notification that, boolean heavy) {
Adrian Roosfb921842017-10-26 14:49:56 +02001993 that.mWhitelistToken = this.mWhitelistToken;
Joe Onorato18e69df2010-05-17 22:26:12 -07001994 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001995 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001996 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001997 that.number = this.number;
1998
1999 // PendingIntents are global, so there's no reason (or way) to clone them.
2000 that.contentIntent = this.contentIntent;
2001 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002002 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07002003
2004 if (this.tickerText != null) {
2005 that.tickerText = this.tickerText.toString();
2006 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002007 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002008 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04002009 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002010 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07002011 that.contentView = this.contentView.clone();
2012 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002013 if (heavy && this.mLargeIcon != null) {
2014 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08002015 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01002016 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07002017 that.sound = this.sound; // android.net.Uri is immutable
2018 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04002019 if (this.audioAttributes != null) {
2020 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
2021 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002022
2023 final long[] vibrate = this.vibrate;
2024 if (vibrate != null) {
2025 final int N = vibrate.length;
2026 final long[] vib = that.vibrate = new long[N];
2027 System.arraycopy(vibrate, 0, vib, 0, N);
2028 }
2029
2030 that.ledARGB = this.ledARGB;
2031 that.ledOnMS = this.ledOnMS;
2032 that.ledOffMS = this.ledOffMS;
2033 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002034
Joe Onorato18e69df2010-05-17 22:26:12 -07002035 that.flags = this.flags;
2036
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002037 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08002038
John Spurlockfd7f1e02014-03-18 16:41:57 -04002039 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002040
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002041 that.mGroupKey = this.mGroupKey;
2042
2043 that.mSortKey = this.mSortKey;
2044
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002045 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002046 try {
2047 that.extras = new Bundle(this.extras);
2048 // will unparcel
2049 that.extras.size();
2050 } catch (BadParcelableException e) {
2051 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
2052 that.extras = null;
2053 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002054 }
2055
Felipe Lemedd85da62016-06-28 11:29:54 -07002056 if (!ArrayUtils.isEmpty(allPendingIntents)) {
2057 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002058 }
2059
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002060 if (this.actions != null) {
2061 that.actions = new Action[this.actions.length];
2062 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08002063 if ( this.actions[i] != null) {
2064 that.actions[i] = this.actions[i].clone();
2065 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002066 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002067 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002068
Daniel Sandler1a497d32013-04-18 14:52:45 -04002069 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002070 that.bigContentView = this.bigContentView.clone();
2071 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002072
Chris Wren8fd39ec2014-02-27 17:43:26 -05002073 if (heavy && this.headsUpContentView != null) {
2074 that.headsUpContentView = this.headsUpContentView.clone();
2075 }
2076
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002077 that.visibility = this.visibility;
2078
2079 if (this.publicVersion != null) {
2080 that.publicVersion = new Notification();
2081 this.publicVersion.cloneInto(that.publicVersion, heavy);
2082 }
2083
Dan Sandler26e81cf2014-05-06 10:01:27 -04002084 that.color = this.color;
2085
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002086 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05002087 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04002088 that.mShortcutId = this.mShortcutId;
2089 that.mBadgeIcon = this.mBadgeIcon;
2090 that.mSettingsText = this.mSettingsText;
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002091 that.mGroupAlertBehavior = this.mGroupAlertBehavior;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002092
Daniel Sandler1a497d32013-04-18 14:52:45 -04002093 if (!heavy) {
2094 that.lightenPayload(); // will clean out extras
2095 }
2096 }
2097
2098 /**
2099 * Removes heavyweight parts of the Notification object for archival or for sending to
2100 * listeners when the full contents are not necessary.
2101 * @hide
2102 */
2103 public final void lightenPayload() {
2104 tickerView = null;
2105 contentView = null;
2106 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05002107 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002108 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002109 if (extras != null && !extras.isEmpty()) {
2110 final Set<String> keyset = extras.keySet();
2111 final int N = keyset.size();
2112 final String[] keys = keyset.toArray(new String[N]);
2113 for (int i=0; i<N; i++) {
2114 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002115 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2116 continue;
2117 }
Dan Sandler50128532015-12-08 15:42:41 -05002118 final Object obj = extras.get(key);
2119 if (obj != null &&
2120 ( obj instanceof Parcelable
2121 || obj instanceof Parcelable[]
2122 || obj instanceof SparseArray
2123 || obj instanceof ArrayList)) {
2124 extras.remove(key);
2125 }
2126 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002127 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002128 }
2129
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002130 /**
2131 * Make sure this CharSequence is safe to put into a bundle, which basically
2132 * means it had better not be some custom Parcelable implementation.
2133 * @hide
2134 */
2135 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002136 if (cs == null) return cs;
2137 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2138 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2139 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002140 if (cs instanceof Parcelable) {
2141 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2142 + " instance is a custom Parcelable and not allowed in Notification");
2143 return cs.toString();
2144 }
Selim Cinek60a54252016-02-26 17:03:25 -08002145 return removeTextSizeSpans(cs);
2146 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002147
Selim Cinek60a54252016-02-26 17:03:25 -08002148 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2149 if (charSequence instanceof Spanned) {
2150 Spanned ss = (Spanned) charSequence;
2151 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2152 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2153 for (Object span : spans) {
2154 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002155 if (resultSpan instanceof CharacterStyle) {
2156 resultSpan = ((CharacterStyle) span).getUnderlying();
2157 }
2158 if (resultSpan instanceof TextAppearanceSpan) {
2159 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002160 resultSpan = new TextAppearanceSpan(
2161 originalSpan.getFamily(),
2162 originalSpan.getTextStyle(),
2163 -1,
2164 originalSpan.getTextColor(),
2165 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002166 } else if (resultSpan instanceof RelativeSizeSpan
2167 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002168 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002169 } else {
2170 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002171 }
2172 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2173 ss.getSpanFlags(span));
2174 }
2175 return builder;
2176 }
2177 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002178 }
2179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 public int describeContents() {
2181 return 0;
2182 }
2183
2184 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002185 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002186 */
Svet Ganovddb94882016-06-23 19:55:24 -07002187 public void writeToParcel(Parcel parcel, int flags) {
2188 // We need to mark all pending intents getting into the notification
2189 // system as being put there to later allow the notification ranker
2190 // to launch them and by doing so add the app to the battery saver white
2191 // list for a short period of time. The problem is that the system
2192 // cannot look into the extras as there may be parcelables there that
2193 // the platform does not know how to handle. To go around that we have
2194 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002195 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002196 if (collectPendingIntents) {
2197 PendingIntent.setOnMarshaledListener(
2198 (PendingIntent intent, Parcel out, int outFlags) -> {
2199 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002200 if (allPendingIntents == null) {
2201 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002202 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002203 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002204 }
2205 });
2206 }
2207 try {
2208 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002209 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002210 writeToParcelImpl(parcel, flags);
2211 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002212 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002213 } finally {
2214 if (collectPendingIntents) {
2215 PendingIntent.setOnMarshaledListener(null);
2216 }
2217 }
2218 }
2219
2220 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002221 parcel.writeInt(1);
2222
Adrian Roosfb921842017-10-26 14:49:56 +02002223 parcel.writeStrongBinder(mWhitelistToken);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002224 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002225 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002226 if (mSmallIcon == null && icon != 0) {
2227 // you snuck an icon in here without using the builder; let's try to keep it
2228 mSmallIcon = Icon.createWithResource("", icon);
2229 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002230 if (mSmallIcon != null) {
2231 parcel.writeInt(1);
2232 mSmallIcon.writeToParcel(parcel, 0);
2233 } else {
2234 parcel.writeInt(0);
2235 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002236 parcel.writeInt(number);
2237 if (contentIntent != null) {
2238 parcel.writeInt(1);
2239 contentIntent.writeToParcel(parcel, 0);
2240 } else {
2241 parcel.writeInt(0);
2242 }
2243 if (deleteIntent != null) {
2244 parcel.writeInt(1);
2245 deleteIntent.writeToParcel(parcel, 0);
2246 } else {
2247 parcel.writeInt(0);
2248 }
2249 if (tickerText != null) {
2250 parcel.writeInt(1);
2251 TextUtils.writeToParcel(tickerText, parcel, flags);
2252 } else {
2253 parcel.writeInt(0);
2254 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002255 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002256 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002257 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002258 } else {
2259 parcel.writeInt(0);
2260 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002261 if (contentView != null) {
2262 parcel.writeInt(1);
2263 contentView.writeToParcel(parcel, 0);
2264 } else {
2265 parcel.writeInt(0);
2266 }
Selim Cinek279fa862016-06-14 10:57:25 -07002267 if (mLargeIcon == null && largeIcon != null) {
2268 // you snuck an icon in here without using the builder; let's try to keep it
2269 mLargeIcon = Icon.createWithBitmap(largeIcon);
2270 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002271 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002272 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002273 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002274 } else {
2275 parcel.writeInt(0);
2276 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002277
2278 parcel.writeInt(defaults);
2279 parcel.writeInt(this.flags);
2280
2281 if (sound != null) {
2282 parcel.writeInt(1);
2283 sound.writeToParcel(parcel, 0);
2284 } else {
2285 parcel.writeInt(0);
2286 }
2287 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002288
2289 if (audioAttributes != null) {
2290 parcel.writeInt(1);
2291 audioAttributes.writeToParcel(parcel, 0);
2292 } else {
2293 parcel.writeInt(0);
2294 }
2295
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002296 parcel.writeLongArray(vibrate);
2297 parcel.writeInt(ledARGB);
2298 parcel.writeInt(ledOnMS);
2299 parcel.writeInt(ledOffMS);
2300 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002301
2302 if (fullScreenIntent != null) {
2303 parcel.writeInt(1);
2304 fullScreenIntent.writeToParcel(parcel, 0);
2305 } else {
2306 parcel.writeInt(0);
2307 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002308
2309 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002310
John Spurlockfd7f1e02014-03-18 16:41:57 -04002311 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002312
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002313 parcel.writeString(mGroupKey);
2314
2315 parcel.writeString(mSortKey);
2316
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002317 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002318
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002319 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002320
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002321 if (bigContentView != null) {
2322 parcel.writeInt(1);
2323 bigContentView.writeToParcel(parcel, 0);
2324 } else {
2325 parcel.writeInt(0);
2326 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002327
Chris Wren8fd39ec2014-02-27 17:43:26 -05002328 if (headsUpContentView != null) {
2329 parcel.writeInt(1);
2330 headsUpContentView.writeToParcel(parcel, 0);
2331 } else {
2332 parcel.writeInt(0);
2333 }
2334
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002335 parcel.writeInt(visibility);
2336
2337 if (publicVersion != null) {
2338 parcel.writeInt(1);
2339 publicVersion.writeToParcel(parcel, 0);
2340 } else {
2341 parcel.writeInt(0);
2342 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002343
2344 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002345
2346 if (mChannelId != null) {
2347 parcel.writeInt(1);
2348 parcel.writeString(mChannelId);
2349 } else {
2350 parcel.writeInt(0);
2351 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002352 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002353
2354 if (mShortcutId != null) {
2355 parcel.writeInt(1);
2356 parcel.writeString(mShortcutId);
2357 } else {
2358 parcel.writeInt(0);
2359 }
2360
2361 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002362
2363 if (mSettingsText != null) {
2364 parcel.writeInt(1);
2365 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2366 } else {
2367 parcel.writeInt(0);
2368 }
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002369
2370 parcel.writeInt(mGroupAlertBehavior);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371 }
2372
2373 /**
2374 * Parcelable.Creator that instantiates Notification objects
2375 */
2376 public static final Parcelable.Creator<Notification> CREATOR
2377 = new Parcelable.Creator<Notification>()
2378 {
2379 public Notification createFromParcel(Parcel parcel)
2380 {
2381 return new Notification(parcel);
2382 }
2383
2384 public Notification[] newArray(int size)
2385 {
2386 return new Notification[size];
2387 }
2388 };
2389
2390 /**
2391 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2392 * layout.
2393 *
2394 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2395 * in the view.</p>
2396 * @param context The context for your application / activity.
2397 * @param contentTitle The title that goes in the expanded entry.
2398 * @param contentText The text that goes in the expanded entry.
2399 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2400 * If this is an activity, it must include the
2401 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002402 * that you take care of task management as described in the
2403 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2404 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002405 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002406 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002407 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002409 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 public void setLatestEventInfo(Context context,
2411 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002412 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2413 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2414 new Throwable());
2415 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002416
Selim Cinek4ac6f602016-06-13 15:47:03 -07002417 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2418 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2419 }
2420
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002421 // ensure that any information already set directly is preserved
2422 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002423
2424 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002425 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002426 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002427 }
2428 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002429 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002430 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002431 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002432
2433 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002434 }
2435
Julia Reynoldsda303542015-11-23 14:00:20 -05002436 /**
2437 * @hide
2438 */
2439 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002440 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002441 }
2442
2443 /**
2444 * @hide
2445 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002446 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002447 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002448 }
2449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002450 @Override
2451 public String toString() {
2452 StringBuilder sb = new StringBuilder();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002453 sb.append("Notification(channel=");
Julia Reynoldsbad42972017-04-25 13:52:49 -04002454 sb.append(getChannelId());
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002455 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002456 sb.append(priority);
2457 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002458 if (contentView != null) {
2459 sb.append(contentView.getPackage());
2460 sb.append("/0x");
2461 sb.append(Integer.toHexString(contentView.getLayoutId()));
2462 } else {
2463 sb.append("null");
2464 }
2465 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002466 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2467 sb.append("default");
2468 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002469 int N = this.vibrate.length-1;
2470 sb.append("[");
2471 for (int i=0; i<N; i++) {
2472 sb.append(this.vibrate[i]);
2473 sb.append(',');
2474 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002475 if (N != -1) {
2476 sb.append(this.vibrate[N]);
2477 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002478 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002479 } else {
2480 sb.append("null");
2481 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002482 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002483 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002484 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002485 } else if (this.sound != null) {
2486 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002487 } else {
2488 sb.append("null");
2489 }
Chris Wren365b6d32015-07-16 10:39:26 -04002490 if (this.tickerText != null) {
2491 sb.append(" tick");
2492 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002493 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002494 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002495 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002496 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002497 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002498 if (this.category != null) {
2499 sb.append(" category=");
2500 sb.append(this.category);
2501 }
2502 if (this.mGroupKey != null) {
2503 sb.append(" groupKey=");
2504 sb.append(this.mGroupKey);
2505 }
2506 if (this.mSortKey != null) {
2507 sb.append(" sortKey=");
2508 sb.append(this.mSortKey);
2509 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002510 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002511 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002512 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002513 }
2514 sb.append(" vis=");
2515 sb.append(visibilityToString(this.visibility));
2516 if (this.publicVersion != null) {
2517 sb.append(" publicVersion=");
2518 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002519 }
2520 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002521 return sb.toString();
2522 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002523
Dan Sandler1b718782014-07-18 12:43:45 -04002524 /**
2525 * {@hide}
2526 */
2527 public static String visibilityToString(int vis) {
2528 switch (vis) {
2529 case VISIBILITY_PRIVATE:
2530 return "PRIVATE";
2531 case VISIBILITY_PUBLIC:
2532 return "PUBLIC";
2533 case VISIBILITY_SECRET:
2534 return "SECRET";
2535 default:
2536 return "UNKNOWN(" + String.valueOf(vis) + ")";
2537 }
2538 }
2539
Joe Onoratocb109a02011-01-18 17:57:41 -08002540 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002541 * {@hide}
2542 */
2543 public static String priorityToString(@Priority int pri) {
2544 switch (pri) {
2545 case PRIORITY_MIN:
2546 return "MIN";
2547 case PRIORITY_LOW:
2548 return "LOW";
2549 case PRIORITY_DEFAULT:
2550 return "DEFAULT";
2551 case PRIORITY_HIGH:
2552 return "HIGH";
2553 case PRIORITY_MAX:
2554 return "MAX";
2555 default:
2556 return "UNKNOWN(" + String.valueOf(pri) + ")";
2557 }
2558 }
2559
Julia Reynolds6ad0aec2017-07-05 08:47:03 -04002560 /**
2561 * @hide
2562 */
2563 public boolean hasCompletedProgress() {
2564 // not a progress notification; can't be complete
2565 if (!extras.containsKey(EXTRA_PROGRESS)
2566 || !extras.containsKey(EXTRA_PROGRESS_MAX)) {
2567 return false;
2568 }
2569 // many apps use max 0 for 'indeterminate'; not complete
2570 if (extras.getInt(EXTRA_PROGRESS_MAX) == 0) {
2571 return false;
2572 }
2573 return extras.getInt(EXTRA_PROGRESS) == extras.getInt(EXTRA_PROGRESS_MAX);
2574 }
2575
Jeff Sharkey000ce802017-04-29 13:13:27 -06002576 /** @removed */
2577 @Deprecated
Julia Reynolds37856052016-11-11 09:20:07 -05002578 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002579 return mChannelId;
2580 }
2581
2582 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002583 * Returns the id of the channel this notification posts to.
2584 */
2585 public String getChannelId() {
2586 return mChannelId;
2587 }
2588
Jeff Sharkey000ce802017-04-29 13:13:27 -06002589 /** @removed */
2590 @Deprecated
Julia Reynolds2a128742016-11-28 14:29:25 -05002591 public long getTimeout() {
2592 return mTimeout;
2593 }
2594
2595 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002596 * Returns the duration from posting after which this notification should be canceled by the
2597 * system, if it's not canceled already.
2598 */
2599 public long getTimeoutAfter() {
2600 return mTimeout;
2601 }
2602
2603 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002604 * Returns what icon should be shown for this notification if it is being displayed in a
2605 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2606 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2607 */
2608 public int getBadgeIconType() {
2609 return mBadgeIcon;
2610 }
2611
2612 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002613 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
Julia Reynoldsbad42972017-04-25 13:52:49 -04002614 *
2615 * <p>Used by some Launchers that display notification content to hide shortcuts that duplicate
2616 * notifications.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002617 */
2618 public String getShortcutId() {
2619 return mShortcutId;
2620 }
2621
Julia Reynolds3aedded2017-03-31 14:42:09 -04002622
2623 /**
2624 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
2625 */
2626 public CharSequence getSettingsText() {
2627 return mSettingsText;
2628 }
2629
Julia Reynolds13d898c2017-02-02 12:22:05 -05002630 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002631 * Returns which type of notifications in a group are responsible for audibly alerting the
2632 * user. See {@link #GROUP_ALERT_ALL}, {@link #GROUP_ALERT_CHILDREN},
2633 * {@link #GROUP_ALERT_SUMMARY}.
2634 */
2635 public @GroupAlertBehavior int getGroupAlertBehavior() {
2636 return mGroupAlertBehavior;
2637 }
2638
2639 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002640 * The small icon representing this notification in the status bar and content view.
2641 *
2642 * @return the small icon representing this notification.
2643 *
2644 * @see Builder#getSmallIcon()
2645 * @see Builder#setSmallIcon(Icon)
2646 */
2647 public Icon getSmallIcon() {
2648 return mSmallIcon;
2649 }
2650
2651 /**
2652 * Used when notifying to clean up legacy small icons.
2653 * @hide
2654 */
2655 public void setSmallIcon(Icon icon) {
2656 mSmallIcon = icon;
2657 }
2658
2659 /**
2660 * The large icon shown in this notification's content view.
2661 * @see Builder#getLargeIcon()
2662 * @see Builder#setLargeIcon(Icon)
2663 */
2664 public Icon getLargeIcon() {
2665 return mLargeIcon;
2666 }
2667
2668 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002669 * @hide
2670 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002671 public boolean isGroupSummary() {
2672 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2673 }
2674
2675 /**
2676 * @hide
2677 */
2678 public boolean isGroupChild() {
2679 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2680 }
2681
2682 /**
Julia Reynolds30203152017-05-26 13:36:31 -04002683 * @hide
2684 */
2685 public boolean suppressAlertingDueToGrouping() {
2686 if (isGroupSummary()
2687 && getGroupAlertBehavior() == Notification.GROUP_ALERT_CHILDREN) {
2688 return true;
2689 } else if (isGroupChild()
2690 && getGroupAlertBehavior() == Notification.GROUP_ALERT_SUMMARY) {
2691 return true;
2692 }
2693 return false;
2694 }
2695
2696 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002697 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002698 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002699 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002700 * content views using the platform's notification layout template. If your app supports
2701 * versions of Android as old as API level 4, you can instead use
2702 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2703 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2704 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002705 *
Scott Main183bf112012-08-13 19:12:13 -07002706 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002707 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002708 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002709 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002710 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2711 * .setContentText(subject)
2712 * .setSmallIcon(R.drawable.new_mail)
2713 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002714 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002715 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002716 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002717 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002718 /**
2719 * @hide
2720 */
2721 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2722 "android.rebuild.contentViewActionCount";
2723 /**
2724 * @hide
2725 */
2726 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2727 = "android.rebuild.bigViewActionCount";
2728 /**
2729 * @hide
2730 */
2731 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2732 = "android.rebuild.hudViewActionCount";
2733
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002734 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002735
Selim Cinek6743c0b2017-01-18 18:24:01 -08002736 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
2737 SystemProperties.getBoolean("notifications.only_title", true);
2738
Selim Cinek389edcd2017-05-11 19:16:44 -07002739 /**
2740 * The lightness difference that has to be added to the primary text color to obtain the
2741 * secondary text color when the background is light.
2742 */
2743 private static final int LIGHTNESS_TEXT_DIFFERENCE_LIGHT = 20;
2744
2745 /**
2746 * The lightness difference that has to be added to the primary text color to obtain the
2747 * secondary text color when the background is dark.
2748 * A bit less then the above value, since it looks better on dark backgrounds.
2749 */
2750 private static final int LIGHTNESS_TEXT_DIFFERENCE_DARK = -10;
2751
Joe Onorato46439ce2010-11-19 13:56:21 -08002752 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002753 private Notification mN;
2754 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002755 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002756 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2757 private ArrayList<String> mPersonList = new ArrayList<String>();
2758 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08002759 private boolean mIsLegacy;
2760 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002761
2762 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002763 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2764 */
2765 private int mCachedContrastColor = COLOR_INVALID;
2766 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08002767 /**
2768 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
2769 */
2770 private int mCachedAmbientColor = COLOR_INVALID;
2771 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08002772
2773 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08002774 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
2775 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
2776 */
2777 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08002778 private int mTextColorsAreForBackground = COLOR_INVALID;
2779 private int mPrimaryTextColor = COLOR_INVALID;
2780 private int mSecondaryTextColor = COLOR_INVALID;
2781 private int mActionBarColor = COLOR_INVALID;
Selim Cinek5fb73f82017-04-20 16:55:38 -07002782 private int mBackgroundColor = COLOR_INVALID;
2783 private int mForegroundColor = COLOR_INVALID;
Selim Cinekac5f0272017-05-02 16:05:41 -07002784 private int mBackgroundColorHint = COLOR_INVALID;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07002785 /**
2786 * A temporary location where actions are stored. If != null the view originally has action
2787 * but doesn't have any for this inflation.
2788 */
2789 private ArrayList<Action> mOriginalActions;
Selim Cineka7679b62017-05-10 16:33:25 -07002790 private boolean mRebuildStyledRemoteViews;
Adrian Roos70d7aa32017-01-11 15:39:06 -08002791
Anthony Chenad4d1582017-04-10 16:07:58 -07002792 private boolean mTintActionButtons;
2793 private boolean mInNightMode;
2794
Adrian Roos70d7aa32017-01-11 15:39:06 -08002795 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002796 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002797 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002798 * @param context
2799 * A {@link Context} that will be used by the Builder to construct the
2800 * RemoteViews. The Context will not be held past the lifetime of this Builder
2801 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002802 * @param channelId
2803 * The constructed Notification will be posted on this
2804 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
2805 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08002806 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002807 public Builder(Context context, String channelId) {
2808 this(context, (Notification) null);
2809 mN.mChannelId = channelId;
2810 }
2811
2812 /**
2813 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
2814 * instead. All posted Notifications must specify a NotificationChannel Id.
2815 */
2816 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002817 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002818 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002819 }
2820
Joe Onoratocb109a02011-01-18 17:57:41 -08002821 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002822 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002823 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002824 public Builder(Context context, Notification toAdopt) {
2825 mContext = context;
Anthony Chenad4d1582017-04-10 16:07:58 -07002826 Resources res = mContext.getResources();
2827 mTintActionButtons = res.getBoolean(R.bool.config_tintNotificationActionButtons);
2828
2829 if (res.getBoolean(R.bool.config_enableNightMode)) {
2830 Configuration currentConfig = res.getConfiguration();
2831 mInNightMode = (currentConfig.uiMode & Configuration.UI_MODE_NIGHT_MASK)
2832 == Configuration.UI_MODE_NIGHT_YES;
2833 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02002834
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002835 if (toAdopt == null) {
2836 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002837 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2838 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2839 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002840 mN.priority = PRIORITY_DEFAULT;
2841 mN.visibility = VISIBILITY_PRIVATE;
2842 } else {
2843 mN = toAdopt;
2844 if (mN.actions != null) {
2845 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002846 }
2847
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002848 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2849 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2850 }
2851
Selim Cinek4ac6f602016-06-13 15:47:03 -07002852 if (mN.getSmallIcon() == null && mN.icon != 0) {
2853 setSmallIcon(mN.icon);
2854 }
2855
2856 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
2857 setLargeIcon(mN.largeIcon);
2858 }
2859
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002860 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2861 if (!TextUtils.isEmpty(templateClass)) {
2862 final Class<? extends Style> styleClass
2863 = getNotificationStyleClass(templateClass);
2864 if (styleClass == null) {
2865 Log.d(TAG, "Unknown style class: " + templateClass);
2866 } else {
2867 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002868 final Constructor<? extends Style> ctor =
2869 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002870 ctor.setAccessible(true);
2871 final Style style = ctor.newInstance();
2872 style.restoreFromExtras(mN.extras);
2873
2874 if (style != null) {
2875 setStyle(style);
2876 }
2877 } catch (Throwable t) {
2878 Log.e(TAG, "Could not create Style", t);
2879 }
2880 }
2881 }
2882
2883 }
2884 }
2885
2886 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08002887 if (mColorUtil == null) {
2888 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002889 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002890 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002891 }
2892
2893 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002894 * If this notification is duplicative of a Launcher shortcut, sets the
2895 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
2896 * the shortcut.
2897 *
Julia Reynoldsbad42972017-04-25 13:52:49 -04002898 * This field will be ignored by Launchers that don't support badging, don't show
2899 * notification content, or don't show {@link android.content.pm.ShortcutManager shortcuts}.
Julia Reynolds13d898c2017-02-02 12:22:05 -05002900 *
2901 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
2902 * supersedes
2903 */
2904 public Builder setShortcutId(String shortcutId) {
2905 mN.mShortcutId = shortcutId;
2906 return this;
2907 }
2908
2909 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002910 * Sets which icon to display as a badge for this notification.
2911 *
2912 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
2913 * {@link #BADGE_ICON_LARGE}.
2914 *
2915 * Note: This value might be ignored, for launchers that don't support badge icons.
2916 */
Julia Reynolds612beb22017-03-30 10:48:30 -04002917 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002918 mN.mBadgeIcon = icon;
2919 return this;
2920 }
2921
2922 /**
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002923 * Sets the group alert behavior for this notification. Use this method to mute this
2924 * notification if alerts for this notification's group should be handled by a different
2925 * notification. This is only applicable for notifications that belong to a
Julia Reynolds399d9bf2017-08-11 12:52:14 -04002926 * {@link #setGroup(String) group}. This must be called on all notifications you want to
2927 * mute. For example, if you want only the summary of your group to make noise, all
2928 * children in the group should have the group alert behavior {@link #GROUP_ALERT_SUMMARY}.
Julia Reynoldsa79c3712017-04-21 10:29:57 -04002929 *
2930 * <p> The default value is {@link #GROUP_ALERT_ALL}.</p>
2931 */
2932 public Builder setGroupAlertBehavior(@GroupAlertBehavior int groupAlertBehavior) {
2933 mN.mGroupAlertBehavior = groupAlertBehavior;
2934 return this;
2935 }
2936
Jeff Sharkey000ce802017-04-29 13:13:27 -06002937 /** @removed */
2938 @Deprecated
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002939 public Builder setChannel(String channelId) {
2940 mN.mChannelId = channelId;
2941 return this;
2942 }
2943
2944 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002945 * Specifies the channel the notification should be delivered on.
2946 */
2947 public Builder setChannelId(String channelId) {
2948 mN.mChannelId = channelId;
2949 return this;
2950 }
2951
Jeff Sharkey000ce802017-04-29 13:13:27 -06002952 /** @removed */
2953 @Deprecated
Julia Reynolds50989772017-02-23 14:32:16 -05002954 public Builder setTimeout(long durationMs) {
2955 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05002956 return this;
2957 }
2958
2959 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04002960 * Specifies a duration in milliseconds after which this notification should be canceled,
2961 * if it is not already canceled.
2962 */
2963 public Builder setTimeoutAfter(long durationMs) {
2964 mN.mTimeout = durationMs;
2965 return this;
2966 }
2967
2968 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002969 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002970 *
2971 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2972 * shown anymore by default and must be opted into by using
2973 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002974 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002975 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002976 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002977 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002978 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002979 return this;
2980 }
2981
Joe Onoratocb109a02011-01-18 17:57:41 -08002982 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002983 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002984 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002985 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2986 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002987 */
2988 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002989 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002990 return this;
2991 }
2992
2993 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002994 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002995 *
2996 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002997 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002998 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002999 * Useful when showing an elapsed time (like an ongoing phone call).
3000 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08003001 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07003002 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08003003 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04003004 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003005 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07003006 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003007 */
3008 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003009 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04003010 return this;
3011 }
3012
3013 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08003014 * Sets the Chronometer to count down instead of counting up.
3015 *
3016 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
3017 * If it isn't set the chronometer will count up.
3018 *
3019 * @see #setUsesChronometer(boolean)
3020 */
Adrian Roos96b7e202016-05-17 13:50:38 -07003021 public Builder setChronometerCountDown(boolean countDown) {
3022 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003023 return this;
3024 }
3025
3026 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003027 * Set the small icon resource, which will be used to represent the notification in the
3028 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08003029 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003030
3031 * The platform template for the expanded view will draw this icon in the left, unless a
3032 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
3033 * icon will be moved to the right-hand side.
3034 *
3035
3036 * @param icon
3037 * A resource ID in the application's package of the drawable to use.
3038 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08003039 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003040 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003041 return setSmallIcon(icon != 0
3042 ? Icon.createWithResource(mContext, icon)
3043 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08003044 }
3045
Joe Onoratocb109a02011-01-18 17:57:41 -08003046 /**
3047 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
3048 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
3049 * LevelListDrawable}.
3050 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003051 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08003052 * @param level The level to use for the icon.
3053 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003054 * @see Notification#icon
3055 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08003056 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07003057 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003058 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003059 return setSmallIcon(icon);
3060 }
3061
3062 /**
3063 * Set the small icon, which will be used to represent the notification in the
3064 * status bar and content view (unless overriden there by a
3065 * {@link #setLargeIcon(Bitmap) large icon}).
3066 *
3067 * @param icon An Icon object to use.
3068 * @see Notification#icon
3069 */
3070 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003071 mN.setSmallIcon(icon);
3072 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
3073 mN.icon = icon.getResId();
3074 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003075 return this;
3076 }
3077
Joe Onoratocb109a02011-01-18 17:57:41 -08003078 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003079 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003080 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003081 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003082 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08003083 return this;
3084 }
3085
Joe Onoratocb109a02011-01-18 17:57:41 -08003086 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003087 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08003088 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003089 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003090 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08003091 return this;
3092 }
3093
Joe Onoratocb109a02011-01-18 17:57:41 -08003094 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003095 * This provides some additional information that is displayed in the notification. No
3096 * guarantees are given where exactly it is displayed.
3097 *
3098 * <p>This information should only be provided if it provides an essential
3099 * benefit to the understanding of the notification. The more text you provide the
3100 * less readable it becomes. For example, an email client should only provide the account
3101 * name here if more than one email account has been added.</p>
3102 *
3103 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
3104 * notification header area.
3105 *
3106 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
3107 * this will be shown in the third line of text in the platform notification template.
3108 * You should not be using {@link #setProgress(int, int, boolean)} at the
3109 * same time on those versions; they occupy the same place.
3110 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003111 */
3112 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003113 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003114 return this;
3115 }
3116
3117 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04003118 * Provides text that will appear as a link to your application's settings.
3119 *
3120 * <p>This text does not appear within notification {@link Style templates} but may
3121 * appear when the user uses an affordance to learn more about the notification.
3122 * Additionally, this text will not appear unless you provide a valid link target by
3123 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
3124 *
3125 * <p>This text is meant to be concise description about what the user can customize
3126 * when they click on this link. The recommended maximum length is 40 characters.
3127 * @param text
3128 * @return
3129 */
3130 public Builder setSettingsText(CharSequence text) {
3131 mN.mSettingsText = safeCharSequence(text);
3132 return this;
3133 }
3134
3135 /**
Adrian Roose458aa82015-12-08 16:17:19 -08003136 * Set the remote input history.
3137 *
3138 * This should be set to the most recent inputs that have been sent
3139 * through a {@link RemoteInput} of this Notification and cleared once the it is no
3140 * longer relevant (e.g. for chat notifications once the other party has responded).
3141 *
3142 * The most recent input must be stored at the 0 index, the second most recent at the
3143 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
3144 * and how much of each individual input is shown.
3145 *
3146 * <p>Note: The reply text will only be shown on notifications that have least one action
3147 * with a {@code RemoteInput}.</p>
3148 */
3149 public Builder setRemoteInputHistory(CharSequence[] text) {
3150 if (text == null) {
3151 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
3152 } else {
3153 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
3154 CharSequence[] safe = new CharSequence[N];
3155 for (int i = 0; i < N; i++) {
3156 safe[i] = safeCharSequence(text[i]);
3157 }
3158 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
3159 }
3160 return this;
3161 }
3162
3163 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05003164 * Sets the number of items this notification represents. May be displayed as a badge count
3165 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08003166 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08003167 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003168 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08003169 return this;
3170 }
3171
Joe Onoratocb109a02011-01-18 17:57:41 -08003172 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003173 * A small piece of additional information pertaining to this notification.
3174 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003175 * The platform template will draw this on the last line of the notification, at the far
3176 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003177 *
3178 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
3179 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
3180 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08003181 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003182 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003183 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003184 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08003185 return this;
3186 }
3187
Joe Onoratocb109a02011-01-18 17:57:41 -08003188 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003189 * Set the progress this notification represents.
3190 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003191 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003192 */
3193 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003194 mN.extras.putInt(EXTRA_PROGRESS, progress);
3195 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3196 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003197 return this;
3198 }
3199
3200 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003201 * Supply a custom RemoteViews to use instead of the platform template.
3202 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003203 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003204 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003205 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003206 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003207 return setCustomContentView(views);
3208 }
3209
3210 /**
3211 * Supply custom RemoteViews to use instead of the platform template.
3212 *
3213 * This will override the layout that would otherwise be constructed by this Builder
3214 * object.
3215 */
3216 public Builder setCustomContentView(RemoteViews contentView) {
3217 mN.contentView = contentView;
3218 return this;
3219 }
3220
3221 /**
3222 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3223 *
3224 * This will override the expanded layout that would otherwise be constructed by this
3225 * Builder object.
3226 */
3227 public Builder setCustomBigContentView(RemoteViews contentView) {
3228 mN.bigContentView = contentView;
3229 return this;
3230 }
3231
3232 /**
3233 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3234 *
3235 * This will override the heads-up layout that would otherwise be constructed by this
3236 * Builder object.
3237 */
3238 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3239 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003240 return this;
3241 }
3242
Joe Onoratocb109a02011-01-18 17:57:41 -08003243 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003244 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3245 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003246 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3247 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3248 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003249 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003250 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003251 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003252 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003253 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003254 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003255 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003256 return this;
3257 }
3258
Joe Onoratocb109a02011-01-18 17:57:41 -08003259 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003260 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3261 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003262 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003263 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003264 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003265 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003266 return this;
3267 }
3268
Joe Onoratocb109a02011-01-18 17:57:41 -08003269 /**
3270 * An intent to launch instead of posting the notification to the status bar.
3271 * Only for use with extremely high-priority notifications demanding the user's
3272 * <strong>immediate</strong> attention, such as an incoming phone call or
3273 * alarm clock that the user has explicitly set to a particular time.
3274 * If this facility is used for something else, please give the user an option
3275 * to turn it off and use a normal notification, as this can be extremely
3276 * disruptive.
3277 *
Chris Wren47c20a12014-06-18 17:27:29 -04003278 * <p>
3279 * The system UI may choose to display a heads-up notification, instead of
3280 * launching this intent, while the user is using the device.
3281 * </p>
3282 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003283 * @param intent The pending intent to launch.
3284 * @param highPriority Passing true will cause this notification to be sent
3285 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003286 *
3287 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003288 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003289 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003290 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003291 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3292 return this;
3293 }
3294
Joe Onoratocb109a02011-01-18 17:57:41 -08003295 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003296 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003297 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003298 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003299 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003300 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003301 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003302 return this;
3303 }
3304
Joe Onoratocb109a02011-01-18 17:57:41 -08003305 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003306 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003307 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003308 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003309 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003310 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003311 setTicker(tickerText);
3312 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003313 return this;
3314 }
3315
Joe Onoratocb109a02011-01-18 17:57:41 -08003316 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003317 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003318 *
3319 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003320 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3321 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003322 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003323 public Builder setLargeIcon(Bitmap b) {
3324 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3325 }
3326
3327 /**
3328 * Add a large icon to the notification content view.
3329 *
3330 * In the platform template, this image will be shown on the left of the notification view
3331 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3332 * badge atop the large icon).
3333 */
3334 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003335 mN.mLargeIcon = icon;
3336 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003337 return this;
3338 }
3339
Joe Onoratocb109a02011-01-18 17:57:41 -08003340 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003341 * Set the sound to play.
3342 *
John Spurlockc0650f022014-07-19 13:22:39 -04003343 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3344 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003345 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003346 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003347 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003348 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003349 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003350 mN.sound = sound;
3351 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003352 return this;
3353 }
3354
Joe Onoratocb109a02011-01-18 17:57:41 -08003355 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003356 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003357 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003358 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3359 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003360 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003361 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003362 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003363 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003364 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003365 mN.sound = sound;
3366 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003367 return this;
3368 }
3369
Joe Onoratocb109a02011-01-18 17:57:41 -08003370 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003371 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3372 * use during playback.
3373 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003374 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003375 * @see Notification#sound
3376 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003377 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003378 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003379 mN.sound = sound;
3380 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003381 return this;
3382 }
3383
3384 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003385 * Set the vibration pattern to use.
3386 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003387 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3388 * <code>pattern</code> parameter.
3389 *
Chris Wren47c20a12014-06-18 17:27:29 -04003390 * <p>
3391 * A notification that vibrates is more likely to be presented as a heads-up notification.
3392 * </p>
3393 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003394 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003395 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003396 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003397 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003398 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003399 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003400 return this;
3401 }
3402
Joe Onoratocb109a02011-01-18 17:57:41 -08003403 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003404 * Set the desired color for the indicator LED on the device, as well as the
3405 * blink duty cycle (specified in milliseconds).
3406 *
3407
3408 * Not all devices will honor all (or even any) of these values.
3409 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003410 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003411 * @see Notification#ledARGB
3412 * @see Notification#ledOnMS
3413 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003414 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003415 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003416 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003417 mN.ledARGB = argb;
3418 mN.ledOnMS = onMs;
3419 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003420 if (onMs != 0 || offMs != 0) {
3421 mN.flags |= FLAG_SHOW_LIGHTS;
3422 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003423 return this;
3424 }
3425
Joe Onoratocb109a02011-01-18 17:57:41 -08003426 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003427 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003428 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003429
3430 * Ongoing notifications cannot be dismissed by the user, so your application or service
3431 * must take care of canceling them.
3432 *
3433
3434 * They are typically used to indicate a background task that the user is actively engaged
3435 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3436 * (e.g., a file download, sync operation, active network connection).
3437 *
3438
3439 * @see Notification#FLAG_ONGOING_EVENT
3440 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003441 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003442 public Builder setOngoing(boolean ongoing) {
3443 setFlag(FLAG_ONGOING_EVENT, ongoing);
3444 return this;
3445 }
3446
Joe Onoratocb109a02011-01-18 17:57:41 -08003447 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003448 * Set whether this notification should be colorized. When set, the color set with
3449 * {@link #setColor(int)} will be used as the background color of this notification.
3450 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003451 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3452 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003453 * <p>
Selim Cinek22714f12017-04-13 16:23:53 -07003454 * For most styles, the coloring will only be applied if the notification is for a
3455 * foreground service notification.
Selim Cinek99104832017-01-25 14:47:33 -08003456 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
Selim Cinek22714f12017-04-13 16:23:53 -07003457 * that have a media session attached there is no such requirement.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003458 *
Selim Cinek7b9605b2017-01-19 17:36:00 -08003459 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003460 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003461 */
3462 public Builder setColorized(boolean colorize) {
3463 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3464 return this;
3465 }
3466
3467 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003468 * Set this flag if you would only like the sound, vibrate
3469 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003470 *
3471 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003472 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003473 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3474 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3475 return this;
3476 }
3477
Joe Onoratocb109a02011-01-18 17:57:41 -08003478 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003479 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003480 *
3481 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003482 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003483 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003484 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003485 return this;
3486 }
3487
Joe Onoratocb109a02011-01-18 17:57:41 -08003488 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003489 * Set whether or not this notification should not bridge to other devices.
3490 *
3491 * <p>Some notifications can be bridged to other devices for remote display.
3492 * This hint can be set to recommend this notification not be bridged.
3493 */
3494 public Builder setLocalOnly(boolean localOnly) {
3495 setFlag(FLAG_LOCAL_ONLY, localOnly);
3496 return this;
3497 }
3498
3499 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003500 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003501 * <p>
3502 * The value should be one or more of the following fields combined with
3503 * bitwise-or:
3504 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3505 * <p>
3506 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003507 *
3508 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003509 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003510 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003511 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003512 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003513 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003514 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003515 return this;
3516 }
3517
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003518 /**
3519 * Set the priority of this notification.
3520 *
3521 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003522 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003523 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003524 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003525 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003526 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003527 return this;
3528 }
Joe Malin8d40d042012-11-05 11:36:40 -08003529
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003530 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003531 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003532 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003533 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003534 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003535 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003536 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003537 return this;
3538 }
3539
3540 /**
Chris Wrendde75302014-03-26 17:24:15 -04003541 * Add a person that is relevant to this notification.
3542 *
Chris Wrene6c48932014-09-29 17:19:27 -04003543 * <P>
3544 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003545 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3546 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3547 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003548 * </P>
3549 *
3550 * <P>
3551 * The person should be specified by the {@code String} representation of a
3552 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3553 * </P>
3554 *
3555 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3556 * URIs. The path part of these URIs must exist in the contacts database, in the
3557 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3558 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
3559 * </P>
3560 *
3561 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003562 * @see Notification#EXTRA_PEOPLE
3563 */
Chris Wrene6c48932014-09-29 17:19:27 -04003564 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003565 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04003566 return this;
3567 }
3568
3569 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003570 * Set this notification to be part of a group of notifications sharing the same key.
3571 * Grouped notifications may display in a cluster or stack on devices which
3572 * support such rendering.
3573 *
3574 * <p>To make this notification the summary for its group, also call
3575 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3576 * {@link #setSortKey}.
3577 * @param groupKey The group key of the group.
3578 * @return this object for method chaining
3579 */
3580 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003581 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003582 return this;
3583 }
3584
3585 /**
3586 * Set this notification to be the group summary for a group of notifications.
3587 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003588 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3589 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003590 * @param isGroupSummary Whether this notification should be a group summary.
3591 * @return this object for method chaining
3592 */
3593 public Builder setGroupSummary(boolean isGroupSummary) {
3594 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3595 return this;
3596 }
3597
3598 /**
3599 * Set a sort key that orders this notification among other notifications from the
3600 * same package. This can be useful if an external sort was already applied and an app
3601 * would like to preserve this. Notifications will be sorted lexicographically using this
3602 * value, although providing different priorities in addition to providing sort key may
3603 * cause this value to be ignored.
3604 *
3605 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003606 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003607 *
3608 * @see String#compareTo(String)
3609 */
3610 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003611 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003612 return this;
3613 }
3614
3615 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003616 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003617 *
Griff Hazen720042b2014-02-24 15:46:56 -08003618 * <p>Values within the Bundle will replace existing extras values in this Builder.
3619 *
3620 * @see Notification#extras
3621 */
Griff Hazen959591e2014-05-15 22:26:18 -07003622 public Builder addExtras(Bundle extras) {
3623 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003624 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003625 }
3626 return this;
3627 }
3628
3629 /**
3630 * Set metadata for this notification.
3631 *
3632 * <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 -04003633 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003634 * called.
3635 *
Griff Hazen720042b2014-02-24 15:46:56 -08003636 * <p>Replaces any existing extras values with those from the provided Bundle.
3637 * Use {@link #addExtras} to merge in metadata instead.
3638 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003639 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003640 */
Griff Hazen959591e2014-05-15 22:26:18 -07003641 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003642 if (extras != null) {
3643 mUserExtras = extras;
3644 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003645 return this;
3646 }
3647
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003648 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003649 * Get the current metadata Bundle used by this notification Builder.
3650 *
3651 * <p>The returned Bundle is shared with this Builder.
3652 *
3653 * <p>The current contents of this Bundle are copied into the Notification each time
3654 * {@link #build()} is called.
3655 *
3656 * @see Notification#extras
3657 */
3658 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003659 return mUserExtras;
3660 }
3661
3662 private Bundle getAllExtras() {
3663 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3664 saveExtras.putAll(mN.extras);
3665 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003666 }
3667
3668 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003669 * Add an action to this notification. Actions are typically displayed by
3670 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003671 * <p>
3672 * Every action must have an icon (32dp square and matching the
3673 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3674 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3675 * <p>
3676 * A notification in its expanded form can display up to 3 actions, from left to right in
3677 * the order they were added. Actions will not be displayed when the notification is
3678 * collapsed, however, so be sure that any essential functions may be accessed by the user
3679 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003680 *
3681 * @param icon Resource ID of a drawable that represents the action.
3682 * @param title Text describing the action.
3683 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003684 *
3685 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003686 */
Dan Sandler86647982015-05-13 23:41:13 -04003687 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003688 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003689 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003690 return this;
3691 }
3692
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003693 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003694 * Add an action to this notification. Actions are typically displayed by
3695 * the system as a button adjacent to the notification content.
3696 * <p>
3697 * Every action must have an icon (32dp square and matching the
3698 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3699 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3700 * <p>
3701 * A notification in its expanded form can display up to 3 actions, from left to right in
3702 * the order they were added. Actions will not be displayed when the notification is
3703 * collapsed, however, so be sure that any essential functions may be accessed by the user
3704 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3705 *
3706 * @param action The action to add.
3707 */
3708 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08003709 if (action != null) {
3710 mActions.add(action);
3711 }
Griff Hazen959591e2014-05-15 22:26:18 -07003712 return this;
3713 }
3714
3715 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003716 * Alter the complete list of actions attached to this notification.
3717 * @see #addAction(Action).
3718 *
3719 * @param actions
3720 * @return
3721 */
3722 public Builder setActions(Action... actions) {
3723 mActions.clear();
3724 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08003725 if (actions[i] != null) {
3726 mActions.add(actions[i]);
3727 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003728 }
3729 return this;
3730 }
3731
3732 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003733 * Add a rich notification style to be applied at build time.
3734 *
3735 * @param style Object responsible for modifying the notification style.
3736 */
3737 public Builder setStyle(Style style) {
3738 if (mStyle != style) {
3739 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003740 if (mStyle != null) {
3741 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003742 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3743 } else {
3744 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003745 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003746 }
3747 return this;
3748 }
3749
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003750 /**
3751 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003752 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003753 * @return The same Builder.
3754 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003755 public Builder setVisibility(@Visibility int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003756 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003757 return this;
3758 }
3759
3760 /**
3761 * Supply a replacement Notification whose contents should be shown in insecure contexts
3762 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3763 * @param n A replacement notification, presumably with some or all info redacted.
3764 * @return The same Builder.
3765 */
3766 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003767 if (n != null) {
3768 mN.publicVersion = new Notification();
3769 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3770 } else {
3771 mN.publicVersion = null;
3772 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003773 return this;
3774 }
3775
Griff Hazenb720abe2014-05-20 13:15:30 -07003776 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003777 * Apply an extender to this notification builder. Extenders may be used to add
3778 * metadata or change options on this builder.
3779 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003780 public Builder extend(Extender extender) {
3781 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003782 return this;
3783 }
3784
Dan Sandler4e787062015-06-17 15:09:48 -04003785 /**
3786 * @hide
3787 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003788 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003789 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003790 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003791 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003792 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003793 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003794 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003795 }
3796
Dan Sandler26e81cf2014-05-06 10:01:27 -04003797 /**
3798 * Sets {@link Notification#color}.
3799 *
3800 * @param argb The accent color to use
3801 *
3802 * @return The same Builder.
3803 */
Tor Norbye80756e32015-03-02 09:39:27 -08003804 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003805 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003806 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003807 return this;
3808 }
3809
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003810 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04003811 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
3812 // This user can never be a badged profile,
3813 // and also includes USER_ALL system notifications.
3814 return null;
3815 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02003816 // Note: This assumes that the current user can read the profile badge of the
3817 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003818 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003819 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003820 }
3821
3822 private Bitmap getProfileBadge() {
3823 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003824 if (badge == null) {
3825 return null;
3826 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003827 final int size = mContext.getResources().getDimensionPixelSize(
3828 R.dimen.notification_badge_size);
3829 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003830 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003831 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003832 badge.draw(canvas);
3833 return bitmap;
3834 }
3835
Selim Cinekc848c3a2016-01-13 15:27:30 -08003836 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003837 Bitmap profileBadge = getProfileBadge();
3838
Kenny Guy98193ea2014-07-24 19:54:37 +01003839 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003840 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3841 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003842 if (isColorized()) {
Sunny Goyal5b153922017-09-21 21:00:36 -07003843 contentView.setDrawableTint(R.id.profile_badge, false,
3844 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003845 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003846 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003847 }
3848
Christoph Studerfe718432014-09-01 18:21:18 +02003849 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003850 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003851 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003852 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003853 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003854 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003855 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003856 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003857 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003858 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003859 }
3860
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003861 /**
3862 * Resets the notification header to its original state
3863 */
3864 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07003865 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
3866 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08003867 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003868 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003869 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003870 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07003871 contentView.setTextViewText(R.id.header_text, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003872 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003873 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003874 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003875 contentView.setImageViewIcon(R.id.profile_badge, null);
3876 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003877 }
3878
3879 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003880 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
3881 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003882 }
3883
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003884 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003885 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003886 }
3887
3888 /**
3889 * @param hasProgress whether the progress bar should be shown and set
3890 */
3891 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003892 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
3893 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07003894 }
3895
Adrian Roos70d7aa32017-01-11 15:39:06 -08003896 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01003897 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003898
Christoph Studerfe718432014-09-01 18:21:18 +02003899 resetStandardTemplate(contentView);
3900
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003901 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003902 updateBackgroundColor(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08003903 bindNotificationHeader(contentView, p.ambient);
Selim Cinek88188f22017-09-19 16:46:56 -07003904 bindLargeIcon(contentView, p.hideLargeIcon, p.alwaysShowReply);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003905 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
3906 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003907 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07003908 contentView.setTextViewText(R.id.title, processTextSpans(p.title));
Adrian Roos72171622017-01-27 10:32:06 -08003909 if (!p.ambient) {
3910 setTextViewColorPrimary(contentView, R.id.title);
3911 }
Selim Cinek954cc232016-05-20 13:29:23 -07003912 contentView.setViewLayoutWidth(R.id.title, showProgress
3913 ? ViewGroup.LayoutParams.WRAP_CONTENT
3914 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08003915 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08003916 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003917 int textId = showProgress ? com.android.internal.R.id.text_line_1
3918 : com.android.internal.R.id.text;
Selim Cinek48f66b72017-08-18 16:17:51 -07003919 contentView.setTextViewText(textId, processTextSpans(p.text));
Adrian Roos72171622017-01-27 10:32:06 -08003920 if (!p.ambient) {
3921 setTextViewColorSecondary(contentView, textId);
3922 }
Selim Cinek41598732016-01-11 16:58:37 -08003923 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003924 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003925
Selim Cinek279fa862016-06-14 10:57:25 -07003926 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003927
Selim Cinek29603462015-11-17 19:04:39 -08003928 return contentView;
3929 }
3930
Selim Cinek48f66b72017-08-18 16:17:51 -07003931 private CharSequence processTextSpans(CharSequence text) {
3932 if (hasForegroundColor()) {
Selim Cinek87c31532017-08-18 18:53:44 -07003933 return NotificationColorUtil.clearColorSpans(text);
Selim Cinek48f66b72017-08-18 16:17:51 -07003934 }
3935 return text;
3936 }
3937
Selim Cinek7b9605b2017-01-19 17:36:00 -08003938 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
3939 ensureColors();
3940 contentView.setTextColor(id, mPrimaryTextColor);
3941 }
3942
Selim Cinek48f66b72017-08-18 16:17:51 -07003943 private boolean hasForegroundColor() {
3944 return mForegroundColor != COLOR_INVALID;
3945 }
3946
Selim Cinek389edcd2017-05-11 19:16:44 -07003947 /**
3948 * @return the primary text color
3949 * @hide
3950 */
3951 @VisibleForTesting
3952 public int getPrimaryTextColor() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08003953 ensureColors();
3954 return mPrimaryTextColor;
3955 }
3956
Selim Cinek389edcd2017-05-11 19:16:44 -07003957 /**
3958 * @return the secondary text color
3959 * @hide
3960 */
3961 @VisibleForTesting
3962 public int getSecondaryTextColor() {
3963 ensureColors();
3964 return mSecondaryTextColor;
3965 }
3966
Selim Cinek7b9605b2017-01-19 17:36:00 -08003967 private int getActionBarColor() {
3968 ensureColors();
3969 return mActionBarColor;
3970 }
3971
Selim Cinek622c64a2017-04-17 17:10:05 -07003972 private int getActionBarColorDeEmphasized() {
3973 int backgroundColor = getBackgroundColor();
3974 return NotificationColorUtil.getShiftedColor(backgroundColor, 12);
3975 }
3976
Selim Cinek7b9605b2017-01-19 17:36:00 -08003977 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
3978 ensureColors();
3979 contentView.setTextColor(id, mSecondaryTextColor);
3980 }
3981
3982 private void ensureColors() {
3983 int backgroundColor = getBackgroundColor();
3984 if (mPrimaryTextColor == COLOR_INVALID
3985 || mSecondaryTextColor == COLOR_INVALID
3986 || mActionBarColor == COLOR_INVALID
3987 || mTextColorsAreForBackground != backgroundColor) {
3988 mTextColorsAreForBackground = backgroundColor;
Selim Cinek48f66b72017-08-18 16:17:51 -07003989 if (!hasForegroundColor() || !isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07003990 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(mContext,
3991 backgroundColor);
3992 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(mContext,
3993 backgroundColor);
Selim Cinekac5f0272017-05-02 16:05:41 -07003994 if (backgroundColor != COLOR_DEFAULT
3995 && (mBackgroundColorHint != COLOR_INVALID || isColorized())) {
3996 mPrimaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
3997 mPrimaryTextColor, backgroundColor, 4.5);
3998 mSecondaryTextColor = NotificationColorUtil.findAlphaToMeetContrast(
3999 mSecondaryTextColor, backgroundColor, 4.5);
4000 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07004001 } else {
4002 double backLum = NotificationColorUtil.calculateLuminance(backgroundColor);
4003 double textLum = NotificationColorUtil.calculateLuminance(mForegroundColor);
4004 double contrast = NotificationColorUtil.calculateContrast(mForegroundColor,
4005 backgroundColor);
Selim Cinek389edcd2017-05-11 19:16:44 -07004006 // We only respect the given colors if worst case Black or White still has
4007 // contrast
4008 boolean backgroundLight = backLum > textLum
4009 && satisfiesTextContrast(backgroundColor, Color.BLACK)
4010 || backLum <= textLum
4011 && !satisfiesTextContrast(backgroundColor, Color.WHITE);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004012 if (contrast < 4.5f) {
Selim Cinek389edcd2017-05-11 19:16:44 -07004013 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004014 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4015 mForegroundColor,
4016 backgroundColor,
4017 true /* findFG */,
4018 4.5f);
4019 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004020 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_LIGHT);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004021 } else {
4022 mSecondaryTextColor =
4023 NotificationColorUtil.findContrastColorAgainstDark(
4024 mForegroundColor,
4025 backgroundColor,
4026 true /* findFG */,
4027 4.5f);
4028 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004029 mSecondaryTextColor, -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004030 }
4031 } else {
4032 mPrimaryTextColor = mForegroundColor;
4033 mSecondaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004034 mPrimaryTextColor, backgroundLight ? LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4035 : LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004036 if (NotificationColorUtil.calculateContrast(mSecondaryTextColor,
4037 backgroundColor) < 4.5f) {
4038 // oh well the secondary is not good enough
Selim Cinek389edcd2017-05-11 19:16:44 -07004039 if (backgroundLight) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07004040 mSecondaryTextColor = NotificationColorUtil.findContrastColor(
4041 mSecondaryTextColor,
4042 backgroundColor,
4043 true /* findFG */,
4044 4.5f);
4045 } else {
4046 mSecondaryTextColor
4047 = NotificationColorUtil.findContrastColorAgainstDark(
4048 mSecondaryTextColor,
4049 backgroundColor,
4050 true /* findFG */,
4051 4.5f);
4052 }
4053 mPrimaryTextColor = NotificationColorUtil.changeColorLightness(
Selim Cinek389edcd2017-05-11 19:16:44 -07004054 mSecondaryTextColor, backgroundLight
4055 ? -LIGHTNESS_TEXT_DIFFERENCE_LIGHT
4056 : -LIGHTNESS_TEXT_DIFFERENCE_DARK);
Selim Cinek5fb73f82017-04-20 16:55:38 -07004057 }
4058 }
4059 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004060 mActionBarColor = NotificationColorUtil.resolveActionBarColor(mContext,
4061 backgroundColor);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004062 }
4063 }
4064
4065 private void updateBackgroundColor(RemoteViews contentView) {
4066 if (isColorized()) {
4067 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
4068 getBackgroundColor());
4069 } else {
4070 // Clear it!
4071 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
4072 0);
4073 }
4074 }
4075
Selim Cinek860b6da2015-12-16 19:02:19 -08004076 /**
4077 * @param remoteView the remote view to update the minheight in
4078 * @param hasMinHeight does it have a mimHeight
4079 * @hide
4080 */
4081 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
4082 int minHeight = 0;
4083 if (hasMinHeight) {
4084 // we need to set the minHeight of the notification
4085 minHeight = mContext.getResources().getDimensionPixelSize(
4086 com.android.internal.R.dimen.notification_min_content_height);
4087 }
4088 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
4089 }
4090
Selim Cinek29603462015-11-17 19:04:39 -08004091 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004092 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
4093 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
4094 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4095 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08004096 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004097 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08004098 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004099 contentView.setProgressBackgroundTintList(
4100 R.id.progress, ColorStateList.valueOf(mContext.getColor(
4101 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08004102 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08004103 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004104 contentView.setProgressTintList(R.id.progress, colorStateList);
4105 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004106 }
Selim Cinek29603462015-11-17 19:04:39 -08004107 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004108 } else {
4109 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08004110 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07004111 }
Joe Onorato561d3852010-11-20 18:09:34 -08004112 }
4113
Selim Cinek88188f22017-09-19 16:46:56 -07004114 private void bindLargeIcon(RemoteViews contentView, boolean hideLargeIcon,
4115 boolean alwaysShowReply) {
Selim Cinek279fa862016-06-14 10:57:25 -07004116 if (mN.mLargeIcon == null && mN.largeIcon != null) {
4117 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
4118 }
Selim Cinek88188f22017-09-19 16:46:56 -07004119 boolean showLargeIcon = mN.mLargeIcon != null && !hideLargeIcon;
4120 if (showLargeIcon) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004121 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4122 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
4123 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004124 int endMargin = R.dimen.notification_content_picture_margin;
4125 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
4126 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
4127 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek88188f22017-09-19 16:46:56 -07004128 }
4129 // Bind the reply action
4130 Action action = findReplyAction();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004131
Selim Cinek88188f22017-09-19 16:46:56 -07004132 boolean actionVisible = action != null && (showLargeIcon || alwaysShowReply);
4133 int replyId = showLargeIcon ? R.id.reply_icon_action : R.id.right_icon;
4134 if (actionVisible) {
4135 // We're only showing the icon as big if we're hiding the large icon
4136 int contrastColor = resolveContrastColor();
4137 int iconColor;
4138 if (showLargeIcon) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004139 contentView.setDrawableTint(R.id.reply_icon_action,
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004140 true /* targetBackground */,
Sunny Goyal5b153922017-09-21 21:00:36 -07004141 contrastColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004142 contentView.setOnClickPendingIntent(R.id.right_icon,
4143 action.actionIntent);
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004144 contentView.setRemoteInputs(R.id.right_icon, action.mRemoteInputs);
Selim Cinek88188f22017-09-19 16:46:56 -07004145 iconColor = NotificationColorUtil.isColorLight(contrastColor)
4146 ? Color.BLACK : Color.WHITE;
4147 } else {
4148 contentView.setImageViewResource(R.id.right_icon,
4149 R.drawable.ic_reply_notification_large);
4150 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
4151 iconColor = contrastColor;
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004152 }
Selim Cinek88188f22017-09-19 16:46:56 -07004153 contentView.setDrawableTint(replyId,
4154 false /* targetBackground */,
4155 iconColor,
4156 PorterDuff.Mode.SRC_ATOP);
4157 contentView.setOnClickPendingIntent(replyId,
4158 action.actionIntent);
4159 contentView.setRemoteInputs(replyId, action.mRemoteInputs);
4160 } else {
4161 contentView.setRemoteInputs(R.id.right_icon, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004162 }
Selim Cinek88188f22017-09-19 16:46:56 -07004163 contentView.setViewVisibility(R.id.reply_icon_action, actionVisible && showLargeIcon
4164 ? View.VISIBLE
4165 : View.GONE);
4166 contentView.setViewVisibility(R.id.right_icon_container, actionVisible || showLargeIcon
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07004167 ? View.VISIBLE
4168 : View.GONE);
4169 }
4170
4171 private Action findReplyAction() {
4172 ArrayList<Action> actions = mActions;
4173 if (mOriginalActions != null) {
4174 actions = mOriginalActions;
4175 }
4176 int numActions = actions.size();
4177 for (int i = 0; i < numActions; i++) {
4178 Action action = actions.get(i);
4179 if (hasValidRemoteInput(action)) {
4180 return action;
4181 }
4182 }
4183 return null;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004184 }
4185
Adrian Roos487374f2017-01-11 15:48:14 -08004186 private void bindNotificationHeader(RemoteViews contentView, boolean ambient) {
4187 bindSmallIcon(contentView, ambient);
4188 bindHeaderAppName(contentView, ambient);
4189 if (!ambient) {
4190 // Ambient view does not have these
4191 bindHeaderText(contentView);
4192 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08004193 bindProfileBadge(contentView);
4194 }
Adrian Roosd83e9992017-03-16 15:17:57 -07004195 bindExpandButton(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004196 }
4197
4198 private void bindExpandButton(RemoteViews contentView) {
Selim Cinek99104832017-01-25 14:47:33 -08004199 int color = getPrimaryHighlightColor();
Sunny Goyal5b153922017-09-21 21:00:36 -07004200 contentView.setDrawableTint(R.id.expand_button, false, color,
4201 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004202 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08004203 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004204 }
4205
Selim Cinek99104832017-01-25 14:47:33 -08004206 /**
4207 * @return the color that is used as the first primary highlight color. This is applied
4208 * in several places like the action buttons or the app name in the header.
4209 */
4210 private int getPrimaryHighlightColor() {
4211 return isColorized() ? getPrimaryTextColor() : resolveContrastColor();
4212 }
4213
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004214 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
4215 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08004216 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004217 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004218 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
4219 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
4220 contentView.setLong(R.id.chronometer, "setBase",
4221 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
4222 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07004223 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07004224 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004225 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004226 } else {
4227 contentView.setViewVisibility(R.id.time, View.VISIBLE);
4228 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004229 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004230 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004231 } else {
4232 // We still want a time to be set but gone, such that we can show and hide it
4233 // on demand in case it's a child notification without anything in the header
4234 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004235 }
4236 }
4237
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004238 private void bindHeaderText(RemoteViews contentView) {
4239 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4240 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05004241 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004242 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05004243 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004244 if (headerText == null
4245 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4246 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
4247 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
4248 }
4249 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004250 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek48f66b72017-08-18 16:17:51 -07004251 contentView.setTextViewText(R.id.header_text, processTextSpans(
4252 processLegacyText(headerText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004253 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07004254 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
4255 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004256 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004257 }
4258 }
4259
Adrian Rooseba05822016-04-22 17:09:27 -07004260 /**
4261 * @hide
4262 */
4263 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004264 CharSequence name = null;
4265 final PackageManager pm = mContext.getPackageManager();
4266 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
4267 // only system packages which lump together a bunch of unrelated stuff
4268 // may substitute a different name to make the purpose of the
4269 // notification more clear. the correct package label should always
4270 // be accessible via SystemUI.
4271 final String pkg = mContext.getPackageName();
4272 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
4273 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
4274 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
4275 name = subName;
4276 } else {
4277 Log.w(TAG, "warning: pkg "
4278 + pkg + " attempting to substitute app name '" + subName
4279 + "' without holding perm "
4280 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
4281 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004282 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04004283 if (TextUtils.isEmpty(name)) {
4284 name = pm.getApplicationLabel(mContext.getApplicationInfo());
4285 }
4286 if (TextUtils.isEmpty(name)) {
4287 // still nothing?
4288 return null;
4289 }
4290
4291 return String.valueOf(name);
4292 }
Adrian Roos487374f2017-01-11 15:48:14 -08004293 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04004294 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08004295 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004296 setTextViewColorPrimary(contentView, R.id.app_name_text);
4297 } else {
4298 contentView.setTextColor(R.id.app_name_text,
4299 ambient ? resolveAmbientColor() : resolveContrastColor());
4300 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004301 }
4302
Adrian Roos487374f2017-01-11 15:48:14 -08004303 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07004304 if (mN.mSmallIcon == null && mN.icon != 0) {
4305 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
4306 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004307 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Sunny Goyal5b153922017-09-21 21:00:36 -07004308 contentView.setInt(R.id.icon, "setImageLevel", mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08004309 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004310 }
4311
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004312 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004313 * @return true if the built notification will show the time or the chronometer; false
4314 * otherwise
4315 */
4316 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07004317 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004318 }
4319
Christoph Studerfe718432014-09-01 18:21:18 +02004320 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004321 // actions_container is only reset when there are no actions to avoid focus issues with
4322 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02004323 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02004324 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08004325
4326 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
4327 big.setTextViewText(R.id.notification_material_reply_text_1, null);
4328
4329 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
4330 big.setTextViewText(R.id.notification_material_reply_text_2, null);
4331 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
4332 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07004333
4334 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02004335 }
4336
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004337 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08004338 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07004339 }
4340
Adrian Roos70d7aa32017-01-11 15:39:06 -08004341 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4342 StandardTemplateParams p) {
4343 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004344
Christoph Studerfe718432014-09-01 18:21:18 +02004345 resetStandardTemplateWithActions(big);
4346
Adrian Roose458aa82015-12-08 16:17:19 -08004347 boolean validRemoteInput = false;
4348
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004349 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004350 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004351 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004352 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004353 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004354 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roos487374f2017-01-11 15:48:14 -08004355 if (p.ambient) {
4356 big.setInt(R.id.actions, "setBackgroundColor", Color.TRANSPARENT);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004357 } else if (isColorized()) {
4358 big.setInt(R.id.actions, "setBackgroundColor", getActionBarColor());
4359 } else {
4360 big.setInt(R.id.actions, "setBackgroundColor", mContext.getColor(
4361 R.color.notification_action_list));
Adrian Roos487374f2017-01-11 15:48:14 -08004362 }
Adrian Roosf852a422016-06-03 13:33:43 -07004363 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4364 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004365 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004366 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004367 Action action = mActions.get(i);
4368 validRemoteInput |= hasValidRemoteInput(action);
4369
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004370 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004371 i % 2 != 0, p.ambient);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004372 big.addView(R.id.actions, button);
4373 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004374 } else {
4375 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004376 }
Adrian Roose458aa82015-12-08 16:17:19 -08004377
4378 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004379 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08004380 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
4381 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004382 big.setTextViewText(R.id.notification_material_reply_text_1,
4383 processTextSpans(replyText[0]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004384 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08004385
4386 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
4387 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004388 big.setTextViewText(R.id.notification_material_reply_text_2,
4389 processTextSpans(replyText[1]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004390 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004391
4392 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
4393 big.setViewVisibility(
4394 R.id.notification_material_reply_text_3, View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07004395 big.setTextViewText(R.id.notification_material_reply_text_3,
4396 processTextSpans(replyText[2]));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004397 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004398 }
4399 }
4400 }
4401
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004402 return big;
4403 }
4404
Adrian Roose458aa82015-12-08 16:17:19 -08004405 private boolean hasValidRemoteInput(Action action) {
4406 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4407 // Weird actions
4408 return false;
4409 }
4410
4411 RemoteInput[] remoteInputs = action.getRemoteInputs();
4412 if (remoteInputs == null) {
4413 return false;
4414 }
4415
4416 for (RemoteInput r : remoteInputs) {
4417 CharSequence[] choices = r.getChoices();
4418 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4419 return true;
4420 }
4421 }
4422 return false;
4423 }
4424
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004425 /**
4426 * Construct a RemoteViews for the final 1U notification layout. In order:
4427 * 1. Custom contentView from the caller
4428 * 2. Style's proposed content view
4429 * 3. Standard template view
4430 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004431 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004432 return createContentView(false /* increasedheight */ );
4433 }
4434
4435 /**
4436 * Construct a RemoteViews for the smaller content view.
4437 *
4438 * @param increasedHeight true if this layout be created with an increased height. Some
4439 * styles may support showing more then just that basic 1U size
4440 * and the system may decide to render important notifications
4441 * slightly bigger even when collapsed.
4442 *
4443 * @hide
4444 */
4445 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004446 if (mN.contentView != null && useExistingRemoteView()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004447 return mN.contentView;
4448 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004449 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004450 if (styleView != null) {
4451 return styleView;
4452 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004453 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004454 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004455 }
4456
Selim Cineka7679b62017-05-10 16:33:25 -07004457 private boolean useExistingRemoteView() {
4458 return mStyle == null || (!mStyle.displayCustomViewInline()
4459 && !mRebuildStyledRemoteViews);
4460 }
4461
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004462 /**
4463 * Construct a RemoteViews for the final big notification layout.
4464 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004465 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004466 RemoteViews result = null;
Selim Cineka7679b62017-05-10 16:33:25 -07004467 if (mN.bigContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004468 return mN.bigContentView;
4469 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004470 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004471 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004472 } else if (mActions.size() != 0) {
4473 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004474 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004475 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004476 return result;
4477 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004478
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004479 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004480 * Construct a RemoteViews for the final notification header only. This will not be
4481 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004482 *
Adrian Roos6f6e1592017-05-02 16:22:53 -07004483 * @param ambient if true, generate the header for the ambient display layout.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004484 * @hide
4485 */
Adrian Roos6f6e1592017-05-02 16:22:53 -07004486 public RemoteViews makeNotificationHeader(boolean ambient) {
Selim Cinek414ad332017-02-24 19:06:12 -08004487 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4488 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004489 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
Adrian Roos6f6e1592017-05-02 16:22:53 -07004490 ambient ? R.layout.notification_template_ambient_header
4491 : R.layout.notification_template_header);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004492 resetNotificationHeader(header);
Adrian Roos6f6e1592017-05-02 16:22:53 -07004493 bindNotificationHeader(header, ambient);
Selim Cinek414ad332017-02-24 19:06:12 -08004494 if (colorized != null) {
4495 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4496 } else {
4497 mN.extras.remove(EXTRA_COLORIZED);
4498 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004499 return header;
4500 }
4501
Adrian Roos487374f2017-01-11 15:48:14 -08004502 /**
4503 * Construct a RemoteViews for the ambient version of the notification.
4504 *
4505 * @hide
4506 */
4507 public RemoteViews makeAmbientNotification() {
4508 RemoteViews ambient = applyStandardTemplateWithActions(
4509 R.layout.notification_template_material_ambient,
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004510 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false));
Adrian Roos487374f2017-01-11 15:48:14 -08004511 return ambient;
4512 }
4513
Selim Cinek29603462015-11-17 19:04:39 -08004514 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004515 if (result != null) {
4516 result.setViewVisibility(R.id.text_line_1, View.GONE);
4517 }
Selim Cinek29603462015-11-17 19:04:39 -08004518 }
4519
Selim Cinek6743c0b2017-01-18 18:24:01 -08004520 /**
4521 * Adapt the Notification header if this view is used as an expanded view.
4522 *
4523 * @hide
4524 */
4525 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004526 if (result != null) {
4527 result.setBoolean(R.id.notification_header, "setExpanded", true);
4528 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004529 }
4530
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004531 /**
4532 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004533 *
4534 * @param increasedHeight true if this layout be created with an increased height. Some
4535 * styles may support showing more then just that basic 1U size
4536 * and the system may decide to render important notifications
4537 * slightly bigger even when collapsed.
4538 *
4539 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004540 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004541 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cineka7679b62017-05-10 16:33:25 -07004542 if (mN.headsUpContentView != null && useExistingRemoteView()) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004543 return mN.headsUpContentView;
4544 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004545 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4546 if (styleView != null) {
4547 return styleView;
4548 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004549 } else if (mActions.size() == 0) {
4550 return null;
4551 }
4552
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004553 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004554 }
4555
Selim Cinek624c02db2015-12-14 21:00:02 -08004556 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08004557 * Construct a RemoteViews for the final heads-up notification layout.
4558 */
4559 public RemoteViews createHeadsUpContentView() {
4560 return createHeadsUpContentView(false /* useIncreasedHeight */);
4561 }
4562
4563 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08004564 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4565 *
4566 * @hide
4567 */
4568 public RemoteViews makePublicContentView() {
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004569 return makePublicView(false /* ambient */);
4570 }
4571
4572 /**
4573 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4574 *
4575 * @hide
4576 */
4577 public RemoteViews makePublicAmbientNotification() {
4578 return makePublicView(true /* ambient */);
4579 }
4580
4581 private RemoteViews makePublicView(boolean ambient) {
Selim Cinek624c02db2015-12-14 21:00:02 -08004582 if (mN.publicVersion != null) {
4583 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004584 return ambient ? builder.makeAmbientNotification() : builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004585 }
4586 Bundle savedBundle = mN.extras;
4587 Style style = mStyle;
4588 mStyle = null;
4589 Icon largeIcon = mN.mLargeIcon;
4590 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004591 Bitmap largeIconLegacy = mN.largeIcon;
4592 mN.largeIcon = null;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004593 ArrayList<Action> actions = mActions;
4594 mActions = new ArrayList<>();
Selim Cinek624c02db2015-12-14 21:00:02 -08004595 Bundle publicExtras = new Bundle();
4596 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
4597 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
4598 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
4599 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07004600 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
4601 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08004602 mN.extras = publicExtras;
Selim Cinek499c20f2017-07-20 14:06:09 -07004603 RemoteViews view;
4604 if (ambient) {
4605 publicExtras.putCharSequence(EXTRA_TITLE,
4606 mContext.getString(com.android.internal.R.string.notification_hidden_text));
4607 view = makeAmbientNotification();
4608 } else{
4609 view = makeNotificationHeader(false /* ambient */);
4610 view.setBoolean(R.id.notification_header, "setExpandOnlyOnButton", true);
4611 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004612 mN.extras = savedBundle;
4613 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07004614 mN.largeIcon = largeIconLegacy;
Adrian Roosb19b06b2017-05-02 18:54:40 -07004615 mActions = actions;
Selim Cinek624c02db2015-12-14 21:00:02 -08004616 mStyle = style;
Adrian Roos1a1ecfc2017-04-17 11:17:59 -07004617 return view;
Selim Cinek624c02db2015-12-14 21:00:02 -08004618 }
4619
Selim Cinek6743c0b2017-01-18 18:24:01 -08004620 /**
4621 * Construct a content view for the display when low - priority
4622 *
4623 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
4624 * a new subtext is created consisting of the content of the
4625 * notification.
4626 * @hide
4627 */
4628 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
4629 int color = mN.color;
4630 mN.color = COLOR_DEFAULT;
4631 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4632 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
4633 CharSequence newSummary = createSummaryText();
4634 if (!TextUtils.isEmpty(newSummary)) {
4635 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
4636 }
4637 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004638
Adrian Roos6f6e1592017-05-02 16:22:53 -07004639 RemoteViews header = makeNotificationHeader(false /* ambient */);
Selim Cinek1b554392017-02-28 17:22:49 -08004640 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08004641 if (summary != null) {
4642 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
4643 } else {
4644 mN.extras.remove(EXTRA_SUB_TEXT);
4645 }
4646 mN.color = color;
4647 return header;
4648 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004649
Selim Cinek6743c0b2017-01-18 18:24:01 -08004650 private CharSequence createSummaryText() {
4651 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
4652 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
4653 return titleText;
4654 }
4655 SpannableStringBuilder summary = new SpannableStringBuilder();
4656 if (titleText == null) {
4657 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
4658 }
4659 BidiFormatter bidi = BidiFormatter.getInstance();
4660 if (titleText != null) {
4661 summary.append(bidi.unicodeWrap(titleText));
4662 }
4663 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
4664 if (titleText != null && contentText != null) {
4665 summary.append(bidi.unicodeWrap(mContext.getText(
4666 R.string.notification_header_divider_symbol_with_spaces)));
4667 }
4668 if (contentText != null) {
4669 summary.append(bidi.unicodeWrap(contentText));
4670 }
4671 return summary;
4672 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05004673
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004674 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004675 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04004676 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004677 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004678 emphazisedMode ? getEmphasizedActionLayoutResource()
4679 : tombstone ? getActionTombstoneLayoutResource()
4680 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04004681 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04004682 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04004683 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004684 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004685 if (action.mRemoteInputs != null) {
4686 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
4687 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004688 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004689 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07004690 // change the background bgColor
Selim Cinek622c64a2017-04-17 17:10:05 -07004691 int bgColor;
4692 if (isColorized()) {
4693 bgColor = oddAction ? getActionBarColor() : getActionBarColorDeEmphasized();
4694 } else {
4695 bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
4696 : R.color.notification_action_list_dark);
4697 }
Sunny Goyal5b153922017-09-21 21:00:36 -07004698 button.setDrawableTint(R.id.button_holder, true,
4699 bgColor, PorterDuff.Mode.SRC_ATOP);
Selim Cinek981962e2016-07-20 20:41:58 -07004700 CharSequence title = action.title;
4701 ColorStateList[] outResultColor = null;
4702 if (isLegacy()) {
Selim Cinek87c31532017-08-18 18:53:44 -07004703 title = NotificationColorUtil.clearColorSpans(title);
Selim Cinek981962e2016-07-20 20:41:58 -07004704 } else {
4705 outResultColor = new ColorStateList[1];
4706 title = ensureColorSpanContrast(title, bgColor, outResultColor);
4707 }
Selim Cinek48f66b72017-08-18 16:17:51 -07004708 button.setTextViewText(R.id.action0, processTextSpans(title));
Selim Cinek7b9605b2017-01-19 17:36:00 -08004709 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07004710 if (outResultColor != null && outResultColor[0] != null) {
4711 // We need to set the text color as well since changing a text to uppercase
4712 // clears its spans.
4713 button.setTextColor(R.id.action0, outResultColor[0]);
Anthony Chenad4d1582017-04-10 16:07:58 -07004714 } else if (mN.color != COLOR_DEFAULT && !isColorized() && mTintActionButtons) {
Selim Cinek981962e2016-07-20 20:41:58 -07004715 button.setTextColor(R.id.action0,resolveContrastColor());
4716 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004717 } else {
Selim Cinek48f66b72017-08-18 16:17:51 -07004718 button.setTextViewText(R.id.action0, processTextSpans(
4719 processLegacyText(action.title)));
Adrian Roos72171622017-01-27 10:32:06 -08004720 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004721 setTextViewColorPrimary(button, R.id.action0);
Anthony Chenad4d1582017-04-10 16:07:58 -07004722 } else if (mN.color != COLOR_DEFAULT && mTintActionButtons) {
Adrian Roos487374f2017-01-11 15:48:14 -08004723 button.setTextColor(R.id.action0,
4724 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004725 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004726 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004727 return button;
4728 }
4729
Joe Onoratocb109a02011-01-18 17:57:41 -08004730 /**
Selim Cinek981962e2016-07-20 20:41:58 -07004731 * Ensures contrast on color spans against a background color. also returns the color of the
4732 * text if a span was found that spans over the whole text.
4733 *
4734 * @param charSequence the charSequence on which the spans are
4735 * @param background the background color to ensure the contrast against
4736 * @param outResultColor an array in which a color will be returned as the first element if
4737 * there exists a full length color span.
4738 * @return the contrasted charSequence
4739 */
4740 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
4741 ColorStateList[] outResultColor) {
4742 if (charSequence instanceof Spanned) {
4743 Spanned ss = (Spanned) charSequence;
4744 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4745 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4746 for (Object span : spans) {
4747 Object resultSpan = span;
4748 int spanStart = ss.getSpanStart(span);
4749 int spanEnd = ss.getSpanEnd(span);
4750 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
4751 if (resultSpan instanceof CharacterStyle) {
4752 resultSpan = ((CharacterStyle) span).getUnderlying();
4753 }
4754 if (resultSpan instanceof TextAppearanceSpan) {
4755 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4756 ColorStateList textColor = originalSpan.getTextColor();
4757 if (textColor != null) {
4758 int[] colors = textColor.getColors();
4759 int[] newColors = new int[colors.length];
4760 for (int i = 0; i < newColors.length; i++) {
4761 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07004762 colors[i], background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07004763 }
4764 textColor = new ColorStateList(textColor.getStates().clone(),
4765 newColors);
4766 resultSpan = new TextAppearanceSpan(
4767 originalSpan.getFamily(),
4768 originalSpan.getTextStyle(),
4769 originalSpan.getTextSize(),
4770 textColor,
4771 originalSpan.getLinkTextColor());
4772 if (fullLength) {
4773 outResultColor[0] = new ColorStateList(
4774 textColor.getStates().clone(), newColors);
4775 }
4776 }
4777 } else if (resultSpan instanceof ForegroundColorSpan) {
4778 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
4779 int foregroundColor = originalSpan.getForegroundColor();
4780 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
Anthony Chenad4d1582017-04-10 16:07:58 -07004781 foregroundColor, background, mInNightMode);
Selim Cinek981962e2016-07-20 20:41:58 -07004782 resultSpan = new ForegroundColorSpan(foregroundColor);
4783 if (fullLength) {
4784 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
4785 }
4786 } else {
4787 resultSpan = span;
4788 }
4789
4790 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
4791 }
4792 return builder;
4793 }
4794 return charSequence;
4795 }
4796
4797 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004798 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07004799 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004800 */
4801 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004802 if (!mIsLegacyInitialized) {
4803 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
4804 < Build.VERSION_CODES.LOLLIPOP;
4805 mIsLegacyInitialized = true;
4806 }
4807 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004808 }
4809
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004810 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004811 return processLegacyText(charSequence, false /* ambient */);
4812 }
4813
4814 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
4815 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
4816 boolean wantLightText = ambient;
4817 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004818 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004819 } else {
4820 return charSequence;
4821 }
4822 }
4823
Dan Sandler26e81cf2014-05-06 10:01:27 -04004824 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004825 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04004826 */
Adrian Roos487374f2017-01-11 15:48:14 -08004827 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
4828 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08004829 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinek99104832017-01-25 14:47:33 -08004830 int color = ambient ? resolveAmbientColor() : getPrimaryHighlightColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08004831 if (colorable) {
Sunny Goyal5b153922017-09-21 21:00:36 -07004832 contentView.setDrawableTint(R.id.icon, false, color,
4833 PorterDuff.Mode.SRC_ATOP);
Selim Cinekea4bef72015-12-02 15:51:10 -08004834
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004835 }
Selim Cinekea4bef72015-12-02 15:51:10 -08004836 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08004837 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004838 }
4839
Dan Sandler26e81cf2014-05-06 10:01:27 -04004840 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004841 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04004842 * if it's grayscale).
4843 */
4844 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04004845 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
4846 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004847 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004848 // resolve color will fall back to the default when legacy
Sunny Goyal5b153922017-09-21 21:00:36 -07004849 contentView.setDrawableTint(R.id.icon, false, resolveContrastColor(),
4850 PorterDuff.Mode.SRC_ATOP);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004851 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004852 }
4853
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004854 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004855 if (mN.color != COLOR_DEFAULT) {
4856 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02004857 }
Jorim Jaggi74419312014-06-10 20:57:21 +02004858 }
4859
Adrian Roos4ff3b122016-02-01 12:26:13 -08004860 int resolveContrastColor() {
4861 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
4862 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004863 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004864
Selim Cinekac5f0272017-05-02 16:05:41 -07004865 int color;
4866 int background = mBackgroundColorHint;
4867 if (mBackgroundColorHint == COLOR_INVALID) {
4868 background = mContext.getColor(
4869 com.android.internal.R.color.notification_material_background_color);
4870 }
4871 if (mN.color == COLOR_DEFAULT) {
4872 ensureColors();
4873 color = mSecondaryTextColor;
4874 } else {
4875 color = NotificationColorUtil.resolveContrastColor(mContext, mN.color,
Anthony Chenad4d1582017-04-10 16:07:58 -07004876 background, mInNightMode);
Selim Cinekac5f0272017-05-02 16:05:41 -07004877 }
4878 if (Color.alpha(color) < 255) {
4879 // alpha doesn't go well for color filters, so let's blend it manually
4880 color = NotificationColorUtil.compositeColors(color, background);
4881 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004882 mCachedContrastColorIsFor = mN.color;
Selim Cinekac5f0272017-05-02 16:05:41 -07004883 return mCachedContrastColor = color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004884 }
4885
Adrian Roos487374f2017-01-11 15:48:14 -08004886 int resolveAmbientColor() {
4887 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
4888 return mCachedAmbientColor;
4889 }
4890 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
4891
4892 mCachedAmbientColorIsFor = mN.color;
4893 return mCachedAmbientColor = contrasted;
4894 }
4895
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004896 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004897 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004898 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08004899 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004900 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004901 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004902 mN.actions = new Action[mActions.size()];
4903 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004904 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004905 if (!mPersonList.isEmpty()) {
4906 mN.extras.putStringArray(EXTRA_PEOPLE,
4907 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004908 }
Selim Cinek247fa012016-02-18 09:50:48 -08004909 if (mN.bigContentView != null || mN.contentView != null
4910 || mN.headsUpContentView != null) {
4911 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
4912 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004913 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08004914 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004915
Julia Reynolds3b848122016-02-26 10:45:32 -05004916 /**
4917 * Creates a Builder from an existing notification so further changes can be made.
4918 * @param context The context for your application / activity.
4919 * @param n The notification to create a Builder from.
4920 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004921 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004922 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004923 ApplicationInfo applicationInfo = n.extras.getParcelable(
4924 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004925 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05004926 if (applicationInfo != null) {
4927 try {
4928 builderContext = context.createApplicationContext(applicationInfo,
4929 Context.CONTEXT_RESTRICTED);
4930 } catch (NameNotFoundException e) {
4931 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
4932 builderContext = context; // try with our context
4933 }
4934 } else {
4935 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02004936 }
4937
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004938 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004939 }
4940
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004941 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004942 * @deprecated Use {@link #build()} instead.
4943 */
4944 @Deprecated
4945 public Notification getNotification() {
4946 return build();
4947 }
4948
4949 /**
4950 * Combine all of the options that have been set and return a new {@link Notification}
4951 * object.
4952 */
4953 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004954 // first, add any extras from the calling code
4955 if (mUserExtras != null) {
4956 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004957 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004958
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004959 mN.creationTime = System.currentTimeMillis();
4960
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004961 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05004962 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02004963
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004964 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004965
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004966 if (mStyle != null) {
Selim Cinekd0426622017-07-11 13:19:59 +02004967 mStyle.reduceImageSizes(mContext);
4968 mStyle.purgeResources();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004969 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004970 }
Selim Cinekd0426622017-07-11 13:19:59 +02004971 mN.reduceImageSizes(mContext);
4972
Adrian Roos5081c0d2016-02-26 16:04:19 -08004973 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
Selim Cineka7679b62017-05-10 16:33:25 -07004974 && (useExistingRemoteView())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004975 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004976 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004977 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
4978 mN.contentView.getSequenceNumber());
4979 }
4980 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004981 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004982 if (mN.bigContentView != null) {
4983 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
4984 mN.bigContentView.getSequenceNumber());
4985 }
4986 }
4987 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004988 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004989 if (mN.headsUpContentView != null) {
4990 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
4991 mN.headsUpContentView.getSequenceNumber());
4992 }
4993 }
4994 }
4995
Julia Reynolds4c0c2022016-02-02 15:11:59 -05004996 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
4997 mN.flags |= FLAG_SHOW_LIGHTS;
4998 }
4999
Adrian Roosfb921842017-10-26 14:49:56 +02005000 mN.allPendingIntents = null;
5001
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005002 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005003 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005004
5005 /**
5006 * Apply this Builder to an existing {@link Notification} object.
5007 *
5008 * @hide
5009 */
5010 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04005011 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05005012 return n;
5013 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005014
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005015 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08005016 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005017 * change. Also removes extenders on low ram devices, as
5018 * {@link android.service.notification.NotificationListenerService} services are disabled.
Adrian Roos184bfe022016-03-03 13:41:44 -08005019 *
5020 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
5021 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005022 * @hide
5023 */
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005024 public static Notification maybeCloneStrippedForDelivery(Notification n, boolean isLowRam) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005025 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08005026
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005027 // Only strip views for known Styles because we won't know how to
5028 // re-create them otherwise.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005029 if (!isLowRam
5030 && !TextUtils.isEmpty(templateClass)
Adrian Roos184bfe022016-03-03 13:41:44 -08005031 && getNotificationStyleClass(templateClass) == null) {
5032 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005033 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005034
5035 // Only strip unmodified BuilderRemoteViews.
5036 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005037 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005038 n.contentView.getSequenceNumber();
5039 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005040 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005041 n.bigContentView.getSequenceNumber();
5042 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005043 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08005044 n.headsUpContentView.getSequenceNumber();
5045
5046 // Nothing to do here, no need to clone.
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005047 if (!isLowRam
5048 && !stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
Adrian Roos184bfe022016-03-03 13:41:44 -08005049 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005050 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005051
5052 Notification clone = n.clone();
5053 if (stripContentView) {
5054 clone.contentView = null;
5055 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
5056 }
5057 if (stripBigContentView) {
5058 clone.bigContentView = null;
5059 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
5060 }
5061 if (stripHeadsUpContentView) {
5062 clone.headsUpContentView = null;
5063 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
5064 }
Julia Reynolds8a3b4592017-06-26 17:15:14 -04005065 if (isLowRam) {
5066 clone.extras.remove(Notification.TvExtender.EXTRA_TV_EXTENDER);
5067 clone.extras.remove(WearableExtender.EXTRA_WEARABLE_EXTENSIONS);
5068 clone.extras.remove(CarExtender.EXTRA_CAR_EXTENDER);
5069 }
Adrian Roos184bfe022016-03-03 13:41:44 -08005070 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05005071 }
5072
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005073 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005074 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005075 }
5076
5077 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005078 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005079 }
5080
5081 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005082 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005083 }
5084
5085 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02005086 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005087 }
5088
5089 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005090 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005091 }
5092
Adrian Roosc1a80b02016-04-05 14:54:55 -07005093 private int getMessagingLayoutResource() {
5094 return R.layout.notification_template_material_messaging;
5095 }
5096
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005097 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005098 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005099 }
5100
Selim Cinek06e9e1f2016-07-08 17:14:16 -07005101 private int getEmphasizedActionLayoutResource() {
5102 return R.layout.notification_material_action_emphasized;
5103 }
5104
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005105 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07005106 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005107 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005108
5109 private int getBackgroundColor() {
5110 if (isColorized()) {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005111 return mBackgroundColor != COLOR_INVALID ? mBackgroundColor : mN.color;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005112 } else {
Selim Cinekac5f0272017-05-02 16:05:41 -07005113 return mBackgroundColorHint != COLOR_INVALID ? mBackgroundColorHint
5114 : COLOR_DEFAULT;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005115 }
5116 }
5117
5118 private boolean isColorized() {
5119 return mN.isColorized();
5120 }
Selim Cinek99104832017-01-25 14:47:33 -08005121
Anthony Chenad4d1582017-04-10 16:07:58 -07005122 private boolean shouldTintActionButtons() {
5123 return mTintActionButtons;
5124 }
5125
Selim Cinek99104832017-01-25 14:47:33 -08005126 private boolean textColorsNeedInversion() {
5127 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
5128 return false;
5129 }
5130 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
5131 return targetSdkVersion > Build.VERSION_CODES.M
5132 && targetSdkVersion < Build.VERSION_CODES.O;
5133 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005134
5135 /**
5136 * Set a color palette to be used as the background and textColors
5137 *
5138 * @param backgroundColor the color to be used as the background
5139 * @param foregroundColor the color to be used as the foreground
5140 *
5141 * @hide
5142 */
5143 public void setColorPalette(int backgroundColor, int foregroundColor) {
5144 mBackgroundColor = backgroundColor;
5145 mForegroundColor = foregroundColor;
5146 mTextColorsAreForBackground = COLOR_INVALID;
5147 ensureColors();
5148 }
Selim Cinekac5f0272017-05-02 16:05:41 -07005149
5150 /**
5151 * Sets the background color for this notification to be a different one then the default.
5152 * This is mainly used to calculate contrast and won't necessarily be applied to the
5153 * background.
5154 *
5155 * @hide
5156 */
5157 public void setBackgroundColorHint(int backgroundColor) {
5158 mBackgroundColorHint = backgroundColor;
5159 }
Selim Cineka7679b62017-05-10 16:33:25 -07005160
5161
5162 /**
5163 * Forces all styled remoteViews to be built from scratch and not use any cached
5164 * RemoteViews.
5165 * This is needed for legacy apps that are baking in their remoteviews into the
5166 * notification.
5167 *
5168 * @hide
5169 */
5170 public void setRebuildStyledRemoteViews(boolean rebuild) {
5171 mRebuildStyledRemoteViews = rebuild;
5172 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08005173 }
5174
5175 /**
Selim Cinekd0426622017-07-11 13:19:59 +02005176 * Reduces the image sizes to conform to a maximum allowed size. This also processes all custom
5177 * remote views.
5178 *
5179 * @hide
5180 */
5181 void reduceImageSizes(Context context) {
5182 if (extras.getBoolean(EXTRA_REDUCED_IMAGES)) {
5183 return;
5184 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005185 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005186 if (mLargeIcon != null || largeIcon != null) {
5187 Resources resources = context.getResources();
5188 Class<? extends Style> style = getNotificationStyle();
Selim Cineka8cb1262017-08-15 16:53:44 -07005189 int maxWidth = resources.getDimensionPixelSize(isLowRam
5190 ? R.dimen.notification_right_icon_size_low_ram
5191 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005192 int maxHeight = maxWidth;
5193 if (MediaStyle.class.equals(style)
5194 || DecoratedMediaCustomViewStyle.class.equals(style)) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005195 maxHeight = resources.getDimensionPixelSize(isLowRam
5196 ? R.dimen.notification_media_image_max_height_low_ram
5197 : R.dimen.notification_media_image_max_height);
5198 maxWidth = resources.getDimensionPixelSize(isLowRam
5199 ? R.dimen.notification_media_image_max_width_low_ram
5200 : R.dimen.notification_media_image_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005201 }
5202 if (mLargeIcon != null) {
5203 mLargeIcon.scaleDownIfNecessary(maxWidth, maxHeight);
5204 }
5205 if (largeIcon != null) {
5206 largeIcon = Icon.scaleDownIfNecessary(largeIcon, maxWidth, maxHeight);
5207 }
5208 }
Selim Cineka8cb1262017-08-15 16:53:44 -07005209 reduceImageSizesForRemoteView(contentView, context, isLowRam);
5210 reduceImageSizesForRemoteView(headsUpContentView, context, isLowRam);
5211 reduceImageSizesForRemoteView(bigContentView, context, isLowRam);
Selim Cinekd0426622017-07-11 13:19:59 +02005212 extras.putBoolean(EXTRA_REDUCED_IMAGES, true);
5213 }
5214
Selim Cineka8cb1262017-08-15 16:53:44 -07005215 private void reduceImageSizesForRemoteView(RemoteViews remoteView, Context context,
5216 boolean isLowRam) {
Selim Cinekd0426622017-07-11 13:19:59 +02005217 if (remoteView != null) {
5218 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005219 int maxWidth = resources.getDimensionPixelSize(isLowRam
5220 ? R.dimen.notification_custom_view_max_image_width_low_ram
5221 : R.dimen.notification_custom_view_max_image_width);
5222 int maxHeight = resources.getDimensionPixelSize(isLowRam
5223 ? R.dimen.notification_custom_view_max_image_height_low_ram
5224 : R.dimen.notification_custom_view_max_image_height);
Selim Cinekd0426622017-07-11 13:19:59 +02005225 remoteView.reduceImageSizes(maxWidth, maxHeight);
5226 }
5227 }
5228
5229 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005230 * @return whether this notification is a foreground service notification
Selim Cinek7b9605b2017-01-19 17:36:00 -08005231 */
Selim Cinek22714f12017-04-13 16:23:53 -07005232 private boolean isForegroundService() {
5233 return (flags & Notification.FLAG_FOREGROUND_SERVICE) != 0;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005234 }
5235
5236 /**
Selim Cinek99104832017-01-25 14:47:33 -08005237 * @return whether this notification has a media session attached
5238 * @hide
5239 */
5240 public boolean hasMediaSession() {
5241 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
5242 }
5243
5244 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005245 * @return the style class of this notification
5246 * @hide
5247 */
5248 public Class<? extends Notification.Style> getNotificationStyle() {
5249 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
5250
5251 if (!TextUtils.isEmpty(templateClass)) {
5252 return Notification.getNotificationStyleClass(templateClass);
5253 }
5254 return null;
5255 }
5256
5257 /**
Selim Cinek22714f12017-04-13 16:23:53 -07005258 * @return true if this notification is colorized.
Selim Cinek7b9605b2017-01-19 17:36:00 -08005259 *
5260 * @hide
5261 */
5262 public boolean isColorized() {
Selim Cinek5fb73f82017-04-20 16:55:38 -07005263 if (isColorizedMedia()) {
5264 return true;
5265 }
Julia Reynolds4db59552017-06-30 13:34:01 -04005266 return extras.getBoolean(EXTRA_COLORIZED)
5267 && (hasColorizedPermission() || isForegroundService());
5268 }
5269
5270 /**
5271 * Returns whether an app can colorize due to the android.permission.USE_COLORIZED_NOTIFICATIONS
5272 * permission. The permission is checked when a notification is enqueued.
5273 */
5274 private boolean hasColorizedPermission() {
5275 return (flags & Notification.FLAG_CAN_COLORIZE) != 0;
Selim Cinek5fb73f82017-04-20 16:55:38 -07005276 }
5277
5278 /**
5279 * @return true if this notification is colorized and it is a media notification
5280 *
5281 * @hide
5282 */
5283 public boolean isColorizedMedia() {
Selim Cinek99104832017-01-25 14:47:33 -08005284 Class<? extends Style> style = getNotificationStyle();
5285 if (MediaStyle.class.equals(style)) {
5286 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
5287 if ((colorized == null || colorized) && hasMediaSession()) {
5288 return true;
5289 }
5290 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5291 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
5292 return true;
5293 }
5294 }
Selim Cinek5fb73f82017-04-20 16:55:38 -07005295 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005296 }
5297
Selim Cinek0847acd2017-04-24 19:48:29 -07005298
5299 /**
5300 * @return true if this is a media notification
5301 *
5302 * @hide
5303 */
5304 public boolean isMediaNotification() {
5305 Class<? extends Style> style = getNotificationStyle();
5306 if (MediaStyle.class.equals(style)) {
5307 return true;
5308 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
5309 return true;
5310 }
5311 return false;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005312 }
5313
Selim Cinek279fa862016-06-14 10:57:25 -07005314 private boolean hasLargeIcon() {
5315 return mLargeIcon != null || largeIcon != null;
5316 }
5317
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005318 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005319 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005320 * @hide
5321 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07005322 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07005323 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
5324 }
5325
5326 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07005327 * @return true if the notification will show a chronometer; false otherwise
5328 * @hide
5329 */
5330 public boolean showsChronometer() {
5331 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
5332 }
5333
5334 /**
Julia Reynolds7ca33072017-06-29 13:58:24 -04005335 * @removed
Julia Reynolds4a02afb2016-12-13 13:39:52 -05005336 */
5337 @SystemApi
5338 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
5339 Class<? extends Style>[] classes = new Class[] {
5340 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
5341 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
5342 MessagingStyle.class };
5343 for (Class<? extends Style> innerClass : classes) {
5344 if (templateClass.equals(innerClass.getName())) {
5345 return innerClass;
5346 }
5347 }
5348 return null;
5349 }
5350
5351 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005352 * An object that can apply a rich notification style to a {@link Notification.Builder}
5353 * object.
5354 */
Griff Hazendfcb0802014-02-11 12:00:00 -08005355 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04005356 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005357
5358 /**
5359 * @hide
5360 */
5361 protected CharSequence mSummaryText = null;
5362
5363 /**
5364 * @hide
5365 */
5366 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04005367
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005368 protected Builder mBuilder;
5369
Chris Wrend6297db2012-05-03 16:20:13 -04005370 /**
5371 * Overrides ContentTitle in the big form of the template.
5372 * This defaults to the value passed to setContentTitle().
5373 */
5374 protected void internalSetBigContentTitle(CharSequence title) {
5375 mBigContentTitle = title;
5376 }
5377
5378 /**
5379 * Set the first line of text after the detail section in the big form of the template.
5380 */
5381 protected void internalSetSummaryText(CharSequence cs) {
5382 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04005383 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04005384 }
5385
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005386 public void setBuilder(Builder builder) {
5387 if (mBuilder != builder) {
5388 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07005389 if (mBuilder != null) {
5390 mBuilder.setStyle(this);
5391 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005392 }
5393 }
5394
Chris Wrend6297db2012-05-03 16:20:13 -04005395 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005396 if (mBuilder == null) {
5397 throw new IllegalArgumentException("Style requires a valid Builder object");
5398 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005399 }
Chris Wrend6297db2012-05-03 16:20:13 -04005400
5401 protected RemoteViews getStandardView(int layoutId) {
5402 checkBuilder();
5403
Christoph Studer4600f9b2014-07-22 22:44:43 +02005404 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005405 CharSequence oldBuilderContentTitle =
5406 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005407 if (mBigContentTitle != null) {
5408 mBuilder.setContentTitle(mBigContentTitle);
5409 }
5410
Chris Wrend6297db2012-05-03 16:20:13 -04005411 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
5412
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005413 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005414
Chris Wrend6297db2012-05-03 16:20:13 -04005415 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
5416 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04005417 } else {
5418 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04005419 }
5420
Chris Wrend6297db2012-05-03 16:20:13 -04005421 return contentView;
5422 }
5423
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005424 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005425 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005426 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08005427 *
5428 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005429 * @hide
5430 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08005431 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005432 return null;
5433 }
5434
5435 /**
5436 * Construct a Style-specific RemoteViews for the final big notification layout.
5437 * @hide
5438 */
5439 public RemoteViews makeBigContentView() {
5440 return null;
5441 }
5442
5443 /**
5444 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08005445 *
5446 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005447 * @hide
5448 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005449 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005450 return null;
5451 }
5452
5453 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005454 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005455 * @hide
5456 */
5457 public void addExtras(Bundle extras) {
5458 if (mSummaryTextSet) {
5459 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
5460 }
5461 if (mBigContentTitle != null) {
5462 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
5463 }
Chris Wren91ad5632013-06-05 15:05:57 -04005464 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005465 }
5466
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005467 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005468 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005469 * @hide
5470 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02005471 protected void restoreFromExtras(Bundle extras) {
5472 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
5473 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
5474 mSummaryTextSet = true;
5475 }
5476 if (extras.containsKey(EXTRA_TITLE_BIG)) {
5477 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
5478 }
5479 }
5480
5481
5482 /**
5483 * @hide
5484 */
5485 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005486 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005487 return wip;
5488 }
5489
Daniel Sandler0ec46202015-06-24 01:27:05 -04005490 /**
5491 * @hide
5492 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005493 public void purgeResources() {}
5494
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005495 /**
5496 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
5497 * attached to.
5498 *
5499 * @return the fully constructed Notification.
5500 */
5501 public Notification build() {
5502 checkBuilder();
5503 return mBuilder.build();
5504 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005505
5506 /**
5507 * @hide
5508 * @return true if the style positions the progress bar on the second line; false if the
5509 * style hides the progress bar
5510 */
5511 protected boolean hasProgress() {
5512 return true;
5513 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005514
5515 /**
5516 * @hide
5517 * @return Whether we should put the summary be put into the notification header
5518 */
5519 public boolean hasSummaryInHeader() {
5520 return true;
5521 }
Selim Cinek593610c2016-02-16 18:42:57 -08005522
5523 /**
5524 * @hide
5525 * @return Whether custom content views are displayed inline in the style
5526 */
5527 public boolean displayCustomViewInline() {
5528 return false;
5529 }
Selim Cinekd0426622017-07-11 13:19:59 +02005530
5531 /**
5532 * Reduces the image sizes contained in this style.
5533 *
5534 * @hide
5535 */
5536 public void reduceImageSizes(Context context) {
5537 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005538 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005539
5540 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005541 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08005542 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005543 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005544 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005545 * Notification notif = new Notification.Builder(mContext)
5546 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
5547 * .setContentText(subject)
5548 * .setSmallIcon(R.drawable.new_post)
5549 * .setLargeIcon(aBitmap)
5550 * .setStyle(new Notification.BigPictureStyle()
5551 * .bigPicture(aBigBitmap))
5552 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005553 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005554 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005555 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005556 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005557 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005558 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005559 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005560 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005561
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005562 public BigPictureStyle() {
5563 }
5564
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005565 /**
5566 * @deprecated use {@code BigPictureStyle()}.
5567 */
5568 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005569 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005570 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005571 }
5572
Chris Wrend6297db2012-05-03 16:20:13 -04005573 /**
5574 * Overrides ContentTitle in the big form of the template.
5575 * This defaults to the value passed to setContentTitle().
5576 */
5577 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005578 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005579 return this;
5580 }
5581
5582 /**
5583 * Set the first line of text after the detail section in the big form of the template.
5584 */
5585 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005586 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005587 return this;
5588 }
5589
Chris Wren0bd664d2012-08-01 13:56:56 -04005590 /**
5591 * Provide the bitmap to be used as the payload for the BigPicture notification.
5592 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005593 public BigPictureStyle bigPicture(Bitmap b) {
5594 mPicture = b;
5595 return this;
5596 }
5597
Chris Wren3745a3d2012-05-22 15:11:52 -04005598 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04005599 * Override the large icon when the big notification is shown.
5600 */
5601 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005602 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
5603 }
5604
5605 /**
5606 * Override the large icon when the big notification is shown.
5607 */
5608 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04005609 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005610 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005611 return this;
5612 }
5613
Riley Andrews0394a0c2015-11-03 23:36:52 -08005614 /** @hide */
5615 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
5616
Daniel Sandler0ec46202015-06-24 01:27:05 -04005617 /**
5618 * @hide
5619 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005620 @Override
5621 public void purgeResources() {
5622 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08005623 if (mPicture != null &&
5624 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08005625 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005626 mPicture = mPicture.createAshmemBitmap();
5627 }
5628 if (mBigLargeIcon != null) {
5629 mBigLargeIcon.convertToAshmem();
5630 }
5631 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01005632
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005633 /**
5634 * @hide
5635 */
Selim Cinekd0426622017-07-11 13:19:59 +02005636 @Override
5637 public void reduceImageSizes(Context context) {
5638 super.reduceImageSizes(context);
5639 Resources resources = context.getResources();
Selim Cineka8cb1262017-08-15 16:53:44 -07005640 boolean isLowRam = ActivityManager.isLowRamDeviceStatic();
Selim Cinekd0426622017-07-11 13:19:59 +02005641 if (mPicture != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005642 int maxPictureWidth = resources.getDimensionPixelSize(isLowRam
5643 ? R.dimen.notification_big_picture_max_height_low_ram
5644 : R.dimen.notification_big_picture_max_height);
5645 int maxPictureHeight = resources.getDimensionPixelSize(isLowRam
5646 ? R.dimen.notification_big_picture_max_width_low_ram
5647 : R.dimen.notification_big_picture_max_width);
Selim Cinekd0426622017-07-11 13:19:59 +02005648 mPicture = Icon.scaleDownIfNecessary(mPicture, maxPictureWidth, maxPictureHeight);
5649 }
5650 if (mBigLargeIcon != null) {
Selim Cineka8cb1262017-08-15 16:53:44 -07005651 int rightIconSize = resources.getDimensionPixelSize(isLowRam
5652 ? R.dimen.notification_right_icon_size_low_ram
5653 : R.dimen.notification_right_icon_size);
Selim Cinekd0426622017-07-11 13:19:59 +02005654 mBigLargeIcon.scaleDownIfNecessary(rightIconSize, rightIconSize);
5655 }
5656 }
5657
5658 /**
5659 * @hide
5660 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005661 public RemoteViews makeBigContentView() {
5662 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01005663 // This covers the following cases:
5664 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005665 // mN.mLargeIcon
5666 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04005667 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07005668 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005669 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005670 oldLargeIcon = mBuilder.mN.mLargeIcon;
5671 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005672 // The legacy largeIcon might not allow us to clear the image, as it's taken in
5673 // replacement if the other one is null. Because we're restoring these legacy icons
5674 // for old listeners, this is in general non-null.
5675 largeIconLegacy = mBuilder.mN.largeIcon;
5676 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005677 }
5678
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005679 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05005680 if (mSummaryTextSet) {
Selim Cinek48f66b72017-08-18 16:17:51 -07005681 contentView.setTextViewText(R.id.text, mBuilder.processTextSpans(
5682 mBuilder.processLegacyText(mSummaryText)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005683 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005684 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05005685 }
Selim Cinek279fa862016-06-14 10:57:25 -07005686 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08005687
Christoph Studer5c510ee2014-12-15 16:32:27 +01005688 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005689 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005690 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005691 }
5692
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005693 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005694 return contentView;
5695 }
5696
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005697 /**
5698 * @hide
5699 */
5700 public void addExtras(Bundle extras) {
5701 super.addExtras(extras);
5702
5703 if (mBigLargeIconSet) {
5704 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
5705 }
5706 extras.putParcelable(EXTRA_PICTURE, mPicture);
5707 }
5708
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005709 /**
5710 * @hide
5711 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005712 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02005713 protected void restoreFromExtras(Bundle extras) {
5714 super.restoreFromExtras(extras);
5715
5716 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01005717 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02005718 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04005719 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005720 mPicture = extras.getParcelable(EXTRA_PICTURE);
5721 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005722
5723 /**
5724 * @hide
5725 */
5726 @Override
5727 public boolean hasSummaryInHeader() {
5728 return false;
5729 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005730 }
5731
5732 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005733 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08005734 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005735 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005736 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005737 * Notification notif = new Notification.Builder(mContext)
5738 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5739 * .setContentText(subject)
5740 * .setSmallIcon(R.drawable.new_mail)
5741 * .setLargeIcon(aBitmap)
5742 * .setStyle(new Notification.BigTextStyle()
5743 * .bigText(aVeryLongString))
5744 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005745 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005746 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005747 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005748 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005749 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005750
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005751 private CharSequence mBigText;
5752
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005753 public BigTextStyle() {
5754 }
5755
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005756 /**
5757 * @deprecated use {@code BigTextStyle()}.
5758 */
5759 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005760 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005761 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005762 }
5763
Chris Wrend6297db2012-05-03 16:20:13 -04005764 /**
5765 * Overrides ContentTitle in the big form of the template.
5766 * This defaults to the value passed to setContentTitle().
5767 */
5768 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005769 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005770 return this;
5771 }
5772
5773 /**
5774 * Set the first line of text after the detail section in the big form of the template.
5775 */
5776 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005777 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005778 return this;
5779 }
5780
Chris Wren0bd664d2012-08-01 13:56:56 -04005781 /**
5782 * Provide the longer text to be displayed in the big form of the
5783 * template in place of the content text.
5784 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005785 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005786 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005787 return this;
5788 }
5789
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005790 /**
5791 * @hide
5792 */
5793 public void addExtras(Bundle extras) {
5794 super.addExtras(extras);
5795
Christoph Studer4600f9b2014-07-22 22:44:43 +02005796 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
5797 }
5798
5799 /**
5800 * @hide
5801 */
5802 @Override
5803 protected void restoreFromExtras(Bundle extras) {
5804 super.restoreFromExtras(extras);
5805
5806 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005807 }
5808
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005809 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005810 * @param increasedHeight true if this layout be created with an increased height.
5811 *
5812 * @hide
5813 */
5814 @Override
5815 public RemoteViews makeContentView(boolean increasedHeight) {
5816 if (increasedHeight) {
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005817 mBuilder.mOriginalActions = mBuilder.mActions;
Selim Cinek7d1009b2017-01-25 15:28:28 -08005818 mBuilder.mActions = new ArrayList<>();
5819 RemoteViews remoteViews = makeBigContentView();
Selim Cinek5d6ef8d2017-05-18 22:16:00 -07005820 mBuilder.mActions = mBuilder.mOriginalActions;
5821 mBuilder.mOriginalActions = null;
Selim Cinek7d1009b2017-01-25 15:28:28 -08005822 return remoteViews;
5823 }
5824 return super.makeContentView(increasedHeight);
5825 }
5826
5827 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005828 * @hide
5829 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005830 @Override
5831 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
5832 if (increasedHeight && mBuilder.mActions.size() > 0) {
5833 return makeBigContentView();
5834 }
5835 return super.makeHeadsUpContentView(increasedHeight);
5836 }
5837
5838 /**
5839 * @hide
5840 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005841 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005842
5843 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07005844 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005845 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04005846
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005847 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08005848
Selim Cinek75998782016-04-26 10:39:17 -07005849 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005850
Selim Cinek3a2c4b92015-12-17 17:01:17 -08005851 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07005852 if (TextUtils.isEmpty(bigTextText)) {
5853 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
5854 // experience
5855 bigTextText = mBuilder.processLegacyText(text);
5856 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07005857 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08005858
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005859 return contentView;
5860 }
5861
Adrian Roosb1f427c2016-05-26 12:27:15 -07005862 static void applyBigTextContentView(Builder builder,
5863 RemoteViews contentView, CharSequence bigTextText) {
Selim Cinek48f66b72017-08-18 16:17:51 -07005864 contentView.setTextViewText(R.id.big_text, builder.processTextSpans(bigTextText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005865 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07005866 contentView.setViewVisibility(R.id.big_text,
5867 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07005868 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07005869 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005870 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005871
5872 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005873 * Helper class for generating large-format notifications that include multiple back-and-forth
5874 * messages of varying types between any number of people.
5875 *
5876 * <br>
5877 * If the platform does not provide large-format notifications, this method has no effect. The
5878 * user will always see the normal notification view.
5879 * <br>
5880 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
5881 * so:
5882 * <pre class="prettyprint">
5883 *
5884 * Notification noti = new Notification.Builder()
5885 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
5886 * .setContentText(subject)
5887 * .setSmallIcon(R.drawable.new_message)
5888 * .setLargeIcon(aBitmap)
5889 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
5890 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
5891 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
5892 * .build();
5893 * </pre>
5894 */
5895 public static class MessagingStyle extends Style {
5896
5897 /**
5898 * The maximum number of messages that will be retained in the Notification itself (the
5899 * number displayed is up to the platform).
5900 */
5901 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
5902
5903 CharSequence mUserDisplayName;
5904 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04005905 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08005906 List<Message> mHistoricMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04005907
5908 MessagingStyle() {
5909 }
5910
5911 /**
Alex Hillsfd590442016-10-07 09:52:44 -04005912 * @param userDisplayName Required - the name to be displayed for any replies sent by the
5913 * user before the posting app reposts the notification with those messages after they've
5914 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04005915 * {@link #addMessage(Notification.MessagingStyle.Message)}
5916 */
Alex Hillsfd590442016-10-07 09:52:44 -04005917 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Alex Hillsfc737de2016-03-23 17:33:02 -04005918 mUserDisplayName = userDisplayName;
5919 }
5920
5921 /**
5922 * Returns the name to be displayed for any replies sent by the user
5923 */
5924 public CharSequence getUserDisplayName() {
5925 return mUserDisplayName;
5926 }
5927
5928 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005929 * Sets the title to be displayed on this conversation. This should only be used for
5930 * group messaging and left unset for one-on-one conversations.
5931 * @param conversationTitle
5932 * @return this object for method chaining.
5933 */
5934 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
5935 mConversationTitle = conversationTitle;
5936 return this;
5937 }
5938
5939 /**
5940 * Return the title to be displayed on this conversation. Can be <code>null</code> and
5941 * should be for one-on-one conversations
5942 */
5943 public CharSequence getConversationTitle() {
5944 return mConversationTitle;
5945 }
5946
5947 /**
5948 * Adds a message for display by this notification. Convenience call for a simple
5949 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
5950 * @param text A {@link CharSequence} to be displayed as the message content
5951 * @param timestamp Time at which the message arrived
5952 * @param sender A {@link CharSequence} to be used for displaying the name of the
5953 * sender. Should be <code>null</code> for messages by the current user, in which case
5954 * the platform will insert {@link #getUserDisplayName()}.
5955 * Should be unique amongst all individuals in the conversation, and should be
5956 * consistent during re-posts of the notification.
5957 *
5958 * @see Message#Message(CharSequence, long, CharSequence)
5959 *
5960 * @return this object for method chaining
5961 */
5962 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08005963 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04005964 }
5965
5966 /**
5967 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08005968 *
5969 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5970 * the newest last.
5971 *
Alex Hillsfc737de2016-03-23 17:33:02 -04005972 * @param message The {@link Message} to be displayed
5973 * @return this object for method chaining
5974 */
5975 public MessagingStyle addMessage(Message message) {
5976 mMessages.add(message);
5977 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5978 mMessages.remove(0);
5979 }
5980 return this;
5981 }
5982
5983 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005984 * Adds a {@link Message} for historic context in this notification.
5985 *
5986 * <p>Messages should be added as historic if they are not the main subject of the
5987 * notification but may give context to a conversation. The system may choose to present
5988 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
5989 *
5990 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5991 * the newest last.
5992 *
5993 * @param message The historic {@link Message} to be added
5994 * @return this object for method chaining
5995 */
5996 public MessagingStyle addHistoricMessage(Message message) {
5997 mHistoricMessages.add(message);
5998 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5999 mHistoricMessages.remove(0);
6000 }
6001 return this;
6002 }
6003
6004 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006005 * Gets the list of {@code Message} objects that represent the notification
6006 */
6007 public List<Message> getMessages() {
6008 return mMessages;
6009 }
6010
6011 /**
Adrian Roos437cd562017-01-18 15:47:03 -08006012 * Gets the list of historic {@code Message}s in the notification.
6013 */
6014 public List<Message> getHistoricMessages() {
6015 return mHistoricMessages;
6016 }
6017
6018 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006019 * @hide
6020 */
6021 @Override
6022 public void addExtras(Bundle extras) {
6023 super.addExtras(extras);
6024 if (mUserDisplayName != null) {
6025 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
6026 }
6027 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006028 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04006029 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006030 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
6031 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04006032 }
Adrian Roos437cd562017-01-18 15:47:03 -08006033 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
6034 Message.getBundleArrayForMessages(mHistoricMessages));
6035 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006036
6037 fixTitleAndTextExtras(extras);
6038 }
6039
6040 private void fixTitleAndTextExtras(Bundle extras) {
6041 Message m = findLatestIncomingMessage();
6042 CharSequence text = (m == null) ? null : m.mText;
6043 CharSequence sender = m == null ? null
6044 : TextUtils.isEmpty(m.mSender) ? mUserDisplayName : m.mSender;
6045 CharSequence title;
6046 if (!TextUtils.isEmpty(mConversationTitle)) {
6047 if (!TextUtils.isEmpty(sender)) {
6048 BidiFormatter bidi = BidiFormatter.getInstance();
6049 title = mBuilder.mContext.getString(
6050 com.android.internal.R.string.notification_messaging_title_template,
6051 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(m.mSender));
6052 } else {
6053 title = mConversationTitle;
6054 }
6055 } else {
6056 title = sender;
6057 }
6058
6059 if (title != null) {
6060 extras.putCharSequence(EXTRA_TITLE, title);
6061 }
6062 if (text != null) {
6063 extras.putCharSequence(EXTRA_TEXT, text);
6064 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006065 }
6066
6067 /**
6068 * @hide
6069 */
6070 @Override
6071 protected void restoreFromExtras(Bundle extras) {
6072 super.restoreFromExtras(extras);
6073
Adrian Roos96b7e202016-05-17 13:50:38 -07006074 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
6075 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08006076 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006077 mMessages = Message.getMessagesFromBundleArray(messages);
Adrian Roos437cd562017-01-18 15:47:03 -08006078 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
Selim Cinek88188f22017-09-19 16:46:56 -07006079 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Alex Hillsfc737de2016-03-23 17:33:02 -04006080 }
6081
6082 /**
6083 * @hide
6084 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07006085 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006086 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07006087 mBuilder.mOriginalActions = mBuilder.mActions;
6088 mBuilder.mActions = new ArrayList<>();
6089 RemoteViews remoteViews = makeBigContentView();
6090 mBuilder.mActions = mBuilder.mOriginalActions;
6091 mBuilder.mOriginalActions = null;
6092 return remoteViews;
Adrian Roosc1a80b02016-04-05 14:54:55 -07006093 }
6094
6095 private Message findLatestIncomingMessage() {
Selim Cinek88188f22017-09-19 16:46:56 -07006096 return findLatestIncomingMessage(mMessages);
6097 }
6098
6099 /**
6100 * @hide
6101 */
6102 @Nullable
6103 public static Message findLatestIncomingMessage(
6104 List<Message> messages) {
6105 for (int i = messages.size() - 1; i >= 0; i--) {
6106 Message m = messages.get(i);
Adrian Roosc1a80b02016-04-05 14:54:55 -07006107 // Incoming messages have a non-empty sender.
6108 if (!TextUtils.isEmpty(m.mSender)) {
6109 return m;
6110 }
6111 }
Selim Cinek88188f22017-09-19 16:46:56 -07006112 if (!messages.isEmpty()) {
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006113 // No incoming messages, fall back to outgoing message
Selim Cinek88188f22017-09-19 16:46:56 -07006114 return messages.get(messages.size() - 1);
Adrian Roos33fbd2c2016-05-27 15:35:28 -07006115 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07006116 return null;
6117 }
6118
6119 /**
6120 * @hide
6121 */
6122 @Override
6123 public RemoteViews makeBigContentView() {
Selim Cinek88188f22017-09-19 16:46:56 -07006124 CharSequence conversationTitle = !TextUtils.isEmpty(super.mBigContentTitle)
Adrian Roosc1a80b02016-04-05 14:54:55 -07006125 ? super.mBigContentTitle
6126 : mConversationTitle;
Selim Cinek88188f22017-09-19 16:46:56 -07006127 boolean isOneToOne = TextUtils.isEmpty(conversationTitle);
6128 if (isOneToOne) {
6129 // Let's add the conversationTitle in case we didn't have one before and all
6130 // messages are from the same sender
6131 conversationTitle = createConversationTitleFromMessages();
Selim Cinekf7409db2017-10-24 16:17:14 -07006132 } else if (hasOnlyWhiteSpaceSenders()) {
6133 isOneToOne = true;
Adrian Roosb1f427c2016-05-26 12:27:15 -07006134 }
Selim Cinek88188f22017-09-19 16:46:56 -07006135 boolean hasTitle = !TextUtils.isEmpty(conversationTitle);
Adrian Roos48d746a2016-04-12 14:57:28 -07006136 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07006137 mBuilder.getMessagingLayoutResource(),
Selim Cinek88188f22017-09-19 16:46:56 -07006138 mBuilder.mParams.reset().hasProgress(false).title(conversationTitle).text(null)
6139 .hideLargeIcon(isOneToOne).alwaysShowReply(true));
Selim Cinek88188f22017-09-19 16:46:56 -07006140 addExtras(mBuilder.mN.extras);
6141 contentView.setInt(R.id.status_bar_latest_event_content, "setLayoutColor",
6142 mBuilder.resolveContrastColor());
6143 contentView.setIcon(R.id.status_bar_latest_event_content, "setLargeIcon",
6144 mBuilder.mN.mLargeIcon);
6145 contentView.setBoolean(R.id.status_bar_latest_event_content, "setIsOneToOne",
6146 isOneToOne);
6147 contentView.setBundle(R.id.status_bar_latest_event_content, "setData",
6148 mBuilder.mN.extras);
Alex Hillsfc737de2016-03-23 17:33:02 -04006149 return contentView;
6150 }
6151
Selim Cinekf7409db2017-10-24 16:17:14 -07006152 private boolean hasOnlyWhiteSpaceSenders() {
6153 for (int i = 0; i < mMessages.size(); i++) {
6154 Message m = mMessages.get(i);
6155 CharSequence sender = m.getSender();
6156 if (!isWhiteSpace(sender)) {
6157 return false;
6158 }
6159 }
6160 return true;
6161 }
6162
6163 private boolean isWhiteSpace(CharSequence sender) {
6164 if (TextUtils.isEmpty(sender)) {
6165 return true;
6166 }
6167 if (sender.toString().matches("^\\s*$")) {
6168 return true;
6169 }
6170 // Let's check if we only have 0 whitespace chars. Some apps did this as a workaround
6171 // For the presentation that we had.
6172 for (int i = 0; i < sender.length(); i++) {
6173 char c = sender.charAt(i);
6174 if (c != '\u200B') {
6175 return false;
6176 }
6177 }
6178 return true;
6179 }
6180
Selim Cinek88188f22017-09-19 16:46:56 -07006181 private CharSequence createConversationTitleFromMessages() {
6182 ArraySet<CharSequence> names = new ArraySet<>();
6183 for (int i = 0; i < mMessages.size(); i++) {
6184 Message m = mMessages.get(i);
6185 CharSequence sender = m.getSender();
6186 if (sender != null) {
6187 names.add(sender);
6188 }
6189 }
6190 SpannableStringBuilder title = new SpannableStringBuilder();
6191 int size = names.size();
6192 for (int i = 0; i < size; i++) {
6193 CharSequence name = names.valueAt(i);
6194 if (!TextUtils.isEmpty(title)) {
6195 title.append(", ");
6196 }
6197 title.append(BidiFormatter.getInstance().unicodeWrap(name));
6198 }
6199 return title;
6200 }
6201
Adrian Roosdedd1df2016-04-26 16:38:47 -07006202 /**
6203 * @hide
6204 */
6205 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006206 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cineke62255c2017-09-28 18:23:23 -07006207 RemoteViews remoteViews = makeBigContentView();
6208 remoteViews.setInt(R.id.notification_messaging, "setMaxDisplayedLines", 1);
6209 return remoteViews;
Adrian Roosdedd1df2016-04-26 16:38:47 -07006210 }
6211
Adrian Roosc1a80b02016-04-05 14:54:55 -07006212 private static TextAppearanceSpan makeFontColorSpan(int color) {
6213 return new TextAppearanceSpan(null, 0, 0,
6214 ColorStateList.valueOf(color), null);
6215 }
6216
Alex Hillsd9b04d92016-04-11 16:38:16 -04006217 public static final class Message {
6218
6219 static final String KEY_TEXT = "text";
6220 static final String KEY_TIMESTAMP = "time";
6221 static final String KEY_SENDER = "sender";
6222 static final String KEY_DATA_MIME_TYPE = "type";
6223 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08006224 static final String KEY_EXTRAS_BUNDLE = "extras";
Alex Hillsfc737de2016-03-23 17:33:02 -04006225
6226 private final CharSequence mText;
6227 private final long mTimestamp;
6228 private final CharSequence mSender;
6229
Shane Brennan5a871862017-03-11 13:14:17 -08006230 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006231 private String mDataMimeType;
6232 private Uri mDataUri;
6233
6234 /**
6235 * Constructor
6236 * @param text A {@link CharSequence} to be displayed as the message content
6237 * @param timestamp Time at which the message arrived
6238 * @param sender A {@link CharSequence} to be used for displaying the name of the
6239 * sender. Should be <code>null</code> for messages by the current user, in which case
6240 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
6241 * Should be unique amongst all individuals in the conversation, and should be
6242 * consistent during re-posts of the notification.
6243 */
6244 public Message(CharSequence text, long timestamp, CharSequence sender){
6245 mText = text;
6246 mTimestamp = timestamp;
6247 mSender = sender;
6248 }
6249
6250 /**
6251 * Sets a binary blob of data and an associated MIME type for a message. In the case
6252 * where the platform doesn't support the MIME type, the original text provided in the
6253 * constructor will be used.
6254 * @param dataMimeType The MIME type of the content. See
6255 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
6256 * types on Android and Android Wear.
6257 * @param dataUri The uri containing the content whose type is given by the MIME type.
6258 * <p class="note">
6259 * <ol>
6260 * <li>Notification Listeners including the System UI need permission to access the
6261 * data the Uri points to. The recommended ways to do this are:</li>
6262 * <li>Store the data in your own ContentProvider, making sure that other apps have
6263 * the correct permission to access your provider. The preferred mechanism for
6264 * providing access is to use per-URI permissions which are temporary and only
6265 * grant access to the receiving application. An easy way to create a
6266 * ContentProvider like this is to use the FileProvider helper class.</li>
6267 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
6268 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
6269 * also store non-media types (see MediaStore.Files for more info). Files can be
6270 * inserted into the MediaStore using scanFile() after which a content:// style
6271 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
6272 * Note that once added to the system MediaStore the content is accessible to any
6273 * app on the device.</li>
6274 * </ol>
6275 * @return this object for method chaining
6276 */
6277 public Message setData(String dataMimeType, Uri dataUri) {
6278 mDataMimeType = dataMimeType;
6279 mDataUri = dataUri;
6280 return this;
6281 }
6282
Alex Hillsfc737de2016-03-23 17:33:02 -04006283 /**
6284 * Get the text to be used for this message, or the fallback text if a type and content
6285 * Uri have been set
6286 */
6287 public CharSequence getText() {
6288 return mText;
6289 }
6290
6291 /**
6292 * Get the time at which this message arrived
6293 */
6294 public long getTimestamp() {
6295 return mTimestamp;
6296 }
6297
6298 /**
Shane Brennan5a871862017-03-11 13:14:17 -08006299 * Get the extras Bundle for this message.
6300 */
6301 public Bundle getExtras() {
6302 return mExtras;
6303 }
6304
6305 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04006306 * Get the text used to display the contact's name in the messaging experience
6307 */
6308 public CharSequence getSender() {
6309 return mSender;
6310 }
6311
6312 /**
6313 * Get the MIME type of the data pointed to by the Uri
6314 */
6315 public String getDataMimeType() {
6316 return mDataMimeType;
6317 }
6318
6319 /**
6320 * Get the the Uri pointing to the content of the message. Can be null, in which case
6321 * {@see #getText()} is used.
6322 */
6323 public Uri getDataUri() {
6324 return mDataUri;
6325 }
6326
Alex Hillsd9b04d92016-04-11 16:38:16 -04006327 private Bundle toBundle() {
6328 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04006329 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006330 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04006331 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006332 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04006333 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006334 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04006335 }
6336 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006337 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04006338 }
6339 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006340 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04006341 }
Shane Brennan5a871862017-03-11 13:14:17 -08006342 if (mExtras != null) {
6343 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
6344 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006345 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04006346 }
6347
Alex Hillsd9b04d92016-04-11 16:38:16 -04006348 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
6349 Bundle[] bundles = new Bundle[messages.size()];
6350 final int N = messages.size();
6351 for (int i = 0; i < N; i++) {
6352 bundles[i] = messages.get(i).toBundle();
6353 }
6354 return bundles;
6355 }
6356
Selim Cinek88188f22017-09-19 16:46:56 -07006357 /**
6358 * @return A list of messages read from the bundles.
6359 *
6360 * @hide
6361 */
6362 public static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
6363 if (bundles == null) {
6364 return new ArrayList<>();
6365 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006366 List<Message> messages = new ArrayList<>(bundles.length);
6367 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07006368 if (bundles[i] instanceof Bundle) {
6369 Message message = getMessageFromBundle((Bundle)bundles[i]);
6370 if (message != null) {
6371 messages.add(message);
6372 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006373 }
6374 }
6375 return messages;
6376 }
6377
6378 static Message getMessageFromBundle(Bundle bundle) {
6379 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07006380 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006381 return null;
6382 } else {
6383 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
6384 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
6385 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
6386 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04006387 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
6388 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04006389 }
Shane Brennan5a871862017-03-11 13:14:17 -08006390 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
6391 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
6392 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04006393 return message;
6394 }
6395 } catch (ClassCastException e) {
6396 return null;
6397 }
6398 }
Alex Hillsfc737de2016-03-23 17:33:02 -04006399 }
6400 }
6401
6402 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04006403 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08006404 *
Robert Ly91c5ce32014-06-08 15:37:00 -07006405 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04006406 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07006407 * Notification notif = new Notification.Builder(mContext)
6408 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
6409 * .setContentText(subject)
6410 * .setSmallIcon(R.drawable.new_mail)
6411 * .setLargeIcon(aBitmap)
6412 * .setStyle(new Notification.InboxStyle()
6413 * .addLine(str1)
6414 * .addLine(str2)
6415 * .setContentTitle(&quot;&quot;)
6416 * .setSummaryText(&quot;+3 more&quot;))
6417 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04006418 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08006419 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04006420 * @see Notification#bigContentView
6421 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006422 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006423 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
6424
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006425 public InboxStyle() {
6426 }
6427
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006428 /**
6429 * @deprecated use {@code InboxStyle()}.
6430 */
6431 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04006432 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04006433 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04006434 }
6435
Chris Wrend6297db2012-05-03 16:20:13 -04006436 /**
6437 * Overrides ContentTitle in the big form of the template.
6438 * This defaults to the value passed to setContentTitle().
6439 */
6440 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006441 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04006442 return this;
6443 }
6444
6445 /**
6446 * Set the first line of text after the detail section in the big form of the template.
6447 */
6448 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006449 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04006450 return this;
6451 }
6452
Chris Wren0bd664d2012-08-01 13:56:56 -04006453 /**
6454 * Append a line to the digest section of the Inbox notification.
6455 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04006456 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04006457 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04006458 return this;
6459 }
6460
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006461 /**
6462 * @hide
6463 */
6464 public void addExtras(Bundle extras) {
6465 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006466
Daniel Sandlerf45564e2013-04-15 15:05:08 -04006467 CharSequence[] a = new CharSequence[mTexts.size()];
6468 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
6469 }
6470
Christoph Studer4600f9b2014-07-22 22:44:43 +02006471 /**
6472 * @hide
6473 */
6474 @Override
6475 protected void restoreFromExtras(Bundle extras) {
6476 super.restoreFromExtras(extras);
6477
6478 mTexts.clear();
6479 if (extras.containsKey(EXTRA_TEXT_LINES)) {
6480 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
6481 }
6482 }
6483
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006484 /**
6485 * @hide
6486 */
6487 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08006488 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02006489 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006490 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
6491 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006492
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006493 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04006494
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006495 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006496
Chris Wrend6297db2012-05-03 16:20:13 -04006497 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 -04006498 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04006499
Chris Wren4ed80d52012-05-17 09:30:03 -04006500 // Make sure all rows are gone in case we reuse a view.
6501 for (int rowId : rowIds) {
6502 contentView.setViewVisibility(rowId, View.GONE);
6503 }
6504
Daniel Sandler879c5e02012-04-17 16:46:51 -04006505 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07006506 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6507 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08006508 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07006509 int onlyViewId = 0;
6510 int maxRows = rowIds.length;
6511 if (mBuilder.mActions.size() > 0) {
6512 maxRows--;
6513 }
6514 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006515 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07006516 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006517 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Selim Cinek48f66b72017-08-18 16:17:51 -07006518 contentView.setTextViewText(rowIds[i],
6519 mBuilder.processTextSpans(mBuilder.processLegacyText(str)));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006520 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07006521 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08006522 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07006523 if (first) {
6524 onlyViewId = rowIds[i];
6525 } else {
6526 onlyViewId = 0;
6527 }
Selim Cinek247fa012016-02-18 09:50:48 -08006528 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04006529 }
6530 i++;
6531 }
Selim Cinek07c80172016-04-21 16:40:47 -07006532 if (onlyViewId != 0) {
6533 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
6534 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6535 R.dimen.notification_text_margin_top);
6536 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
6537 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006538
Daniel Sandler879c5e02012-04-17 16:46:51 -04006539 return contentView;
6540 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006541
Selim Cinek247fa012016-02-18 09:50:48 -08006542 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08006543 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08006544 if (first) {
6545 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
6546 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
6547 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07006548 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006549 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08006550 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006551 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006552 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08006553 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006554 }
Dan Sandler842dd772014-05-15 09:36:47 -04006555
6556 /**
6557 * Notification style for media playback notifications.
6558 *
6559 * In the expanded form, {@link Notification#bigContentView}, up to 5
6560 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04006561 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04006562 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
6563 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
6564 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08006565 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04006566 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
6567 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01006568 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04006569 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08006570 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006571 * Notifications created with MediaStyle will have their category set to
6572 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
6573 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08006574 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07006575 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
6576 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04006577 * the System UI can identify this as a notification representing an active media session
6578 * and respond accordingly (by showing album artwork in the lockscreen, for example).
6579 *
Selim Cinek99104832017-01-25 14:47:33 -08006580 * <p>
6581 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
6582 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
6583 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
6584 * <p>
6585 *
Dan Sandler842dd772014-05-15 09:36:47 -04006586 * To use this style with your Notification, feed it to
6587 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6588 * <pre class="prettyprint">
6589 * Notification noti = new Notification.Builder()
6590 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01006591 * .setContentTitle(&quot;Track title&quot;)
6592 * .setContentText(&quot;Artist - Album&quot;)
6593 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006594 * .setStyle(<b>new Notification.MediaStyle()</b>
6595 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04006596 * .build();
6597 * </pre>
6598 *
6599 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08006600 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04006601 */
6602 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006603 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04006604 static final int MAX_MEDIA_BUTTONS = 5;
6605
6606 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07006607 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04006608
6609 public MediaStyle() {
6610 }
6611
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006612 /**
6613 * @deprecated use {@code MediaStyle()}.
6614 */
6615 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04006616 public MediaStyle(Builder builder) {
6617 setBuilder(builder);
6618 }
6619
6620 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006621 * 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 -04006622 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006623 *
6624 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04006625 */
6626 public MediaStyle setShowActionsInCompactView(int...actions) {
6627 mActionsToShowInCompact = actions;
6628 return this;
6629 }
6630
6631 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07006632 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
6633 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04006634 */
Jeff Browndba34ba2014-06-24 20:46:03 -07006635 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04006636 mToken = token;
6637 return this;
6638 }
6639
Christoph Studer4600f9b2014-07-22 22:44:43 +02006640 /**
6641 * @hide
6642 */
Dan Sandler842dd772014-05-15 09:36:47 -04006643 @Override
6644 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006645 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006646 if (wip.category == null) {
6647 wip.category = Notification.CATEGORY_TRANSPORT;
6648 }
Dan Sandler842dd772014-05-15 09:36:47 -04006649 return wip;
6650 }
6651
Christoph Studer4600f9b2014-07-22 22:44:43 +02006652 /**
6653 * @hide
6654 */
6655 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006656 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006657 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006658 }
6659
6660 /**
6661 * @hide
6662 */
6663 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006664 public RemoteViews makeBigContentView() {
6665 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006666 }
6667
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006668 /**
6669 * @hide
6670 */
6671 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006672 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006673 RemoteViews expanded = makeMediaBigContentView();
6674 return expanded != null ? expanded : makeMediaContentView();
6675 }
6676
Dan Sandler842dd772014-05-15 09:36:47 -04006677 /** @hide */
6678 @Override
6679 public void addExtras(Bundle extras) {
6680 super.addExtras(extras);
6681
6682 if (mToken != null) {
6683 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
6684 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01006685 if (mActionsToShowInCompact != null) {
6686 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
6687 }
Dan Sandler842dd772014-05-15 09:36:47 -04006688 }
6689
Christoph Studer4600f9b2014-07-22 22:44:43 +02006690 /**
6691 * @hide
6692 */
6693 @Override
6694 protected void restoreFromExtras(Bundle extras) {
6695 super.restoreFromExtras(extras);
6696
6697 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
6698 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
6699 }
6700 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
6701 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
6702 }
6703 }
6704
Selim Cinek5bf069a2015-11-10 19:14:27 -05006705 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04006706 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07006707 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07006708 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04006709 button.setImageViewIcon(R.id.action0, action.getIcon());
Anthony Chenad4d1582017-04-10 16:07:58 -07006710
6711 // If the action buttons should not be tinted, then just use the default
6712 // notification color. Otherwise, just use the passed-in color.
6713 int tintColor = mBuilder.shouldTintActionButtons() || mBuilder.isColorized()
6714 ? color
6715 : NotificationColorUtil.resolveColor(mBuilder.mContext,
6716 Notification.COLOR_DEFAULT);
6717
Sunny Goyal5b153922017-09-21 21:00:36 -07006718 button.setDrawableTint(R.id.action0, false, tintColor,
6719 PorterDuff.Mode.SRC_ATOP);
Dan Sandler842dd772014-05-15 09:36:47 -04006720 if (!tombstone) {
6721 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
6722 }
6723 button.setContentDescription(R.id.action0, action.title);
6724 return button;
6725 }
6726
6727 private RemoteViews makeMediaContentView() {
6728 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006729 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04006730
6731 final int numActions = mBuilder.mActions.size();
6732 final int N = mActionsToShowInCompact == null
6733 ? 0
6734 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
6735 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006736 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04006737 for (int i = 0; i < N; i++) {
6738 if (i >= numActions) {
6739 throw new IllegalArgumentException(String.format(
6740 "setShowActionsInCompactView: action %d out of bounds (max %d)",
6741 i, numActions - 1));
6742 }
6743
6744 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05006745 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08006746 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006747 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006748 }
6749 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08006750 handleImage(view);
6751 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006752 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006753 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006754 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08006755 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006756 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04006757 return view;
6758 }
6759
Selim Cinek99104832017-01-25 14:47:33 -08006760 private int getPrimaryHighlightColor() {
6761 return mBuilder.getPrimaryHighlightColor();
6762 }
6763
Dan Sandler842dd772014-05-15 09:36:47 -04006764 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006765 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006766 // Dont add an expanded view if there is no more content to be revealed
6767 int actionsInCompact = mActionsToShowInCompact == null
6768 ? 0
6769 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07006770 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006771 return null;
6772 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006773 RemoteViews big = mBuilder.applyStandardTemplate(
6774 R.layout.notification_template_material_big_media,
6775 false);
Dan Sandler842dd772014-05-15 09:36:47 -04006776
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006777 if (actionCount > 0) {
6778 big.removeAllViews(com.android.internal.R.id.media_actions);
6779 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05006780 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08006781 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006782 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006783 }
6784 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006785 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04006786 return big;
6787 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006788
Selim Cinek5bf069a2015-11-10 19:14:27 -05006789 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07006790 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006791 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
6792 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006793 }
6794 }
6795
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006796 /**
6797 * @hide
6798 */
6799 @Override
6800 protected boolean hasProgress() {
6801 return false;
6802 }
Dan Sandler842dd772014-05-15 09:36:47 -04006803 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006804
Selim Cinek593610c2016-02-16 18:42:57 -08006805 /**
6806 * Notification style for custom views that are decorated by the system
6807 *
6808 * <p>Instead of providing a notification that is completely custom, a developer can set this
6809 * style and still obtain system decorations like the notification header with the expand
6810 * affordance and actions.
6811 *
6812 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6813 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6814 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6815 * corresponding custom views to display.
6816 *
6817 * To use this style with your Notification, feed it to
6818 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6819 * <pre class="prettyprint">
6820 * Notification noti = new Notification.Builder()
6821 * .setSmallIcon(R.drawable.ic_stat_player)
6822 * .setLargeIcon(albumArtBitmap))
6823 * .setCustomContentView(contentView);
6824 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
6825 * .build();
6826 * </pre>
6827 */
6828 public static class DecoratedCustomViewStyle extends Style {
6829
6830 public DecoratedCustomViewStyle() {
6831 }
6832
Selim Cinek593610c2016-02-16 18:42:57 -08006833 /**
6834 * @hide
6835 */
6836 public boolean displayCustomViewInline() {
6837 return true;
6838 }
6839
6840 /**
6841 * @hide
6842 */
6843 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006844 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006845 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
6846 }
6847
6848 /**
6849 * @hide
6850 */
6851 @Override
6852 public RemoteViews makeBigContentView() {
6853 return makeDecoratedBigContentView();
6854 }
6855
6856 /**
6857 * @hide
6858 */
6859 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006860 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006861 return makeDecoratedHeadsUpContentView();
6862 }
6863
Selim Cinek593610c2016-02-16 18:42:57 -08006864 private RemoteViews makeDecoratedHeadsUpContentView() {
6865 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
6866 ? mBuilder.mN.contentView
6867 : mBuilder.mN.headsUpContentView;
6868 if (mBuilder.mActions.size() == 0) {
6869 return makeStandardTemplateWithCustomContent(headsUpContentView);
6870 }
6871 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6872 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006873 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006874 return remoteViews;
6875 }
6876
Selim Cinek593610c2016-02-16 18:42:57 -08006877 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
6878 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
6879 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006880 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08006881 return remoteViews;
6882 }
6883
Selim Cinek593610c2016-02-16 18:42:57 -08006884 private RemoteViews makeDecoratedBigContentView() {
6885 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
6886 ? mBuilder.mN.contentView
6887 : mBuilder.mN.bigContentView;
6888 if (mBuilder.mActions.size() == 0) {
6889 return makeStandardTemplateWithCustomContent(bigContentView);
6890 }
6891 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6892 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006893 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006894 return remoteViews;
6895 }
Selim Cinek247fa012016-02-18 09:50:48 -08006896
6897 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
6898 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08006899 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07006900 // Need to clone customContent before adding, because otherwise it can no longer be
6901 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08006902 customContent = customContent.clone();
Anthony Chen8f5f3582017-04-11 11:18:37 -07006903 remoteViews.removeAllViewsExceptId(R.id.notification_main_column, R.id.progress);
6904 remoteViews.addView(R.id.notification_main_column, customContent, 0 /* index */);
Selim Cinekfc8073c2017-08-16 17:50:20 -07006905 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08006906 }
Selim Cinek247fa012016-02-18 09:50:48 -08006907 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006908 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006909 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006910 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08006911 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006912 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08006913 }
Selim Cinek593610c2016-02-16 18:42:57 -08006914 }
6915
Selim Cinek03eb3b72016-02-18 10:39:45 -08006916 /**
6917 * Notification style for media custom views that are decorated by the system
6918 *
6919 * <p>Instead of providing a media notification that is completely custom, a developer can set
6920 * this style and still obtain system decorations like the notification header with the expand
6921 * affordance and actions.
6922 *
6923 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6924 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6925 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6926 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08006927 * <p>
6928 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
6929 * notification by using {@link Notification.Builder#setColorized(boolean)}.
6930 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08006931 * To use this style with your Notification, feed it to
6932 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6933 * <pre class="prettyprint">
6934 * Notification noti = new Notification.Builder()
6935 * .setSmallIcon(R.drawable.ic_stat_player)
6936 * .setLargeIcon(albumArtBitmap))
6937 * .setCustomContentView(contentView);
6938 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
6939 * .setMediaSession(mySession))
6940 * .build();
6941 * </pre>
6942 *
6943 * @see android.app.Notification.DecoratedCustomViewStyle
6944 * @see android.app.Notification.MediaStyle
6945 */
6946 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
6947
6948 public DecoratedMediaCustomViewStyle() {
6949 }
6950
Selim Cinek03eb3b72016-02-18 10:39:45 -08006951 /**
6952 * @hide
6953 */
6954 public boolean displayCustomViewInline() {
6955 return true;
6956 }
6957
6958 /**
6959 * @hide
6960 */
6961 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006962 public RemoteViews makeContentView(boolean increasedHeight) {
6963 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006964 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6965 mBuilder.mN.contentView);
6966 }
6967
6968 /**
6969 * @hide
6970 */
6971 @Override
6972 public RemoteViews makeBigContentView() {
6973 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
6974 ? mBuilder.mN.bigContentView
6975 : mBuilder.mN.contentView;
6976 return makeBigContentViewWithCustomContent(customRemoteView);
6977 }
6978
6979 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
6980 RemoteViews remoteViews = super.makeBigContentView();
6981 if (remoteViews != null) {
6982 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
6983 customRemoteView);
6984 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08006985 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006986 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6987 customRemoteView);
6988 } else {
6989 return null;
6990 }
6991 }
6992
6993 /**
6994 * @hide
6995 */
6996 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006997 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08006998 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
6999 ? mBuilder.mN.headsUpContentView
7000 : mBuilder.mN.contentView;
7001 return makeBigContentViewWithCustomContent(customRemoteView);
7002 }
7003
7004 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
7005 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08007006 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07007007 // Need to clone customContent before adding, because otherwise it can no longer be
7008 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08007009 customContent = customContent.clone();
Selim Cinek87c31532017-08-18 18:53:44 -07007010 customContent.overrideTextColors(mBuilder.getPrimaryTextColor());
Selim Cinekf91017e2016-03-14 12:25:09 -07007011 remoteViews.removeAllViews(id);
7012 remoteViews.addView(id, customContent);
Selim Cinekfc8073c2017-08-16 17:50:20 -07007013 remoteViews.setReapplyDisallowed();
Adrian Roos5081c0d2016-02-26 16:04:19 -08007014 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08007015 return remoteViews;
7016 }
7017 }
7018
Christoph Studer4600f9b2014-07-22 22:44:43 +02007019 // When adding a new Style subclass here, don't forget to update
7020 // Builder.getNotificationStyleClass.
7021
Griff Hazen61a9e862014-05-22 16:05:19 -07007022 /**
7023 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
7024 * metadata or change options on a notification builder.
7025 */
7026 public interface Extender {
7027 /**
7028 * Apply this extender to a notification builder.
7029 * @param builder the builder to be modified.
7030 * @return the build object for chaining.
7031 */
7032 public Builder extend(Builder builder);
7033 }
7034
7035 /**
7036 * Helper class to add wearable extensions to notifications.
7037 * <p class="note"> See
7038 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
7039 * for Android Wear</a> for more information on how to use this class.
7040 * <p>
7041 * To create a notification with wearable extensions:
7042 * <ol>
7043 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
7044 * properties.
7045 * <li>Create a {@link android.app.Notification.WearableExtender}.
7046 * <li>Set wearable-specific properties using the
7047 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
7048 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
7049 * notification.
7050 * <li>Post the notification to the notification system with the
7051 * {@code NotificationManager.notify(...)} methods.
7052 * </ol>
7053 *
7054 * <pre class="prettyprint">
7055 * Notification notif = new Notification.Builder(mContext)
7056 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
7057 * .setContentText(subject)
7058 * .setSmallIcon(R.drawable.new_mail)
7059 * .extend(new Notification.WearableExtender()
7060 * .setContentIcon(R.drawable.new_mail))
7061 * .build();
7062 * NotificationManager notificationManger =
7063 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
7064 * notificationManger.notify(0, notif);</pre>
7065 *
7066 * <p>Wearable extensions can be accessed on an existing notification by using the
7067 * {@code WearableExtender(Notification)} constructor,
7068 * and then using the {@code get} methods to access values.
7069 *
7070 * <pre class="prettyprint">
7071 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
7072 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07007073 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007074 */
7075 public static final class WearableExtender implements Extender {
7076 /**
7077 * Sentinel value for an action index that is unset.
7078 */
7079 public static final int UNSET_ACTION_INDEX = -1;
7080
7081 /**
7082 * Size value for use with {@link #setCustomSizePreset} to show this notification with
7083 * default sizing.
7084 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07007085 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07007086 * on their content.
7087 */
7088 public static final int SIZE_DEFAULT = 0;
7089
7090 /**
7091 * Size value for use with {@link #setCustomSizePreset} to show this notification
7092 * with an extra small size.
7093 * <p>This value is only applicable for custom display notifications created using
7094 * {@link #setDisplayIntent}.
7095 */
7096 public static final int SIZE_XSMALL = 1;
7097
7098 /**
7099 * Size value for use with {@link #setCustomSizePreset} to show this notification
7100 * with a small size.
7101 * <p>This value is only applicable for custom display notifications created using
7102 * {@link #setDisplayIntent}.
7103 */
7104 public static final int SIZE_SMALL = 2;
7105
7106 /**
7107 * Size value for use with {@link #setCustomSizePreset} to show this notification
7108 * with a medium size.
7109 * <p>This value is only applicable for custom display notifications created using
7110 * {@link #setDisplayIntent}.
7111 */
7112 public static final int SIZE_MEDIUM = 3;
7113
7114 /**
7115 * Size value for use with {@link #setCustomSizePreset} to show this notification
7116 * with a large size.
7117 * <p>This value is only applicable for custom display notifications created using
7118 * {@link #setDisplayIntent}.
7119 */
7120 public static final int SIZE_LARGE = 4;
7121
Griff Hazend5f11f92014-05-27 15:40:09 -07007122 /**
7123 * Size value for use with {@link #setCustomSizePreset} to show this notification
7124 * full screen.
7125 * <p>This value is only applicable for custom display notifications created using
7126 * {@link #setDisplayIntent}.
7127 */
7128 public static final int SIZE_FULL_SCREEN = 5;
7129
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007130 /**
7131 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
7132 * short amount of time when this notification is displayed on the screen. This
7133 * is the default value.
7134 */
7135 public static final int SCREEN_TIMEOUT_SHORT = 0;
7136
7137 /**
7138 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
7139 * for a longer amount of time when this notification is displayed on the screen.
7140 */
7141 public static final int SCREEN_TIMEOUT_LONG = -1;
7142
Griff Hazen61a9e862014-05-22 16:05:19 -07007143 /** Notification extra which contains wearable extensions */
7144 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
7145
Pete Gastaf6781d2014-10-07 15:17:05 -04007146 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07007147 private static final String KEY_ACTIONS = "actions";
7148 private static final String KEY_FLAGS = "flags";
7149 private static final String KEY_DISPLAY_INTENT = "displayIntent";
7150 private static final String KEY_PAGES = "pages";
7151 private static final String KEY_BACKGROUND = "background";
7152 private static final String KEY_CONTENT_ICON = "contentIcon";
7153 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
7154 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
7155 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
7156 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
7157 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007158 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04007159 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007160 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07007161
7162 // Flags bitwise-ored to mFlags
7163 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
7164 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
7165 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
7166 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007167 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04007168 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04007169 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07007170
7171 // Default value for flags integer
7172 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
7173
7174 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
7175 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
7176
7177 private ArrayList<Action> mActions = new ArrayList<Action>();
7178 private int mFlags = DEFAULT_FLAGS;
7179 private PendingIntent mDisplayIntent;
7180 private ArrayList<Notification> mPages = new ArrayList<Notification>();
7181 private Bitmap mBackground;
7182 private int mContentIcon;
7183 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
7184 private int mContentActionIndex = UNSET_ACTION_INDEX;
7185 private int mCustomSizePreset = SIZE_DEFAULT;
7186 private int mCustomContentHeight;
7187 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007188 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007189 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007190 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007191
7192 /**
7193 * Create a {@link android.app.Notification.WearableExtender} with default
7194 * options.
7195 */
7196 public WearableExtender() {
7197 }
7198
7199 public WearableExtender(Notification notif) {
7200 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
7201 if (wearableBundle != null) {
7202 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
7203 if (actions != null) {
7204 mActions.addAll(actions);
7205 }
7206
7207 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
7208 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
7209
7210 Notification[] pages = getNotificationArrayFromBundle(
7211 wearableBundle, KEY_PAGES);
7212 if (pages != null) {
7213 Collections.addAll(mPages, pages);
7214 }
7215
7216 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
7217 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
7218 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
7219 DEFAULT_CONTENT_ICON_GRAVITY);
7220 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
7221 UNSET_ACTION_INDEX);
7222 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
7223 SIZE_DEFAULT);
7224 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
7225 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007226 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04007227 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007228 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07007229 }
7230 }
7231
7232 /**
7233 * Apply wearable extensions to a notification that is being built. This is typically
7234 * called by the {@link android.app.Notification.Builder#extend} method of
7235 * {@link android.app.Notification.Builder}.
7236 */
7237 @Override
7238 public Notification.Builder extend(Notification.Builder builder) {
7239 Bundle wearableBundle = new Bundle();
7240
7241 if (!mActions.isEmpty()) {
7242 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
7243 }
7244 if (mFlags != DEFAULT_FLAGS) {
7245 wearableBundle.putInt(KEY_FLAGS, mFlags);
7246 }
7247 if (mDisplayIntent != null) {
7248 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
7249 }
7250 if (!mPages.isEmpty()) {
7251 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
7252 new Notification[mPages.size()]));
7253 }
7254 if (mBackground != null) {
7255 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
7256 }
7257 if (mContentIcon != 0) {
7258 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
7259 }
7260 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
7261 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
7262 }
7263 if (mContentActionIndex != UNSET_ACTION_INDEX) {
7264 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
7265 mContentActionIndex);
7266 }
7267 if (mCustomSizePreset != SIZE_DEFAULT) {
7268 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
7269 }
7270 if (mCustomContentHeight != 0) {
7271 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
7272 }
7273 if (mGravity != DEFAULT_GRAVITY) {
7274 wearableBundle.putInt(KEY_GRAVITY, mGravity);
7275 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007276 if (mHintScreenTimeout != 0) {
7277 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
7278 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04007279 if (mDismissalId != null) {
7280 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
7281 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007282 if (mBridgeTag != null) {
7283 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
7284 }
Griff Hazen61a9e862014-05-22 16:05:19 -07007285
7286 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
7287 return builder;
7288 }
7289
7290 @Override
7291 public WearableExtender clone() {
7292 WearableExtender that = new WearableExtender();
7293 that.mActions = new ArrayList<Action>(this.mActions);
7294 that.mFlags = this.mFlags;
7295 that.mDisplayIntent = this.mDisplayIntent;
7296 that.mPages = new ArrayList<Notification>(this.mPages);
7297 that.mBackground = this.mBackground;
7298 that.mContentIcon = this.mContentIcon;
7299 that.mContentIconGravity = this.mContentIconGravity;
7300 that.mContentActionIndex = this.mContentActionIndex;
7301 that.mCustomSizePreset = this.mCustomSizePreset;
7302 that.mCustomContentHeight = this.mCustomContentHeight;
7303 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007304 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04007305 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007306 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07007307 return that;
7308 }
7309
7310 /**
7311 * Add a wearable action to this notification.
7312 *
7313 * <p>When wearable actions are added using this method, the set of actions that
7314 * show on a wearable device splits from devices that only show actions added
7315 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7316 * of which actions display on different devices.
7317 *
7318 * @param action the action to add to this notification
7319 * @return this object for method chaining
7320 * @see android.app.Notification.Action
7321 */
7322 public WearableExtender addAction(Action action) {
7323 mActions.add(action);
7324 return this;
7325 }
7326
7327 /**
7328 * Adds wearable actions to this notification.
7329 *
7330 * <p>When wearable actions are added using this method, the set of actions that
7331 * show on a wearable device splits from devices that only show actions added
7332 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
7333 * of which actions display on different devices.
7334 *
7335 * @param actions the actions to add to this notification
7336 * @return this object for method chaining
7337 * @see android.app.Notification.Action
7338 */
7339 public WearableExtender addActions(List<Action> actions) {
7340 mActions.addAll(actions);
7341 return this;
7342 }
7343
7344 /**
7345 * Clear all wearable actions present on this builder.
7346 * @return this object for method chaining.
7347 * @see #addAction
7348 */
7349 public WearableExtender clearActions() {
7350 mActions.clear();
7351 return this;
7352 }
7353
7354 /**
7355 * Get the wearable actions present on this notification.
7356 */
7357 public List<Action> getActions() {
7358 return mActions;
7359 }
7360
7361 /**
7362 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07007363 * this notification. The {@link PendingIntent} provided should be for an activity.
7364 *
7365 * <pre class="prettyprint">
7366 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
7367 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
7368 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
7369 * Notification notif = new Notification.Builder(context)
7370 * .extend(new Notification.WearableExtender()
7371 * .setDisplayIntent(displayPendingIntent)
7372 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
7373 * .build();</pre>
7374 *
7375 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07007376 * should have an empty task affinity. It is also recommended to use the device
7377 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07007378 *
7379 * <p>Example AndroidManifest.xml entry:
7380 * <pre class="prettyprint">
7381 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
7382 * android:exported=&quot;true&quot;
7383 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07007384 * android:taskAffinity=&quot;&quot;
7385 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07007386 *
7387 * @param intent the {@link PendingIntent} for an activity
7388 * @return this object for method chaining
7389 * @see android.app.Notification.WearableExtender#getDisplayIntent
7390 */
7391 public WearableExtender setDisplayIntent(PendingIntent intent) {
7392 mDisplayIntent = intent;
7393 return this;
7394 }
7395
7396 /**
7397 * Get the intent to launch inside of an activity view when displaying this
7398 * notification. This {@code PendingIntent} should be for an activity.
7399 */
7400 public PendingIntent getDisplayIntent() {
7401 return mDisplayIntent;
7402 }
7403
7404 /**
7405 * Add an additional page of content to display with this notification. The current
7406 * notification forms the first page, and pages added using this function form
7407 * subsequent pages. This field can be used to separate a notification into multiple
7408 * sections.
7409 *
7410 * @param page the notification to add as another page
7411 * @return this object for method chaining
7412 * @see android.app.Notification.WearableExtender#getPages
7413 */
7414 public WearableExtender addPage(Notification page) {
7415 mPages.add(page);
7416 return this;
7417 }
7418
7419 /**
7420 * Add additional pages of content to display with this notification. The current
7421 * notification forms the first page, and pages added using this function form
7422 * subsequent pages. This field can be used to separate a notification into multiple
7423 * sections.
7424 *
7425 * @param pages a list of notifications
7426 * @return this object for method chaining
7427 * @see android.app.Notification.WearableExtender#getPages
7428 */
7429 public WearableExtender addPages(List<Notification> pages) {
7430 mPages.addAll(pages);
7431 return this;
7432 }
7433
7434 /**
7435 * Clear all additional pages present on this builder.
7436 * @return this object for method chaining.
7437 * @see #addPage
7438 */
7439 public WearableExtender clearPages() {
7440 mPages.clear();
7441 return this;
7442 }
7443
7444 /**
7445 * Get the array of additional pages of content for displaying this notification. The
7446 * current notification forms the first page, and elements within this array form
7447 * subsequent pages. This field can be used to separate a notification into multiple
7448 * sections.
7449 * @return the pages for this notification
7450 */
7451 public List<Notification> getPages() {
7452 return mPages;
7453 }
7454
7455 /**
7456 * Set a background image to be displayed behind the notification content.
7457 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
7458 * will work with any notification style.
7459 *
7460 * @param background the background bitmap
7461 * @return this object for method chaining
7462 * @see android.app.Notification.WearableExtender#getBackground
7463 */
7464 public WearableExtender setBackground(Bitmap background) {
7465 mBackground = background;
7466 return this;
7467 }
7468
7469 /**
7470 * Get a background image to be displayed behind the notification content.
7471 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
7472 * will work with any notification style.
7473 *
7474 * @return the background image
7475 * @see android.app.Notification.WearableExtender#setBackground
7476 */
7477 public Bitmap getBackground() {
7478 return mBackground;
7479 }
7480
7481 /**
7482 * Set an icon that goes with the content of this notification.
7483 */
7484 public WearableExtender setContentIcon(int icon) {
7485 mContentIcon = icon;
7486 return this;
7487 }
7488
7489 /**
7490 * Get an icon that goes with the content of this notification.
7491 */
7492 public int getContentIcon() {
7493 return mContentIcon;
7494 }
7495
7496 /**
7497 * Set the gravity that the content icon should have within the notification display.
7498 * Supported values include {@link android.view.Gravity#START} and
7499 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7500 * @see #setContentIcon
7501 */
7502 public WearableExtender setContentIconGravity(int contentIconGravity) {
7503 mContentIconGravity = contentIconGravity;
7504 return this;
7505 }
7506
7507 /**
7508 * Get the gravity that the content icon should have within the notification display.
7509 * Supported values include {@link android.view.Gravity#START} and
7510 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7511 * @see #getContentIcon
7512 */
7513 public int getContentIconGravity() {
7514 return mContentIconGravity;
7515 }
7516
7517 /**
7518 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07007519 * this notification. This action will no longer display separately from the
7520 * notification's content.
7521 *
Griff Hazenca48d352014-05-28 22:37:13 -07007522 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007523 * set, although the list of available actions comes from the main notification and not
7524 * from the child page's notification.
7525 *
7526 * @param actionIndex The index of the action to hoist onto the current notification page.
7527 * If wearable actions were added to the main notification, this index
7528 * will apply to that list, otherwise it will apply to the regular
7529 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07007530 */
7531 public WearableExtender setContentAction(int actionIndex) {
7532 mContentActionIndex = actionIndex;
7533 return this;
7534 }
7535
7536 /**
Griff Hazenca48d352014-05-28 22:37:13 -07007537 * Get the index of the notification action, if any, that was specified as being clickable
7538 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07007539 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07007540 *
Griff Hazenca48d352014-05-28 22:37:13 -07007541 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007542 * set, although the list of available actions comes from the main notification and not
7543 * from the child page's notification.
7544 *
7545 * <p>If wearable specific actions were added to the main notification, this index will
7546 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07007547 *
7548 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07007549 */
7550 public int getContentAction() {
7551 return mContentActionIndex;
7552 }
7553
7554 /**
7555 * Set the gravity that this notification should have within the available viewport space.
7556 * Supported values include {@link android.view.Gravity#TOP},
7557 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7558 * The default value is {@link android.view.Gravity#BOTTOM}.
7559 */
7560 public WearableExtender setGravity(int gravity) {
7561 mGravity = gravity;
7562 return this;
7563 }
7564
7565 /**
7566 * Get the gravity that this notification should have within the available viewport space.
7567 * Supported values include {@link android.view.Gravity#TOP},
7568 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7569 * The default value is {@link android.view.Gravity#BOTTOM}.
7570 */
7571 public int getGravity() {
7572 return mGravity;
7573 }
7574
7575 /**
7576 * Set the custom size preset for the display of this notification out of the available
7577 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7578 * {@link #SIZE_LARGE}.
7579 * <p>Some custom size presets are only applicable for custom display notifications created
7580 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
7581 * documentation for the preset in question. See also
7582 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
7583 */
7584 public WearableExtender setCustomSizePreset(int sizePreset) {
7585 mCustomSizePreset = sizePreset;
7586 return this;
7587 }
7588
7589 /**
7590 * Get the custom size preset for the display of this notification out of the available
7591 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7592 * {@link #SIZE_LARGE}.
7593 * <p>Some custom size presets are only applicable for custom display notifications created
7594 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
7595 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
7596 */
7597 public int getCustomSizePreset() {
7598 return mCustomSizePreset;
7599 }
7600
7601 /**
7602 * Set the custom height in pixels for the display of this notification's content.
7603 * <p>This option is only available for custom display notifications created
7604 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
7605 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
7606 * {@link #getCustomContentHeight}.
7607 */
7608 public WearableExtender setCustomContentHeight(int height) {
7609 mCustomContentHeight = height;
7610 return this;
7611 }
7612
7613 /**
7614 * Get the custom height in pixels for the display of this notification's content.
7615 * <p>This option is only available for custom display notifications created
7616 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
7617 * {@link #setCustomContentHeight}.
7618 */
7619 public int getCustomContentHeight() {
7620 return mCustomContentHeight;
7621 }
7622
7623 /**
7624 * Set whether the scrolling position for the contents of this notification should start
7625 * at the bottom of the contents instead of the top when the contents are too long to
7626 * display within the screen. Default is false (start scroll at the top).
7627 */
7628 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
7629 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
7630 return this;
7631 }
7632
7633 /**
7634 * Get whether the scrolling position for the contents of this notification should start
7635 * at the bottom of the contents instead of the top when the contents are too long to
7636 * display within the screen. Default is false (start scroll at the top).
7637 */
7638 public boolean getStartScrollBottom() {
7639 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
7640 }
7641
7642 /**
7643 * Set whether the content intent is available when the wearable device is not connected
7644 * to a companion device. The user can still trigger this intent when the wearable device
7645 * is offline, but a visual hint will indicate that the content intent may not be available.
7646 * Defaults to true.
7647 */
7648 public WearableExtender setContentIntentAvailableOffline(
7649 boolean contentIntentAvailableOffline) {
7650 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
7651 return this;
7652 }
7653
7654 /**
7655 * Get whether the content intent is available when the wearable device is not connected
7656 * to a companion device. The user can still trigger this intent when the wearable device
7657 * is offline, but a visual hint will indicate that the content intent may not be available.
7658 * Defaults to true.
7659 */
7660 public boolean getContentIntentAvailableOffline() {
7661 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
7662 }
7663
7664 /**
7665 * Set a hint that this notification's icon should not be displayed.
7666 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
7667 * @return this object for method chaining
7668 */
7669 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
7670 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
7671 return this;
7672 }
7673
7674 /**
7675 * Get a hint that this notification's icon should not be displayed.
7676 * @return {@code true} if this icon should not be displayed, false otherwise.
7677 * The default value is {@code false} if this was never set.
7678 */
7679 public boolean getHintHideIcon() {
7680 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
7681 }
7682
7683 /**
7684 * Set a visual hint that only the background image of this notification should be
7685 * displayed, and other semantic content should be hidden. This hint is only applicable
7686 * to sub-pages added using {@link #addPage}.
7687 */
7688 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
7689 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
7690 return this;
7691 }
7692
7693 /**
7694 * Get a visual hint that only the background image of this notification should be
7695 * displayed, and other semantic content should be hidden. This hint is only applicable
7696 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
7697 */
7698 public boolean getHintShowBackgroundOnly() {
7699 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
7700 }
7701
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007702 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007703 * Set a hint that this notification's background should not be clipped if possible,
7704 * and should instead be resized to fully display on the screen, retaining the aspect
7705 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007706 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
7707 * @return this object for method chaining
7708 */
7709 public WearableExtender setHintAvoidBackgroundClipping(
7710 boolean hintAvoidBackgroundClipping) {
7711 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
7712 return this;
7713 }
7714
7715 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007716 * Get a hint that this notification's background should not be clipped if possible,
7717 * and should instead be resized to fully display on the screen, retaining the aspect
7718 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007719 * @return {@code true} if it's ok if the background is clipped on the screen, false
7720 * otherwise. The default value is {@code false} if this was never set.
7721 */
7722 public boolean getHintAvoidBackgroundClipping() {
7723 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
7724 }
7725
7726 /**
7727 * Set a hint that the screen should remain on for at least this duration when
7728 * this notification is displayed on the screen.
7729 * @param timeout The requested screen timeout in milliseconds. Can also be either
7730 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7731 * @return this object for method chaining
7732 */
7733 public WearableExtender setHintScreenTimeout(int timeout) {
7734 mHintScreenTimeout = timeout;
7735 return this;
7736 }
7737
7738 /**
7739 * Get the duration, in milliseconds, that the screen should remain on for
7740 * when this notification is displayed.
7741 * @return the duration in milliseconds if > 0, or either one of the sentinel values
7742 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7743 */
7744 public int getHintScreenTimeout() {
7745 return mHintScreenTimeout;
7746 }
7747
Alex Hills9ab3a232016-04-05 14:54:56 -04007748 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04007749 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
7750 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7751 * qr codes, as well as other simple black-and-white tickets.
7752 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
7753 * @return this object for method chaining
7754 */
7755 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
7756 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
7757 return this;
7758 }
7759
7760 /**
7761 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
7762 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7763 * qr codes, as well as other simple black-and-white tickets.
7764 * @return {@code true} if it should be displayed in ambient, false otherwise
7765 * otherwise. The default value is {@code false} if this was never set.
7766 */
7767 public boolean getHintAmbientBigPicture() {
7768 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
7769 }
7770
7771 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04007772 * Set a hint that this notification's content intent will launch an {@link Activity}
7773 * directly, telling the platform that it can generate the appropriate transitions.
7774 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
7775 * an activity and transitions should be generated, false otherwise.
7776 * @return this object for method chaining
7777 */
7778 public WearableExtender setHintContentIntentLaunchesActivity(
7779 boolean hintContentIntentLaunchesActivity) {
7780 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
7781 return this;
7782 }
7783
7784 /**
7785 * Get a hint that this notification's content intent will launch an {@link Activity}
7786 * directly, telling the platform that it can generate the appropriate transitions
7787 * @return {@code true} if the content intent will launch an activity and transitions should
7788 * be generated, false otherwise. The default value is {@code false} if this was never set.
7789 */
7790 public boolean getHintContentIntentLaunchesActivity() {
7791 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
7792 }
7793
Nadia Benbernou948627e2016-04-14 14:41:08 -04007794 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007795 * Sets the dismissal id for this notification. If a notification is posted with a
7796 * dismissal id, then when that notification is canceled, notifications on other wearables
7797 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04007798 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007799 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04007800 * @param dismissalId the dismissal id of the notification.
7801 * @return this object for method chaining
7802 */
7803 public WearableExtender setDismissalId(String dismissalId) {
7804 mDismissalId = dismissalId;
7805 return this;
7806 }
7807
7808 /**
7809 * Returns the dismissal id of the notification.
7810 * @return the dismissal id of the notification or null if it has not been set.
7811 */
7812 public String getDismissalId() {
7813 return mDismissalId;
7814 }
7815
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007816 /**
7817 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
7818 * posted from a phone to provide finer-grained control on what notifications are bridged
7819 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
7820 * Features to Notifications</a> for more information.
7821 * @param bridgeTag the bridge tag of the notification.
7822 * @return this object for method chaining
7823 */
7824 public WearableExtender setBridgeTag(String bridgeTag) {
7825 mBridgeTag = bridgeTag;
7826 return this;
7827 }
7828
7829 /**
7830 * Returns the bridge tag of the notification.
7831 * @return the bridge tag or null if not present.
7832 */
7833 public String getBridgeTag() {
7834 return mBridgeTag;
7835 }
7836
Griff Hazen61a9e862014-05-22 16:05:19 -07007837 private void setFlag(int mask, boolean value) {
7838 if (value) {
7839 mFlags |= mask;
7840 } else {
7841 mFlags &= ~mask;
7842 }
7843 }
7844 }
7845
7846 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007847 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
7848 * with car extensions:
7849 *
7850 * <ol>
7851 * <li>Create an {@link Notification.Builder}, setting any desired
7852 * properties.
7853 * <li>Create a {@link CarExtender}.
7854 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
7855 * {@link CarExtender}.
7856 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
7857 * to apply the extensions to a notification.
7858 * </ol>
7859 *
7860 * <pre class="prettyprint">
7861 * Notification notification = new Notification.Builder(context)
7862 * ...
7863 * .extend(new CarExtender()
7864 * .set*(...))
7865 * .build();
7866 * </pre>
7867 *
7868 * <p>Car extensions can be accessed on an existing notification by using the
7869 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
7870 * to access values.
7871 */
7872 public static final class CarExtender implements Extender {
7873 private static final String TAG = "CarExtender";
7874
7875 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
7876 private static final String EXTRA_LARGE_ICON = "large_icon";
7877 private static final String EXTRA_CONVERSATION = "car_conversation";
7878 private static final String EXTRA_COLOR = "app_color";
7879
7880 private Bitmap mLargeIcon;
7881 private UnreadConversation mUnreadConversation;
7882 private int mColor = Notification.COLOR_DEFAULT;
7883
7884 /**
7885 * Create a {@link CarExtender} with default options.
7886 */
7887 public CarExtender() {
7888 }
7889
7890 /**
7891 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
7892 *
7893 * @param notif The notification from which to copy options.
7894 */
7895 public CarExtender(Notification notif) {
7896 Bundle carBundle = notif.extras == null ?
7897 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
7898 if (carBundle != null) {
7899 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
7900 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
7901
7902 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
7903 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
7904 }
7905 }
7906
7907 /**
7908 * Apply car extensions to a notification that is being built. This is typically called by
7909 * the {@link Notification.Builder#extend(Notification.Extender)}
7910 * method of {@link Notification.Builder}.
7911 */
7912 @Override
7913 public Notification.Builder extend(Notification.Builder builder) {
7914 Bundle carExtensions = new Bundle();
7915
7916 if (mLargeIcon != null) {
7917 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
7918 }
7919 if (mColor != Notification.COLOR_DEFAULT) {
7920 carExtensions.putInt(EXTRA_COLOR, mColor);
7921 }
7922
7923 if (mUnreadConversation != null) {
7924 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
7925 carExtensions.putBundle(EXTRA_CONVERSATION, b);
7926 }
7927
7928 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
7929 return builder;
7930 }
7931
7932 /**
7933 * Sets the accent color to use when Android Auto presents the notification.
7934 *
7935 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
7936 * to accent the displayed notification. However, not all colors are acceptable in an
7937 * automotive setting. This method can be used to override the color provided in the
7938 * notification in such a situation.
7939 */
Tor Norbye80756e32015-03-02 09:39:27 -08007940 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007941 mColor = color;
7942 return this;
7943 }
7944
7945 /**
7946 * Gets the accent color.
7947 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007948 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007949 */
Tor Norbye80756e32015-03-02 09:39:27 -08007950 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007951 public int getColor() {
7952 return mColor;
7953 }
7954
7955 /**
7956 * Sets the large icon of the car notification.
7957 *
7958 * If no large icon is set in the extender, Android Auto will display the icon
7959 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
7960 *
7961 * @param largeIcon The large icon to use in the car notification.
7962 * @return This object for method chaining.
7963 */
7964 public CarExtender setLargeIcon(Bitmap largeIcon) {
7965 mLargeIcon = largeIcon;
7966 return this;
7967 }
7968
7969 /**
7970 * Gets the large icon used in this car notification, or null if no icon has been set.
7971 *
7972 * @return The large icon for the car notification.
7973 * @see CarExtender#setLargeIcon
7974 */
7975 public Bitmap getLargeIcon() {
7976 return mLargeIcon;
7977 }
7978
7979 /**
7980 * Sets the unread conversation in a message notification.
7981 *
7982 * @param unreadConversation The unread part of the conversation this notification conveys.
7983 * @return This object for method chaining.
7984 */
7985 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
7986 mUnreadConversation = unreadConversation;
7987 return this;
7988 }
7989
7990 /**
7991 * Returns the unread conversation conveyed by this notification.
7992 * @see #setUnreadConversation(UnreadConversation)
7993 */
7994 public UnreadConversation getUnreadConversation() {
7995 return mUnreadConversation;
7996 }
7997
7998 /**
7999 * A class which holds the unread messages from a conversation.
8000 */
8001 public static class UnreadConversation {
8002 private static final String KEY_AUTHOR = "author";
8003 private static final String KEY_TEXT = "text";
8004 private static final String KEY_MESSAGES = "messages";
8005 private static final String KEY_REMOTE_INPUT = "remote_input";
8006 private static final String KEY_ON_REPLY = "on_reply";
8007 private static final String KEY_ON_READ = "on_read";
8008 private static final String KEY_PARTICIPANTS = "participants";
8009 private static final String KEY_TIMESTAMP = "timestamp";
8010
8011 private final String[] mMessages;
8012 private final RemoteInput mRemoteInput;
8013 private final PendingIntent mReplyPendingIntent;
8014 private final PendingIntent mReadPendingIntent;
8015 private final String[] mParticipants;
8016 private final long mLatestTimestamp;
8017
8018 UnreadConversation(String[] messages, RemoteInput remoteInput,
8019 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
8020 String[] participants, long latestTimestamp) {
8021 mMessages = messages;
8022 mRemoteInput = remoteInput;
8023 mReadPendingIntent = readPendingIntent;
8024 mReplyPendingIntent = replyPendingIntent;
8025 mParticipants = participants;
8026 mLatestTimestamp = latestTimestamp;
8027 }
8028
8029 /**
8030 * Gets the list of messages conveyed by this notification.
8031 */
8032 public String[] getMessages() {
8033 return mMessages;
8034 }
8035
8036 /**
8037 * Gets the remote input that will be used to convey the response to a message list, or
8038 * null if no such remote input exists.
8039 */
8040 public RemoteInput getRemoteInput() {
8041 return mRemoteInput;
8042 }
8043
8044 /**
8045 * Gets the pending intent that will be triggered when the user replies to this
8046 * notification.
8047 */
8048 public PendingIntent getReplyPendingIntent() {
8049 return mReplyPendingIntent;
8050 }
8051
8052 /**
8053 * Gets the pending intent that Android Auto will send after it reads aloud all messages
8054 * in this object's message list.
8055 */
8056 public PendingIntent getReadPendingIntent() {
8057 return mReadPendingIntent;
8058 }
8059
8060 /**
8061 * Gets the participants in the conversation.
8062 */
8063 public String[] getParticipants() {
8064 return mParticipants;
8065 }
8066
8067 /**
8068 * Gets the firs participant in the conversation.
8069 */
8070 public String getParticipant() {
8071 return mParticipants.length > 0 ? mParticipants[0] : null;
8072 }
8073
8074 /**
8075 * Gets the timestamp of the conversation.
8076 */
8077 public long getLatestTimestamp() {
8078 return mLatestTimestamp;
8079 }
8080
8081 Bundle getBundleForUnreadConversation() {
8082 Bundle b = new Bundle();
8083 String author = null;
8084 if (mParticipants != null && mParticipants.length > 1) {
8085 author = mParticipants[0];
8086 }
8087 Parcelable[] messages = new Parcelable[mMessages.length];
8088 for (int i = 0; i < messages.length; i++) {
8089 Bundle m = new Bundle();
8090 m.putString(KEY_TEXT, mMessages[i]);
8091 m.putString(KEY_AUTHOR, author);
8092 messages[i] = m;
8093 }
8094 b.putParcelableArray(KEY_MESSAGES, messages);
8095 if (mRemoteInput != null) {
8096 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
8097 }
8098 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
8099 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
8100 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
8101 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
8102 return b;
8103 }
8104
8105 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
8106 if (b == null) {
8107 return null;
8108 }
8109 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
8110 String[] messages = null;
8111 if (parcelableMessages != null) {
8112 String[] tmp = new String[parcelableMessages.length];
8113 boolean success = true;
8114 for (int i = 0; i < tmp.length; i++) {
8115 if (!(parcelableMessages[i] instanceof Bundle)) {
8116 success = false;
8117 break;
8118 }
8119 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
8120 if (tmp[i] == null) {
8121 success = false;
8122 break;
8123 }
8124 }
8125 if (success) {
8126 messages = tmp;
8127 } else {
8128 return null;
8129 }
8130 }
8131
8132 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
8133 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
8134
8135 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
8136
8137 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
8138 if (participants == null || participants.length != 1) {
8139 return null;
8140 }
8141
8142 return new UnreadConversation(messages,
8143 remoteInput,
8144 onReply,
8145 onRead,
8146 participants, b.getLong(KEY_TIMESTAMP));
8147 }
8148 };
8149
8150 /**
8151 * Builder class for {@link CarExtender.UnreadConversation} objects.
8152 */
8153 public static class Builder {
8154 private final List<String> mMessages = new ArrayList<String>();
8155 private final String mParticipant;
8156 private RemoteInput mRemoteInput;
8157 private PendingIntent mReadPendingIntent;
8158 private PendingIntent mReplyPendingIntent;
8159 private long mLatestTimestamp;
8160
8161 /**
8162 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
8163 *
8164 * @param name The name of the other participant in the conversation.
8165 */
8166 public Builder(String name) {
8167 mParticipant = name;
8168 }
8169
8170 /**
8171 * Appends a new unread message to the list of messages for this conversation.
8172 *
8173 * The messages should be added from oldest to newest.
8174 *
8175 * @param message The text of the new unread message.
8176 * @return This object for method chaining.
8177 */
8178 public Builder addMessage(String message) {
8179 mMessages.add(message);
8180 return this;
8181 }
8182
8183 /**
8184 * Sets the pending intent and remote input which will convey the reply to this
8185 * notification.
8186 *
8187 * @param pendingIntent The pending intent which will be triggered on a reply.
8188 * @param remoteInput The remote input parcelable which will carry the reply.
8189 * @return This object for method chaining.
8190 *
8191 * @see CarExtender.UnreadConversation#getRemoteInput
8192 * @see CarExtender.UnreadConversation#getReplyPendingIntent
8193 */
8194 public Builder setReplyAction(
8195 PendingIntent pendingIntent, RemoteInput remoteInput) {
8196 mRemoteInput = remoteInput;
8197 mReplyPendingIntent = pendingIntent;
8198
8199 return this;
8200 }
8201
8202 /**
8203 * Sets the pending intent that will be sent once the messages in this notification
8204 * are read.
8205 *
8206 * @param pendingIntent The pending intent to use.
8207 * @return This object for method chaining.
8208 */
8209 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
8210 mReadPendingIntent = pendingIntent;
8211 return this;
8212 }
8213
8214 /**
8215 * Sets the timestamp of the most recent message in an unread conversation.
8216 *
8217 * If a messaging notification has been posted by your application and has not
8218 * yet been cancelled, posting a later notification with the same id and tag
8219 * but without a newer timestamp may result in Android Auto not displaying a
8220 * heads up notification for the later notification.
8221 *
8222 * @param timestamp The timestamp of the most recent message in the conversation.
8223 * @return This object for method chaining.
8224 */
8225 public Builder setLatestTimestamp(long timestamp) {
8226 mLatestTimestamp = timestamp;
8227 return this;
8228 }
8229
8230 /**
8231 * Builds a new unread conversation object.
8232 *
8233 * @return The new unread conversation object.
8234 */
8235 public UnreadConversation build() {
8236 String[] messages = mMessages.toArray(new String[mMessages.size()]);
8237 String[] participants = { mParticipant };
8238 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
8239 mReadPendingIntent, participants, mLatestTimestamp);
8240 }
8241 }
8242 }
8243
8244 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008245 * <p>Helper class to add Android TV extensions to notifications. To create a notification
8246 * with a TV extension:
8247 *
8248 * <ol>
8249 * <li>Create an {@link Notification.Builder}, setting any desired properties.
8250 * <li>Create a {@link TvExtender}.
8251 * <li>Set TV-specific properties using the {@code set} methods of
8252 * {@link TvExtender}.
8253 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
8254 * to apply the extension to a notification.
8255 * </ol>
8256 *
8257 * <pre class="prettyprint">
8258 * Notification notification = new Notification.Builder(context)
8259 * ...
8260 * .extend(new TvExtender()
8261 * .set*(...))
8262 * .build();
8263 * </pre>
8264 *
8265 * <p>TV extensions can be accessed on an existing notification by using the
8266 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
8267 * to access values.
8268 *
8269 * @hide
8270 */
8271 @SystemApi
8272 public static final class TvExtender implements Extender {
8273 private static final String TAG = "TvExtender";
8274
8275 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
8276 private static final String EXTRA_FLAGS = "flags";
8277 private static final String EXTRA_CONTENT_INTENT = "content_intent";
8278 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008279 private static final String EXTRA_CHANNEL_ID = "channel_id";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008280
8281 // Flags bitwise-ored to mFlags
8282 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
8283
8284 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008285 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008286 private PendingIntent mContentIntent;
8287 private PendingIntent mDeleteIntent;
8288
8289 /**
8290 * Create a {@link TvExtender} with default options.
8291 */
8292 public TvExtender() {
8293 mFlags = FLAG_AVAILABLE_ON_TV;
8294 }
8295
8296 /**
8297 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
8298 *
8299 * @param notif The notification from which to copy options.
8300 */
8301 public TvExtender(Notification notif) {
8302 Bundle bundle = notif.extras == null ?
8303 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
8304 if (bundle != null) {
8305 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008306 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008307 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
8308 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
8309 }
8310 }
8311
8312 /**
8313 * Apply a TV extension to a notification that is being built. This is typically called by
8314 * the {@link Notification.Builder#extend(Notification.Extender)}
8315 * method of {@link Notification.Builder}.
8316 */
8317 @Override
8318 public Notification.Builder extend(Notification.Builder builder) {
8319 Bundle bundle = new Bundle();
8320
8321 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008322 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008323 if (mContentIntent != null) {
8324 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
8325 }
8326
8327 if (mDeleteIntent != null) {
8328 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
8329 }
8330
8331 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
8332 return builder;
8333 }
8334
8335 /**
8336 * Returns true if this notification should be shown on TV. This method return true
8337 * if the notification was extended with a TvExtender.
8338 */
8339 public boolean isAvailableOnTv() {
8340 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
8341 }
8342
8343 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008344 * Specifies the channel the notification should be delivered on when shown on TV.
8345 * It can be different from the channel that the notification is delivered to when
8346 * posting on a non-TV device.
8347 */
8348 public TvExtender setChannel(String channelId) {
8349 mChannelId = channelId;
8350 return this;
8351 }
8352
8353 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008354 * Specifies the channel the notification should be delivered on when shown on TV.
8355 * It can be different from the channel that the notification is delivered to when
8356 * posting on a non-TV device.
8357 */
8358 public TvExtender setChannelId(String channelId) {
8359 mChannelId = channelId;
8360 return this;
8361 }
8362
Jeff Sharkey000ce802017-04-29 13:13:27 -06008363 /** @removed */
8364 @Deprecated
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08008365 public String getChannel() {
8366 return mChannelId;
8367 }
8368
8369 /**
Julia Reynoldsbad42972017-04-25 13:52:49 -04008370 * Returns the id of the channel this notification posts to on TV.
8371 */
8372 public String getChannelId() {
8373 return mChannelId;
8374 }
8375
8376 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08008377 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
8378 * If provided, it is used instead of the content intent specified
8379 * at the level of Notification.
8380 */
8381 public TvExtender setContentIntent(PendingIntent intent) {
8382 mContentIntent = intent;
8383 return this;
8384 }
8385
8386 /**
8387 * Returns the TV-specific content intent. If this method returns null, the
8388 * main content intent on the notification should be used.
8389 *
8390 * @see {@link Notification#contentIntent}
8391 */
8392 public PendingIntent getContentIntent() {
8393 return mContentIntent;
8394 }
8395
8396 /**
8397 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
8398 * by the user on TV. If provided, it is used instead of the delete intent specified
8399 * at the level of Notification.
8400 */
8401 public TvExtender setDeleteIntent(PendingIntent intent) {
8402 mDeleteIntent = intent;
8403 return this;
8404 }
8405
8406 /**
8407 * Returns the TV-specific delete intent. If this method returns null, the
8408 * main delete intent on the notification should be used.
8409 *
8410 * @see {@link Notification#deleteIntent}
8411 */
8412 public PendingIntent getDeleteIntent() {
8413 return mDeleteIntent;
8414 }
8415 }
8416
8417 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07008418 * Get an array of Notification objects from a parcelable array bundle field.
8419 * Update the bundle to have a typed array so fetches in the future don't need
8420 * to do an array copy.
8421 */
8422 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
8423 Parcelable[] array = bundle.getParcelableArray(key);
8424 if (array instanceof Notification[] || array == null) {
8425 return (Notification[]) array;
8426 }
8427 Notification[] typedArray = Arrays.copyOf(array, array.length,
8428 Notification[].class);
8429 bundle.putParcelableArray(key, typedArray);
8430 return typedArray;
8431 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02008432
8433 private static class BuilderRemoteViews extends RemoteViews {
8434 public BuilderRemoteViews(Parcel parcel) {
8435 super(parcel);
8436 }
8437
Kenny Guy77320062014-08-27 21:37:15 +01008438 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
8439 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02008440 }
8441
8442 @Override
8443 public BuilderRemoteViews clone() {
8444 Parcel p = Parcel.obtain();
8445 writeToParcel(p, 0);
8446 p.setDataPosition(0);
8447 BuilderRemoteViews brv = new BuilderRemoteViews(p);
8448 p.recycle();
8449 return brv;
8450 }
8451 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08008452
8453 private static class StandardTemplateParams {
8454 boolean hasProgress = true;
8455 boolean ambient = false;
8456 CharSequence title;
8457 CharSequence text;
Selim Cinek88188f22017-09-19 16:46:56 -07008458 boolean hideLargeIcon;
8459 public boolean alwaysShowReply;
Adrian Roos70d7aa32017-01-11 15:39:06 -08008460
8461 final StandardTemplateParams reset() {
8462 hasProgress = true;
8463 ambient = false;
8464 title = null;
8465 text = null;
8466 return this;
8467 }
8468
8469 final StandardTemplateParams hasProgress(boolean hasProgress) {
8470 this.hasProgress = hasProgress;
8471 return this;
8472 }
8473
8474 final StandardTemplateParams title(CharSequence title) {
8475 this.title = title;
8476 return this;
8477 }
8478
8479 final StandardTemplateParams text(CharSequence text) {
8480 this.text = text;
8481 return this;
8482 }
8483
Selim Cinek88188f22017-09-19 16:46:56 -07008484 final StandardTemplateParams alwaysShowReply(boolean alwaysShowReply) {
8485 this.alwaysShowReply = alwaysShowReply;
8486 return this;
8487 }
8488
8489 final StandardTemplateParams hideLargeIcon(boolean hideLargeIcon) {
8490 this.hideLargeIcon = hideLargeIcon;
8491 return this;
8492 }
8493
Adrian Roos70d7aa32017-01-11 15:39:06 -08008494 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08008495 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08008496 this.ambient = ambient;
8497 return this;
8498 }
8499
8500 final StandardTemplateParams fillTextsFrom(Builder b) {
8501 Bundle extras = b.mN.extras;
Lucas Dupin06c5e642017-09-13 16:34:58 -07008502 this.title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
8503
8504 // Big text notifications should contain their content when viewed in ambient mode.
8505 CharSequence text = extras.getCharSequence(EXTRA_BIG_TEXT);
8506 if (!ambient || TextUtils.isEmpty(text)) {
8507 text = extras.getCharSequence(EXTRA_TEXT);
8508 }
8509 this.text = b.processLegacyText(text, ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08008510 return this;
8511 }
8512 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08008513}