blob: 96d44a5215b026ed374f61982805ec19a2167503 [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
Tor Norbye80756e32015-03-02 09:39:27 -080019import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070020import android.annotation.DrawableRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070021import android.annotation.IntDef;
Alex Hillsfd590442016-10-07 09:52:44 -040022import android.annotation.NonNull;
Daniel Sandler01df1c62014-06-09 10:54:01 -040023import android.annotation.SdkConstant;
24import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040025import android.annotation.SystemApi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.Context;
27import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010028import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040029import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020030import android.content.pm.PackageManager.NameNotFoundException;
Julia Reynolds13d898c2017-02-02 12:22:05 -050031import android.content.pm.ShortcutInfo;
Jorim Jaggief72a192014-08-26 21:57:46 +020032import android.content.res.ColorStateList;
Joe Onoratoef1e7762010-09-17 18:38:38 -040033import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010034import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070035import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010036import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010037import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040038import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040039import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070040import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080041import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070042import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040044import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020045import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050046import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.Parcel;
48import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040049import android.os.SystemClock;
Selim Cinek6743c0b2017-01-18 18:24:01 -080050import android.os.SystemProperties;
Jeff Sharkey6d515712012-09-20 16:06:08 -070051import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070052import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080053import android.text.SpannableStringBuilder;
54import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080056import android.text.style.AbsoluteSizeSpan;
Selim Cinek981962e2016-07-20 20:41:58 -070057import android.text.style.BackgroundColorSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080058import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070059import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080060import android.text.style.RelativeSizeSpan;
61import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070062import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040063import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050064import android.util.SparseArray;
Griff Hazen61a9e862014-05-22 16:05:19 -070065import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080066import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080067import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070068import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070069import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.widget.RemoteViews;
71
Griff Hazen959591e2014-05-15 22:26:18 -070072import com.android.internal.R;
Svet Ganovddb94882016-06-23 19:55:24 -070073import com.android.internal.util.ArrayUtils;
Griff Hazenc091ba82014-05-16 10:13:26 -070074import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070075
Tor Norbyed9273d62013-05-30 15:59:53 -070076import java.lang.annotation.Retention;
77import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020078import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050079import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070080import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070081import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070082import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050083import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080084
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085/**
86 * A class that represents how a persistent notification is to be presented to
87 * the user using the {@link android.app.NotificationManager}.
88 *
Joe Onoratocb109a02011-01-18 17:57:41 -080089 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
90 * easier to construct Notifications.</p>
91 *
Joe Fernandez558459f2011-10-13 16:47:36 -070092 * <div class="special reference">
93 * <h3>Developer Guides</h3>
94 * <p>For a guide to creating notifications, read the
95 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
96 * developer guide.</p>
97 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 */
99public class Notification implements Parcelable
100{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400101 private static final String TAG = "Notification";
102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400104 * An activity that provides a user interface for adjusting notification preferences for its
105 * containing application. Optional but recommended for apps that post
106 * {@link android.app.Notification Notifications}.
107 */
108 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
109 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
110 = "android.intent.category.NOTIFICATION_PREFERENCES";
111
112 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500113 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
114 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
115 * what in app notifications settings should be shown.
116 */
117 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
118
119 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 * Use all default values (where applicable).
121 */
122 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500123
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 /**
125 * Use the default notification sound. This will ignore any given
126 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500127 *
Chris Wren47c20a12014-06-18 17:27:29 -0400128 * <p>
129 * A notification that is noisy is more likely to be presented as a heads-up notification.
130 * </p>
131 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500133 */
134
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 public static final int DEFAULT_SOUND = 1;
136
137 /**
138 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500139 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700140 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500141 *
Chris Wren47c20a12014-06-18 17:27:29 -0400142 * <p>
143 * A notification that vibrates is more likely to be presented as a heads-up notification.
144 * </p>
145 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500147 */
148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 /**
152 * Use the default notification lights. This will ignore the
153 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
154 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500155 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500157 */
158
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200162 * Maximum length of CharSequences accepted by Builder and friends.
163 *
164 * <p>
165 * Avoids spamming the system with overly large strings such as full e-mails.
166 */
167 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
168
169 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800170 * Maximum entries of reply text that are accepted by Builder and friends.
171 */
172 private static final int MAX_REPLY_HISTORY = 5;
173
174 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500175 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800176 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500177 * Default value: {@link System#currentTimeMillis() Now}.
178 *
179 * Choose a timestamp that will be most relevant to the user. For most finite events, this
180 * corresponds to the time the event happened (or will happen, in the case of events that have
181 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800182 * timestamped according to when the activity began.
183 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500184 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800185 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500186 * <ul>
187 * <li>Notification of a new chat message should be stamped when the message was received.</li>
188 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
189 * <li>Notification of a completed file download should be stamped when the download finished.</li>
190 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
191 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
192 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800193 * </ul>
194 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700195 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
196 * anymore by default and must be opted into by using
197 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 */
199 public long when;
200
201 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700202 * The creation time of the notification
203 */
204 private long creationTime;
205
206 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800207 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400208 *
209 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 */
Dan Sandler86647982015-05-13 23:41:13 -0400211 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700212 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 public int icon;
214
215 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800216 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
217 * leave it at its default value of 0.
218 *
219 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700220 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800221 */
222 public int iconLevel;
223
224 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500225 * The number of events that this notification represents. For example, in a new mail
226 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800227 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500228 * The system may or may not use this field to modify the appearance of the notification. For
229 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
230 * superimposed over the icon in the status bar. Starting with
231 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
232 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500233 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
234 * badge icon in Launchers that support badging.
Joe Malin8d40d042012-11-05 11:36:40 -0800235 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 */
Julia Reynolds13d898c2017-02-02 12:22:05 -0500237 public int number = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238
239 /**
240 * The intent to execute when the expanded status entry is clicked. If
241 * this is an activity, it must include the
242 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800243 * that you take care of task management as described in the
244 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800245 * Stack</a> document. In particular, make sure to read the notification section
246 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
247 * Notifications</a> for the correct ways to launch an application from a
248 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 */
250 public PendingIntent contentIntent;
251
252 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500253 * The intent to execute when the notification is explicitly dismissed by the user, either with
254 * the "Clear All" button or by swiping it away individually.
255 *
256 * This probably shouldn't be launching an activity since several of those will be sent
257 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800258 */
259 public PendingIntent deleteIntent;
260
261 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700262 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800263 *
Chris Wren47c20a12014-06-18 17:27:29 -0400264 * <p>
265 * The system UI may choose to display a heads-up notification, instead of
266 * launching this intent, while the user is using the device.
267 * </p>
268 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800269 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400270 */
271 public PendingIntent fullScreenIntent;
272
273 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400274 * Text that summarizes this notification for accessibility services.
275 *
276 * As of the L release, this text is no longer shown on screen, but it is still useful to
277 * accessibility services (where it serves as an audible announcement of the notification's
278 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400279 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800280 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800281 */
282 public CharSequence tickerText;
283
284 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400285 * Formerly, a view showing the {@link #tickerText}.
286 *
287 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400288 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400289 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800290 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400291
292 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400293 * The view that will represent this notification in the notification list (which is pulled
294 * down from the status bar).
295 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500296 * As of N, this field may be null. The notification view is determined by the inputs
297 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400298 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400300 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 public RemoteViews contentView;
302
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400303 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400304 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400305 * opportunity to show more detail. The system UI may choose to show this
306 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400307 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500308 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400309 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
310 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400311 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400312 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400313 public RemoteViews bigContentView;
314
Chris Wren8fd39ec2014-02-27 17:43:26 -0500315
316 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400317 * A medium-format version of {@link #contentView}, providing the Notification an
318 * opportunity to add action buttons to contentView. At its discretion, the system UI may
319 * choose to show this as a heads-up notification, which will pop up so the user can see
320 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400321 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500322 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400323 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
324 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500325 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400326 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500327 public RemoteViews headsUpContentView;
328
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400329 /**
Dan Sandler86647982015-05-13 23:41:13 -0400330 * A large bitmap to be shown in the notification content area.
331 *
332 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 */
Dan Sandler86647982015-05-13 23:41:13 -0400334 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800335 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336
337 /**
338 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500339 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400341 * A notification that is noisy is more likely to be presented as a heads-up notification.
342 * </p>
343 *
344 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500345 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800346 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500347 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500349 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 public Uri sound;
351
352 /**
353 * Use this constant as the value for audioStreamType to request that
354 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700355 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400356 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500357 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800358 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700359 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 public static final int STREAM_DEFAULT = -1;
361
362 /**
363 * The audio stream type to use when playing the sound.
364 * Should be one of the STREAM_ constants from
365 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400366 *
367 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800368 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700369 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800370 public int audioStreamType = STREAM_DEFAULT;
371
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400373 * The default value of {@link #audioAttributes}.
374 */
375 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
376 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
377 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
378 .build();
379
380 /**
381 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500382 *
383 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400384 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500385 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400386 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
387
388 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500389 * The pattern with which to vibrate.
390 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 * <p>
392 * To vibrate the default pattern, see {@link #defaults}.
393 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500394 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500396 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800397 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500398 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 public long[] vibrate;
400
401 /**
402 * The color of the led. The hardware will do its best approximation.
403 *
404 * @see #FLAG_SHOW_LIGHTS
405 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500406 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800407 */
Tor Norbye80756e32015-03-02 09:39:27 -0800408 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500409 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800410 public int ledARGB;
411
412 /**
413 * The number of milliseconds for the LED to be on while it's flashing.
414 * The hardware will do its best approximation.
415 *
416 * @see #FLAG_SHOW_LIGHTS
417 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500418 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800419 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500420 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 public int ledOnMS;
422
423 /**
424 * The number of milliseconds for the LED to be off while it's flashing.
425 * The hardware will do its best approximation.
426 *
427 * @see #FLAG_SHOW_LIGHTS
428 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500429 *
430 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800431 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500432 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 public int ledOffMS;
434
435 /**
436 * Specifies which values should be taken from the defaults.
437 * <p>
438 * To set, OR the desired from {@link #DEFAULT_SOUND},
439 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
440 * values, use {@link #DEFAULT_ALL}.
441 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500442 *
443 * @deprecated use {@link NotificationChannel#getSound()} and
444 * {@link NotificationChannel#shouldShowLights()} and
445 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500447 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800448 public int defaults;
449
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800450 /**
451 * Bit to be bitwise-ored into the {@link #flags} field that should be
452 * set if you want the LED on for this notification.
453 * <ul>
454 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
455 * or 0 for both ledOnMS and ledOffMS.</li>
456 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
457 * <li>To flash the LED, pass the number of milliseconds that it should
458 * be on and off to ledOnMS and ledOffMS.</li>
459 * </ul>
460 * <p>
461 * Since hardware varies, you are not guaranteed that any of the values
462 * you pass are honored exactly. Use the system defaults (TODO) if possible
463 * because they will be set to values that work on any given hardware.
464 * <p>
465 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500466 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500467 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500469 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
471
472 /**
473 * Bit to be bitwise-ored into the {@link #flags} field that should be
474 * set if this notification is in reference to something that is ongoing,
475 * like a phone call. It should not be set if this notification is in
476 * reference to something that happened at a particular point in time,
477 * like a missed phone call.
478 */
479 public static final int FLAG_ONGOING_EVENT = 0x00000002;
480
481 /**
482 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700483 * the audio will be repeated until the notification is
484 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800485 */
486 public static final int FLAG_INSISTENT = 0x00000004;
487
488 /**
489 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700490 * set if you would only like the sound, vibrate and ticker to be played
491 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492 */
493 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
494
495 /**
496 * Bit to be bitwise-ored into the {@link #flags} field that should be
497 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500498 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 */
500 public static final int FLAG_AUTO_CANCEL = 0x00000010;
501
502 /**
503 * Bit to be bitwise-ored into the {@link #flags} field that should be
504 * set if the notification should not be canceled when the user clicks
505 * the Clear all button.
506 */
507 public static final int FLAG_NO_CLEAR = 0x00000020;
508
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700509 /**
510 * Bit to be bitwise-ored into the {@link #flags} field that should be
511 * set if this notification represents a currently running service. This
512 * will normally be set for you by {@link Service#startForeground}.
513 */
514 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
515
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400516 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500517 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800518 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500519 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400520 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700521 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500522 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400523
Griff Hazendfcb0802014-02-11 12:00:00 -0800524 /**
525 * Bit to be bitswise-ored into the {@link #flags} field that should be
526 * set if this notification is relevant to the current device only
527 * and it is not recommended that it bridge to other devices.
528 */
529 public static final int FLAG_LOCAL_ONLY = 0x00000100;
530
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700531 /**
532 * Bit to be bitswise-ored into the {@link #flags} field that should be
533 * set if this notification is the group summary for a group of notifications.
534 * Grouped notifications may display in a cluster or stack on devices which
535 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
536 */
537 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
538
Julia Reynoldse46bb372016-03-17 11:05:58 -0400539 /**
540 * Bit to be bitswise-ored into the {@link #flags} field that should be
541 * set if this notification is the group summary for an auto-group of notifications.
542 *
543 * @hide
544 */
545 @SystemApi
546 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
547
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800548 public int flags;
549
Tor Norbyed9273d62013-05-30 15:59:53 -0700550 /** @hide */
551 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
552 @Retention(RetentionPolicy.SOURCE)
553 public @interface Priority {}
554
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500556 * Default notification {@link #priority}. If your application does not prioritize its own
557 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500558 *
559 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500560 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500561 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500562 public static final int PRIORITY_DEFAULT = 0;
563
564 /**
565 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
566 * items smaller, or at a different position in the list, compared with your app's
567 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500568 *
569 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500570 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500571 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500572 public static final int PRIORITY_LOW = -1;
573
574 /**
575 * Lowest {@link #priority}; these items might not be shown to the user except under special
576 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500577 *
578 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500579 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500580 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500581 public static final int PRIORITY_MIN = -2;
582
583 /**
584 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
585 * show these items larger, or at a different position in notification lists, compared with
586 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500587 *
588 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500589 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500590 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500591 public static final int PRIORITY_HIGH = 1;
592
593 /**
594 * Highest {@link #priority}, for your application's most important items that require the
595 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500596 *
597 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} 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_MAX = 2;
601
602 /**
603 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800604 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500605 * Priority is an indication of how much of the user's valuable attention should be consumed by
606 * this notification. Low-priority notifications may be hidden from the user in certain
607 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500608 * system will make a determination about how to interpret this priority when presenting
609 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400610 *
611 * <p>
612 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
613 * as a heads-up notification.
614 * </p>
615 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500616 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500617 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700618 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500619 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500620 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800621
Dan Sandler26e81cf2014-05-06 10:01:27 -0400622 /**
623 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
624 * to be applied by the standard Style templates when presenting this notification.
625 *
626 * The current template design constructs a colorful header image by overlaying the
627 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
628 * ignored.
629 */
Tor Norbye80756e32015-03-02 09:39:27 -0800630 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400631 public int color = COLOR_DEFAULT;
632
633 /**
634 * Special value of {@link #color} telling the system not to decorate this notification with
635 * any special color but instead use default colors when presenting this notification.
636 */
Tor Norbye80756e32015-03-02 09:39:27 -0800637 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400638 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600639
640 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800641 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800642 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800643 */
644 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800645 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800646
647 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700648 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
649 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600650 * lockscreen).
651 *
652 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
653 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
654 * shown in all situations, but the contents are only available if the device is unlocked for
655 * the appropriate user.
656 *
657 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
658 * can be read even in an "insecure" context (that is, above a secure lockscreen).
659 * To modify the public version of this notification—for example, to redact some portions—see
660 * {@link Builder#setPublicVersion(Notification)}.
661 *
662 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
663 * and ticker until the user has bypassed the lockscreen.
664 */
665 public int visibility;
666
Griff Hazenfc3922d2014-08-20 11:56:44 -0700667 /**
668 * Notification visibility: Show this notification in its entirety on all lockscreens.
669 *
670 * {@see #visibility}
671 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600672 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700673
674 /**
675 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
676 * private information on secure lockscreens.
677 *
678 * {@see #visibility}
679 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600680 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700681
682 /**
683 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
684 *
685 * {@see #visibility}
686 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600687 public static final int VISIBILITY_SECRET = -1;
688
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500689 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400690 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500691 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400692 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500693
694 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400695 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500696 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400697 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500698
699 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400700 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500701 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400702 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500703
704 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400705 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500706 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400707 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500708
709 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400710 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500711 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400712 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500713
714 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400715 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500716 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400717 public static final String CATEGORY_ALARM = "alarm";
718
719 /**
720 * Notification category: progress of a long-running background operation.
721 */
722 public static final String CATEGORY_PROGRESS = "progress";
723
724 /**
725 * Notification category: social network or sharing update.
726 */
727 public static final String CATEGORY_SOCIAL = "social";
728
729 /**
730 * Notification category: error in background operation or authentication status.
731 */
732 public static final String CATEGORY_ERROR = "err";
733
734 /**
735 * Notification category: media transport control for playback.
736 */
737 public static final String CATEGORY_TRANSPORT = "transport";
738
739 /**
740 * Notification category: system or device status update. Reserved for system use.
741 */
742 public static final String CATEGORY_SYSTEM = "sys";
743
744 /**
745 * Notification category: indication of running background service.
746 */
747 public static final String CATEGORY_SERVICE = "service";
748
749 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400750 * Notification category: a specific, timely recommendation for a single thing.
751 * For example, a news app might want to recommend a news story it believes the user will
752 * want to read next.
753 */
754 public static final String CATEGORY_RECOMMENDATION = "recommendation";
755
756 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400757 * Notification category: ongoing information about device or contextual status.
758 */
759 public static final String CATEGORY_STATUS = "status";
760
761 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400762 * Notification category: user-scheduled reminder.
763 */
764 public static final String CATEGORY_REMINDER = "reminder";
765
766 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400767 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
768 * that best describes this Notification. May be used by the system for ranking and filtering.
769 */
770 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500771
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700772 private String mGroupKey;
773
774 /**
775 * Get the key used to group this notification into a cluster or stack
776 * with other notifications on devices which support such rendering.
777 */
778 public String getGroup() {
779 return mGroupKey;
780 }
781
782 private String mSortKey;
783
784 /**
785 * Get a sort key that orders this notification among other notifications from the
786 * same package. This can be useful if an external sort was already applied and an app
787 * would like to preserve this. Notifications will be sorted lexicographically using this
788 * value, although providing different priorities in addition to providing sort key may
789 * cause this value to be ignored.
790 *
791 * <p>This sort key can also be used to order members of a notification group. See
792 * {@link Builder#setGroup}.
793 *
794 * @see String#compareTo(String)
795 */
796 public String getSortKey() {
797 return mSortKey;
798 }
799
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500800 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400801 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400802 * <p>
803 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
804 * APIs, and are intended to be used by
805 * {@link android.service.notification.NotificationListenerService} implementations to extract
806 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500807 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400808 public Bundle extras = new Bundle();
809
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400810 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700811 * All pending intents in the notification as the system needs to be able to access them but
812 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700813 * custom parcelable objects.
814 *
815 * @hide
816 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700817 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700818
819 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400820 * {@link #extras} key: this is the title of the notification,
821 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
822 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500823 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400824
825 /**
826 * {@link #extras} key: this is the title of the notification when shown in expanded form,
827 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
828 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400829 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400830
831 /**
832 * {@link #extras} key: this is the main text payload, as supplied to
833 * {@link Builder#setContentText(CharSequence)}.
834 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500835 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400836
837 /**
838 * {@link #extras} key: this is a third line of text, as supplied to
839 * {@link Builder#setSubText(CharSequence)}.
840 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400841 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400842
843 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800844 * {@link #extras} key: this is the remote input history, as supplied to
845 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700846 *
847 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
848 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
849 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
850 * notifications once the other party has responded).
851 *
852 * The extra with this key is of type CharSequence[] and contains the most recent entry at
853 * the 0 index, the second most recent at the 1 index, etc.
854 *
855 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800856 */
857 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
858
859 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400860 * {@link #extras} key: this is a small piece of additional text as supplied to
861 * {@link Builder#setContentInfo(CharSequence)}.
862 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400863 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400864
865 /**
866 * {@link #extras} key: this is a line of summary information intended to be shown
867 * alongside expanded notifications, as supplied to (e.g.)
868 * {@link BigTextStyle#setSummaryText(CharSequence)}.
869 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400870 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400871
872 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200873 * {@link #extras} key: this is the longer text shown in the big form of a
874 * {@link BigTextStyle} notification, as supplied to
875 * {@link BigTextStyle#bigText(CharSequence)}.
876 */
877 public static final String EXTRA_BIG_TEXT = "android.bigText";
878
879 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400880 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
881 * supplied to {@link Builder#setSmallIcon(int)}.
882 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500883 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400884
885 /**
886 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
887 * notification payload, as
888 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
889 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400890 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400891
892 /**
893 * {@link #extras} key: this is a bitmap to be used instead of the one from
894 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
895 * shown in its expanded form, as supplied to
896 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
897 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400898 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400899
900 /**
901 * {@link #extras} key: this is the progress value supplied to
902 * {@link Builder#setProgress(int, int, boolean)}.
903 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400904 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400905
906 /**
907 * {@link #extras} key: this is the maximum value supplied to
908 * {@link Builder#setProgress(int, int, boolean)}.
909 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400910 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400911
912 /**
913 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
914 * {@link Builder#setProgress(int, int, boolean)}.
915 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400916 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400917
918 /**
919 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
920 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
921 * {@link Builder#setUsesChronometer(boolean)}.
922 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400923 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400924
925 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800926 * {@link #extras} key: whether the chronometer set on the notification should count down
927 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -0700928 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -0800929 */
Adrian Roos96b7e202016-05-17 13:50:38 -0700930 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -0800931
932 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400933 * {@link #extras} key: whether {@link #when} should be shown,
934 * as supplied to {@link Builder#setShowWhen(boolean)}.
935 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400936 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400937
938 /**
939 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
940 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
941 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400942 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400943
944 /**
945 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
946 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
947 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400948 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400949
950 /**
951 * {@link #extras} key: A string representing the name of the specific
952 * {@link android.app.Notification.Style} used to create this notification.
953 */
Chris Wren91ad5632013-06-05 15:05:57 -0400954 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400955
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400956 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400957 * {@link #extras} key: A String array containing the people that this notification relates to,
958 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400959 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400960 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500961
962 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400963 * Allow certain system-generated notifications to appear before the device is provisioned.
964 * Only available to notifications coming from the android package.
965 * @hide
966 */
967 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
968
969 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700970 * {@link #extras} key: A
971 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
972 * in the background when the notification is selected. The URI must point to an image stream
973 * suitable for passing into
974 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
975 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
976 * URI used for this purpose must require no permissions to read the image data.
977 */
978 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
979
980 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400981 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700982 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400983 * {@link android.app.Notification.MediaStyle} notification.
984 */
985 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
986
987 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100988 * {@link #extras} key: the indices of actions to be shown in the compact view,
989 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
990 */
991 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
992
Christoph Studer943aa672014-08-03 20:31:16 +0200993 /**
Alex Hillsfc737de2016-03-23 17:33:02 -0400994 * {@link #extras} key: the username to be displayed for all messages sent by the user including
995 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -0700996 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
997 * {@link CharSequence}
Alex Hillsfc737de2016-03-23 17:33:02 -0400998 */
999 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1000
1001 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001002 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001003 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001004 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001005 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001006
1007 /**
1008 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1009 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001010 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1011 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001012 */
1013 public static final String EXTRA_MESSAGES = "android.messages";
1014
1015 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001016 * {@link #extras} key: an array of
1017 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1018 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1019 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1020 * array of bundles.
1021 */
1022 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1023
1024 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001025 * {@link #extras} key: whether the notification should be colorized as
1026 * supplied to {@link Builder#setColorized(boolean)}}.
1027 */
1028 public static final String EXTRA_COLORIZED = "android.colorized";
1029
1030 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +01001031 * {@link #extras} key: the user that built the notification.
1032 *
1033 * @hide
1034 */
1035 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
1036
1037 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001038 * @hide
1039 */
1040 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1041
Selim Cinek247fa012016-02-18 09:50:48 -08001042 /**
1043 * @hide
1044 */
1045 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1046
Shane Brennan472a3b32016-12-12 15:28:10 -08001047 /**
1048 * {@link #extras} key: the audio contents of this notification.
1049 *
1050 * This is for use when rendering the notification on an audio-focused interface;
1051 * the audio contents are a complete sound sample that contains the contents/body of the
1052 * notification. This may be used in substitute of a Text-to-Speech reading of the
1053 * notification. For example if the notification represents a voice message this should point
1054 * to the audio of that message.
1055 *
1056 * The data stored under this key should be a String representation of a Uri that contains the
1057 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1058 *
1059 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1060 * has a field for holding data URI. That field can be used for audio.
1061 * See {@code Message#setData}.
1062 *
1063 * Example usage:
1064 * <pre>
1065 * {@code
1066 * Notification.Builder myBuilder = (build your Notification as normal);
1067 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1068 * }
1069 * </pre>
1070 */
1071 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1072
Dan Sandler80eaa592016-04-14 23:34:54 -04001073 /** @hide */
1074 @SystemApi
Dan Sandler732bd6c2016-04-12 14:20:32 -04001075 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1076
Dan Sandlerd63f9322015-05-06 15:18:49 -04001077 private Icon mSmallIcon;
1078 private Icon mLargeIcon;
1079
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001080 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001081 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001082
Julia Reynolds13d898c2017-02-02 12:22:05 -05001083 private String mShortcutId;
1084
1085 /**
1086 * If this notification is being shown as a badge, always show as a number.
1087 */
1088 public static final int BADGE_ICON_NONE = 0;
1089
1090 /**
1091 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1092 * represent this notification.
1093 */
1094 public static final int BADGE_ICON_SMALL = 1;
1095
1096 /**
1097 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1098 * represent this notification.
1099 */
1100 public static final int BADGE_ICON_LARGE = 2;
1101 private int mBadgeIcon = BADGE_ICON_LARGE;
1102
Chris Wren51c75102013-07-16 20:49:17 -04001103 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001104 * Structure to encapsulate a named action that can be shown as part of this notification.
1105 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1106 * selected by the user.
1107 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001108 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1109 * or {@link Notification.Builder#addAction(Notification.Action)}
1110 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001111 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001112 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001113 /**
1114 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1115 * {@link RemoteInput}s.
1116 *
1117 * This is intended for {@link RemoteInput}s that only accept data, meaning
1118 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1119 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1120 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1121 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1122 *
1123 * You can test if a RemoteInput matches these constraints using
1124 * {@link RemoteInput#isDataOnly}.
1125 */
1126 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1127
Griff Hazen959591e2014-05-15 22:26:18 -07001128 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001129 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001130 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001131 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001132
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001133 /**
1134 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001135 *
1136 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001137 */
Dan Sandler86647982015-05-13 23:41:13 -04001138 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001139 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001140
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001141 /**
1142 * Title of the action.
1143 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001144 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001145
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001146 /**
1147 * Intent to send when the user invokes this action. May be null, in which case the action
1148 * may be rendered in a disabled presentation by the system UI.
1149 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001150 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001151
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001152 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001153 if (in.readInt() != 0) {
1154 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001155 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1156 icon = mIcon.getResId();
1157 }
Dan Sandler86647982015-05-13 23:41:13 -04001158 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001159 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1160 if (in.readInt() == 1) {
1161 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1162 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001163 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001164 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001165 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001166 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001167
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001168 /**
Dan Sandler86647982015-05-13 23:41:13 -04001169 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001170 */
Dan Sandler86647982015-05-13 23:41:13 -04001171 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001172 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001173 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001174 }
1175
Adrian Roos7af53622016-10-12 13:44:05 -07001176 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001177 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001178 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001179 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001180 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1181 this.icon = icon.getResId();
1182 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001183 this.title = title;
1184 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001185 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001186 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001187 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001188 }
1189
1190 /**
Dan Sandler86647982015-05-13 23:41:13 -04001191 * Return an icon representing the action.
1192 */
1193 public Icon getIcon() {
1194 if (mIcon == null && icon != 0) {
1195 // you snuck an icon in here without using the builder; let's try to keep it
1196 mIcon = Icon.createWithResource("", icon);
1197 }
1198 return mIcon;
1199 }
1200
1201 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001202 * Get additional metadata carried around with this Action.
1203 */
1204 public Bundle getExtras() {
1205 return mExtras;
1206 }
1207
1208 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001209 * Return whether the platform should automatically generate possible replies for this
1210 * {@link Action}
1211 */
1212 public boolean getAllowGeneratedReplies() {
1213 return mAllowGeneratedReplies;
1214 }
1215
1216 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001217 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001218 * May return null if no remote inputs were added. Only returns inputs which accept
1219 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001220 */
1221 public RemoteInput[] getRemoteInputs() {
1222 return mRemoteInputs;
1223 }
1224
1225 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001226 * Get the list of inputs to be collected from the user that ONLY accept data when this
1227 * action is sent. These remote inputs are guaranteed to return true on a call to
1228 * {@link RemoteInput#isDataOnly}.
1229 *
1230 * May return null if no data-only remote inputs were added.
1231 *
1232 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1233 * of non-textual RemoteInputs do not access these remote inputs.
1234 */
1235 public RemoteInput[] getDataOnlyRemoteInputs() {
1236 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1237 }
1238
1239 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001240 * Builder class for {@link Action} objects.
1241 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001242 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001243 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001244 private final CharSequence mTitle;
1245 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001246 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001247 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001248 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001249
1250 /**
1251 * Construct a new builder for {@link Action} object.
1252 * @param icon icon to show for this action
1253 * @param title the title of the action
1254 * @param intent the {@link PendingIntent} to fire when users trigger this action
1255 */
Dan Sandler86647982015-05-13 23:41:13 -04001256 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001257 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001258 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001259 }
1260
1261 /**
1262 * Construct a new builder for {@link Action} object.
1263 * @param icon icon to show for this action
1264 * @param title the title of the action
1265 * @param intent the {@link PendingIntent} to fire when users trigger this action
1266 */
1267 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001268 this(icon, title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001269 }
1270
1271 /**
1272 * Construct a new builder for {@link Action} object using the fields from an
1273 * {@link Action}.
1274 * @param action the action to read fields from.
1275 */
1276 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001277 this(action.getIcon(), action.title, action.actionIntent,
1278 new Bundle(action.mExtras), action.getRemoteInputs(),
1279 action.getAllowGeneratedReplies());
Griff Hazen959591e2014-05-15 22:26:18 -07001280 }
1281
Dan Sandler86647982015-05-13 23:41:13 -04001282 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Adrian Roos7af53622016-10-12 13:44:05 -07001283 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Griff Hazen959591e2014-05-15 22:26:18 -07001284 mIcon = icon;
1285 mTitle = title;
1286 mIntent = intent;
1287 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001288 if (remoteInputs != null) {
1289 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1290 Collections.addAll(mRemoteInputs, remoteInputs);
1291 }
Adrian Roos7af53622016-10-12 13:44:05 -07001292 mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001293 }
1294
1295 /**
1296 * Merge additional metadata into this builder.
1297 *
1298 * <p>Values within the Bundle will replace existing extras values in this Builder.
1299 *
1300 * @see Notification.Action#extras
1301 */
1302 public Builder addExtras(Bundle extras) {
1303 if (extras != null) {
1304 mExtras.putAll(extras);
1305 }
1306 return this;
1307 }
1308
1309 /**
1310 * Get the metadata Bundle used by this Builder.
1311 *
1312 * <p>The returned Bundle is shared with this Builder.
1313 */
1314 public Bundle getExtras() {
1315 return mExtras;
1316 }
1317
1318 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001319 * Add an input to be collected from the user when this action is sent.
1320 * Response values can be retrieved from the fired intent by using the
1321 * {@link RemoteInput#getResultsFromIntent} function.
1322 * @param remoteInput a {@link RemoteInput} to add to the action
1323 * @return this object for method chaining
1324 */
1325 public Builder addRemoteInput(RemoteInput remoteInput) {
1326 if (mRemoteInputs == null) {
1327 mRemoteInputs = new ArrayList<RemoteInput>();
1328 }
1329 mRemoteInputs.add(remoteInput);
1330 return this;
1331 }
1332
1333 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001334 * Set whether the platform should automatically generate possible replies to add to
1335 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1336 * {@link RemoteInput}, this has no effect.
1337 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1338 * otherwise
1339 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001340 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001341 */
1342 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1343 mAllowGeneratedReplies = allowGeneratedReplies;
1344 return this;
1345 }
1346
1347 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001348 * Apply an extender to this action builder. Extenders may be used to add
1349 * metadata or change options on this builder.
1350 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001351 public Builder extend(Extender extender) {
1352 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001353 return this;
1354 }
1355
1356 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001357 * Combine all of the options that have been set and return a new {@link Action}
1358 * object.
1359 * @return the built action
1360 */
1361 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001362 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1363 RemoteInput[] previousDataInputs =
1364 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001365 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001366 for (RemoteInput input : previousDataInputs) {
1367 dataOnlyInputs.add(input);
1368 }
1369 }
1370 List<RemoteInput> textInputs = new ArrayList<>();
1371 if (mRemoteInputs != null) {
1372 for (RemoteInput input : mRemoteInputs) {
1373 if (input.isDataOnly()) {
1374 dataOnlyInputs.add(input);
1375 } else {
1376 textInputs.add(input);
1377 }
1378 }
1379 }
1380 if (!dataOnlyInputs.isEmpty()) {
1381 RemoteInput[] dataInputsArr =
1382 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1383 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1384 }
1385 RemoteInput[] textInputsArr = textInputs.isEmpty()
1386 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1387 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001388 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001389 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001390 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001391
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001392 @Override
1393 public Action clone() {
1394 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001395 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001396 title,
1397 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001398 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001399 getRemoteInputs(),
1400 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001401 }
1402 @Override
1403 public int describeContents() {
1404 return 0;
1405 }
1406 @Override
1407 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001408 final Icon ic = getIcon();
1409 if (ic != null) {
1410 out.writeInt(1);
1411 ic.writeToParcel(out, 0);
1412 } else {
1413 out.writeInt(0);
1414 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001415 TextUtils.writeToParcel(title, out, flags);
1416 if (actionIntent != null) {
1417 out.writeInt(1);
1418 actionIntent.writeToParcel(out, flags);
1419 } else {
1420 out.writeInt(0);
1421 }
Griff Hazen959591e2014-05-15 22:26:18 -07001422 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001423 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001424 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001425 }
Griff Hazen959591e2014-05-15 22:26:18 -07001426 public static final Parcelable.Creator<Action> CREATOR =
1427 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001428 public Action createFromParcel(Parcel in) {
1429 return new Action(in);
1430 }
1431 public Action[] newArray(int size) {
1432 return new Action[size];
1433 }
1434 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001435
1436 /**
1437 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1438 * metadata or change options on an action builder.
1439 */
1440 public interface Extender {
1441 /**
1442 * Apply this extender to a notification action builder.
1443 * @param builder the builder to be modified.
1444 * @return the build object for chaining.
1445 */
1446 public Builder extend(Builder builder);
1447 }
1448
1449 /**
1450 * Wearable extender for notification actions. To add extensions to an action,
1451 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1452 * the {@code WearableExtender()} constructor and apply it to a
1453 * {@link android.app.Notification.Action.Builder} using
1454 * {@link android.app.Notification.Action.Builder#extend}.
1455 *
1456 * <pre class="prettyprint">
1457 * Notification.Action action = new Notification.Action.Builder(
1458 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001459 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001460 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001461 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001462 */
1463 public static final class WearableExtender implements Extender {
1464 /** Notification action extra which contains wearable extensions */
1465 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1466
Pete Gastaf6781d2014-10-07 15:17:05 -04001467 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001468 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001469 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1470 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1471 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001472
1473 // Flags bitwise-ored to mFlags
1474 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001475 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001476 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001477
1478 // Default value for flags integer
1479 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1480
1481 private int mFlags = DEFAULT_FLAGS;
1482
Pete Gastaf6781d2014-10-07 15:17:05 -04001483 private CharSequence mInProgressLabel;
1484 private CharSequence mConfirmLabel;
1485 private CharSequence mCancelLabel;
1486
Griff Hazen61a9e862014-05-22 16:05:19 -07001487 /**
1488 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1489 * options.
1490 */
1491 public WearableExtender() {
1492 }
1493
1494 /**
1495 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1496 * wearable options present in an existing notification action.
1497 * @param action the notification action to inspect.
1498 */
1499 public WearableExtender(Action action) {
1500 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1501 if (wearableBundle != null) {
1502 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001503 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1504 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1505 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001506 }
1507 }
1508
1509 /**
1510 * Apply wearable extensions to a notification action that is being built. This is
1511 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1512 * method of {@link android.app.Notification.Action.Builder}.
1513 */
1514 @Override
1515 public Action.Builder extend(Action.Builder builder) {
1516 Bundle wearableBundle = new Bundle();
1517
1518 if (mFlags != DEFAULT_FLAGS) {
1519 wearableBundle.putInt(KEY_FLAGS, mFlags);
1520 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001521 if (mInProgressLabel != null) {
1522 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1523 }
1524 if (mConfirmLabel != null) {
1525 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1526 }
1527 if (mCancelLabel != null) {
1528 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1529 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001530
1531 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1532 return builder;
1533 }
1534
1535 @Override
1536 public WearableExtender clone() {
1537 WearableExtender that = new WearableExtender();
1538 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001539 that.mInProgressLabel = this.mInProgressLabel;
1540 that.mConfirmLabel = this.mConfirmLabel;
1541 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001542 return that;
1543 }
1544
1545 /**
1546 * Set whether this action is available when the wearable device is not connected to
1547 * a companion device. The user can still trigger this action when the wearable device is
1548 * offline, but a visual hint will indicate that the action may not be available.
1549 * Defaults to true.
1550 */
1551 public WearableExtender setAvailableOffline(boolean availableOffline) {
1552 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1553 return this;
1554 }
1555
1556 /**
1557 * Get whether this action is available when the wearable device is not connected to
1558 * a companion device. The user can still trigger this action when the wearable device is
1559 * offline, but a visual hint will indicate that the action may not be available.
1560 * Defaults to true.
1561 */
1562 public boolean isAvailableOffline() {
1563 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1564 }
1565
1566 private void setFlag(int mask, boolean value) {
1567 if (value) {
1568 mFlags |= mask;
1569 } else {
1570 mFlags &= ~mask;
1571 }
1572 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001573
1574 /**
1575 * Set a label to display while the wearable is preparing to automatically execute the
1576 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1577 *
1578 * @param label the label to display while the action is being prepared to execute
1579 * @return this object for method chaining
1580 */
1581 public WearableExtender setInProgressLabel(CharSequence label) {
1582 mInProgressLabel = label;
1583 return this;
1584 }
1585
1586 /**
1587 * Get the label to display while the wearable is preparing to automatically execute
1588 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1589 *
1590 * @return the label to display while the action is being prepared to execute
1591 */
1592 public CharSequence getInProgressLabel() {
1593 return mInProgressLabel;
1594 }
1595
1596 /**
1597 * Set a label to display to confirm that the action should be executed.
1598 * This is usually an imperative verb like "Send".
1599 *
1600 * @param label the label to confirm the action should be executed
1601 * @return this object for method chaining
1602 */
1603 public WearableExtender setConfirmLabel(CharSequence label) {
1604 mConfirmLabel = label;
1605 return this;
1606 }
1607
1608 /**
1609 * Get the label to display to confirm that the action should be executed.
1610 * This is usually an imperative verb like "Send".
1611 *
1612 * @return the label to confirm the action should be executed
1613 */
1614 public CharSequence getConfirmLabel() {
1615 return mConfirmLabel;
1616 }
1617
1618 /**
1619 * Set a label to display to cancel the action.
1620 * This is usually an imperative verb, like "Cancel".
1621 *
1622 * @param label the label to display to cancel the action
1623 * @return this object for method chaining
1624 */
1625 public WearableExtender setCancelLabel(CharSequence label) {
1626 mCancelLabel = label;
1627 return this;
1628 }
1629
1630 /**
1631 * Get the label to display to cancel the action.
1632 * This is usually an imperative verb like "Cancel".
1633 *
1634 * @return the label to display to cancel the action
1635 */
1636 public CharSequence getCancelLabel() {
1637 return mCancelLabel;
1638 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001639
1640 /**
1641 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1642 * platform that it can generate the appropriate transitions.
1643 * @param hintLaunchesActivity {@code true} if the content intent will launch
1644 * an activity and transitions should be generated, false otherwise.
1645 * @return this object for method chaining
1646 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001647 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001648 boolean hintLaunchesActivity) {
1649 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1650 return this;
1651 }
1652
1653 /**
1654 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1655 * platform that it can generate the appropriate transitions
1656 * @return {@code true} if the content intent will launch an activity and transitions
1657 * should be generated, false otherwise. The default value is {@code false} if this was
1658 * never set.
1659 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001660 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001661 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1662 }
Alex Hills9f087612016-06-07 09:08:59 -04001663
1664 /**
1665 * Set a hint that this Action should be displayed inline.
1666 *
1667 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1668 * otherwise
1669 * @return this object for method chaining
1670 */
1671 public WearableExtender setHintDisplayActionInline(
1672 boolean hintDisplayInline) {
1673 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1674 return this;
1675 }
1676
1677 /**
1678 * Get a hint that this Action should be displayed inline.
1679 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08001680 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04001681 * otherwise. The default value is {@code false} if this was never set.
1682 */
1683 public boolean getHintDisplayActionInline() {
1684 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1685 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001686 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001687 }
1688
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001689 /**
1690 * Array of all {@link Action} structures attached to this notification by
1691 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1692 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1693 * interface for invoking actions.
1694 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001695 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001696
1697 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001698 * Replacement version of this notification whose content will be shown
1699 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1700 * and {@link #VISIBILITY_PUBLIC}.
1701 */
1702 public Notification publicVersion;
1703
1704 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001705 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001706 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001707 */
1708 public Notification()
1709 {
1710 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001711 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001712 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 }
1714
1715 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001716 * @hide
1717 */
1718 public Notification(Context context, int icon, CharSequence tickerText, long when,
1719 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1720 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001721 new Builder(context)
1722 .setWhen(when)
1723 .setSmallIcon(icon)
1724 .setTicker(tickerText)
1725 .setContentTitle(contentTitle)
1726 .setContentText(contentText)
1727 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1728 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001729 }
1730
1731 /**
1732 * Constructs a Notification object with the information needed to
1733 * have a status bar icon without the standard expanded view.
1734 *
1735 * @param icon The resource id of the icon to put in the status bar.
1736 * @param tickerText The text that flows by in the status bar when the notification first
1737 * activates.
1738 * @param when The time to show in the time field. In the System.currentTimeMillis
1739 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001740 *
1741 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001742 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001743 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 public Notification(int icon, CharSequence tickerText, long when)
1745 {
1746 this.icon = icon;
1747 this.tickerText = tickerText;
1748 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001749 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001750 }
1751
1752 /**
1753 * Unflatten the notification from a parcel.
1754 */
Svet Ganovddb94882016-06-23 19:55:24 -07001755 @SuppressWarnings("unchecked")
1756 public Notification(Parcel parcel) {
1757 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
1758 // intents in extras are always written as the last entry.
1759 readFromParcelImpl(parcel);
1760 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001761 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07001762 }
1763
1764 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 {
1766 int version = parcel.readInt();
1767
1768 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001769 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001770 if (parcel.readInt() != 0) {
1771 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001772 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1773 icon = mSmallIcon.getResId();
1774 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001775 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001776 number = parcel.readInt();
1777 if (parcel.readInt() != 0) {
1778 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1779 }
1780 if (parcel.readInt() != 0) {
1781 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1782 }
1783 if (parcel.readInt() != 0) {
1784 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1785 }
1786 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001787 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001788 }
1789 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001790 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1791 }
Joe Onorato561d3852010-11-20 18:09:34 -08001792 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001793 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001794 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 defaults = parcel.readInt();
1796 flags = parcel.readInt();
1797 if (parcel.readInt() != 0) {
1798 sound = Uri.CREATOR.createFromParcel(parcel);
1799 }
1800
1801 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001802 if (parcel.readInt() != 0) {
1803 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1804 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 vibrate = parcel.createLongArray();
1806 ledARGB = parcel.readInt();
1807 ledOnMS = parcel.readInt();
1808 ledOffMS = parcel.readInt();
1809 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001810
1811 if (parcel.readInt() != 0) {
1812 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1813 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001814
1815 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001816
John Spurlockfd7f1e02014-03-18 16:41:57 -04001817 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001818
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001819 mGroupKey = parcel.readString();
1820
1821 mSortKey = parcel.readString();
1822
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001823 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001824
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001825 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1826
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001827 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001828 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1829 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001830
Chris Wren8fd39ec2014-02-27 17:43:26 -05001831 if (parcel.readInt() != 0) {
1832 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1833 }
1834
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001835 visibility = parcel.readInt();
1836
1837 if (parcel.readInt() != 0) {
1838 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1839 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001840
1841 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001842
1843 if (parcel.readInt() != 0) {
1844 mChannelId = parcel.readString();
1845 }
Julia Reynolds2a128742016-11-28 14:29:25 -05001846 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05001847
1848 if (parcel.readInt() != 0) {
1849 mShortcutId = parcel.readString();
1850 }
1851
1852 mBadgeIcon = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 }
1854
Andy Stadler110988c2010-12-03 14:29:16 -08001855 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001856 public Notification clone() {
1857 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001858 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001859 return that;
1860 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001861
Daniel Sandler1a497d32013-04-18 14:52:45 -04001862 /**
1863 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1864 * of this into that.
1865 * @hide
1866 */
1867 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001868 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001869 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001870 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001871 that.number = this.number;
1872
1873 // PendingIntents are global, so there's no reason (or way) to clone them.
1874 that.contentIntent = this.contentIntent;
1875 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001876 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001877
1878 if (this.tickerText != null) {
1879 that.tickerText = this.tickerText.toString();
1880 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001881 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001882 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001883 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001884 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001885 that.contentView = this.contentView.clone();
1886 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001887 if (heavy && this.mLargeIcon != null) {
1888 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001889 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001890 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001891 that.sound = this.sound; // android.net.Uri is immutable
1892 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001893 if (this.audioAttributes != null) {
1894 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1895 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001896
1897 final long[] vibrate = this.vibrate;
1898 if (vibrate != null) {
1899 final int N = vibrate.length;
1900 final long[] vib = that.vibrate = new long[N];
1901 System.arraycopy(vibrate, 0, vib, 0, N);
1902 }
1903
1904 that.ledARGB = this.ledARGB;
1905 that.ledOnMS = this.ledOnMS;
1906 that.ledOffMS = this.ledOffMS;
1907 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001908
Joe Onorato18e69df2010-05-17 22:26:12 -07001909 that.flags = this.flags;
1910
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001911 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001912
John Spurlockfd7f1e02014-03-18 16:41:57 -04001913 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001914
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001915 that.mGroupKey = this.mGroupKey;
1916
1917 that.mSortKey = this.mSortKey;
1918
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001919 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001920 try {
1921 that.extras = new Bundle(this.extras);
1922 // will unparcel
1923 that.extras.size();
1924 } catch (BadParcelableException e) {
1925 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1926 that.extras = null;
1927 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001928 }
1929
Felipe Lemedd85da62016-06-28 11:29:54 -07001930 if (!ArrayUtils.isEmpty(allPendingIntents)) {
1931 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07001932 }
1933
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001934 if (this.actions != null) {
1935 that.actions = new Action[this.actions.length];
1936 for(int i=0; i<this.actions.length; i++) {
1937 that.actions[i] = this.actions[i].clone();
1938 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001939 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001940
Daniel Sandler1a497d32013-04-18 14:52:45 -04001941 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001942 that.bigContentView = this.bigContentView.clone();
1943 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001944
Chris Wren8fd39ec2014-02-27 17:43:26 -05001945 if (heavy && this.headsUpContentView != null) {
1946 that.headsUpContentView = this.headsUpContentView.clone();
1947 }
1948
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001949 that.visibility = this.visibility;
1950
1951 if (this.publicVersion != null) {
1952 that.publicVersion = new Notification();
1953 this.publicVersion.cloneInto(that.publicVersion, heavy);
1954 }
1955
Dan Sandler26e81cf2014-05-06 10:01:27 -04001956 that.color = this.color;
1957
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001958 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001959 that.mTimeout = this.mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001960
Daniel Sandler1a497d32013-04-18 14:52:45 -04001961 if (!heavy) {
1962 that.lightenPayload(); // will clean out extras
1963 }
1964 }
1965
1966 /**
1967 * Removes heavyweight parts of the Notification object for archival or for sending to
1968 * listeners when the full contents are not necessary.
1969 * @hide
1970 */
1971 public final void lightenPayload() {
1972 tickerView = null;
1973 contentView = null;
1974 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001975 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001976 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05001977 if (extras != null && !extras.isEmpty()) {
1978 final Set<String> keyset = extras.keySet();
1979 final int N = keyset.size();
1980 final String[] keys = keyset.toArray(new String[N]);
1981 for (int i=0; i<N; i++) {
1982 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08001983 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
1984 continue;
1985 }
Dan Sandler50128532015-12-08 15:42:41 -05001986 final Object obj = extras.get(key);
1987 if (obj != null &&
1988 ( obj instanceof Parcelable
1989 || obj instanceof Parcelable[]
1990 || obj instanceof SparseArray
1991 || obj instanceof ArrayList)) {
1992 extras.remove(key);
1993 }
1994 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001995 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001996 }
1997
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001998 /**
1999 * Make sure this CharSequence is safe to put into a bundle, which basically
2000 * means it had better not be some custom Parcelable implementation.
2001 * @hide
2002 */
2003 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002004 if (cs == null) return cs;
2005 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2006 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2007 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002008 if (cs instanceof Parcelable) {
2009 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2010 + " instance is a custom Parcelable and not allowed in Notification");
2011 return cs.toString();
2012 }
Selim Cinek60a54252016-02-26 17:03:25 -08002013 return removeTextSizeSpans(cs);
2014 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002015
Selim Cinek60a54252016-02-26 17:03:25 -08002016 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2017 if (charSequence instanceof Spanned) {
2018 Spanned ss = (Spanned) charSequence;
2019 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2020 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2021 for (Object span : spans) {
2022 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002023 if (resultSpan instanceof CharacterStyle) {
2024 resultSpan = ((CharacterStyle) span).getUnderlying();
2025 }
2026 if (resultSpan instanceof TextAppearanceSpan) {
2027 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002028 resultSpan = new TextAppearanceSpan(
2029 originalSpan.getFamily(),
2030 originalSpan.getTextStyle(),
2031 -1,
2032 originalSpan.getTextColor(),
2033 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002034 } else if (resultSpan instanceof RelativeSizeSpan
2035 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002036 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002037 } else {
2038 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002039 }
2040 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2041 ss.getSpanFlags(span));
2042 }
2043 return builder;
2044 }
2045 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002046 }
2047
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002048 public int describeContents() {
2049 return 0;
2050 }
2051
2052 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002053 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002054 */
Svet Ganovddb94882016-06-23 19:55:24 -07002055 public void writeToParcel(Parcel parcel, int flags) {
2056 // We need to mark all pending intents getting into the notification
2057 // system as being put there to later allow the notification ranker
2058 // to launch them and by doing so add the app to the battery saver white
2059 // list for a short period of time. The problem is that the system
2060 // cannot look into the extras as there may be parcelables there that
2061 // the platform does not know how to handle. To go around that we have
2062 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002063 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002064 if (collectPendingIntents) {
2065 PendingIntent.setOnMarshaledListener(
2066 (PendingIntent intent, Parcel out, int outFlags) -> {
2067 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002068 if (allPendingIntents == null) {
2069 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002070 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002071 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002072 }
2073 });
2074 }
2075 try {
2076 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002077 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002078 writeToParcelImpl(parcel, flags);
2079 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002080 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002081 } finally {
2082 if (collectPendingIntents) {
2083 PendingIntent.setOnMarshaledListener(null);
2084 }
2085 }
2086 }
2087
2088 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 parcel.writeInt(1);
2090
2091 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002092 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002093 if (mSmallIcon == null && icon != 0) {
2094 // you snuck an icon in here without using the builder; let's try to keep it
2095 mSmallIcon = Icon.createWithResource("", icon);
2096 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002097 if (mSmallIcon != null) {
2098 parcel.writeInt(1);
2099 mSmallIcon.writeToParcel(parcel, 0);
2100 } else {
2101 parcel.writeInt(0);
2102 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002103 parcel.writeInt(number);
2104 if (contentIntent != null) {
2105 parcel.writeInt(1);
2106 contentIntent.writeToParcel(parcel, 0);
2107 } else {
2108 parcel.writeInt(0);
2109 }
2110 if (deleteIntent != null) {
2111 parcel.writeInt(1);
2112 deleteIntent.writeToParcel(parcel, 0);
2113 } else {
2114 parcel.writeInt(0);
2115 }
2116 if (tickerText != null) {
2117 parcel.writeInt(1);
2118 TextUtils.writeToParcel(tickerText, parcel, flags);
2119 } else {
2120 parcel.writeInt(0);
2121 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002122 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002123 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002124 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002125 } else {
2126 parcel.writeInt(0);
2127 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 if (contentView != null) {
2129 parcel.writeInt(1);
2130 contentView.writeToParcel(parcel, 0);
2131 } else {
2132 parcel.writeInt(0);
2133 }
Selim Cinek279fa862016-06-14 10:57:25 -07002134 if (mLargeIcon == null && largeIcon != null) {
2135 // you snuck an icon in here without using the builder; let's try to keep it
2136 mLargeIcon = Icon.createWithBitmap(largeIcon);
2137 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002138 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002139 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002140 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002141 } else {
2142 parcel.writeInt(0);
2143 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002144
2145 parcel.writeInt(defaults);
2146 parcel.writeInt(this.flags);
2147
2148 if (sound != null) {
2149 parcel.writeInt(1);
2150 sound.writeToParcel(parcel, 0);
2151 } else {
2152 parcel.writeInt(0);
2153 }
2154 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002155
2156 if (audioAttributes != null) {
2157 parcel.writeInt(1);
2158 audioAttributes.writeToParcel(parcel, 0);
2159 } else {
2160 parcel.writeInt(0);
2161 }
2162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002163 parcel.writeLongArray(vibrate);
2164 parcel.writeInt(ledARGB);
2165 parcel.writeInt(ledOnMS);
2166 parcel.writeInt(ledOffMS);
2167 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002168
2169 if (fullScreenIntent != null) {
2170 parcel.writeInt(1);
2171 fullScreenIntent.writeToParcel(parcel, 0);
2172 } else {
2173 parcel.writeInt(0);
2174 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002175
2176 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002177
John Spurlockfd7f1e02014-03-18 16:41:57 -04002178 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002179
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002180 parcel.writeString(mGroupKey);
2181
2182 parcel.writeString(mSortKey);
2183
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002184 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002185
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002186 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002187
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002188 if (bigContentView != null) {
2189 parcel.writeInt(1);
2190 bigContentView.writeToParcel(parcel, 0);
2191 } else {
2192 parcel.writeInt(0);
2193 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002194
Chris Wren8fd39ec2014-02-27 17:43:26 -05002195 if (headsUpContentView != null) {
2196 parcel.writeInt(1);
2197 headsUpContentView.writeToParcel(parcel, 0);
2198 } else {
2199 parcel.writeInt(0);
2200 }
2201
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002202 parcel.writeInt(visibility);
2203
2204 if (publicVersion != null) {
2205 parcel.writeInt(1);
2206 publicVersion.writeToParcel(parcel, 0);
2207 } else {
2208 parcel.writeInt(0);
2209 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002210
2211 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002212
2213 if (mChannelId != null) {
2214 parcel.writeInt(1);
2215 parcel.writeString(mChannelId);
2216 } else {
2217 parcel.writeInt(0);
2218 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002219 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002220
2221 if (mShortcutId != null) {
2222 parcel.writeInt(1);
2223 parcel.writeString(mShortcutId);
2224 } else {
2225 parcel.writeInt(0);
2226 }
2227
2228 parcel.writeInt(mBadgeIcon);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 }
2230
2231 /**
2232 * Parcelable.Creator that instantiates Notification objects
2233 */
2234 public static final Parcelable.Creator<Notification> CREATOR
2235 = new Parcelable.Creator<Notification>()
2236 {
2237 public Notification createFromParcel(Parcel parcel)
2238 {
2239 return new Notification(parcel);
2240 }
2241
2242 public Notification[] newArray(int size)
2243 {
2244 return new Notification[size];
2245 }
2246 };
2247
2248 /**
2249 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2250 * layout.
2251 *
2252 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2253 * in the view.</p>
2254 * @param context The context for your application / activity.
2255 * @param contentTitle The title that goes in the expanded entry.
2256 * @param contentText The text that goes in the expanded entry.
2257 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2258 * If this is an activity, it must include the
2259 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002260 * that you take care of task management as described in the
2261 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2262 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002263 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002264 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002265 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002266 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002267 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002268 public void setLatestEventInfo(Context context,
2269 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002270 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2271 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2272 new Throwable());
2273 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002274
Selim Cinek4ac6f602016-06-13 15:47:03 -07002275 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2276 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2277 }
2278
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002279 // ensure that any information already set directly is preserved
2280 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002281
2282 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002283 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002284 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002285 }
2286 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002287 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002288 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002289 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002290
2291 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002292 }
2293
Julia Reynoldsda303542015-11-23 14:00:20 -05002294 /**
2295 * @hide
2296 */
2297 public static void addFieldsFromContext(Context context, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002298 addFieldsFromContext(context.getApplicationInfo(), context.getUserId(), notification);
2299 }
2300
2301 /**
2302 * @hide
2303 */
2304 public static void addFieldsFromContext(ApplicationInfo ai, int userId,
2305 Notification notification) {
2306 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
2307 notification.extras.putInt(EXTRA_ORIGINATING_USERID, userId);
Julia Reynoldsda303542015-11-23 14:00:20 -05002308 }
2309
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002310 @Override
2311 public String toString() {
2312 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002313 sb.append("Notification(pri=");
2314 sb.append(priority);
2315 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002316 if (contentView != null) {
2317 sb.append(contentView.getPackage());
2318 sb.append("/0x");
2319 sb.append(Integer.toHexString(contentView.getLayoutId()));
2320 } else {
2321 sb.append("null");
2322 }
2323 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002324 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2325 sb.append("default");
2326 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002327 int N = this.vibrate.length-1;
2328 sb.append("[");
2329 for (int i=0; i<N; i++) {
2330 sb.append(this.vibrate[i]);
2331 sb.append(',');
2332 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002333 if (N != -1) {
2334 sb.append(this.vibrate[N]);
2335 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002336 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002337 } else {
2338 sb.append("null");
2339 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002340 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002341 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002342 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002343 } else if (this.sound != null) {
2344 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002345 } else {
2346 sb.append("null");
2347 }
Chris Wren365b6d32015-07-16 10:39:26 -04002348 if (this.tickerText != null) {
2349 sb.append(" tick");
2350 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002351 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002352 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002353 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002354 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002355 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002356 if (this.category != null) {
2357 sb.append(" category=");
2358 sb.append(this.category);
2359 }
2360 if (this.mGroupKey != null) {
2361 sb.append(" groupKey=");
2362 sb.append(this.mGroupKey);
2363 }
2364 if (this.mSortKey != null) {
2365 sb.append(" sortKey=");
2366 sb.append(this.mSortKey);
2367 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002368 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002369 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002370 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002371 }
2372 sb.append(" vis=");
2373 sb.append(visibilityToString(this.visibility));
2374 if (this.publicVersion != null) {
2375 sb.append(" publicVersion=");
2376 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002377 }
2378 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002379 return sb.toString();
2380 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002381
Dan Sandler1b718782014-07-18 12:43:45 -04002382 /**
2383 * {@hide}
2384 */
2385 public static String visibilityToString(int vis) {
2386 switch (vis) {
2387 case VISIBILITY_PRIVATE:
2388 return "PRIVATE";
2389 case VISIBILITY_PUBLIC:
2390 return "PUBLIC";
2391 case VISIBILITY_SECRET:
2392 return "SECRET";
2393 default:
2394 return "UNKNOWN(" + String.valueOf(vis) + ")";
2395 }
2396 }
2397
Joe Onoratocb109a02011-01-18 17:57:41 -08002398 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002399 * {@hide}
2400 */
2401 public static String priorityToString(@Priority int pri) {
2402 switch (pri) {
2403 case PRIORITY_MIN:
2404 return "MIN";
2405 case PRIORITY_LOW:
2406 return "LOW";
2407 case PRIORITY_DEFAULT:
2408 return "DEFAULT";
2409 case PRIORITY_HIGH:
2410 return "HIGH";
2411 case PRIORITY_MAX:
2412 return "MAX";
2413 default:
2414 return "UNKNOWN(" + String.valueOf(pri) + ")";
2415 }
2416 }
2417
2418 /**
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002419 * Returns the id of the channel this notification posts to.
2420 */
Julia Reynolds37856052016-11-11 09:20:07 -05002421 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002422 return mChannelId;
2423 }
2424
2425 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002426 * Returns the time at which this notification should be canceled by the system, if it's not
2427 * canceled already.
Julia Reynolds2a128742016-11-28 14:29:25 -05002428 */
2429 public long getTimeout() {
2430 return mTimeout;
2431 }
2432
2433 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002434 * Returns what icon should be shown for this notification if it is being displayed in a
2435 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2436 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2437 */
2438 public int getBadgeIcon() {
2439 return mBadgeIcon;
2440 }
2441
2442 /**
2443 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
2444 */
2445 public String getShortcutId() {
2446 return mShortcutId;
2447 }
2448
2449 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002450 * The small icon representing this notification in the status bar and content view.
2451 *
2452 * @return the small icon representing this notification.
2453 *
2454 * @see Builder#getSmallIcon()
2455 * @see Builder#setSmallIcon(Icon)
2456 */
2457 public Icon getSmallIcon() {
2458 return mSmallIcon;
2459 }
2460
2461 /**
2462 * Used when notifying to clean up legacy small icons.
2463 * @hide
2464 */
2465 public void setSmallIcon(Icon icon) {
2466 mSmallIcon = icon;
2467 }
2468
2469 /**
2470 * The large icon shown in this notification's content view.
2471 * @see Builder#getLargeIcon()
2472 * @see Builder#setLargeIcon(Icon)
2473 */
2474 public Icon getLargeIcon() {
2475 return mLargeIcon;
2476 }
2477
2478 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002479 * @hide
2480 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002481 public boolean isGroupSummary() {
2482 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2483 }
2484
2485 /**
2486 * @hide
2487 */
2488 public boolean isGroupChild() {
2489 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2490 }
2491
2492 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002493 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002494 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002495 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002496 * content views using the platform's notification layout template. If your app supports
2497 * versions of Android as old as API level 4, you can instead use
2498 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2499 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2500 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002501 *
Scott Main183bf112012-08-13 19:12:13 -07002502 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002503 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002504 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002505 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002506 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2507 * .setContentText(subject)
2508 * .setSmallIcon(R.drawable.new_mail)
2509 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002510 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002511 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002512 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002513 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002514 /**
2515 * @hide
2516 */
2517 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2518 "android.rebuild.contentViewActionCount";
2519 /**
2520 * @hide
2521 */
2522 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2523 = "android.rebuild.bigViewActionCount";
2524 /**
2525 * @hide
2526 */
2527 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2528 = "android.rebuild.hudViewActionCount";
2529
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002530 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002531
Selim Cinek6743c0b2017-01-18 18:24:01 -08002532 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
2533 SystemProperties.getBoolean("notifications.only_title", true);
2534
Joe Onorato46439ce2010-11-19 13:56:21 -08002535 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002536 private Notification mN;
2537 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002538 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002539 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2540 private ArrayList<String> mPersonList = new ArrayList<String>();
2541 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08002542 private boolean mIsLegacy;
2543 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002544
2545 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002546 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2547 */
2548 private int mCachedContrastColor = COLOR_INVALID;
2549 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08002550 /**
2551 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
2552 */
2553 private int mCachedAmbientColor = COLOR_INVALID;
2554 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08002555
2556 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08002557 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
2558 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
2559 */
2560 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08002561 private int mTextColorsAreForBackground = COLOR_INVALID;
2562 private int mPrimaryTextColor = COLOR_INVALID;
2563 private int mSecondaryTextColor = COLOR_INVALID;
2564 private int mActionBarColor = COLOR_INVALID;
Adrian Roos70d7aa32017-01-11 15:39:06 -08002565
2566 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002567 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002568 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002569
2570 * <table>
2571 * <tr><th align=right>priority</th>
2572 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2573 * <tr><th align=right>when</th>
2574 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2575 * <tr><th align=right>audio stream</th>
2576 * <td>{@link #STREAM_DEFAULT}</td></tr>
2577 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002578 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002579
2580 * @param context
2581 * A {@link Context} that will be used by the Builder to construct the
2582 * RemoteViews. The Context will not be held past the lifetime of this Builder
2583 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002584 * @param channelId
2585 * The constructed Notification will be posted on this
2586 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
2587 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08002588 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002589 public Builder(Context context, String channelId) {
2590 this(context, (Notification) null);
2591 mN.mChannelId = channelId;
2592 }
2593
2594 /**
2595 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
2596 * instead. All posted Notifications must specify a NotificationChannel Id.
2597 */
2598 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002599 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002600 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002601 }
2602
Joe Onoratocb109a02011-01-18 17:57:41 -08002603 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002604 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002605 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002606 public Builder(Context context, Notification toAdopt) {
2607 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002608
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002609 if (toAdopt == null) {
2610 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002611 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2612 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2613 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002614 mN.priority = PRIORITY_DEFAULT;
2615 mN.visibility = VISIBILITY_PRIVATE;
2616 } else {
2617 mN = toAdopt;
2618 if (mN.actions != null) {
2619 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002620 }
2621
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002622 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2623 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2624 }
2625
Selim Cinek4ac6f602016-06-13 15:47:03 -07002626 if (mN.getSmallIcon() == null && mN.icon != 0) {
2627 setSmallIcon(mN.icon);
2628 }
2629
2630 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
2631 setLargeIcon(mN.largeIcon);
2632 }
2633
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002634 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2635 if (!TextUtils.isEmpty(templateClass)) {
2636 final Class<? extends Style> styleClass
2637 = getNotificationStyleClass(templateClass);
2638 if (styleClass == null) {
2639 Log.d(TAG, "Unknown style class: " + templateClass);
2640 } else {
2641 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002642 final Constructor<? extends Style> ctor =
2643 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002644 ctor.setAccessible(true);
2645 final Style style = ctor.newInstance();
2646 style.restoreFromExtras(mN.extras);
2647
2648 if (style != null) {
2649 setStyle(style);
2650 }
2651 } catch (Throwable t) {
2652 Log.e(TAG, "Could not create Style", t);
2653 }
2654 }
2655 }
2656
2657 }
2658 }
2659
2660 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08002661 if (mColorUtil == null) {
2662 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002663 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002664 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002665 }
2666
2667 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002668 * If this notification is duplicative of a Launcher shortcut, sets the
2669 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
2670 * the shortcut.
2671 *
2672 * This field will be ignored by Launchers that don't support badging or
2673 * {@link android.content.pm.ShortcutManager shortcuts}.
2674 *
2675 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
2676 * supersedes
2677 */
2678 public Builder setShortcutId(String shortcutId) {
2679 mN.mShortcutId = shortcutId;
2680 return this;
2681 }
2682
2683 /**
2684 * Sets which icon to display as a badge for this notification.
2685 *
2686 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
2687 * {@link #BADGE_ICON_LARGE}.
2688 *
2689 * Note: This value might be ignored, for launchers that don't support badge icons.
2690 */
2691 public Builder chooseBadgeIcon(int icon) {
2692 mN.mBadgeIcon = icon;
2693 return this;
2694 }
2695
2696 /**
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002697 * Specifies the channel the notification should be delivered on.
2698 */
2699 public Builder setChannel(String channelId) {
2700 mN.mChannelId = channelId;
2701 return this;
2702 }
2703
2704 /**
Julia Reynolds2a128742016-11-28 14:29:25 -05002705 * Specifies the time at which this notification should be canceled, if it is not already
2706 * canceled.
2707 */
2708 public Builder setTimeout(long when) {
2709 mN.mTimeout = when;
2710 return this;
2711 }
2712
2713 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002714 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002715 *
2716 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2717 * shown anymore by default and must be opted into by using
2718 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002719 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002720 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002721 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002722 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002723 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002724 return this;
2725 }
2726
Joe Onoratocb109a02011-01-18 17:57:41 -08002727 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002728 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002729 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002730 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2731 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002732 */
2733 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002734 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002735 return this;
2736 }
2737
2738 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002739 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002740 *
2741 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002742 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002743 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002744 * Useful when showing an elapsed time (like an ongoing phone call).
2745 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002746 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07002747 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08002748 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002749 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002750 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07002751 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002752 */
2753 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002754 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002755 return this;
2756 }
2757
2758 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002759 * Sets the Chronometer to count down instead of counting up.
2760 *
2761 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2762 * If it isn't set the chronometer will count up.
2763 *
2764 * @see #setUsesChronometer(boolean)
2765 */
Adrian Roos96b7e202016-05-17 13:50:38 -07002766 public Builder setChronometerCountDown(boolean countDown) {
2767 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08002768 return this;
2769 }
2770
2771 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002772 * Set the small icon resource, which will be used to represent the notification in the
2773 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002774 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002775
2776 * The platform template for the expanded view will draw this icon in the left, unless a
2777 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2778 * icon will be moved to the right-hand side.
2779 *
2780
2781 * @param icon
2782 * A resource ID in the application's package of the drawable to use.
2783 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002784 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002785 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002786 return setSmallIcon(icon != 0
2787 ? Icon.createWithResource(mContext, icon)
2788 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002789 }
2790
Joe Onoratocb109a02011-01-18 17:57:41 -08002791 /**
2792 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2793 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2794 * LevelListDrawable}.
2795 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002796 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002797 * @param level The level to use for the icon.
2798 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002799 * @see Notification#icon
2800 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002801 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002802 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002803 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002804 return setSmallIcon(icon);
2805 }
2806
2807 /**
2808 * Set the small icon, which will be used to represent the notification in the
2809 * status bar and content view (unless overriden there by a
2810 * {@link #setLargeIcon(Bitmap) large icon}).
2811 *
2812 * @param icon An Icon object to use.
2813 * @see Notification#icon
2814 */
2815 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002816 mN.setSmallIcon(icon);
2817 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2818 mN.icon = icon.getResId();
2819 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002820 return this;
2821 }
2822
Joe Onoratocb109a02011-01-18 17:57:41 -08002823 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002824 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002825 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002826 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002827 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002828 return this;
2829 }
2830
Joe Onoratocb109a02011-01-18 17:57:41 -08002831 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002832 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002833 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002834 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002835 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002836 return this;
2837 }
2838
Joe Onoratocb109a02011-01-18 17:57:41 -08002839 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002840 * This provides some additional information that is displayed in the notification. No
2841 * guarantees are given where exactly it is displayed.
2842 *
2843 * <p>This information should only be provided if it provides an essential
2844 * benefit to the understanding of the notification. The more text you provide the
2845 * less readable it becomes. For example, an email client should only provide the account
2846 * name here if more than one email account has been added.</p>
2847 *
2848 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
2849 * notification header area.
2850 *
2851 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
2852 * this will be shown in the third line of text in the platform notification template.
2853 * You should not be using {@link #setProgress(int, int, boolean)} at the
2854 * same time on those versions; they occupy the same place.
2855 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002856 */
2857 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002858 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002859 return this;
2860 }
2861
2862 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002863 * Set the remote input history.
2864 *
2865 * This should be set to the most recent inputs that have been sent
2866 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2867 * longer relevant (e.g. for chat notifications once the other party has responded).
2868 *
2869 * The most recent input must be stored at the 0 index, the second most recent at the
2870 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2871 * and how much of each individual input is shown.
2872 *
2873 * <p>Note: The reply text will only be shown on notifications that have least one action
2874 * with a {@code RemoteInput}.</p>
2875 */
2876 public Builder setRemoteInputHistory(CharSequence[] text) {
2877 if (text == null) {
2878 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2879 } else {
2880 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2881 CharSequence[] safe = new CharSequence[N];
2882 for (int i = 0; i < N; i++) {
2883 safe[i] = safeCharSequence(text[i]);
2884 }
2885 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2886 }
2887 return this;
2888 }
2889
2890 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002891 * Sets the number of items this notification represents. May be displayed as a badge count
2892 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08002893 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002894 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002895 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002896 return this;
2897 }
2898
Joe Onoratocb109a02011-01-18 17:57:41 -08002899 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002900 * A small piece of additional information pertaining to this notification.
2901 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002902 * The platform template will draw this on the last line of the notification, at the far
2903 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002904 *
2905 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
2906 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
2907 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08002908 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002909 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002910 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002911 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002912 return this;
2913 }
2914
Joe Onoratocb109a02011-01-18 17:57:41 -08002915 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002916 * Set the progress this notification represents.
2917 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002918 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002919 */
2920 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002921 mN.extras.putInt(EXTRA_PROGRESS, progress);
2922 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
2923 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002924 return this;
2925 }
2926
2927 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002928 * Supply a custom RemoteViews to use instead of the platform template.
2929 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002930 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08002931 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002932 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002933 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002934 return setCustomContentView(views);
2935 }
2936
2937 /**
2938 * Supply custom RemoteViews to use instead of the platform template.
2939 *
2940 * This will override the layout that would otherwise be constructed by this Builder
2941 * object.
2942 */
2943 public Builder setCustomContentView(RemoteViews contentView) {
2944 mN.contentView = contentView;
2945 return this;
2946 }
2947
2948 /**
2949 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
2950 *
2951 * This will override the expanded layout that would otherwise be constructed by this
2952 * Builder object.
2953 */
2954 public Builder setCustomBigContentView(RemoteViews contentView) {
2955 mN.bigContentView = contentView;
2956 return this;
2957 }
2958
2959 /**
2960 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
2961 *
2962 * This will override the heads-up layout that would otherwise be constructed by this
2963 * Builder object.
2964 */
2965 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
2966 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002967 return this;
2968 }
2969
Joe Onoratocb109a02011-01-18 17:57:41 -08002970 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002971 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2972 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002973 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2974 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2975 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002976 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002977 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002978 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002979 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002980 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002981 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002982 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002983 return this;
2984 }
2985
Joe Onoratocb109a02011-01-18 17:57:41 -08002986 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002987 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2988 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002989 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002990 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002991 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002992 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002993 return this;
2994 }
2995
Joe Onoratocb109a02011-01-18 17:57:41 -08002996 /**
2997 * An intent to launch instead of posting the notification to the status bar.
2998 * Only for use with extremely high-priority notifications demanding the user's
2999 * <strong>immediate</strong> attention, such as an incoming phone call or
3000 * alarm clock that the user has explicitly set to a particular time.
3001 * If this facility is used for something else, please give the user an option
3002 * to turn it off and use a normal notification, as this can be extremely
3003 * disruptive.
3004 *
Chris Wren47c20a12014-06-18 17:27:29 -04003005 * <p>
3006 * The system UI may choose to display a heads-up notification, instead of
3007 * launching this intent, while the user is using the device.
3008 * </p>
3009 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003010 * @param intent The pending intent to launch.
3011 * @param highPriority Passing true will cause this notification to be sent
3012 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003013 *
3014 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003015 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003016 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003017 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003018 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3019 return this;
3020 }
3021
Joe Onoratocb109a02011-01-18 17:57:41 -08003022 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003023 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003024 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003025 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003026 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003027 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003028 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003029 return this;
3030 }
3031
Joe Onoratocb109a02011-01-18 17:57:41 -08003032 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003033 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003034 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003035 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003036 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003037 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003038 setTicker(tickerText);
3039 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003040 return this;
3041 }
3042
Joe Onoratocb109a02011-01-18 17:57:41 -08003043 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003044 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003045 *
3046 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003047 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3048 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003049 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003050 public Builder setLargeIcon(Bitmap b) {
3051 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3052 }
3053
3054 /**
3055 * Add a large icon to the notification content view.
3056 *
3057 * In the platform template, this image will be shown on the left of the notification view
3058 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3059 * badge atop the large icon).
3060 */
3061 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003062 mN.mLargeIcon = icon;
3063 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003064 return this;
3065 }
3066
Joe Onoratocb109a02011-01-18 17:57:41 -08003067 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003068 * Set the sound to play.
3069 *
John Spurlockc0650f022014-07-19 13:22:39 -04003070 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3071 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003072 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003073 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003074 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003075 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003076 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003077 mN.sound = sound;
3078 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003079 return this;
3080 }
3081
Joe Onoratocb109a02011-01-18 17:57:41 -08003082 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003083 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003084 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003085 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3086 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003087 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003088 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003089 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003090 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003091 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003092 mN.sound = sound;
3093 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003094 return this;
3095 }
3096
Joe Onoratocb109a02011-01-18 17:57:41 -08003097 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003098 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3099 * use during playback.
3100 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003101 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003102 * @see Notification#sound
3103 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003104 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003105 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003106 mN.sound = sound;
3107 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003108 return this;
3109 }
3110
3111 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003112 * Set the vibration pattern to use.
3113 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003114 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3115 * <code>pattern</code> parameter.
3116 *
Chris Wren47c20a12014-06-18 17:27:29 -04003117 * <p>
3118 * A notification that vibrates is more likely to be presented as a heads-up notification.
3119 * </p>
3120 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003121 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003122 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003123 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003124 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003125 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003126 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003127 return this;
3128 }
3129
Joe Onoratocb109a02011-01-18 17:57:41 -08003130 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003131 * Set the desired color for the indicator LED on the device, as well as the
3132 * blink duty cycle (specified in milliseconds).
3133 *
3134
3135 * Not all devices will honor all (or even any) of these values.
3136 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003137 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003138 * @see Notification#ledARGB
3139 * @see Notification#ledOnMS
3140 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003141 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003142 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003143 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003144 mN.ledARGB = argb;
3145 mN.ledOnMS = onMs;
3146 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003147 if (onMs != 0 || offMs != 0) {
3148 mN.flags |= FLAG_SHOW_LIGHTS;
3149 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003150 return this;
3151 }
3152
Joe Onoratocb109a02011-01-18 17:57:41 -08003153 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003154 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003155 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003156
3157 * Ongoing notifications cannot be dismissed by the user, so your application or service
3158 * must take care of canceling them.
3159 *
3160
3161 * They are typically used to indicate a background task that the user is actively engaged
3162 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3163 * (e.g., a file download, sync operation, active network connection).
3164 *
3165
3166 * @see Notification#FLAG_ONGOING_EVENT
3167 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003168 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003169 public Builder setOngoing(boolean ongoing) {
3170 setFlag(FLAG_ONGOING_EVENT, ongoing);
3171 return this;
3172 }
3173
Joe Onoratocb109a02011-01-18 17:57:41 -08003174 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003175 * Set whether this notification should be colorized. When set, the color set with
3176 * {@link #setColor(int)} will be used as the background color of this notification.
3177 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003178 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3179 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003180 * <p>
3181 * For most styles, the coloring will only be applied if the notification is ongoing.
3182 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
3183 * that have a media session attached there is no requirement for it to be ongoing.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003184 *
3185 * @see Builder#setOngoing(boolean)
3186 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003187 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003188 */
3189 public Builder setColorized(boolean colorize) {
3190 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3191 return this;
3192 }
3193
3194 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003195 * Set this flag if you would only like the sound, vibrate
3196 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003197 *
3198 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003199 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003200 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3201 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3202 return this;
3203 }
3204
Joe Onoratocb109a02011-01-18 17:57:41 -08003205 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003206 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003207 *
3208 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003209 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003210 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003211 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003212 return this;
3213 }
3214
Joe Onoratocb109a02011-01-18 17:57:41 -08003215 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003216 * Set whether or not this notification should not bridge to other devices.
3217 *
3218 * <p>Some notifications can be bridged to other devices for remote display.
3219 * This hint can be set to recommend this notification not be bridged.
3220 */
3221 public Builder setLocalOnly(boolean localOnly) {
3222 setFlag(FLAG_LOCAL_ONLY, localOnly);
3223 return this;
3224 }
3225
3226 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003227 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003228 * <p>
3229 * The value should be one or more of the following fields combined with
3230 * bitwise-or:
3231 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3232 * <p>
3233 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003234 *
3235 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003236 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003237 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003238 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003239 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003240 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003241 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003242 return this;
3243 }
3244
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003245 /**
3246 * Set the priority of this notification.
3247 *
3248 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003249 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003250 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003251 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003252 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003253 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003254 return this;
3255 }
Joe Malin8d40d042012-11-05 11:36:40 -08003256
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003257 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003258 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003259 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003260 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003261 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003262 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003263 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003264 return this;
3265 }
3266
3267 /**
Chris Wrendde75302014-03-26 17:24:15 -04003268 * Add a person that is relevant to this notification.
3269 *
Chris Wrene6c48932014-09-29 17:19:27 -04003270 * <P>
3271 * Depending on user preferences, this annotation may allow the notification to pass
3272 * through interruption filters, and to appear more prominently in the user interface.
3273 * </P>
3274 *
3275 * <P>
3276 * The person should be specified by the {@code String} representation of a
3277 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3278 * </P>
3279 *
3280 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3281 * URIs. The path part of these URIs must exist in the contacts database, in the
3282 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3283 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
3284 * </P>
3285 *
3286 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003287 * @see Notification#EXTRA_PEOPLE
3288 */
Chris Wrene6c48932014-09-29 17:19:27 -04003289 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003290 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04003291 return this;
3292 }
3293
3294 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003295 * Set this notification to be part of a group of notifications sharing the same key.
3296 * Grouped notifications may display in a cluster or stack on devices which
3297 * support such rendering.
3298 *
3299 * <p>To make this notification the summary for its group, also call
3300 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3301 * {@link #setSortKey}.
3302 * @param groupKey The group key of the group.
3303 * @return this object for method chaining
3304 */
3305 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003306 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003307 return this;
3308 }
3309
3310 /**
3311 * Set this notification to be the group summary for a group of notifications.
3312 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003313 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3314 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003315 * @param isGroupSummary Whether this notification should be a group summary.
3316 * @return this object for method chaining
3317 */
3318 public Builder setGroupSummary(boolean isGroupSummary) {
3319 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3320 return this;
3321 }
3322
3323 /**
3324 * Set a sort key that orders this notification among other notifications from the
3325 * same package. This can be useful if an external sort was already applied and an app
3326 * would like to preserve this. Notifications will be sorted lexicographically using this
3327 * value, although providing different priorities in addition to providing sort key may
3328 * cause this value to be ignored.
3329 *
3330 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003331 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003332 *
3333 * @see String#compareTo(String)
3334 */
3335 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003336 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003337 return this;
3338 }
3339
3340 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003341 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003342 *
Griff Hazen720042b2014-02-24 15:46:56 -08003343 * <p>Values within the Bundle will replace existing extras values in this Builder.
3344 *
3345 * @see Notification#extras
3346 */
Griff Hazen959591e2014-05-15 22:26:18 -07003347 public Builder addExtras(Bundle extras) {
3348 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003349 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003350 }
3351 return this;
3352 }
3353
3354 /**
3355 * Set metadata for this notification.
3356 *
3357 * <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 -04003358 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003359 * called.
3360 *
Griff Hazen720042b2014-02-24 15:46:56 -08003361 * <p>Replaces any existing extras values with those from the provided Bundle.
3362 * Use {@link #addExtras} to merge in metadata instead.
3363 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003364 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003365 */
Griff Hazen959591e2014-05-15 22:26:18 -07003366 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003367 if (extras != null) {
3368 mUserExtras = extras;
3369 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003370 return this;
3371 }
3372
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003373 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003374 * Get the current metadata Bundle used by this notification Builder.
3375 *
3376 * <p>The returned Bundle is shared with this Builder.
3377 *
3378 * <p>The current contents of this Bundle are copied into the Notification each time
3379 * {@link #build()} is called.
3380 *
3381 * @see Notification#extras
3382 */
3383 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003384 return mUserExtras;
3385 }
3386
3387 private Bundle getAllExtras() {
3388 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3389 saveExtras.putAll(mN.extras);
3390 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003391 }
3392
3393 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003394 * Add an action to this notification. Actions are typically displayed by
3395 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003396 * <p>
3397 * Every action must have an icon (32dp square and matching the
3398 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3399 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3400 * <p>
3401 * A notification in its expanded form can display up to 3 actions, from left to right in
3402 * the order they were added. Actions will not be displayed when the notification is
3403 * collapsed, however, so be sure that any essential functions may be accessed by the user
3404 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003405 *
3406 * @param icon Resource ID of a drawable that represents the action.
3407 * @param title Text describing the action.
3408 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003409 *
3410 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003411 */
Dan Sandler86647982015-05-13 23:41:13 -04003412 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003413 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003414 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003415 return this;
3416 }
3417
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003418 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003419 * Add an action to this notification. Actions are typically displayed by
3420 * the system as a button adjacent to the notification content.
3421 * <p>
3422 * Every action must have an icon (32dp square and matching the
3423 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3424 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3425 * <p>
3426 * A notification in its expanded form can display up to 3 actions, from left to right in
3427 * the order they were added. Actions will not be displayed when the notification is
3428 * collapsed, however, so be sure that any essential functions may be accessed by the user
3429 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3430 *
3431 * @param action The action to add.
3432 */
3433 public Builder addAction(Action action) {
3434 mActions.add(action);
3435 return this;
3436 }
3437
3438 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003439 * Alter the complete list of actions attached to this notification.
3440 * @see #addAction(Action).
3441 *
3442 * @param actions
3443 * @return
3444 */
3445 public Builder setActions(Action... actions) {
3446 mActions.clear();
3447 for (int i = 0; i < actions.length; i++) {
3448 mActions.add(actions[i]);
3449 }
3450 return this;
3451 }
3452
3453 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003454 * Add a rich notification style to be applied at build time.
3455 *
3456 * @param style Object responsible for modifying the notification style.
3457 */
3458 public Builder setStyle(Style style) {
3459 if (mStyle != style) {
3460 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003461 if (mStyle != null) {
3462 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003463 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3464 } else {
3465 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003466 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003467 }
3468 return this;
3469 }
3470
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003471 /**
3472 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003473 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003474 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
3475 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
3476 *
3477 * @return The same Builder.
3478 */
3479 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003480 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003481 return this;
3482 }
3483
3484 /**
3485 * Supply a replacement Notification whose contents should be shown in insecure contexts
3486 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3487 * @param n A replacement notification, presumably with some or all info redacted.
3488 * @return The same Builder.
3489 */
3490 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003491 if (n != null) {
3492 mN.publicVersion = new Notification();
3493 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3494 } else {
3495 mN.publicVersion = null;
3496 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003497 return this;
3498 }
3499
Griff Hazenb720abe2014-05-20 13:15:30 -07003500 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003501 * Apply an extender to this notification builder. Extenders may be used to add
3502 * metadata or change options on this builder.
3503 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003504 public Builder extend(Extender extender) {
3505 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003506 return this;
3507 }
3508
Dan Sandler4e787062015-06-17 15:09:48 -04003509 /**
3510 * @hide
3511 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003512 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003513 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003514 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003515 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003516 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003517 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003518 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003519 }
3520
Dan Sandler26e81cf2014-05-06 10:01:27 -04003521 /**
3522 * Sets {@link Notification#color}.
3523 *
3524 * @param argb The accent color to use
3525 *
3526 * @return The same Builder.
3527 */
Tor Norbye80756e32015-03-02 09:39:27 -08003528 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003529 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003530 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003531 return this;
3532 }
3533
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003534 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04003535 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
3536 // This user can never be a badged profile,
3537 // and also includes USER_ALL system notifications.
3538 return null;
3539 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02003540 // Note: This assumes that the current user can read the profile badge of the
3541 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003542 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003543 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003544 }
3545
3546 private Bitmap getProfileBadge() {
3547 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003548 if (badge == null) {
3549 return null;
3550 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003551 final int size = mContext.getResources().getDimensionPixelSize(
3552 R.dimen.notification_badge_size);
3553 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003554 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003555 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003556 badge.draw(canvas);
3557 return bitmap;
3558 }
3559
Selim Cinekc848c3a2016-01-13 15:27:30 -08003560 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003561 Bitmap profileBadge = getProfileBadge();
3562
Kenny Guy98193ea2014-07-24 19:54:37 +01003563 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003564 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3565 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003566 if (isColorized()) {
3567 contentView.setDrawableParameters(R.id.profile_badge, false, -1,
3568 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP, -1);
3569 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003570 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003571 }
3572
Christoph Studerfe718432014-09-01 18:21:18 +02003573 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003574 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003575 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003576 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003577 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003578 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003579 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003580 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003581 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003582 contentView.setTextViewText(R.id.text_line_1, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003583 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003584 }
3585
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003586 /**
3587 * Resets the notification header to its original state
3588 */
3589 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07003590 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
3591 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08003592 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003593 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003594 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003595 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07003596 contentView.setTextViewText(R.id.header_text, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003597 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003598 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003599 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003600 contentView.setImageViewIcon(R.id.profile_badge, null);
3601 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003602 }
3603
3604 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003605 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
3606 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003607 }
3608
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003609 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003610 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003611 }
3612
3613 /**
3614 * @param hasProgress whether the progress bar should be shown and set
3615 */
3616 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003617 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
3618 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07003619 }
3620
Adrian Roos70d7aa32017-01-11 15:39:06 -08003621 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01003622 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003623
Christoph Studerfe718432014-09-01 18:21:18 +02003624 resetStandardTemplate(contentView);
3625
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003626 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003627 updateBackgroundColor(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08003628 bindNotificationHeader(contentView, p.ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003629 bindLargeIcon(contentView);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003630 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
3631 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003632 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003633 contentView.setTextViewText(R.id.title, p.title);
Adrian Roos72171622017-01-27 10:32:06 -08003634 if (!p.ambient) {
3635 setTextViewColorPrimary(contentView, R.id.title);
3636 }
Selim Cinek954cc232016-05-20 13:29:23 -07003637 contentView.setViewLayoutWidth(R.id.title, showProgress
3638 ? ViewGroup.LayoutParams.WRAP_CONTENT
3639 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08003640 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08003641 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003642 int textId = showProgress ? com.android.internal.R.id.text_line_1
3643 : com.android.internal.R.id.text;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003644 contentView.setTextViewText(textId, p.text);
Adrian Roos72171622017-01-27 10:32:06 -08003645 if (!p.ambient) {
3646 setTextViewColorSecondary(contentView, textId);
3647 }
Selim Cinek41598732016-01-11 16:58:37 -08003648 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003649 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003650
Selim Cinek279fa862016-06-14 10:57:25 -07003651 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003652
Selim Cinek29603462015-11-17 19:04:39 -08003653 return contentView;
3654 }
3655
Selim Cinek7b9605b2017-01-19 17:36:00 -08003656 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
3657 ensureColors();
3658 contentView.setTextColor(id, mPrimaryTextColor);
3659 }
3660
3661 private int getPrimaryTextColor() {
3662 ensureColors();
3663 return mPrimaryTextColor;
3664 }
3665
3666 private int getActionBarColor() {
3667 ensureColors();
3668 return mActionBarColor;
3669 }
3670
3671 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
3672 ensureColors();
3673 contentView.setTextColor(id, mSecondaryTextColor);
3674 }
3675
3676 private void ensureColors() {
3677 int backgroundColor = getBackgroundColor();
3678 if (mPrimaryTextColor == COLOR_INVALID
3679 || mSecondaryTextColor == COLOR_INVALID
3680 || mActionBarColor == COLOR_INVALID
3681 || mTextColorsAreForBackground != backgroundColor) {
3682 mTextColorsAreForBackground = backgroundColor;
3683 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(
3684 mContext, backgroundColor);
3685 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(
3686 mContext, backgroundColor);
Selim Cinek875ba9b2017-02-13 16:20:17 -08003687 mActionBarColor = NotificationColorUtil.resolveActionBarColor(mContext,
3688 backgroundColor);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003689 }
3690 }
3691
3692 private void updateBackgroundColor(RemoteViews contentView) {
3693 if (isColorized()) {
3694 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
3695 getBackgroundColor());
3696 } else {
3697 // Clear it!
3698 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
3699 0);
3700 }
3701 }
3702
Selim Cinek860b6da2015-12-16 19:02:19 -08003703 /**
3704 * @param remoteView the remote view to update the minheight in
3705 * @param hasMinHeight does it have a mimHeight
3706 * @hide
3707 */
3708 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3709 int minHeight = 0;
3710 if (hasMinHeight) {
3711 // we need to set the minHeight of the notification
3712 minHeight = mContext.getResources().getDimensionPixelSize(
3713 com.android.internal.R.dimen.notification_min_content_height);
3714 }
3715 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3716 }
3717
Selim Cinek29603462015-11-17 19:04:39 -08003718 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003719 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3720 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3721 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3722 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003723 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003724 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003725 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003726 contentView.setProgressBackgroundTintList(
3727 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3728 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003729 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003730 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003731 contentView.setProgressTintList(R.id.progress, colorStateList);
3732 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003733 }
Selim Cinek29603462015-11-17 19:04:39 -08003734 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003735 } else {
3736 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003737 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003738 }
Joe Onorato561d3852010-11-20 18:09:34 -08003739 }
3740
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003741 private void bindLargeIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07003742 if (mN.mLargeIcon == null && mN.largeIcon != null) {
3743 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
3744 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003745 if (mN.mLargeIcon != null) {
3746 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3747 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3748 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003749 int endMargin = R.dimen.notification_content_picture_margin;
3750 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
3751 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
3752 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003753 }
3754 }
3755
Adrian Roos487374f2017-01-11 15:48:14 -08003756 private void bindNotificationHeader(RemoteViews contentView, boolean ambient) {
3757 bindSmallIcon(contentView, ambient);
3758 bindHeaderAppName(contentView, ambient);
3759 if (!ambient) {
3760 // Ambient view does not have these
3761 bindHeaderText(contentView);
3762 bindHeaderChronometerAndTime(contentView);
3763 bindExpandButton(contentView);
3764 bindProfileBadge(contentView);
3765 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003766 }
3767
3768 private void bindExpandButton(RemoteViews contentView) {
Selim Cinek99104832017-01-25 14:47:33 -08003769 int color = getPrimaryHighlightColor();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003770 contentView.setDrawableParameters(R.id.expand_button, false, -1, color,
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003771 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003772 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08003773 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003774 }
3775
Selim Cinek99104832017-01-25 14:47:33 -08003776 /**
3777 * @return the color that is used as the first primary highlight color. This is applied
3778 * in several places like the action buttons or the app name in the header.
3779 */
3780 private int getPrimaryHighlightColor() {
3781 return isColorized() ? getPrimaryTextColor() : resolveContrastColor();
3782 }
3783
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003784 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3785 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003786 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003787 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003788 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3789 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3790 contentView.setLong(R.id.chronometer, "setBase",
3791 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3792 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07003793 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07003794 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003795 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003796 } else {
3797 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3798 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003799 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003800 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003801 } else {
3802 // We still want a time to be set but gone, such that we can show and hide it
3803 // on demand in case it's a child notification without anything in the header
3804 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003805 }
3806 }
3807
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003808 private void bindHeaderText(RemoteViews contentView) {
3809 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3810 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05003811 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003812 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05003813 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003814 if (headerText == null
3815 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3816 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3817 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
3818 }
3819 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003820 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003821 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08003822 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003823 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
3824 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003825 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003826 }
3827 }
3828
Adrian Rooseba05822016-04-22 17:09:27 -07003829 /**
3830 * @hide
3831 */
3832 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04003833 CharSequence name = null;
3834 final PackageManager pm = mContext.getPackageManager();
3835 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
3836 // only system packages which lump together a bunch of unrelated stuff
3837 // may substitute a different name to make the purpose of the
3838 // notification more clear. the correct package label should always
3839 // be accessible via SystemUI.
3840 final String pkg = mContext.getPackageName();
3841 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
3842 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
3843 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
3844 name = subName;
3845 } else {
3846 Log.w(TAG, "warning: pkg "
3847 + pkg + " attempting to substitute app name '" + subName
3848 + "' without holding perm "
3849 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
3850 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003851 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04003852 if (TextUtils.isEmpty(name)) {
3853 name = pm.getApplicationLabel(mContext.getApplicationInfo());
3854 }
3855 if (TextUtils.isEmpty(name)) {
3856 // still nothing?
3857 return null;
3858 }
3859
3860 return String.valueOf(name);
3861 }
Adrian Roos487374f2017-01-11 15:48:14 -08003862 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04003863 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08003864 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08003865 setTextViewColorPrimary(contentView, R.id.app_name_text);
3866 } else {
3867 contentView.setTextColor(R.id.app_name_text,
3868 ambient ? resolveAmbientColor() : resolveContrastColor());
3869 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003870 }
3871
Adrian Roos487374f2017-01-11 15:48:14 -08003872 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07003873 if (mN.mSmallIcon == null && mN.icon != 0) {
3874 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
3875 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003876 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Adrian Roos9b45a152016-06-28 13:32:29 -07003877 contentView.setDrawableParameters(R.id.icon, false /* targetBackground */,
3878 -1 /* alpha */, -1 /* colorFilter */, null /* mode */, mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08003879 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003880 }
3881
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003882 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003883 * @return true if the built notification will show the time or the chronometer; false
3884 * otherwise
3885 */
3886 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07003887 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003888 }
3889
Christoph Studerfe718432014-09-01 18:21:18 +02003890 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003891 // actions_container is only reset when there are no actions to avoid focus issues with
3892 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02003893 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003894 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003895
3896 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3897 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3898
3899 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3900 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3901 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3902 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07003903
3904 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003905 }
3906
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003907 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003908 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07003909 }
3910
Adrian Roos70d7aa32017-01-11 15:39:06 -08003911 private RemoteViews applyStandardTemplateWithActions(int layoutId,
3912 StandardTemplateParams p) {
3913 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003914
Christoph Studerfe718432014-09-01 18:21:18 +02003915 resetStandardTemplateWithActions(big);
3916
Adrian Roose458aa82015-12-08 16:17:19 -08003917 boolean validRemoteInput = false;
3918
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003919 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08003920 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003921 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003922 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08003923 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003924 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roos487374f2017-01-11 15:48:14 -08003925 if (p.ambient) {
3926 big.setInt(R.id.actions, "setBackgroundColor", Color.TRANSPARENT);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003927 } else if (isColorized()) {
3928 big.setInt(R.id.actions, "setBackgroundColor", getActionBarColor());
3929 } else {
3930 big.setInt(R.id.actions, "setBackgroundColor", mContext.getColor(
3931 R.color.notification_action_list));
Adrian Roos487374f2017-01-11 15:48:14 -08003932 }
Adrian Roosf852a422016-06-03 13:33:43 -07003933 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
3934 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003935 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003936 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003937 Action action = mActions.get(i);
3938 validRemoteInput |= hasValidRemoteInput(action);
3939
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003940 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08003941 i % 2 != 0, p.ambient);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003942 big.addView(R.id.actions, button);
3943 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003944 } else {
3945 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003946 }
Adrian Roose458aa82015-12-08 16:17:19 -08003947
3948 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08003949 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08003950 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3951 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3952 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003953 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08003954
3955 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3956 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3957 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003958 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08003959
3960 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3961 big.setViewVisibility(
3962 R.id.notification_material_reply_text_3, View.VISIBLE);
3963 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003964 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08003965 }
3966 }
3967 }
3968
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003969 return big;
3970 }
3971
Adrian Roose458aa82015-12-08 16:17:19 -08003972 private boolean hasValidRemoteInput(Action action) {
3973 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
3974 // Weird actions
3975 return false;
3976 }
3977
3978 RemoteInput[] remoteInputs = action.getRemoteInputs();
3979 if (remoteInputs == null) {
3980 return false;
3981 }
3982
3983 for (RemoteInput r : remoteInputs) {
3984 CharSequence[] choices = r.getChoices();
3985 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
3986 return true;
3987 }
3988 }
3989 return false;
3990 }
3991
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003992 /**
3993 * Construct a RemoteViews for the final 1U notification layout. In order:
3994 * 1. Custom contentView from the caller
3995 * 2. Style's proposed content view
3996 * 3. Standard template view
3997 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003998 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08003999 return createContentView(false /* increasedheight */ );
4000 }
4001
4002 /**
4003 * Construct a RemoteViews for the smaller content view.
4004 *
4005 * @param increasedHeight true if this layout be created with an increased height. Some
4006 * styles may support showing more then just that basic 1U size
4007 * and the system may decide to render important notifications
4008 * slightly bigger even when collapsed.
4009 *
4010 * @hide
4011 */
4012 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08004013 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004014 return mN.contentView;
4015 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004016 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004017 if (styleView != null) {
4018 return styleView;
4019 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004020 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004021 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004022 }
4023
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004024 /**
4025 * Construct a RemoteViews for the final big notification layout.
4026 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004027 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004028 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08004029 if (mN.bigContentView != null
4030 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004031 return mN.bigContentView;
4032 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004033 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004034 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004035 } else if (mActions.size() != 0) {
4036 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004037 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004038 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004039 return result;
4040 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004041
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004042 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004043 * Construct a RemoteViews for the final notification header only. This will not be
4044 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004045 *
4046 * @hide
4047 */
4048 public RemoteViews makeNotificationHeader() {
Selim Cinek414ad332017-02-24 19:06:12 -08004049 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4050 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004051 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
4052 R.layout.notification_template_header);
4053 resetNotificationHeader(header);
Adrian Roos487374f2017-01-11 15:48:14 -08004054 bindNotificationHeader(header, false /* ambient */);
Selim Cinek414ad332017-02-24 19:06:12 -08004055 if (colorized != null) {
4056 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4057 } else {
4058 mN.extras.remove(EXTRA_COLORIZED);
4059 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004060 return header;
4061 }
4062
Adrian Roos487374f2017-01-11 15:48:14 -08004063 /**
4064 * Construct a RemoteViews for the ambient version of the notification.
4065 *
4066 * @hide
4067 */
4068 public RemoteViews makeAmbientNotification() {
4069 RemoteViews ambient = applyStandardTemplateWithActions(
4070 R.layout.notification_template_material_ambient,
4071 mParams.reset().fillTextsFrom(this).hasProgress(false).ambient(true));
4072 return ambient;
4073 }
4074
Selim Cinek29603462015-11-17 19:04:39 -08004075 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004076 if (result != null) {
4077 result.setViewVisibility(R.id.text_line_1, View.GONE);
4078 }
Selim Cinek29603462015-11-17 19:04:39 -08004079 }
4080
Selim Cinek6743c0b2017-01-18 18:24:01 -08004081 /**
4082 * Adapt the Notification header if this view is used as an expanded view.
4083 *
4084 * @hide
4085 */
4086 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004087 if (result != null) {
4088 result.setBoolean(R.id.notification_header, "setExpanded", true);
4089 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004090 }
4091
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004092 /**
4093 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004094 *
4095 * @param increasedHeight true if this layout be created with an increased height. Some
4096 * styles may support showing more then just that basic 1U size
4097 * and the system may decide to render important notifications
4098 * slightly bigger even when collapsed.
4099 *
4100 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004101 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004102 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08004103 if (mN.headsUpContentView != null
4104 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004105 return mN.headsUpContentView;
4106 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004107 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4108 if (styleView != null) {
4109 return styleView;
4110 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004111 } else if (mActions.size() == 0) {
4112 return null;
4113 }
4114
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004115 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004116 }
4117
Selim Cinek624c02db2015-12-14 21:00:02 -08004118 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08004119 * Construct a RemoteViews for the final heads-up notification layout.
4120 */
4121 public RemoteViews createHeadsUpContentView() {
4122 return createHeadsUpContentView(false /* useIncreasedHeight */);
4123 }
4124
4125 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08004126 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4127 *
4128 * @hide
4129 */
4130 public RemoteViews makePublicContentView() {
4131 if (mN.publicVersion != null) {
4132 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Julia Reynolds3b848122016-02-26 10:45:32 -05004133 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004134 }
4135 Bundle savedBundle = mN.extras;
4136 Style style = mStyle;
4137 mStyle = null;
4138 Icon largeIcon = mN.mLargeIcon;
4139 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004140 Bitmap largeIconLegacy = mN.largeIcon;
4141 mN.largeIcon = null;
Selim Cinek624c02db2015-12-14 21:00:02 -08004142 Bundle publicExtras = new Bundle();
4143 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
4144 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
4145 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
4146 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07004147 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
4148 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08004149 publicExtras.putCharSequence(EXTRA_TITLE,
4150 mContext.getString(R.string.notification_hidden_text));
4151 mN.extras = publicExtras;
4152 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
4153 mN.extras = savedBundle;
4154 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07004155 mN.largeIcon = largeIconLegacy;
Selim Cinek624c02db2015-12-14 21:00:02 -08004156 mStyle = style;
4157 return publicView;
4158 }
4159
Selim Cinek6743c0b2017-01-18 18:24:01 -08004160 /**
4161 * Construct a content view for the display when low - priority
4162 *
4163 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
4164 * a new subtext is created consisting of the content of the
4165 * notification.
4166 * @hide
4167 */
4168 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
4169 int color = mN.color;
4170 mN.color = COLOR_DEFAULT;
4171 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4172 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
4173 CharSequence newSummary = createSummaryText();
4174 if (!TextUtils.isEmpty(newSummary)) {
4175 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
4176 }
4177 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004178
Selim Cinek6743c0b2017-01-18 18:24:01 -08004179 RemoteViews header = makeNotificationHeader();
Selim Cinek1b554392017-02-28 17:22:49 -08004180 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08004181 if (summary != null) {
4182 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
4183 } else {
4184 mN.extras.remove(EXTRA_SUB_TEXT);
4185 }
4186 mN.color = color;
4187 return header;
4188 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004189
Selim Cinek6743c0b2017-01-18 18:24:01 -08004190 private CharSequence createSummaryText() {
4191 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
4192 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
4193 return titleText;
4194 }
4195 SpannableStringBuilder summary = new SpannableStringBuilder();
4196 if (titleText == null) {
4197 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
4198 }
4199 BidiFormatter bidi = BidiFormatter.getInstance();
4200 if (titleText != null) {
4201 summary.append(bidi.unicodeWrap(titleText));
4202 }
4203 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
4204 if (titleText != null && contentText != null) {
4205 summary.append(bidi.unicodeWrap(mContext.getText(
4206 R.string.notification_header_divider_symbol_with_spaces)));
4207 }
4208 if (contentText != null) {
4209 summary.append(bidi.unicodeWrap(contentText));
4210 }
4211 return summary;
4212 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05004213
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004214 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004215 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04004216 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004217 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004218 emphazisedMode ? getEmphasizedActionLayoutResource()
4219 : tombstone ? getActionTombstoneLayoutResource()
4220 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04004221 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04004222 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04004223 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004224 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004225 if (action.mRemoteInputs != null) {
4226 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
4227 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004228 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004229 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07004230 // change the background bgColor
4231 int bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
4232 : R.color.notification_action_list_dark);
4233 button.setDrawableParameters(R.id.button_holder, true, -1, bgColor,
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004234 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinek981962e2016-07-20 20:41:58 -07004235 CharSequence title = action.title;
4236 ColorStateList[] outResultColor = null;
4237 if (isLegacy()) {
4238 title = clearColorSpans(title);
4239 } else {
4240 outResultColor = new ColorStateList[1];
4241 title = ensureColorSpanContrast(title, bgColor, outResultColor);
4242 }
4243 button.setTextViewText(R.id.action0, title);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004244 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07004245 if (outResultColor != null && outResultColor[0] != null) {
4246 // We need to set the text color as well since changing a text to uppercase
4247 // clears its spans.
4248 button.setTextColor(R.id.action0, outResultColor[0]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004249 } else if (mN.color != COLOR_DEFAULT && !isColorized()) {
Selim Cinek981962e2016-07-20 20:41:58 -07004250 button.setTextColor(R.id.action0,resolveContrastColor());
4251 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004252 } else {
Selim Cinek981962e2016-07-20 20:41:58 -07004253 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Adrian Roos72171622017-01-27 10:32:06 -08004254 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004255 setTextViewColorPrimary(button, R.id.action0);
4256 } else if (mN.color != COLOR_DEFAULT) {
Adrian Roos487374f2017-01-11 15:48:14 -08004257 button.setTextColor(R.id.action0,
4258 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004259 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004260 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004261 return button;
4262 }
4263
Joe Onoratocb109a02011-01-18 17:57:41 -08004264 /**
Selim Cinek981962e2016-07-20 20:41:58 -07004265 * Clears all color spans of a text
4266 * @param charSequence the input text
4267 * @return the same text but without color spans
4268 */
4269 private CharSequence clearColorSpans(CharSequence charSequence) {
4270 if (charSequence instanceof Spanned) {
4271 Spanned ss = (Spanned) charSequence;
4272 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4273 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4274 for (Object span : spans) {
4275 Object resultSpan = span;
4276 if (resultSpan instanceof CharacterStyle) {
4277 resultSpan = ((CharacterStyle) span).getUnderlying();
4278 }
4279 if (resultSpan instanceof TextAppearanceSpan) {
4280 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4281 if (originalSpan.getTextColor() != null) {
4282 resultSpan = new TextAppearanceSpan(
4283 originalSpan.getFamily(),
4284 originalSpan.getTextStyle(),
4285 originalSpan.getTextSize(),
4286 null,
4287 originalSpan.getLinkTextColor());
4288 }
4289 } else if (resultSpan instanceof ForegroundColorSpan
4290 || (resultSpan instanceof BackgroundColorSpan)) {
4291 continue;
4292 } else {
4293 resultSpan = span;
4294 }
4295 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
4296 ss.getSpanFlags(span));
4297 }
4298 return builder;
4299 }
4300 return charSequence;
4301 }
4302
4303 /**
4304 * Ensures contrast on color spans against a background color. also returns the color of the
4305 * text if a span was found that spans over the whole text.
4306 *
4307 * @param charSequence the charSequence on which the spans are
4308 * @param background the background color to ensure the contrast against
4309 * @param outResultColor an array in which a color will be returned as the first element if
4310 * there exists a full length color span.
4311 * @return the contrasted charSequence
4312 */
4313 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
4314 ColorStateList[] outResultColor) {
4315 if (charSequence instanceof Spanned) {
4316 Spanned ss = (Spanned) charSequence;
4317 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4318 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4319 for (Object span : spans) {
4320 Object resultSpan = span;
4321 int spanStart = ss.getSpanStart(span);
4322 int spanEnd = ss.getSpanEnd(span);
4323 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
4324 if (resultSpan instanceof CharacterStyle) {
4325 resultSpan = ((CharacterStyle) span).getUnderlying();
4326 }
4327 if (resultSpan instanceof TextAppearanceSpan) {
4328 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4329 ColorStateList textColor = originalSpan.getTextColor();
4330 if (textColor != null) {
4331 int[] colors = textColor.getColors();
4332 int[] newColors = new int[colors.length];
4333 for (int i = 0; i < newColors.length; i++) {
4334 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
4335 colors[i], background);
4336 }
4337 textColor = new ColorStateList(textColor.getStates().clone(),
4338 newColors);
4339 resultSpan = new TextAppearanceSpan(
4340 originalSpan.getFamily(),
4341 originalSpan.getTextStyle(),
4342 originalSpan.getTextSize(),
4343 textColor,
4344 originalSpan.getLinkTextColor());
4345 if (fullLength) {
4346 outResultColor[0] = new ColorStateList(
4347 textColor.getStates().clone(), newColors);
4348 }
4349 }
4350 } else if (resultSpan instanceof ForegroundColorSpan) {
4351 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
4352 int foregroundColor = originalSpan.getForegroundColor();
4353 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
4354 foregroundColor, background);
4355 resultSpan = new ForegroundColorSpan(foregroundColor);
4356 if (fullLength) {
4357 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
4358 }
4359 } else {
4360 resultSpan = span;
4361 }
4362
4363 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
4364 }
4365 return builder;
4366 }
4367 return charSequence;
4368 }
4369
4370 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004371 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07004372 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004373 */
4374 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004375 if (!mIsLegacyInitialized) {
4376 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
4377 < Build.VERSION_CODES.LOLLIPOP;
4378 mIsLegacyInitialized = true;
4379 }
4380 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004381 }
4382
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004383 private CharSequence processLegacyText(CharSequence charSequence) {
Selim Cinek99104832017-01-25 14:47:33 -08004384 if (isLegacy() || textColorsNeedInversion()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004385 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004386 } else {
4387 return charSequence;
4388 }
4389 }
4390
Dan Sandler26e81cf2014-05-06 10:01:27 -04004391 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004392 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04004393 */
Adrian Roos487374f2017-01-11 15:48:14 -08004394 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
4395 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08004396 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinek99104832017-01-25 14:47:33 -08004397 int color = ambient ? resolveAmbientColor() : getPrimaryHighlightColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08004398 if (colorable) {
Adrian Roos487374f2017-01-11 15:48:14 -08004399 contentView.setDrawableParameters(R.id.icon, false, -1, color,
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004400 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08004401
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004402 }
Selim Cinekea4bef72015-12-02 15:51:10 -08004403 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08004404 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004405 }
4406
Dan Sandler26e81cf2014-05-06 10:01:27 -04004407 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004408 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04004409 * if it's grayscale).
4410 */
4411 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04004412 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
4413 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004414 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004415 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08004416 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04004417 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004418 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004419 }
4420
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004421 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004422 if (mN.color != COLOR_DEFAULT) {
4423 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02004424 }
Jorim Jaggi74419312014-06-10 20:57:21 +02004425 }
4426
Adrian Roos4ff3b122016-02-01 12:26:13 -08004427 int resolveContrastColor() {
4428 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
4429 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004430 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004431 final int contrasted = NotificationColorUtil.resolveContrastColor(mContext, mN.color);
4432
4433 mCachedContrastColorIsFor = mN.color;
4434 return mCachedContrastColor = contrasted;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004435 }
4436
Adrian Roos487374f2017-01-11 15:48:14 -08004437 int resolveAmbientColor() {
4438 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
4439 return mCachedAmbientColor;
4440 }
4441 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
4442
4443 mCachedAmbientColorIsFor = mN.color;
4444 return mCachedAmbientColor = contrasted;
4445 }
4446
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004447 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004448 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004449 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08004450 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004451 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004452 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004453 mN.actions = new Action[mActions.size()];
4454 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004455 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004456 if (!mPersonList.isEmpty()) {
4457 mN.extras.putStringArray(EXTRA_PEOPLE,
4458 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004459 }
Selim Cinek247fa012016-02-18 09:50:48 -08004460 if (mN.bigContentView != null || mN.contentView != null
4461 || mN.headsUpContentView != null) {
4462 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
4463 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004464 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08004465 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004466
Julia Reynolds3b848122016-02-26 10:45:32 -05004467 /**
4468 * Creates a Builder from an existing notification so further changes can be made.
4469 * @param context The context for your application / activity.
4470 * @param n The notification to create a Builder from.
4471 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004472 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004473 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004474 ApplicationInfo applicationInfo = n.extras.getParcelable(
4475 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004476 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05004477 if (applicationInfo != null) {
4478 try {
4479 builderContext = context.createApplicationContext(applicationInfo,
4480 Context.CONTEXT_RESTRICTED);
4481 } catch (NameNotFoundException e) {
4482 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
4483 builderContext = context; // try with our context
4484 }
4485 } else {
4486 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02004487 }
4488
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004489 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004490 }
4491
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004492 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004493 * @deprecated Use {@link #build()} instead.
4494 */
4495 @Deprecated
4496 public Notification getNotification() {
4497 return build();
4498 }
4499
4500 /**
4501 * Combine all of the options that have been set and return a new {@link Notification}
4502 * object.
4503 */
4504 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004505 // first, add any extras from the calling code
4506 if (mUserExtras != null) {
4507 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004508 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004509
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004510 mN.creationTime = System.currentTimeMillis();
4511
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004512 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05004513 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02004514
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004515 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004516
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004517 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004518 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004519 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004520
Adrian Roos5081c0d2016-02-26 16:04:19 -08004521 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4522 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004523 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004524 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004525 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
4526 mN.contentView.getSequenceNumber());
4527 }
4528 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004529 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004530 if (mN.bigContentView != null) {
4531 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
4532 mN.bigContentView.getSequenceNumber());
4533 }
4534 }
4535 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004536 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004537 if (mN.headsUpContentView != null) {
4538 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
4539 mN.headsUpContentView.getSequenceNumber());
4540 }
4541 }
4542 }
4543
Julia Reynolds4c0c2022016-02-02 15:11:59 -05004544 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
4545 mN.flags |= FLAG_SHOW_LIGHTS;
4546 }
4547
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004548 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004549 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004550
4551 /**
4552 * Apply this Builder to an existing {@link Notification} object.
4553 *
4554 * @hide
4555 */
4556 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04004557 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004558 return n;
4559 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004560
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004561 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08004562 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
4563 * change.
4564 *
4565 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
4566 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004567 * @hide
4568 */
Adrian Roos184bfe022016-03-03 13:41:44 -08004569 public static Notification maybeCloneStrippedForDelivery(Notification n) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004570 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08004571
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004572 // Only strip views for known Styles because we won't know how to
4573 // re-create them otherwise.
Adrian Roos184bfe022016-03-03 13:41:44 -08004574 if (!TextUtils.isEmpty(templateClass)
4575 && getNotificationStyleClass(templateClass) == null) {
4576 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004577 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004578
4579 // Only strip unmodified BuilderRemoteViews.
4580 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004581 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004582 n.contentView.getSequenceNumber();
4583 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004584 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004585 n.bigContentView.getSequenceNumber();
4586 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004587 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004588 n.headsUpContentView.getSequenceNumber();
4589
4590 // Nothing to do here, no need to clone.
4591 if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
4592 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004593 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004594
4595 Notification clone = n.clone();
4596 if (stripContentView) {
4597 clone.contentView = null;
4598 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
4599 }
4600 if (stripBigContentView) {
4601 clone.bigContentView = null;
4602 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
4603 }
4604 if (stripHeadsUpContentView) {
4605 clone.headsUpContentView = null;
4606 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
4607 }
4608 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004609 }
4610
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004611 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004612 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004613 }
4614
4615 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004616 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004617 }
4618
4619 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004620 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004621 }
4622
4623 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004624 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004625 }
4626
4627 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004628 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004629 }
4630
Adrian Roosc1a80b02016-04-05 14:54:55 -07004631 private int getMessagingLayoutResource() {
4632 return R.layout.notification_template_material_messaging;
4633 }
4634
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004635 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004636 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004637 }
4638
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004639 private int getEmphasizedActionLayoutResource() {
4640 return R.layout.notification_material_action_emphasized;
4641 }
4642
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004643 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004644 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004645 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004646
4647 private int getBackgroundColor() {
4648 if (isColorized()) {
4649 return mN.color;
4650 } else {
4651 return COLOR_DEFAULT;
4652 }
4653 }
4654
4655 private boolean isColorized() {
4656 return mN.isColorized();
4657 }
Selim Cinek99104832017-01-25 14:47:33 -08004658
4659 private boolean textColorsNeedInversion() {
4660 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
4661 return false;
4662 }
4663 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
4664 return targetSdkVersion > Build.VERSION_CODES.M
4665 && targetSdkVersion < Build.VERSION_CODES.O;
4666 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004667 }
4668
4669 /**
4670 * @return whether this notification is ongoing and can't be dismissed by the user.
4671 */
4672 private boolean isOngoing() {
4673 final int ongoingFlags = Notification.FLAG_FOREGROUND_SERVICE
4674 | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
4675 return (flags & ongoingFlags) != 0;
4676 }
4677
4678 /**
Selim Cinek99104832017-01-25 14:47:33 -08004679 * @return whether this notification has a media session attached
4680 * @hide
4681 */
4682 public boolean hasMediaSession() {
4683 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
4684 }
4685
4686 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08004687 * @return the style class of this notification
4688 * @hide
4689 */
4690 public Class<? extends Notification.Style> getNotificationStyle() {
4691 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
4692
4693 if (!TextUtils.isEmpty(templateClass)) {
4694 return Notification.getNotificationStyleClass(templateClass);
4695 }
4696 return null;
4697 }
4698
4699 /**
Selim Cinek99104832017-01-25 14:47:33 -08004700 * @return true if this notification is colorized. This also factors in whether the
Selim Cinek7b9605b2017-01-19 17:36:00 -08004701 * notification is ongoing.
4702 *
4703 * @hide
4704 */
4705 public boolean isColorized() {
Selim Cinek99104832017-01-25 14:47:33 -08004706 Class<? extends Style> style = getNotificationStyle();
4707 if (MediaStyle.class.equals(style)) {
4708 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
4709 if ((colorized == null || colorized) && hasMediaSession()) {
4710 return true;
4711 }
4712 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
4713 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
4714 return true;
4715 }
4716 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004717 return extras.getBoolean(EXTRA_COLORIZED) && isOngoing();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004718 }
4719
Selim Cinek279fa862016-06-14 10:57:25 -07004720 private boolean hasLargeIcon() {
4721 return mLargeIcon != null || largeIcon != null;
4722 }
4723
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004724 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004725 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004726 * @hide
4727 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07004728 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004729 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
4730 }
4731
4732 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004733 * @return true if the notification will show a chronometer; false otherwise
4734 * @hide
4735 */
4736 public boolean showsChronometer() {
4737 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
4738 }
4739
4740 /**
Julia Reynolds4a02afb2016-12-13 13:39:52 -05004741 * @hide
4742 */
4743 @SystemApi
4744 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
4745 Class<? extends Style>[] classes = new Class[] {
4746 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
4747 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
4748 MessagingStyle.class };
4749 for (Class<? extends Style> innerClass : classes) {
4750 if (templateClass.equals(innerClass.getName())) {
4751 return innerClass;
4752 }
4753 }
4754 return null;
4755 }
4756
4757 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004758 * An object that can apply a rich notification style to a {@link Notification.Builder}
4759 * object.
4760 */
Griff Hazendfcb0802014-02-11 12:00:00 -08004761 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04004762 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004763
4764 /**
4765 * @hide
4766 */
4767 protected CharSequence mSummaryText = null;
4768
4769 /**
4770 * @hide
4771 */
4772 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04004773
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004774 protected Builder mBuilder;
4775
Chris Wrend6297db2012-05-03 16:20:13 -04004776 /**
4777 * Overrides ContentTitle in the big form of the template.
4778 * This defaults to the value passed to setContentTitle().
4779 */
4780 protected void internalSetBigContentTitle(CharSequence title) {
4781 mBigContentTitle = title;
4782 }
4783
4784 /**
4785 * Set the first line of text after the detail section in the big form of the template.
4786 */
4787 protected void internalSetSummaryText(CharSequence cs) {
4788 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04004789 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04004790 }
4791
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004792 public void setBuilder(Builder builder) {
4793 if (mBuilder != builder) {
4794 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004795 if (mBuilder != null) {
4796 mBuilder.setStyle(this);
4797 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004798 }
4799 }
4800
Chris Wrend6297db2012-05-03 16:20:13 -04004801 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004802 if (mBuilder == null) {
4803 throw new IllegalArgumentException("Style requires a valid Builder object");
4804 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004805 }
Chris Wrend6297db2012-05-03 16:20:13 -04004806
4807 protected RemoteViews getStandardView(int layoutId) {
4808 checkBuilder();
4809
Christoph Studer4600f9b2014-07-22 22:44:43 +02004810 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004811 CharSequence oldBuilderContentTitle =
4812 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004813 if (mBigContentTitle != null) {
4814 mBuilder.setContentTitle(mBigContentTitle);
4815 }
4816
Chris Wrend6297db2012-05-03 16:20:13 -04004817 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
4818
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004819 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004820
Chris Wrend6297db2012-05-03 16:20:13 -04004821 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
4822 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04004823 } else {
4824 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004825 }
4826
Chris Wrend6297db2012-05-03 16:20:13 -04004827 return contentView;
4828 }
4829
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004830 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08004831 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004832 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08004833 *
4834 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004835 * @hide
4836 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08004837 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004838 return null;
4839 }
4840
4841 /**
4842 * Construct a Style-specific RemoteViews for the final big notification layout.
4843 * @hide
4844 */
4845 public RemoteViews makeBigContentView() {
4846 return null;
4847 }
4848
4849 /**
4850 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004851 *
4852 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004853 * @hide
4854 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004855 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004856 return null;
4857 }
4858
4859 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004860 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004861 * @hide
4862 */
4863 public void addExtras(Bundle extras) {
4864 if (mSummaryTextSet) {
4865 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
4866 }
4867 if (mBigContentTitle != null) {
4868 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
4869 }
Chris Wren91ad5632013-06-05 15:05:57 -04004870 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004871 }
4872
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004873 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004874 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004875 * @hide
4876 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02004877 protected void restoreFromExtras(Bundle extras) {
4878 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
4879 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
4880 mSummaryTextSet = true;
4881 }
4882 if (extras.containsKey(EXTRA_TITLE_BIG)) {
4883 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
4884 }
4885 }
4886
4887
4888 /**
4889 * @hide
4890 */
4891 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004892 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004893 return wip;
4894 }
4895
Daniel Sandler0ec46202015-06-24 01:27:05 -04004896 /**
4897 * @hide
4898 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004899 public void purgeResources() {}
4900
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004901 /**
4902 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
4903 * attached to.
4904 *
4905 * @return the fully constructed Notification.
4906 */
4907 public Notification build() {
4908 checkBuilder();
4909 return mBuilder.build();
4910 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004911
4912 /**
4913 * @hide
4914 * @return true if the style positions the progress bar on the second line; false if the
4915 * style hides the progress bar
4916 */
4917 protected boolean hasProgress() {
4918 return true;
4919 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004920
4921 /**
4922 * @hide
4923 * @return Whether we should put the summary be put into the notification header
4924 */
4925 public boolean hasSummaryInHeader() {
4926 return true;
4927 }
Selim Cinek593610c2016-02-16 18:42:57 -08004928
4929 /**
4930 * @hide
4931 * @return Whether custom content views are displayed inline in the style
4932 */
4933 public boolean displayCustomViewInline() {
4934 return false;
4935 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004936 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004937
4938 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004939 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08004940 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004941 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004942 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004943 * Notification notif = new Notification.Builder(mContext)
4944 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
4945 * .setContentText(subject)
4946 * .setSmallIcon(R.drawable.new_post)
4947 * .setLargeIcon(aBitmap)
4948 * .setStyle(new Notification.BigPictureStyle()
4949 * .bigPicture(aBigBitmap))
4950 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004951 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004952 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004953 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004954 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004955 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004956 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004957 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004958 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004959
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004960 public BigPictureStyle() {
4961 }
4962
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004963 /**
4964 * @deprecated use {@code BigPictureStyle()}.
4965 */
4966 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004967 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004968 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004969 }
4970
Chris Wrend6297db2012-05-03 16:20:13 -04004971 /**
4972 * Overrides ContentTitle in the big form of the template.
4973 * This defaults to the value passed to setContentTitle().
4974 */
4975 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004976 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004977 return this;
4978 }
4979
4980 /**
4981 * Set the first line of text after the detail section in the big form of the template.
4982 */
4983 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004984 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004985 return this;
4986 }
4987
Chris Wren0bd664d2012-08-01 13:56:56 -04004988 /**
4989 * Provide the bitmap to be used as the payload for the BigPicture notification.
4990 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004991 public BigPictureStyle bigPicture(Bitmap b) {
4992 mPicture = b;
4993 return this;
4994 }
4995
Chris Wren3745a3d2012-05-22 15:11:52 -04004996 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04004997 * Override the large icon when the big notification is shown.
4998 */
4999 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005000 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
5001 }
5002
5003 /**
5004 * Override the large icon when the big notification is shown.
5005 */
5006 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04005007 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005008 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005009 return this;
5010 }
5011
Riley Andrews0394a0c2015-11-03 23:36:52 -08005012 /** @hide */
5013 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
5014
Daniel Sandler0ec46202015-06-24 01:27:05 -04005015 /**
5016 * @hide
5017 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005018 @Override
5019 public void purgeResources() {
5020 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08005021 if (mPicture != null &&
5022 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08005023 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005024 mPicture = mPicture.createAshmemBitmap();
5025 }
5026 if (mBigLargeIcon != null) {
5027 mBigLargeIcon.convertToAshmem();
5028 }
5029 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01005030
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005031 /**
5032 * @hide
5033 */
5034 public RemoteViews makeBigContentView() {
5035 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01005036 // This covers the following cases:
5037 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005038 // mN.mLargeIcon
5039 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04005040 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07005041 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005042 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005043 oldLargeIcon = mBuilder.mN.mLargeIcon;
5044 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005045 // The legacy largeIcon might not allow us to clear the image, as it's taken in
5046 // replacement if the other one is null. Because we're restoring these legacy icons
5047 // for old listeners, this is in general non-null.
5048 largeIconLegacy = mBuilder.mN.largeIcon;
5049 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005050 }
5051
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005052 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05005053 if (mSummaryTextSet) {
5054 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005055 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005056 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05005057 }
Selim Cinek279fa862016-06-14 10:57:25 -07005058 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08005059
Christoph Studer5c510ee2014-12-15 16:32:27 +01005060 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005061 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005062 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005063 }
5064
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005065 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005066 return contentView;
5067 }
5068
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005069 /**
5070 * @hide
5071 */
5072 public void addExtras(Bundle extras) {
5073 super.addExtras(extras);
5074
5075 if (mBigLargeIconSet) {
5076 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
5077 }
5078 extras.putParcelable(EXTRA_PICTURE, mPicture);
5079 }
5080
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005081 /**
5082 * @hide
5083 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005084 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02005085 protected void restoreFromExtras(Bundle extras) {
5086 super.restoreFromExtras(extras);
5087
5088 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01005089 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02005090 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04005091 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005092 mPicture = extras.getParcelable(EXTRA_PICTURE);
5093 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005094
5095 /**
5096 * @hide
5097 */
5098 @Override
5099 public boolean hasSummaryInHeader() {
5100 return false;
5101 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005102 }
5103
5104 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005105 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08005106 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005107 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005108 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005109 * Notification notif = new Notification.Builder(mContext)
5110 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5111 * .setContentText(subject)
5112 * .setSmallIcon(R.drawable.new_mail)
5113 * .setLargeIcon(aBitmap)
5114 * .setStyle(new Notification.BigTextStyle()
5115 * .bigText(aVeryLongString))
5116 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005117 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005118 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005119 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005120 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005121 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005122
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005123 private CharSequence mBigText;
5124
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005125 public BigTextStyle() {
5126 }
5127
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005128 /**
5129 * @deprecated use {@code BigTextStyle()}.
5130 */
5131 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005132 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005133 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005134 }
5135
Chris Wrend6297db2012-05-03 16:20:13 -04005136 /**
5137 * Overrides ContentTitle in the big form of the template.
5138 * This defaults to the value passed to setContentTitle().
5139 */
5140 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005141 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005142 return this;
5143 }
5144
5145 /**
5146 * Set the first line of text after the detail section in the big form of the template.
5147 */
5148 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005149 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005150 return this;
5151 }
5152
Chris Wren0bd664d2012-08-01 13:56:56 -04005153 /**
5154 * Provide the longer text to be displayed in the big form of the
5155 * template in place of the content text.
5156 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005157 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005158 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005159 return this;
5160 }
5161
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005162 /**
5163 * @hide
5164 */
5165 public void addExtras(Bundle extras) {
5166 super.addExtras(extras);
5167
Christoph Studer4600f9b2014-07-22 22:44:43 +02005168 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
5169 }
5170
5171 /**
5172 * @hide
5173 */
5174 @Override
5175 protected void restoreFromExtras(Bundle extras) {
5176 super.restoreFromExtras(extras);
5177
5178 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005179 }
5180
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005181 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005182 * @param increasedHeight true if this layout be created with an increased height.
5183 *
5184 * @hide
5185 */
5186 @Override
5187 public RemoteViews makeContentView(boolean increasedHeight) {
5188 if (increasedHeight) {
5189 ArrayList<Action> actions = mBuilder.mActions;
5190 mBuilder.mActions = new ArrayList<>();
5191 RemoteViews remoteViews = makeBigContentView();
5192 mBuilder.mActions = actions;
5193 return remoteViews;
5194 }
5195 return super.makeContentView(increasedHeight);
5196 }
5197
5198 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005199 * @hide
5200 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005201 @Override
5202 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
5203 if (increasedHeight && mBuilder.mActions.size() > 0) {
5204 return makeBigContentView();
5205 }
5206 return super.makeHeadsUpContentView(increasedHeight);
5207 }
5208
5209 /**
5210 * @hide
5211 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005212 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005213
5214 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07005215 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005216 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04005217
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005218 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08005219
Selim Cinek75998782016-04-26 10:39:17 -07005220 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005221
Selim Cinek3a2c4b92015-12-17 17:01:17 -08005222 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07005223 if (TextUtils.isEmpty(bigTextText)) {
5224 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
5225 // experience
5226 bigTextText = mBuilder.processLegacyText(text);
5227 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07005228 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08005229
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005230 return contentView;
5231 }
5232
Adrian Roosb1f427c2016-05-26 12:27:15 -07005233 static void applyBigTextContentView(Builder builder,
5234 RemoteViews contentView, CharSequence bigTextText) {
5235 contentView.setTextViewText(R.id.big_text, bigTextText);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005236 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07005237 contentView.setViewVisibility(R.id.big_text,
5238 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07005239 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07005240 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005241 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005242
5243 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005244 * Helper class for generating large-format notifications that include multiple back-and-forth
5245 * messages of varying types between any number of people.
5246 *
5247 * <br>
5248 * If the platform does not provide large-format notifications, this method has no effect. The
5249 * user will always see the normal notification view.
5250 * <br>
5251 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
5252 * so:
5253 * <pre class="prettyprint">
5254 *
5255 * Notification noti = new Notification.Builder()
5256 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
5257 * .setContentText(subject)
5258 * .setSmallIcon(R.drawable.new_message)
5259 * .setLargeIcon(aBitmap)
5260 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
5261 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
5262 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
5263 * .build();
5264 * </pre>
5265 */
5266 public static class MessagingStyle extends Style {
5267
5268 /**
5269 * The maximum number of messages that will be retained in the Notification itself (the
5270 * number displayed is up to the platform).
5271 */
5272 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
5273
5274 CharSequence mUserDisplayName;
5275 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04005276 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08005277 List<Message> mHistoricMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04005278
5279 MessagingStyle() {
5280 }
5281
5282 /**
Alex Hillsfd590442016-10-07 09:52:44 -04005283 * @param userDisplayName Required - the name to be displayed for any replies sent by the
5284 * user before the posting app reposts the notification with those messages after they've
5285 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04005286 * {@link #addMessage(Notification.MessagingStyle.Message)}
5287 */
Alex Hillsfd590442016-10-07 09:52:44 -04005288 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Alex Hillsfc737de2016-03-23 17:33:02 -04005289 mUserDisplayName = userDisplayName;
5290 }
5291
5292 /**
5293 * Returns the name to be displayed for any replies sent by the user
5294 */
5295 public CharSequence getUserDisplayName() {
5296 return mUserDisplayName;
5297 }
5298
5299 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005300 * Sets the title to be displayed on this conversation. This should only be used for
5301 * group messaging and left unset for one-on-one conversations.
5302 * @param conversationTitle
5303 * @return this object for method chaining.
5304 */
5305 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
5306 mConversationTitle = conversationTitle;
5307 return this;
5308 }
5309
5310 /**
5311 * Return the title to be displayed on this conversation. Can be <code>null</code> and
5312 * should be for one-on-one conversations
5313 */
5314 public CharSequence getConversationTitle() {
5315 return mConversationTitle;
5316 }
5317
5318 /**
5319 * Adds a message for display by this notification. Convenience call for a simple
5320 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
5321 * @param text A {@link CharSequence} to be displayed as the message content
5322 * @param timestamp Time at which the message arrived
5323 * @param sender A {@link CharSequence} to be used for displaying the name of the
5324 * sender. Should be <code>null</code> for messages by the current user, in which case
5325 * the platform will insert {@link #getUserDisplayName()}.
5326 * Should be unique amongst all individuals in the conversation, and should be
5327 * consistent during re-posts of the notification.
5328 *
5329 * @see Message#Message(CharSequence, long, CharSequence)
5330 *
5331 * @return this object for method chaining
5332 */
5333 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08005334 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04005335 }
5336
5337 /**
5338 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08005339 *
5340 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5341 * the newest last.
5342 *
Alex Hillsfc737de2016-03-23 17:33:02 -04005343 * @param message The {@link Message} to be displayed
5344 * @return this object for method chaining
5345 */
5346 public MessagingStyle addMessage(Message message) {
5347 mMessages.add(message);
5348 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5349 mMessages.remove(0);
5350 }
5351 return this;
5352 }
5353
5354 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005355 * Adds a {@link Message} for historic context in this notification.
5356 *
5357 * <p>Messages should be added as historic if they are not the main subject of the
5358 * notification but may give context to a conversation. The system may choose to present
5359 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
5360 *
5361 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5362 * the newest last.
5363 *
5364 * @param message The historic {@link Message} to be added
5365 * @return this object for method chaining
5366 */
5367 public MessagingStyle addHistoricMessage(Message message) {
5368 mHistoricMessages.add(message);
5369 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5370 mHistoricMessages.remove(0);
5371 }
5372 return this;
5373 }
5374
5375 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005376 * Gets the list of {@code Message} objects that represent the notification
5377 */
5378 public List<Message> getMessages() {
5379 return mMessages;
5380 }
5381
5382 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005383 * Gets the list of historic {@code Message}s in the notification.
5384 */
5385 public List<Message> getHistoricMessages() {
5386 return mHistoricMessages;
5387 }
5388
5389 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005390 * @hide
5391 */
5392 @Override
5393 public void addExtras(Bundle extras) {
5394 super.addExtras(extras);
5395 if (mUserDisplayName != null) {
5396 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
5397 }
5398 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005399 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04005400 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005401 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
5402 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04005403 }
Adrian Roos437cd562017-01-18 15:47:03 -08005404 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
5405 Message.getBundleArrayForMessages(mHistoricMessages));
5406 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07005407
5408 fixTitleAndTextExtras(extras);
5409 }
5410
5411 private void fixTitleAndTextExtras(Bundle extras) {
5412 Message m = findLatestIncomingMessage();
5413 CharSequence text = (m == null) ? null : m.mText;
5414 CharSequence sender = m == null ? null
5415 : TextUtils.isEmpty(m.mSender) ? mUserDisplayName : m.mSender;
5416 CharSequence title;
5417 if (!TextUtils.isEmpty(mConversationTitle)) {
5418 if (!TextUtils.isEmpty(sender)) {
5419 BidiFormatter bidi = BidiFormatter.getInstance();
5420 title = mBuilder.mContext.getString(
5421 com.android.internal.R.string.notification_messaging_title_template,
5422 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(m.mSender));
5423 } else {
5424 title = mConversationTitle;
5425 }
5426 } else {
5427 title = sender;
5428 }
5429
5430 if (title != null) {
5431 extras.putCharSequence(EXTRA_TITLE, title);
5432 }
5433 if (text != null) {
5434 extras.putCharSequence(EXTRA_TEXT, text);
5435 }
Alex Hillsfc737de2016-03-23 17:33:02 -04005436 }
5437
5438 /**
5439 * @hide
5440 */
5441 @Override
5442 protected void restoreFromExtras(Bundle extras) {
5443 super.restoreFromExtras(extras);
5444
5445 mMessages.clear();
Adrian Roos437cd562017-01-18 15:47:03 -08005446 mHistoricMessages.clear();
Adrian Roos96b7e202016-05-17 13:50:38 -07005447 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
5448 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08005449 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
5450 if (messages != null && messages instanceof Parcelable[]) {
5451 mMessages = Message.getMessagesFromBundleArray(messages);
5452 }
5453 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
5454 if (histMessages != null && histMessages instanceof Parcelable[]) {
5455 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Alex Hillsfc737de2016-03-23 17:33:02 -04005456 }
5457 }
5458
5459 /**
5460 * @hide
5461 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07005462 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08005463 public RemoteViews makeContentView(boolean increasedHeight) {
5464 if (!increasedHeight) {
5465 Message m = findLatestIncomingMessage();
5466 CharSequence title = mConversationTitle != null
5467 ? mConversationTitle
5468 : (m == null) ? null : m.mSender;
5469 CharSequence text = (m == null)
5470 ? null
5471 : mConversationTitle != null ? makeMessageLine(m, mBuilder) : m.mText;
Alex Hillsfc737de2016-03-23 17:33:02 -04005472
Selim Cinek7d1009b2017-01-25 15:28:28 -08005473 return mBuilder.applyStandardTemplate(mBuilder.getBaseLayoutResource(),
5474 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
5475 } else {
5476 ArrayList<Action> actions = mBuilder.mActions;
5477 mBuilder.mActions = new ArrayList<>();
5478 RemoteViews remoteViews = makeBigContentView();
5479 mBuilder.mActions = actions;
5480 return remoteViews;
5481 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07005482 }
5483
5484 private Message findLatestIncomingMessage() {
5485 for (int i = mMessages.size() - 1; i >= 0; i--) {
5486 Message m = mMessages.get(i);
5487 // Incoming messages have a non-empty sender.
5488 if (!TextUtils.isEmpty(m.mSender)) {
5489 return m;
5490 }
5491 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07005492 if (!mMessages.isEmpty()) {
5493 // No incoming messages, fall back to outgoing message
5494 return mMessages.get(mMessages.size() - 1);
5495 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07005496 return null;
5497 }
5498
5499 /**
5500 * @hide
5501 */
5502 @Override
5503 public RemoteViews makeBigContentView() {
5504 CharSequence title = !TextUtils.isEmpty(super.mBigContentTitle)
5505 ? super.mBigContentTitle
5506 : mConversationTitle;
5507 boolean hasTitle = !TextUtils.isEmpty(title);
5508
Adrian Roosfeafa052016-06-01 17:09:45 -07005509 if (mMessages.size() == 1) {
5510 // Special case for a single message: Use the big text style
5511 // so the collapsed and expanded versions match nicely.
5512 CharSequence bigTitle;
5513 CharSequence text;
5514 if (hasTitle) {
5515 bigTitle = title;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005516 text = makeMessageLine(mMessages.get(0), mBuilder);
Adrian Roosfeafa052016-06-01 17:09:45 -07005517 } else {
5518 bigTitle = mMessages.get(0).mSender;
5519 text = mMessages.get(0).mText;
5520 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07005521 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
5522 mBuilder.getBigTextLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08005523 mBuilder.mParams.reset().hasProgress(false).title(bigTitle).text(null));
Adrian Roosb1f427c2016-05-26 12:27:15 -07005524 BigTextStyle.applyBigTextContentView(mBuilder, contentView, text);
5525 return contentView;
5526 }
5527
Adrian Roos48d746a2016-04-12 14:57:28 -07005528 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07005529 mBuilder.getMessagingLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08005530 mBuilder.mParams.reset().hasProgress(false).title(title).text(null));
Adrian Roosc1a80b02016-04-05 14:54:55 -07005531
5532 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
5533 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
5534
5535 // Make sure all rows are gone in case we reuse a view.
5536 for (int rowId : rowIds) {
5537 contentView.setViewVisibility(rowId, View.GONE);
5538 }
5539
5540 int i=0;
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005541 contentView.setViewLayoutMarginBottomDimen(R.id.line1,
5542 hasTitle ? R.dimen.notification_messaging_spacing : 0);
Adrian Roosc1a80b02016-04-05 14:54:55 -07005543 contentView.setInt(R.id.notification_messaging, "setNumIndentLines",
Selim Cinek279fa862016-06-14 10:57:25 -07005544 !mBuilder.mN.hasLargeIcon() ? 0 : (hasTitle ? 1 : 2));
Adrian Roosc1a80b02016-04-05 14:54:55 -07005545
Adrian Roosfeafa052016-06-01 17:09:45 -07005546 int contractedChildId = View.NO_ID;
5547 Message contractedMessage = findLatestIncomingMessage();
Adrian Roos437cd562017-01-18 15:47:03 -08005548 int firstHistoricMessage = Math.max(0, mHistoricMessages.size()
5549 - (rowIds.length - mMessages.size()));
5550 while (firstHistoricMessage + i < mHistoricMessages.size() && i < rowIds.length) {
5551 Message m = mHistoricMessages.get(firstHistoricMessage + i);
5552 int rowId = rowIds[i];
5553
Selim Cinek7b9605b2017-01-19 17:36:00 -08005554 contentView.setTextViewText(rowId, makeMessageLine(m, mBuilder));
Adrian Roos437cd562017-01-18 15:47:03 -08005555
5556 if (contractedMessage == m) {
5557 contractedChildId = rowId;
5558 }
5559
5560 i++;
5561 }
5562
Adrian Roosc1a80b02016-04-05 14:54:55 -07005563 int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
5564 while (firstMessage + i < mMessages.size() && i < rowIds.length) {
5565 Message m = mMessages.get(firstMessage + i);
5566 int rowId = rowIds[i];
5567
5568 contentView.setViewVisibility(rowId, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005569 contentView.setTextViewText(rowId, makeMessageLine(m, mBuilder));
5570 mBuilder.setTextViewColorSecondary(contentView, rowId);
Adrian Roosc1a80b02016-04-05 14:54:55 -07005571
Adrian Roosfeafa052016-06-01 17:09:45 -07005572 if (contractedMessage == m) {
5573 contractedChildId = rowId;
5574 }
5575
Adrian Roosc1a80b02016-04-05 14:54:55 -07005576 i++;
5577 }
Adrian Roos437cd562017-01-18 15:47:03 -08005578 // Clear the remaining views for reapply. Ensures that historic message views can
5579 // reliably be identified as being GONE and having non-null text.
5580 while (i < rowIds.length) {
5581 int rowId = rowIds[i];
5582 contentView.setTextViewText(rowId, null);
5583 i++;
5584 }
5585
Adrian Roosfeafa052016-06-01 17:09:45 -07005586 // Record this here to allow transformation between the contracted and expanded views.
5587 contentView.setInt(R.id.notification_messaging, "setContractedChildId",
5588 contractedChildId);
Alex Hillsfc737de2016-03-23 17:33:02 -04005589 return contentView;
5590 }
5591
Selim Cinek7b9605b2017-01-19 17:36:00 -08005592 private CharSequence makeMessageLine(Message m, Builder builder) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07005593 BidiFormatter bidi = BidiFormatter.getInstance();
5594 SpannableStringBuilder sb = new SpannableStringBuilder();
Selim Cinek7b9605b2017-01-19 17:36:00 -08005595 boolean colorize = builder.isColorized();
Adrian Roosc1a80b02016-04-05 14:54:55 -07005596 if (TextUtils.isEmpty(m.mSender)) {
5597 CharSequence replyName = mUserDisplayName == null ? "" : mUserDisplayName;
5598 sb.append(bidi.unicodeWrap(replyName),
Selim Cinek7b9605b2017-01-19 17:36:00 -08005599 makeFontColorSpan(colorize
5600 ? builder.getPrimaryTextColor()
5601 : mBuilder.resolveContrastColor()),
Adrian Roosc1a80b02016-04-05 14:54:55 -07005602 0 /* flags */);
5603 } else {
5604 sb.append(bidi.unicodeWrap(m.mSender),
Selim Cinek7b9605b2017-01-19 17:36:00 -08005605 makeFontColorSpan(colorize
5606 ? builder.getPrimaryTextColor()
5607 : Color.BLACK),
Adrian Roosc1a80b02016-04-05 14:54:55 -07005608 0 /* flags */);
5609 }
5610 CharSequence text = m.mText == null ? "" : m.mText;
5611 sb.append(" ").append(bidi.unicodeWrap(text));
5612 return sb;
5613 }
5614
Adrian Roosdedd1df2016-04-26 16:38:47 -07005615 /**
5616 * @hide
5617 */
5618 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08005619 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
5620 if (increasedHeight) {
5621 return makeBigContentView();
5622 }
Adrian Roosdedd1df2016-04-26 16:38:47 -07005623 Message m = findLatestIncomingMessage();
5624 CharSequence title = mConversationTitle != null
5625 ? mConversationTitle
5626 : (m == null) ? null : m.mSender;
5627 CharSequence text = (m == null)
5628 ? null
Selim Cinek7b9605b2017-01-19 17:36:00 -08005629 : mConversationTitle != null ? makeMessageLine(m, mBuilder) : m.mText;
Adrian Roosdedd1df2016-04-26 16:38:47 -07005630
5631 return mBuilder.applyStandardTemplateWithActions(mBuilder.getBigBaseLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08005632 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
Adrian Roosdedd1df2016-04-26 16:38:47 -07005633 }
5634
Adrian Roosc1a80b02016-04-05 14:54:55 -07005635 private static TextAppearanceSpan makeFontColorSpan(int color) {
5636 return new TextAppearanceSpan(null, 0, 0,
5637 ColorStateList.valueOf(color), null);
5638 }
5639
Alex Hillsd9b04d92016-04-11 16:38:16 -04005640 public static final class Message {
5641
5642 static final String KEY_TEXT = "text";
5643 static final String KEY_TIMESTAMP = "time";
5644 static final String KEY_SENDER = "sender";
5645 static final String KEY_DATA_MIME_TYPE = "type";
5646 static final String KEY_DATA_URI= "uri";
Alex Hillsfc737de2016-03-23 17:33:02 -04005647
5648 private final CharSequence mText;
5649 private final long mTimestamp;
5650 private final CharSequence mSender;
5651
5652 private String mDataMimeType;
5653 private Uri mDataUri;
5654
5655 /**
5656 * Constructor
5657 * @param text A {@link CharSequence} to be displayed as the message content
5658 * @param timestamp Time at which the message arrived
5659 * @param sender A {@link CharSequence} to be used for displaying the name of the
5660 * sender. Should be <code>null</code> for messages by the current user, in which case
5661 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
5662 * Should be unique amongst all individuals in the conversation, and should be
5663 * consistent during re-posts of the notification.
5664 */
5665 public Message(CharSequence text, long timestamp, CharSequence sender){
5666 mText = text;
5667 mTimestamp = timestamp;
5668 mSender = sender;
5669 }
5670
5671 /**
5672 * Sets a binary blob of data and an associated MIME type for a message. In the case
5673 * where the platform doesn't support the MIME type, the original text provided in the
5674 * constructor will be used.
5675 * @param dataMimeType The MIME type of the content. See
5676 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
5677 * types on Android and Android Wear.
5678 * @param dataUri The uri containing the content whose type is given by the MIME type.
5679 * <p class="note">
5680 * <ol>
5681 * <li>Notification Listeners including the System UI need permission to access the
5682 * data the Uri points to. The recommended ways to do this are:</li>
5683 * <li>Store the data in your own ContentProvider, making sure that other apps have
5684 * the correct permission to access your provider. The preferred mechanism for
5685 * providing access is to use per-URI permissions which are temporary and only
5686 * grant access to the receiving application. An easy way to create a
5687 * ContentProvider like this is to use the FileProvider helper class.</li>
5688 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
5689 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
5690 * also store non-media types (see MediaStore.Files for more info). Files can be
5691 * inserted into the MediaStore using scanFile() after which a content:// style
5692 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
5693 * Note that once added to the system MediaStore the content is accessible to any
5694 * app on the device.</li>
5695 * </ol>
5696 * @return this object for method chaining
5697 */
5698 public Message setData(String dataMimeType, Uri dataUri) {
5699 mDataMimeType = dataMimeType;
5700 mDataUri = dataUri;
5701 return this;
5702 }
5703
Alex Hillsfc737de2016-03-23 17:33:02 -04005704 /**
5705 * Get the text to be used for this message, or the fallback text if a type and content
5706 * Uri have been set
5707 */
5708 public CharSequence getText() {
5709 return mText;
5710 }
5711
5712 /**
5713 * Get the time at which this message arrived
5714 */
5715 public long getTimestamp() {
5716 return mTimestamp;
5717 }
5718
5719 /**
5720 * Get the text used to display the contact's name in the messaging experience
5721 */
5722 public CharSequence getSender() {
5723 return mSender;
5724 }
5725
5726 /**
5727 * Get the MIME type of the data pointed to by the Uri
5728 */
5729 public String getDataMimeType() {
5730 return mDataMimeType;
5731 }
5732
5733 /**
5734 * Get the the Uri pointing to the content of the message. Can be null, in which case
5735 * {@see #getText()} is used.
5736 */
5737 public Uri getDataUri() {
5738 return mDataUri;
5739 }
5740
Alex Hillsd9b04d92016-04-11 16:38:16 -04005741 private Bundle toBundle() {
5742 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04005743 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005744 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04005745 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005746 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04005747 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005748 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04005749 }
5750 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005751 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04005752 }
5753 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005754 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04005755 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005756 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04005757 }
5758
Alex Hillsd9b04d92016-04-11 16:38:16 -04005759 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
5760 Bundle[] bundles = new Bundle[messages.size()];
5761 final int N = messages.size();
5762 for (int i = 0; i < N; i++) {
5763 bundles[i] = messages.get(i).toBundle();
5764 }
5765 return bundles;
5766 }
5767
Adrian Roosdedd1df2016-04-26 16:38:47 -07005768 static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005769 List<Message> messages = new ArrayList<>(bundles.length);
5770 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07005771 if (bundles[i] instanceof Bundle) {
5772 Message message = getMessageFromBundle((Bundle)bundles[i]);
5773 if (message != null) {
5774 messages.add(message);
5775 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005776 }
5777 }
5778 return messages;
5779 }
5780
5781 static Message getMessageFromBundle(Bundle bundle) {
5782 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07005783 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005784 return null;
5785 } else {
5786 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
5787 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
5788 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
5789 bundle.containsKey(KEY_DATA_URI)) {
5790
5791 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
5792 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04005793 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005794 return message;
5795 }
5796 } catch (ClassCastException e) {
5797 return null;
5798 }
5799 }
Alex Hillsfc737de2016-03-23 17:33:02 -04005800 }
5801 }
5802
5803 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04005804 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08005805 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005806 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04005807 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005808 * Notification notif = new Notification.Builder(mContext)
5809 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
5810 * .setContentText(subject)
5811 * .setSmallIcon(R.drawable.new_mail)
5812 * .setLargeIcon(aBitmap)
5813 * .setStyle(new Notification.InboxStyle()
5814 * .addLine(str1)
5815 * .addLine(str2)
5816 * .setContentTitle(&quot;&quot;)
5817 * .setSummaryText(&quot;+3 more&quot;))
5818 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04005819 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005820 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04005821 * @see Notification#bigContentView
5822 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005823 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005824 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
5825
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005826 public InboxStyle() {
5827 }
5828
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005829 /**
5830 * @deprecated use {@code InboxStyle()}.
5831 */
5832 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04005833 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005834 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04005835 }
5836
Chris Wrend6297db2012-05-03 16:20:13 -04005837 /**
5838 * Overrides ContentTitle in the big form of the template.
5839 * This defaults to the value passed to setContentTitle().
5840 */
5841 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005842 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005843 return this;
5844 }
5845
5846 /**
5847 * Set the first line of text after the detail section in the big form of the template.
5848 */
5849 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005850 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005851 return this;
5852 }
5853
Chris Wren0bd664d2012-08-01 13:56:56 -04005854 /**
5855 * Append a line to the digest section of the Inbox notification.
5856 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04005857 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005858 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04005859 return this;
5860 }
5861
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005862 /**
5863 * @hide
5864 */
5865 public void addExtras(Bundle extras) {
5866 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005867
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005868 CharSequence[] a = new CharSequence[mTexts.size()];
5869 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
5870 }
5871
Christoph Studer4600f9b2014-07-22 22:44:43 +02005872 /**
5873 * @hide
5874 */
5875 @Override
5876 protected void restoreFromExtras(Bundle extras) {
5877 super.restoreFromExtras(extras);
5878
5879 mTexts.clear();
5880 if (extras.containsKey(EXTRA_TEXT_LINES)) {
5881 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
5882 }
5883 }
5884
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005885 /**
5886 * @hide
5887 */
5888 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08005889 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02005890 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005891 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
5892 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005893
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005894 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04005895
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005896 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005897
Chris Wrend6297db2012-05-03 16:20:13 -04005898 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 -04005899 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04005900
Chris Wren4ed80d52012-05-17 09:30:03 -04005901 // Make sure all rows are gone in case we reuse a view.
5902 for (int rowId : rowIds) {
5903 contentView.setViewVisibility(rowId, View.GONE);
5904 }
5905
Daniel Sandler879c5e02012-04-17 16:46:51 -04005906 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07005907 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
5908 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08005909 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07005910 int onlyViewId = 0;
5911 int maxRows = rowIds.length;
5912 if (mBuilder.mActions.size() > 0) {
5913 maxRows--;
5914 }
5915 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005916 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07005917 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005918 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005919 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005920 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07005921 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08005922 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07005923 if (first) {
5924 onlyViewId = rowIds[i];
5925 } else {
5926 onlyViewId = 0;
5927 }
Selim Cinek247fa012016-02-18 09:50:48 -08005928 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04005929 }
5930 i++;
5931 }
Selim Cinek07c80172016-04-21 16:40:47 -07005932 if (onlyViewId != 0) {
5933 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
5934 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
5935 R.dimen.notification_text_margin_top);
5936 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
5937 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005938
Daniel Sandler879c5e02012-04-17 16:46:51 -04005939 return contentView;
5940 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005941
Selim Cinek247fa012016-02-18 09:50:48 -08005942 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08005943 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08005944 if (first) {
5945 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
5946 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
5947 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07005948 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005949 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08005950 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005951 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005952 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08005953 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005954 }
Dan Sandler842dd772014-05-15 09:36:47 -04005955
5956 /**
5957 * Notification style for media playback notifications.
5958 *
5959 * In the expanded form, {@link Notification#bigContentView}, up to 5
5960 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04005961 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04005962 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
5963 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
5964 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08005965 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04005966 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
5967 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01005968 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04005969 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08005970 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005971 * Notifications created with MediaStyle will have their category set to
5972 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
5973 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08005974 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07005975 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
5976 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04005977 * the System UI can identify this as a notification representing an active media session
5978 * and respond accordingly (by showing album artwork in the lockscreen, for example).
5979 *
Selim Cinek99104832017-01-25 14:47:33 -08005980 * <p>
5981 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
5982 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
5983 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
5984 * <p>
5985 *
Dan Sandler842dd772014-05-15 09:36:47 -04005986 * To use this style with your Notification, feed it to
5987 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5988 * <pre class="prettyprint">
5989 * Notification noti = new Notification.Builder()
5990 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01005991 * .setContentTitle(&quot;Track title&quot;)
5992 * .setContentText(&quot;Artist - Album&quot;)
5993 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005994 * .setStyle(<b>new Notification.MediaStyle()</b>
5995 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04005996 * .build();
5997 * </pre>
5998 *
5999 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08006000 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04006001 */
6002 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006003 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04006004 static final int MAX_MEDIA_BUTTONS = 5;
6005
6006 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07006007 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04006008
6009 public MediaStyle() {
6010 }
6011
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006012 /**
6013 * @deprecated use {@code MediaStyle()}.
6014 */
6015 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04006016 public MediaStyle(Builder builder) {
6017 setBuilder(builder);
6018 }
6019
6020 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006021 * 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 -04006022 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006023 *
6024 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04006025 */
6026 public MediaStyle setShowActionsInCompactView(int...actions) {
6027 mActionsToShowInCompact = actions;
6028 return this;
6029 }
6030
6031 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07006032 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
6033 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04006034 */
Jeff Browndba34ba2014-06-24 20:46:03 -07006035 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04006036 mToken = token;
6037 return this;
6038 }
6039
Christoph Studer4600f9b2014-07-22 22:44:43 +02006040 /**
6041 * @hide
6042 */
Dan Sandler842dd772014-05-15 09:36:47 -04006043 @Override
6044 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006045 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006046 if (wip.category == null) {
6047 wip.category = Notification.CATEGORY_TRANSPORT;
6048 }
Dan Sandler842dd772014-05-15 09:36:47 -04006049 return wip;
6050 }
6051
Christoph Studer4600f9b2014-07-22 22:44:43 +02006052 /**
6053 * @hide
6054 */
6055 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006056 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006057 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006058 }
6059
6060 /**
6061 * @hide
6062 */
6063 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006064 public RemoteViews makeBigContentView() {
6065 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006066 }
6067
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006068 /**
6069 * @hide
6070 */
6071 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006072 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006073 RemoteViews expanded = makeMediaBigContentView();
6074 return expanded != null ? expanded : makeMediaContentView();
6075 }
6076
Dan Sandler842dd772014-05-15 09:36:47 -04006077 /** @hide */
6078 @Override
6079 public void addExtras(Bundle extras) {
6080 super.addExtras(extras);
6081
6082 if (mToken != null) {
6083 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
6084 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01006085 if (mActionsToShowInCompact != null) {
6086 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
6087 }
Dan Sandler842dd772014-05-15 09:36:47 -04006088 }
6089
Christoph Studer4600f9b2014-07-22 22:44:43 +02006090 /**
6091 * @hide
6092 */
6093 @Override
6094 protected void restoreFromExtras(Bundle extras) {
6095 super.restoreFromExtras(extras);
6096
6097 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
6098 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
6099 }
6100 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
6101 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
6102 }
6103 }
6104
Selim Cinek5bf069a2015-11-10 19:14:27 -05006105 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04006106 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07006107 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07006108 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04006109 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05006110 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
6111 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04006112 if (!tombstone) {
6113 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
6114 }
6115 button.setContentDescription(R.id.action0, action.title);
6116 return button;
6117 }
6118
6119 private RemoteViews makeMediaContentView() {
6120 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006121 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04006122
6123 final int numActions = mBuilder.mActions.size();
6124 final int N = mActionsToShowInCompact == null
6125 ? 0
6126 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
6127 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006128 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04006129 for (int i = 0; i < N; i++) {
6130 if (i >= numActions) {
6131 throw new IllegalArgumentException(String.format(
6132 "setShowActionsInCompactView: action %d out of bounds (max %d)",
6133 i, numActions - 1));
6134 }
6135
6136 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05006137 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08006138 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006139 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006140 }
6141 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08006142 handleImage(view);
6143 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006144 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006145 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006146 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08006147 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006148 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04006149 return view;
6150 }
6151
Selim Cinek99104832017-01-25 14:47:33 -08006152 private int getPrimaryHighlightColor() {
6153 return mBuilder.getPrimaryHighlightColor();
6154 }
6155
Dan Sandler842dd772014-05-15 09:36:47 -04006156 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006157 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006158 // Dont add an expanded view if there is no more content to be revealed
6159 int actionsInCompact = mActionsToShowInCompact == null
6160 ? 0
6161 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07006162 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006163 return null;
6164 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006165 RemoteViews big = mBuilder.applyStandardTemplate(
6166 R.layout.notification_template_material_big_media,
6167 false);
Dan Sandler842dd772014-05-15 09:36:47 -04006168
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006169 if (actionCount > 0) {
6170 big.removeAllViews(com.android.internal.R.id.media_actions);
6171 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05006172 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08006173 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006174 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006175 }
6176 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006177 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04006178 return big;
6179 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006180
Selim Cinek5bf069a2015-11-10 19:14:27 -05006181 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07006182 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006183 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
6184 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006185 }
6186 }
6187
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006188 /**
6189 * @hide
6190 */
6191 @Override
6192 protected boolean hasProgress() {
6193 return false;
6194 }
Dan Sandler842dd772014-05-15 09:36:47 -04006195 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006196
Selim Cinek593610c2016-02-16 18:42:57 -08006197 /**
6198 * Notification style for custom views that are decorated by the system
6199 *
6200 * <p>Instead of providing a notification that is completely custom, a developer can set this
6201 * style and still obtain system decorations like the notification header with the expand
6202 * affordance and actions.
6203 *
6204 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6205 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6206 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6207 * corresponding custom views to display.
6208 *
6209 * To use this style with your Notification, feed it to
6210 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6211 * <pre class="prettyprint">
6212 * Notification noti = new Notification.Builder()
6213 * .setSmallIcon(R.drawable.ic_stat_player)
6214 * .setLargeIcon(albumArtBitmap))
6215 * .setCustomContentView(contentView);
6216 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
6217 * .build();
6218 * </pre>
6219 */
6220 public static class DecoratedCustomViewStyle extends Style {
6221
6222 public DecoratedCustomViewStyle() {
6223 }
6224
Selim Cinek593610c2016-02-16 18:42:57 -08006225 /**
6226 * @hide
6227 */
6228 public boolean displayCustomViewInline() {
6229 return true;
6230 }
6231
6232 /**
6233 * @hide
6234 */
6235 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006236 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006237 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
6238 }
6239
6240 /**
6241 * @hide
6242 */
6243 @Override
6244 public RemoteViews makeBigContentView() {
6245 return makeDecoratedBigContentView();
6246 }
6247
6248 /**
6249 * @hide
6250 */
6251 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006252 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006253 return makeDecoratedHeadsUpContentView();
6254 }
6255
Selim Cinek593610c2016-02-16 18:42:57 -08006256 private RemoteViews makeDecoratedHeadsUpContentView() {
6257 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
6258 ? mBuilder.mN.contentView
6259 : mBuilder.mN.headsUpContentView;
6260 if (mBuilder.mActions.size() == 0) {
6261 return makeStandardTemplateWithCustomContent(headsUpContentView);
6262 }
6263 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6264 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006265 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006266 return remoteViews;
6267 }
6268
Selim Cinek593610c2016-02-16 18:42:57 -08006269 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
6270 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
6271 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006272 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08006273 return remoteViews;
6274 }
6275
Selim Cinek593610c2016-02-16 18:42:57 -08006276 private RemoteViews makeDecoratedBigContentView() {
6277 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
6278 ? mBuilder.mN.contentView
6279 : mBuilder.mN.bigContentView;
6280 if (mBuilder.mActions.size() == 0) {
6281 return makeStandardTemplateWithCustomContent(bigContentView);
6282 }
6283 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6284 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006285 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006286 return remoteViews;
6287 }
Selim Cinek247fa012016-02-18 09:50:48 -08006288
6289 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
6290 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08006291 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07006292 // Need to clone customContent before adding, because otherwise it can no longer be
6293 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08006294 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07006295 remoteViews.removeAllViews(R.id.notification_main_column);
6296 remoteViews.addView(R.id.notification_main_column, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08006297 }
Selim Cinek247fa012016-02-18 09:50:48 -08006298 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006299 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006300 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006301 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08006302 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006303 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08006304 }
Selim Cinek593610c2016-02-16 18:42:57 -08006305 }
6306
Selim Cinek03eb3b72016-02-18 10:39:45 -08006307 /**
6308 * Notification style for media custom views that are decorated by the system
6309 *
6310 * <p>Instead of providing a media notification that is completely custom, a developer can set
6311 * this style and still obtain system decorations like the notification header with the expand
6312 * affordance and actions.
6313 *
6314 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6315 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6316 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6317 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08006318 * <p>
6319 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
6320 * notification by using {@link Notification.Builder#setColorized(boolean)}.
6321 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08006322 * To use this style with your Notification, feed it to
6323 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6324 * <pre class="prettyprint">
6325 * Notification noti = new Notification.Builder()
6326 * .setSmallIcon(R.drawable.ic_stat_player)
6327 * .setLargeIcon(albumArtBitmap))
6328 * .setCustomContentView(contentView);
6329 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
6330 * .setMediaSession(mySession))
6331 * .build();
6332 * </pre>
6333 *
6334 * @see android.app.Notification.DecoratedCustomViewStyle
6335 * @see android.app.Notification.MediaStyle
6336 */
6337 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
6338
6339 public DecoratedMediaCustomViewStyle() {
6340 }
6341
Selim Cinek03eb3b72016-02-18 10:39:45 -08006342 /**
6343 * @hide
6344 */
6345 public boolean displayCustomViewInline() {
6346 return true;
6347 }
6348
6349 /**
6350 * @hide
6351 */
6352 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006353 public RemoteViews makeContentView(boolean increasedHeight) {
6354 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006355 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6356 mBuilder.mN.contentView);
6357 }
6358
6359 /**
6360 * @hide
6361 */
6362 @Override
6363 public RemoteViews makeBigContentView() {
6364 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
6365 ? mBuilder.mN.bigContentView
6366 : mBuilder.mN.contentView;
6367 return makeBigContentViewWithCustomContent(customRemoteView);
6368 }
6369
6370 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
6371 RemoteViews remoteViews = super.makeBigContentView();
6372 if (remoteViews != null) {
6373 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
6374 customRemoteView);
6375 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08006376 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006377 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6378 customRemoteView);
6379 } else {
6380 return null;
6381 }
6382 }
6383
6384 /**
6385 * @hide
6386 */
6387 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006388 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08006389 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
6390 ? mBuilder.mN.headsUpContentView
6391 : mBuilder.mN.contentView;
6392 return makeBigContentViewWithCustomContent(customRemoteView);
6393 }
6394
6395 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
6396 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08006397 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07006398 // Need to clone customContent before adding, because otherwise it can no longer be
6399 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08006400 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07006401 remoteViews.removeAllViews(id);
6402 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08006403 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08006404 return remoteViews;
6405 }
6406 }
6407
Christoph Studer4600f9b2014-07-22 22:44:43 +02006408 // When adding a new Style subclass here, don't forget to update
6409 // Builder.getNotificationStyleClass.
6410
Griff Hazen61a9e862014-05-22 16:05:19 -07006411 /**
6412 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
6413 * metadata or change options on a notification builder.
6414 */
6415 public interface Extender {
6416 /**
6417 * Apply this extender to a notification builder.
6418 * @param builder the builder to be modified.
6419 * @return the build object for chaining.
6420 */
6421 public Builder extend(Builder builder);
6422 }
6423
6424 /**
6425 * Helper class to add wearable extensions to notifications.
6426 * <p class="note"> See
6427 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
6428 * for Android Wear</a> for more information on how to use this class.
6429 * <p>
6430 * To create a notification with wearable extensions:
6431 * <ol>
6432 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
6433 * properties.
6434 * <li>Create a {@link android.app.Notification.WearableExtender}.
6435 * <li>Set wearable-specific properties using the
6436 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
6437 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
6438 * notification.
6439 * <li>Post the notification to the notification system with the
6440 * {@code NotificationManager.notify(...)} methods.
6441 * </ol>
6442 *
6443 * <pre class="prettyprint">
6444 * Notification notif = new Notification.Builder(mContext)
6445 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6446 * .setContentText(subject)
6447 * .setSmallIcon(R.drawable.new_mail)
6448 * .extend(new Notification.WearableExtender()
6449 * .setContentIcon(R.drawable.new_mail))
6450 * .build();
6451 * NotificationManager notificationManger =
6452 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
6453 * notificationManger.notify(0, notif);</pre>
6454 *
6455 * <p>Wearable extensions can be accessed on an existing notification by using the
6456 * {@code WearableExtender(Notification)} constructor,
6457 * and then using the {@code get} methods to access values.
6458 *
6459 * <pre class="prettyprint">
6460 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
6461 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07006462 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07006463 */
6464 public static final class WearableExtender implements Extender {
6465 /**
6466 * Sentinel value for an action index that is unset.
6467 */
6468 public static final int UNSET_ACTION_INDEX = -1;
6469
6470 /**
6471 * Size value for use with {@link #setCustomSizePreset} to show this notification with
6472 * default sizing.
6473 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07006474 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07006475 * on their content.
6476 */
6477 public static final int SIZE_DEFAULT = 0;
6478
6479 /**
6480 * Size value for use with {@link #setCustomSizePreset} to show this notification
6481 * with an extra small size.
6482 * <p>This value is only applicable for custom display notifications created using
6483 * {@link #setDisplayIntent}.
6484 */
6485 public static final int SIZE_XSMALL = 1;
6486
6487 /**
6488 * Size value for use with {@link #setCustomSizePreset} to show this notification
6489 * with a small size.
6490 * <p>This value is only applicable for custom display notifications created using
6491 * {@link #setDisplayIntent}.
6492 */
6493 public static final int SIZE_SMALL = 2;
6494
6495 /**
6496 * Size value for use with {@link #setCustomSizePreset} to show this notification
6497 * with a medium size.
6498 * <p>This value is only applicable for custom display notifications created using
6499 * {@link #setDisplayIntent}.
6500 */
6501 public static final int SIZE_MEDIUM = 3;
6502
6503 /**
6504 * Size value for use with {@link #setCustomSizePreset} to show this notification
6505 * with a large size.
6506 * <p>This value is only applicable for custom display notifications created using
6507 * {@link #setDisplayIntent}.
6508 */
6509 public static final int SIZE_LARGE = 4;
6510
Griff Hazend5f11f92014-05-27 15:40:09 -07006511 /**
6512 * Size value for use with {@link #setCustomSizePreset} to show this notification
6513 * full screen.
6514 * <p>This value is only applicable for custom display notifications created using
6515 * {@link #setDisplayIntent}.
6516 */
6517 public static final int SIZE_FULL_SCREEN = 5;
6518
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006519 /**
6520 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
6521 * short amount of time when this notification is displayed on the screen. This
6522 * is the default value.
6523 */
6524 public static final int SCREEN_TIMEOUT_SHORT = 0;
6525
6526 /**
6527 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
6528 * for a longer amount of time when this notification is displayed on the screen.
6529 */
6530 public static final int SCREEN_TIMEOUT_LONG = -1;
6531
Griff Hazen61a9e862014-05-22 16:05:19 -07006532 /** Notification extra which contains wearable extensions */
6533 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
6534
Pete Gastaf6781d2014-10-07 15:17:05 -04006535 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07006536 private static final String KEY_ACTIONS = "actions";
6537 private static final String KEY_FLAGS = "flags";
6538 private static final String KEY_DISPLAY_INTENT = "displayIntent";
6539 private static final String KEY_PAGES = "pages";
6540 private static final String KEY_BACKGROUND = "background";
6541 private static final String KEY_CONTENT_ICON = "contentIcon";
6542 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
6543 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
6544 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
6545 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
6546 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006547 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04006548 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006549 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07006550
6551 // Flags bitwise-ored to mFlags
6552 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
6553 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
6554 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
6555 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006556 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04006557 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04006558 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07006559
6560 // Default value for flags integer
6561 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
6562
6563 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
6564 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
6565
6566 private ArrayList<Action> mActions = new ArrayList<Action>();
6567 private int mFlags = DEFAULT_FLAGS;
6568 private PendingIntent mDisplayIntent;
6569 private ArrayList<Notification> mPages = new ArrayList<Notification>();
6570 private Bitmap mBackground;
6571 private int mContentIcon;
6572 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
6573 private int mContentActionIndex = UNSET_ACTION_INDEX;
6574 private int mCustomSizePreset = SIZE_DEFAULT;
6575 private int mCustomContentHeight;
6576 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006577 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04006578 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006579 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07006580
6581 /**
6582 * Create a {@link android.app.Notification.WearableExtender} with default
6583 * options.
6584 */
6585 public WearableExtender() {
6586 }
6587
6588 public WearableExtender(Notification notif) {
6589 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
6590 if (wearableBundle != null) {
6591 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
6592 if (actions != null) {
6593 mActions.addAll(actions);
6594 }
6595
6596 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
6597 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
6598
6599 Notification[] pages = getNotificationArrayFromBundle(
6600 wearableBundle, KEY_PAGES);
6601 if (pages != null) {
6602 Collections.addAll(mPages, pages);
6603 }
6604
6605 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
6606 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
6607 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
6608 DEFAULT_CONTENT_ICON_GRAVITY);
6609 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
6610 UNSET_ACTION_INDEX);
6611 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
6612 SIZE_DEFAULT);
6613 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
6614 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006615 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04006616 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006617 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07006618 }
6619 }
6620
6621 /**
6622 * Apply wearable extensions to a notification that is being built. This is typically
6623 * called by the {@link android.app.Notification.Builder#extend} method of
6624 * {@link android.app.Notification.Builder}.
6625 */
6626 @Override
6627 public Notification.Builder extend(Notification.Builder builder) {
6628 Bundle wearableBundle = new Bundle();
6629
6630 if (!mActions.isEmpty()) {
6631 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
6632 }
6633 if (mFlags != DEFAULT_FLAGS) {
6634 wearableBundle.putInt(KEY_FLAGS, mFlags);
6635 }
6636 if (mDisplayIntent != null) {
6637 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
6638 }
6639 if (!mPages.isEmpty()) {
6640 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
6641 new Notification[mPages.size()]));
6642 }
6643 if (mBackground != null) {
6644 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
6645 }
6646 if (mContentIcon != 0) {
6647 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
6648 }
6649 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
6650 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
6651 }
6652 if (mContentActionIndex != UNSET_ACTION_INDEX) {
6653 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
6654 mContentActionIndex);
6655 }
6656 if (mCustomSizePreset != SIZE_DEFAULT) {
6657 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
6658 }
6659 if (mCustomContentHeight != 0) {
6660 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
6661 }
6662 if (mGravity != DEFAULT_GRAVITY) {
6663 wearableBundle.putInt(KEY_GRAVITY, mGravity);
6664 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006665 if (mHintScreenTimeout != 0) {
6666 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
6667 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04006668 if (mDismissalId != null) {
6669 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
6670 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006671 if (mBridgeTag != null) {
6672 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
6673 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006674
6675 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
6676 return builder;
6677 }
6678
6679 @Override
6680 public WearableExtender clone() {
6681 WearableExtender that = new WearableExtender();
6682 that.mActions = new ArrayList<Action>(this.mActions);
6683 that.mFlags = this.mFlags;
6684 that.mDisplayIntent = this.mDisplayIntent;
6685 that.mPages = new ArrayList<Notification>(this.mPages);
6686 that.mBackground = this.mBackground;
6687 that.mContentIcon = this.mContentIcon;
6688 that.mContentIconGravity = this.mContentIconGravity;
6689 that.mContentActionIndex = this.mContentActionIndex;
6690 that.mCustomSizePreset = this.mCustomSizePreset;
6691 that.mCustomContentHeight = this.mCustomContentHeight;
6692 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006693 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04006694 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006695 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07006696 return that;
6697 }
6698
6699 /**
6700 * Add a wearable action to this notification.
6701 *
6702 * <p>When wearable actions are added using this method, the set of actions that
6703 * show on a wearable device splits from devices that only show actions added
6704 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
6705 * of which actions display on different devices.
6706 *
6707 * @param action the action to add to this notification
6708 * @return this object for method chaining
6709 * @see android.app.Notification.Action
6710 */
6711 public WearableExtender addAction(Action action) {
6712 mActions.add(action);
6713 return this;
6714 }
6715
6716 /**
6717 * Adds wearable actions to this notification.
6718 *
6719 * <p>When wearable actions are added using this method, the set of actions that
6720 * show on a wearable device splits from devices that only show actions added
6721 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
6722 * of which actions display on different devices.
6723 *
6724 * @param actions the actions to add to this notification
6725 * @return this object for method chaining
6726 * @see android.app.Notification.Action
6727 */
6728 public WearableExtender addActions(List<Action> actions) {
6729 mActions.addAll(actions);
6730 return this;
6731 }
6732
6733 /**
6734 * Clear all wearable actions present on this builder.
6735 * @return this object for method chaining.
6736 * @see #addAction
6737 */
6738 public WearableExtender clearActions() {
6739 mActions.clear();
6740 return this;
6741 }
6742
6743 /**
6744 * Get the wearable actions present on this notification.
6745 */
6746 public List<Action> getActions() {
6747 return mActions;
6748 }
6749
6750 /**
6751 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07006752 * this notification. The {@link PendingIntent} provided should be for an activity.
6753 *
6754 * <pre class="prettyprint">
6755 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
6756 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
6757 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
6758 * Notification notif = new Notification.Builder(context)
6759 * .extend(new Notification.WearableExtender()
6760 * .setDisplayIntent(displayPendingIntent)
6761 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
6762 * .build();</pre>
6763 *
6764 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07006765 * should have an empty task affinity. It is also recommended to use the device
6766 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07006767 *
6768 * <p>Example AndroidManifest.xml entry:
6769 * <pre class="prettyprint">
6770 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
6771 * android:exported=&quot;true&quot;
6772 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07006773 * android:taskAffinity=&quot;&quot;
6774 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07006775 *
6776 * @param intent the {@link PendingIntent} for an activity
6777 * @return this object for method chaining
6778 * @see android.app.Notification.WearableExtender#getDisplayIntent
6779 */
6780 public WearableExtender setDisplayIntent(PendingIntent intent) {
6781 mDisplayIntent = intent;
6782 return this;
6783 }
6784
6785 /**
6786 * Get the intent to launch inside of an activity view when displaying this
6787 * notification. This {@code PendingIntent} should be for an activity.
6788 */
6789 public PendingIntent getDisplayIntent() {
6790 return mDisplayIntent;
6791 }
6792
6793 /**
6794 * Add an additional page of content to display with this notification. The current
6795 * notification forms the first page, and pages added using this function form
6796 * subsequent pages. This field can be used to separate a notification into multiple
6797 * sections.
6798 *
6799 * @param page the notification to add as another page
6800 * @return this object for method chaining
6801 * @see android.app.Notification.WearableExtender#getPages
6802 */
6803 public WearableExtender addPage(Notification page) {
6804 mPages.add(page);
6805 return this;
6806 }
6807
6808 /**
6809 * Add additional pages of content to display with this notification. The current
6810 * notification forms the first page, and pages added using this function form
6811 * subsequent pages. This field can be used to separate a notification into multiple
6812 * sections.
6813 *
6814 * @param pages a list of notifications
6815 * @return this object for method chaining
6816 * @see android.app.Notification.WearableExtender#getPages
6817 */
6818 public WearableExtender addPages(List<Notification> pages) {
6819 mPages.addAll(pages);
6820 return this;
6821 }
6822
6823 /**
6824 * Clear all additional pages present on this builder.
6825 * @return this object for method chaining.
6826 * @see #addPage
6827 */
6828 public WearableExtender clearPages() {
6829 mPages.clear();
6830 return this;
6831 }
6832
6833 /**
6834 * Get the array of additional pages of content for displaying this notification. The
6835 * current notification forms the first page, and elements within this array form
6836 * subsequent pages. This field can be used to separate a notification into multiple
6837 * sections.
6838 * @return the pages for this notification
6839 */
6840 public List<Notification> getPages() {
6841 return mPages;
6842 }
6843
6844 /**
6845 * Set a background image to be displayed behind the notification content.
6846 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
6847 * will work with any notification style.
6848 *
6849 * @param background the background bitmap
6850 * @return this object for method chaining
6851 * @see android.app.Notification.WearableExtender#getBackground
6852 */
6853 public WearableExtender setBackground(Bitmap background) {
6854 mBackground = background;
6855 return this;
6856 }
6857
6858 /**
6859 * Get a background image to be displayed behind the notification content.
6860 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
6861 * will work with any notification style.
6862 *
6863 * @return the background image
6864 * @see android.app.Notification.WearableExtender#setBackground
6865 */
6866 public Bitmap getBackground() {
6867 return mBackground;
6868 }
6869
6870 /**
6871 * Set an icon that goes with the content of this notification.
6872 */
6873 public WearableExtender setContentIcon(int icon) {
6874 mContentIcon = icon;
6875 return this;
6876 }
6877
6878 /**
6879 * Get an icon that goes with the content of this notification.
6880 */
6881 public int getContentIcon() {
6882 return mContentIcon;
6883 }
6884
6885 /**
6886 * Set the gravity that the content icon should have within the notification display.
6887 * Supported values include {@link android.view.Gravity#START} and
6888 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
6889 * @see #setContentIcon
6890 */
6891 public WearableExtender setContentIconGravity(int contentIconGravity) {
6892 mContentIconGravity = contentIconGravity;
6893 return this;
6894 }
6895
6896 /**
6897 * Get the gravity that the content icon should have within the notification display.
6898 * Supported values include {@link android.view.Gravity#START} and
6899 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
6900 * @see #getContentIcon
6901 */
6902 public int getContentIconGravity() {
6903 return mContentIconGravity;
6904 }
6905
6906 /**
6907 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07006908 * this notification. This action will no longer display separately from the
6909 * notification's content.
6910 *
Griff Hazenca48d352014-05-28 22:37:13 -07006911 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07006912 * set, although the list of available actions comes from the main notification and not
6913 * from the child page's notification.
6914 *
6915 * @param actionIndex The index of the action to hoist onto the current notification page.
6916 * If wearable actions were added to the main notification, this index
6917 * will apply to that list, otherwise it will apply to the regular
6918 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07006919 */
6920 public WearableExtender setContentAction(int actionIndex) {
6921 mContentActionIndex = actionIndex;
6922 return this;
6923 }
6924
6925 /**
Griff Hazenca48d352014-05-28 22:37:13 -07006926 * Get the index of the notification action, if any, that was specified as being clickable
6927 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07006928 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07006929 *
Griff Hazenca48d352014-05-28 22:37:13 -07006930 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07006931 * set, although the list of available actions comes from the main notification and not
6932 * from the child page's notification.
6933 *
6934 * <p>If wearable specific actions were added to the main notification, this index will
6935 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07006936 *
6937 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07006938 */
6939 public int getContentAction() {
6940 return mContentActionIndex;
6941 }
6942
6943 /**
6944 * Set the gravity that this notification should have within the available viewport space.
6945 * Supported values include {@link android.view.Gravity#TOP},
6946 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
6947 * The default value is {@link android.view.Gravity#BOTTOM}.
6948 */
6949 public WearableExtender setGravity(int gravity) {
6950 mGravity = gravity;
6951 return this;
6952 }
6953
6954 /**
6955 * Get the gravity that this notification should have within the available viewport space.
6956 * Supported values include {@link android.view.Gravity#TOP},
6957 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
6958 * The default value is {@link android.view.Gravity#BOTTOM}.
6959 */
6960 public int getGravity() {
6961 return mGravity;
6962 }
6963
6964 /**
6965 * Set the custom size preset for the display of this notification out of the available
6966 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
6967 * {@link #SIZE_LARGE}.
6968 * <p>Some custom size presets are only applicable for custom display notifications created
6969 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
6970 * documentation for the preset in question. See also
6971 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
6972 */
6973 public WearableExtender setCustomSizePreset(int sizePreset) {
6974 mCustomSizePreset = sizePreset;
6975 return this;
6976 }
6977
6978 /**
6979 * Get the custom size preset for the display of this notification out of the available
6980 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
6981 * {@link #SIZE_LARGE}.
6982 * <p>Some custom size presets are only applicable for custom display notifications created
6983 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
6984 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
6985 */
6986 public int getCustomSizePreset() {
6987 return mCustomSizePreset;
6988 }
6989
6990 /**
6991 * Set the custom height in pixels for the display of this notification's content.
6992 * <p>This option is only available for custom display notifications created
6993 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
6994 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
6995 * {@link #getCustomContentHeight}.
6996 */
6997 public WearableExtender setCustomContentHeight(int height) {
6998 mCustomContentHeight = height;
6999 return this;
7000 }
7001
7002 /**
7003 * Get the custom height in pixels for the display of this notification's content.
7004 * <p>This option is only available for custom display notifications created
7005 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
7006 * {@link #setCustomContentHeight}.
7007 */
7008 public int getCustomContentHeight() {
7009 return mCustomContentHeight;
7010 }
7011
7012 /**
7013 * Set whether the scrolling position for the contents of this notification should start
7014 * at the bottom of the contents instead of the top when the contents are too long to
7015 * display within the screen. Default is false (start scroll at the top).
7016 */
7017 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
7018 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
7019 return this;
7020 }
7021
7022 /**
7023 * Get whether the scrolling position for the contents of this notification should start
7024 * at the bottom of the contents instead of the top when the contents are too long to
7025 * display within the screen. Default is false (start scroll at the top).
7026 */
7027 public boolean getStartScrollBottom() {
7028 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
7029 }
7030
7031 /**
7032 * Set whether the content intent is available when the wearable device is not connected
7033 * to a companion device. The user can still trigger this intent when the wearable device
7034 * is offline, but a visual hint will indicate that the content intent may not be available.
7035 * Defaults to true.
7036 */
7037 public WearableExtender setContentIntentAvailableOffline(
7038 boolean contentIntentAvailableOffline) {
7039 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
7040 return this;
7041 }
7042
7043 /**
7044 * Get whether the content intent is available when the wearable device is not connected
7045 * to a companion device. The user can still trigger this intent when the wearable device
7046 * is offline, but a visual hint will indicate that the content intent may not be available.
7047 * Defaults to true.
7048 */
7049 public boolean getContentIntentAvailableOffline() {
7050 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
7051 }
7052
7053 /**
7054 * Set a hint that this notification's icon should not be displayed.
7055 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
7056 * @return this object for method chaining
7057 */
7058 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
7059 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
7060 return this;
7061 }
7062
7063 /**
7064 * Get a hint that this notification's icon should not be displayed.
7065 * @return {@code true} if this icon should not be displayed, false otherwise.
7066 * The default value is {@code false} if this was never set.
7067 */
7068 public boolean getHintHideIcon() {
7069 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
7070 }
7071
7072 /**
7073 * Set a visual hint that only the background image of this notification should be
7074 * displayed, and other semantic content should be hidden. This hint is only applicable
7075 * to sub-pages added using {@link #addPage}.
7076 */
7077 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
7078 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
7079 return this;
7080 }
7081
7082 /**
7083 * Get a visual hint that only the background image of this notification should be
7084 * displayed, and other semantic content should be hidden. This hint is only applicable
7085 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
7086 */
7087 public boolean getHintShowBackgroundOnly() {
7088 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
7089 }
7090
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007091 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007092 * Set a hint that this notification's background should not be clipped if possible,
7093 * and should instead be resized to fully display on the screen, retaining the aspect
7094 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007095 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
7096 * @return this object for method chaining
7097 */
7098 public WearableExtender setHintAvoidBackgroundClipping(
7099 boolean hintAvoidBackgroundClipping) {
7100 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
7101 return this;
7102 }
7103
7104 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007105 * Get a hint that this notification's background should not be clipped if possible,
7106 * and should instead be resized to fully display on the screen, retaining the aspect
7107 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007108 * @return {@code true} if it's ok if the background is clipped on the screen, false
7109 * otherwise. The default value is {@code false} if this was never set.
7110 */
7111 public boolean getHintAvoidBackgroundClipping() {
7112 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
7113 }
7114
7115 /**
7116 * Set a hint that the screen should remain on for at least this duration when
7117 * this notification is displayed on the screen.
7118 * @param timeout The requested screen timeout in milliseconds. Can also be either
7119 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7120 * @return this object for method chaining
7121 */
7122 public WearableExtender setHintScreenTimeout(int timeout) {
7123 mHintScreenTimeout = timeout;
7124 return this;
7125 }
7126
7127 /**
7128 * Get the duration, in milliseconds, that the screen should remain on for
7129 * when this notification is displayed.
7130 * @return the duration in milliseconds if > 0, or either one of the sentinel values
7131 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7132 */
7133 public int getHintScreenTimeout() {
7134 return mHintScreenTimeout;
7135 }
7136
Alex Hills9ab3a232016-04-05 14:54:56 -04007137 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04007138 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
7139 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7140 * qr codes, as well as other simple black-and-white tickets.
7141 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
7142 * @return this object for method chaining
7143 */
7144 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
7145 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
7146 return this;
7147 }
7148
7149 /**
7150 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
7151 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7152 * qr codes, as well as other simple black-and-white tickets.
7153 * @return {@code true} if it should be displayed in ambient, false otherwise
7154 * otherwise. The default value is {@code false} if this was never set.
7155 */
7156 public boolean getHintAmbientBigPicture() {
7157 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
7158 }
7159
7160 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04007161 * Set a hint that this notification's content intent will launch an {@link Activity}
7162 * directly, telling the platform that it can generate the appropriate transitions.
7163 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
7164 * an activity and transitions should be generated, false otherwise.
7165 * @return this object for method chaining
7166 */
7167 public WearableExtender setHintContentIntentLaunchesActivity(
7168 boolean hintContentIntentLaunchesActivity) {
7169 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
7170 return this;
7171 }
7172
7173 /**
7174 * Get a hint that this notification's content intent will launch an {@link Activity}
7175 * directly, telling the platform that it can generate the appropriate transitions
7176 * @return {@code true} if the content intent will launch an activity and transitions should
7177 * be generated, false otherwise. The default value is {@code false} if this was never set.
7178 */
7179 public boolean getHintContentIntentLaunchesActivity() {
7180 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
7181 }
7182
Nadia Benbernou948627e2016-04-14 14:41:08 -04007183 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007184 * Sets the dismissal id for this notification. If a notification is posted with a
7185 * dismissal id, then when that notification is canceled, notifications on other wearables
7186 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04007187 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007188 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04007189 * @param dismissalId the dismissal id of the notification.
7190 * @return this object for method chaining
7191 */
7192 public WearableExtender setDismissalId(String dismissalId) {
7193 mDismissalId = dismissalId;
7194 return this;
7195 }
7196
7197 /**
7198 * Returns the dismissal id of the notification.
7199 * @return the dismissal id of the notification or null if it has not been set.
7200 */
7201 public String getDismissalId() {
7202 return mDismissalId;
7203 }
7204
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007205 /**
7206 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
7207 * posted from a phone to provide finer-grained control on what notifications are bridged
7208 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
7209 * Features to Notifications</a> for more information.
7210 * @param bridgeTag the bridge tag of the notification.
7211 * @return this object for method chaining
7212 */
7213 public WearableExtender setBridgeTag(String bridgeTag) {
7214 mBridgeTag = bridgeTag;
7215 return this;
7216 }
7217
7218 /**
7219 * Returns the bridge tag of the notification.
7220 * @return the bridge tag or null if not present.
7221 */
7222 public String getBridgeTag() {
7223 return mBridgeTag;
7224 }
7225
Griff Hazen61a9e862014-05-22 16:05:19 -07007226 private void setFlag(int mask, boolean value) {
7227 if (value) {
7228 mFlags |= mask;
7229 } else {
7230 mFlags &= ~mask;
7231 }
7232 }
7233 }
7234
7235 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007236 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
7237 * with car extensions:
7238 *
7239 * <ol>
7240 * <li>Create an {@link Notification.Builder}, setting any desired
7241 * properties.
7242 * <li>Create a {@link CarExtender}.
7243 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
7244 * {@link CarExtender}.
7245 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
7246 * to apply the extensions to a notification.
7247 * </ol>
7248 *
7249 * <pre class="prettyprint">
7250 * Notification notification = new Notification.Builder(context)
7251 * ...
7252 * .extend(new CarExtender()
7253 * .set*(...))
7254 * .build();
7255 * </pre>
7256 *
7257 * <p>Car extensions can be accessed on an existing notification by using the
7258 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
7259 * to access values.
7260 */
7261 public static final class CarExtender implements Extender {
7262 private static final String TAG = "CarExtender";
7263
7264 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
7265 private static final String EXTRA_LARGE_ICON = "large_icon";
7266 private static final String EXTRA_CONVERSATION = "car_conversation";
7267 private static final String EXTRA_COLOR = "app_color";
7268
7269 private Bitmap mLargeIcon;
7270 private UnreadConversation mUnreadConversation;
7271 private int mColor = Notification.COLOR_DEFAULT;
7272
7273 /**
7274 * Create a {@link CarExtender} with default options.
7275 */
7276 public CarExtender() {
7277 }
7278
7279 /**
7280 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
7281 *
7282 * @param notif The notification from which to copy options.
7283 */
7284 public CarExtender(Notification notif) {
7285 Bundle carBundle = notif.extras == null ?
7286 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
7287 if (carBundle != null) {
7288 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
7289 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
7290
7291 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
7292 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
7293 }
7294 }
7295
7296 /**
7297 * Apply car extensions to a notification that is being built. This is typically called by
7298 * the {@link Notification.Builder#extend(Notification.Extender)}
7299 * method of {@link Notification.Builder}.
7300 */
7301 @Override
7302 public Notification.Builder extend(Notification.Builder builder) {
7303 Bundle carExtensions = new Bundle();
7304
7305 if (mLargeIcon != null) {
7306 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
7307 }
7308 if (mColor != Notification.COLOR_DEFAULT) {
7309 carExtensions.putInt(EXTRA_COLOR, mColor);
7310 }
7311
7312 if (mUnreadConversation != null) {
7313 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
7314 carExtensions.putBundle(EXTRA_CONVERSATION, b);
7315 }
7316
7317 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
7318 return builder;
7319 }
7320
7321 /**
7322 * Sets the accent color to use when Android Auto presents the notification.
7323 *
7324 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
7325 * to accent the displayed notification. However, not all colors are acceptable in an
7326 * automotive setting. This method can be used to override the color provided in the
7327 * notification in such a situation.
7328 */
Tor Norbye80756e32015-03-02 09:39:27 -08007329 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007330 mColor = color;
7331 return this;
7332 }
7333
7334 /**
7335 * Gets the accent color.
7336 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007337 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007338 */
Tor Norbye80756e32015-03-02 09:39:27 -08007339 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007340 public int getColor() {
7341 return mColor;
7342 }
7343
7344 /**
7345 * Sets the large icon of the car notification.
7346 *
7347 * If no large icon is set in the extender, Android Auto will display the icon
7348 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
7349 *
7350 * @param largeIcon The large icon to use in the car notification.
7351 * @return This object for method chaining.
7352 */
7353 public CarExtender setLargeIcon(Bitmap largeIcon) {
7354 mLargeIcon = largeIcon;
7355 return this;
7356 }
7357
7358 /**
7359 * Gets the large icon used in this car notification, or null if no icon has been set.
7360 *
7361 * @return The large icon for the car notification.
7362 * @see CarExtender#setLargeIcon
7363 */
7364 public Bitmap getLargeIcon() {
7365 return mLargeIcon;
7366 }
7367
7368 /**
7369 * Sets the unread conversation in a message notification.
7370 *
7371 * @param unreadConversation The unread part of the conversation this notification conveys.
7372 * @return This object for method chaining.
7373 */
7374 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
7375 mUnreadConversation = unreadConversation;
7376 return this;
7377 }
7378
7379 /**
7380 * Returns the unread conversation conveyed by this notification.
7381 * @see #setUnreadConversation(UnreadConversation)
7382 */
7383 public UnreadConversation getUnreadConversation() {
7384 return mUnreadConversation;
7385 }
7386
7387 /**
7388 * A class which holds the unread messages from a conversation.
7389 */
7390 public static class UnreadConversation {
7391 private static final String KEY_AUTHOR = "author";
7392 private static final String KEY_TEXT = "text";
7393 private static final String KEY_MESSAGES = "messages";
7394 private static final String KEY_REMOTE_INPUT = "remote_input";
7395 private static final String KEY_ON_REPLY = "on_reply";
7396 private static final String KEY_ON_READ = "on_read";
7397 private static final String KEY_PARTICIPANTS = "participants";
7398 private static final String KEY_TIMESTAMP = "timestamp";
7399
7400 private final String[] mMessages;
7401 private final RemoteInput mRemoteInput;
7402 private final PendingIntent mReplyPendingIntent;
7403 private final PendingIntent mReadPendingIntent;
7404 private final String[] mParticipants;
7405 private final long mLatestTimestamp;
7406
7407 UnreadConversation(String[] messages, RemoteInput remoteInput,
7408 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
7409 String[] participants, long latestTimestamp) {
7410 mMessages = messages;
7411 mRemoteInput = remoteInput;
7412 mReadPendingIntent = readPendingIntent;
7413 mReplyPendingIntent = replyPendingIntent;
7414 mParticipants = participants;
7415 mLatestTimestamp = latestTimestamp;
7416 }
7417
7418 /**
7419 * Gets the list of messages conveyed by this notification.
7420 */
7421 public String[] getMessages() {
7422 return mMessages;
7423 }
7424
7425 /**
7426 * Gets the remote input that will be used to convey the response to a message list, or
7427 * null if no such remote input exists.
7428 */
7429 public RemoteInput getRemoteInput() {
7430 return mRemoteInput;
7431 }
7432
7433 /**
7434 * Gets the pending intent that will be triggered when the user replies to this
7435 * notification.
7436 */
7437 public PendingIntent getReplyPendingIntent() {
7438 return mReplyPendingIntent;
7439 }
7440
7441 /**
7442 * Gets the pending intent that Android Auto will send after it reads aloud all messages
7443 * in this object's message list.
7444 */
7445 public PendingIntent getReadPendingIntent() {
7446 return mReadPendingIntent;
7447 }
7448
7449 /**
7450 * Gets the participants in the conversation.
7451 */
7452 public String[] getParticipants() {
7453 return mParticipants;
7454 }
7455
7456 /**
7457 * Gets the firs participant in the conversation.
7458 */
7459 public String getParticipant() {
7460 return mParticipants.length > 0 ? mParticipants[0] : null;
7461 }
7462
7463 /**
7464 * Gets the timestamp of the conversation.
7465 */
7466 public long getLatestTimestamp() {
7467 return mLatestTimestamp;
7468 }
7469
7470 Bundle getBundleForUnreadConversation() {
7471 Bundle b = new Bundle();
7472 String author = null;
7473 if (mParticipants != null && mParticipants.length > 1) {
7474 author = mParticipants[0];
7475 }
7476 Parcelable[] messages = new Parcelable[mMessages.length];
7477 for (int i = 0; i < messages.length; i++) {
7478 Bundle m = new Bundle();
7479 m.putString(KEY_TEXT, mMessages[i]);
7480 m.putString(KEY_AUTHOR, author);
7481 messages[i] = m;
7482 }
7483 b.putParcelableArray(KEY_MESSAGES, messages);
7484 if (mRemoteInput != null) {
7485 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
7486 }
7487 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
7488 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
7489 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
7490 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
7491 return b;
7492 }
7493
7494 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
7495 if (b == null) {
7496 return null;
7497 }
7498 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
7499 String[] messages = null;
7500 if (parcelableMessages != null) {
7501 String[] tmp = new String[parcelableMessages.length];
7502 boolean success = true;
7503 for (int i = 0; i < tmp.length; i++) {
7504 if (!(parcelableMessages[i] instanceof Bundle)) {
7505 success = false;
7506 break;
7507 }
7508 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
7509 if (tmp[i] == null) {
7510 success = false;
7511 break;
7512 }
7513 }
7514 if (success) {
7515 messages = tmp;
7516 } else {
7517 return null;
7518 }
7519 }
7520
7521 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
7522 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
7523
7524 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
7525
7526 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
7527 if (participants == null || participants.length != 1) {
7528 return null;
7529 }
7530
7531 return new UnreadConversation(messages,
7532 remoteInput,
7533 onReply,
7534 onRead,
7535 participants, b.getLong(KEY_TIMESTAMP));
7536 }
7537 };
7538
7539 /**
7540 * Builder class for {@link CarExtender.UnreadConversation} objects.
7541 */
7542 public static class Builder {
7543 private final List<String> mMessages = new ArrayList<String>();
7544 private final String mParticipant;
7545 private RemoteInput mRemoteInput;
7546 private PendingIntent mReadPendingIntent;
7547 private PendingIntent mReplyPendingIntent;
7548 private long mLatestTimestamp;
7549
7550 /**
7551 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
7552 *
7553 * @param name The name of the other participant in the conversation.
7554 */
7555 public Builder(String name) {
7556 mParticipant = name;
7557 }
7558
7559 /**
7560 * Appends a new unread message to the list of messages for this conversation.
7561 *
7562 * The messages should be added from oldest to newest.
7563 *
7564 * @param message The text of the new unread message.
7565 * @return This object for method chaining.
7566 */
7567 public Builder addMessage(String message) {
7568 mMessages.add(message);
7569 return this;
7570 }
7571
7572 /**
7573 * Sets the pending intent and remote input which will convey the reply to this
7574 * notification.
7575 *
7576 * @param pendingIntent The pending intent which will be triggered on a reply.
7577 * @param remoteInput The remote input parcelable which will carry the reply.
7578 * @return This object for method chaining.
7579 *
7580 * @see CarExtender.UnreadConversation#getRemoteInput
7581 * @see CarExtender.UnreadConversation#getReplyPendingIntent
7582 */
7583 public Builder setReplyAction(
7584 PendingIntent pendingIntent, RemoteInput remoteInput) {
7585 mRemoteInput = remoteInput;
7586 mReplyPendingIntent = pendingIntent;
7587
7588 return this;
7589 }
7590
7591 /**
7592 * Sets the pending intent that will be sent once the messages in this notification
7593 * are read.
7594 *
7595 * @param pendingIntent The pending intent to use.
7596 * @return This object for method chaining.
7597 */
7598 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
7599 mReadPendingIntent = pendingIntent;
7600 return this;
7601 }
7602
7603 /**
7604 * Sets the timestamp of the most recent message in an unread conversation.
7605 *
7606 * If a messaging notification has been posted by your application and has not
7607 * yet been cancelled, posting a later notification with the same id and tag
7608 * but without a newer timestamp may result in Android Auto not displaying a
7609 * heads up notification for the later notification.
7610 *
7611 * @param timestamp The timestamp of the most recent message in the conversation.
7612 * @return This object for method chaining.
7613 */
7614 public Builder setLatestTimestamp(long timestamp) {
7615 mLatestTimestamp = timestamp;
7616 return this;
7617 }
7618
7619 /**
7620 * Builds a new unread conversation object.
7621 *
7622 * @return The new unread conversation object.
7623 */
7624 public UnreadConversation build() {
7625 String[] messages = mMessages.toArray(new String[mMessages.size()]);
7626 String[] participants = { mParticipant };
7627 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
7628 mReadPendingIntent, participants, mLatestTimestamp);
7629 }
7630 }
7631 }
7632
7633 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007634 * <p>Helper class to add Android TV extensions to notifications. To create a notification
7635 * with a TV extension:
7636 *
7637 * <ol>
7638 * <li>Create an {@link Notification.Builder}, setting any desired properties.
7639 * <li>Create a {@link TvExtender}.
7640 * <li>Set TV-specific properties using the {@code set} methods of
7641 * {@link TvExtender}.
7642 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
7643 * to apply the extension to a notification.
7644 * </ol>
7645 *
7646 * <pre class="prettyprint">
7647 * Notification notification = new Notification.Builder(context)
7648 * ...
7649 * .extend(new TvExtender()
7650 * .set*(...))
7651 * .build();
7652 * </pre>
7653 *
7654 * <p>TV extensions can be accessed on an existing notification by using the
7655 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
7656 * to access values.
7657 *
7658 * @hide
7659 */
7660 @SystemApi
7661 public static final class TvExtender implements Extender {
7662 private static final String TAG = "TvExtender";
7663
7664 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
7665 private static final String EXTRA_FLAGS = "flags";
7666 private static final String EXTRA_CONTENT_INTENT = "content_intent";
7667 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007668 private static final String EXTRA_CHANNEL_ID = "channel_id";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007669
7670 // Flags bitwise-ored to mFlags
7671 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
7672
7673 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007674 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007675 private PendingIntent mContentIntent;
7676 private PendingIntent mDeleteIntent;
7677
7678 /**
7679 * Create a {@link TvExtender} with default options.
7680 */
7681 public TvExtender() {
7682 mFlags = FLAG_AVAILABLE_ON_TV;
7683 }
7684
7685 /**
7686 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
7687 *
7688 * @param notif The notification from which to copy options.
7689 */
7690 public TvExtender(Notification notif) {
7691 Bundle bundle = notif.extras == null ?
7692 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
7693 if (bundle != null) {
7694 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007695 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007696 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
7697 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
7698 }
7699 }
7700
7701 /**
7702 * Apply a TV extension to a notification that is being built. This is typically called by
7703 * the {@link Notification.Builder#extend(Notification.Extender)}
7704 * method of {@link Notification.Builder}.
7705 */
7706 @Override
7707 public Notification.Builder extend(Notification.Builder builder) {
7708 Bundle bundle = new Bundle();
7709
7710 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007711 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007712 if (mContentIntent != null) {
7713 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
7714 }
7715
7716 if (mDeleteIntent != null) {
7717 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
7718 }
7719
7720 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
7721 return builder;
7722 }
7723
7724 /**
7725 * Returns true if this notification should be shown on TV. This method return true
7726 * if the notification was extended with a TvExtender.
7727 */
7728 public boolean isAvailableOnTv() {
7729 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
7730 }
7731
7732 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007733 * Specifies the channel the notification should be delivered on when shown on TV.
7734 * It can be different from the channel that the notification is delivered to when
7735 * posting on a non-TV device.
7736 */
7737 public TvExtender setChannel(String channelId) {
7738 mChannelId = channelId;
7739 return this;
7740 }
7741
7742 /**
7743 * Returns the id of the channel this notification posts to on TV.
7744 */
7745 public String getChannel() {
7746 return mChannelId;
7747 }
7748
7749 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007750 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
7751 * If provided, it is used instead of the content intent specified
7752 * at the level of Notification.
7753 */
7754 public TvExtender setContentIntent(PendingIntent intent) {
7755 mContentIntent = intent;
7756 return this;
7757 }
7758
7759 /**
7760 * Returns the TV-specific content intent. If this method returns null, the
7761 * main content intent on the notification should be used.
7762 *
7763 * @see {@link Notification#contentIntent}
7764 */
7765 public PendingIntent getContentIntent() {
7766 return mContentIntent;
7767 }
7768
7769 /**
7770 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
7771 * by the user on TV. If provided, it is used instead of the delete intent specified
7772 * at the level of Notification.
7773 */
7774 public TvExtender setDeleteIntent(PendingIntent intent) {
7775 mDeleteIntent = intent;
7776 return this;
7777 }
7778
7779 /**
7780 * Returns the TV-specific delete intent. If this method returns null, the
7781 * main delete intent on the notification should be used.
7782 *
7783 * @see {@link Notification#deleteIntent}
7784 */
7785 public PendingIntent getDeleteIntent() {
7786 return mDeleteIntent;
7787 }
7788 }
7789
7790 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07007791 * Get an array of Notification objects from a parcelable array bundle field.
7792 * Update the bundle to have a typed array so fetches in the future don't need
7793 * to do an array copy.
7794 */
7795 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
7796 Parcelable[] array = bundle.getParcelableArray(key);
7797 if (array instanceof Notification[] || array == null) {
7798 return (Notification[]) array;
7799 }
7800 Notification[] typedArray = Arrays.copyOf(array, array.length,
7801 Notification[].class);
7802 bundle.putParcelableArray(key, typedArray);
7803 return typedArray;
7804 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02007805
7806 private static class BuilderRemoteViews extends RemoteViews {
7807 public BuilderRemoteViews(Parcel parcel) {
7808 super(parcel);
7809 }
7810
Kenny Guy77320062014-08-27 21:37:15 +01007811 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
7812 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007813 }
7814
7815 @Override
7816 public BuilderRemoteViews clone() {
7817 Parcel p = Parcel.obtain();
7818 writeToParcel(p, 0);
7819 p.setDataPosition(0);
7820 BuilderRemoteViews brv = new BuilderRemoteViews(p);
7821 p.recycle();
7822 return brv;
7823 }
7824 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08007825
7826 private static class StandardTemplateParams {
7827 boolean hasProgress = true;
7828 boolean ambient = false;
7829 CharSequence title;
7830 CharSequence text;
7831
7832 final StandardTemplateParams reset() {
7833 hasProgress = true;
7834 ambient = false;
7835 title = null;
7836 text = null;
7837 return this;
7838 }
7839
7840 final StandardTemplateParams hasProgress(boolean hasProgress) {
7841 this.hasProgress = hasProgress;
7842 return this;
7843 }
7844
7845 final StandardTemplateParams title(CharSequence title) {
7846 this.title = title;
7847 return this;
7848 }
7849
7850 final StandardTemplateParams text(CharSequence text) {
7851 this.text = text;
7852 return this;
7853 }
7854
7855 final StandardTemplateParams ambient(boolean ambient) {
7856 this.ambient = ambient;
7857 return this;
7858 }
7859
7860 final StandardTemplateParams fillTextsFrom(Builder b) {
7861 Bundle extras = b.mN.extras;
7862 title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE));
7863 text = b.processLegacyText(extras.getCharSequence(EXTRA_TEXT));
7864 return this;
7865 }
7866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007867}