blob: 92510eb3fe59d57fb30700543c59f61029b2a705 [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);
3687 mActionBarColor = NotificationColorUtil.resolveActionBarColor(backgroundColor);
3688 }
3689 }
3690
3691 private void updateBackgroundColor(RemoteViews contentView) {
3692 if (isColorized()) {
3693 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
3694 getBackgroundColor());
3695 } else {
3696 // Clear it!
3697 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
3698 0);
3699 }
3700 }
3701
Selim Cinek860b6da2015-12-16 19:02:19 -08003702 /**
3703 * @param remoteView the remote view to update the minheight in
3704 * @param hasMinHeight does it have a mimHeight
3705 * @hide
3706 */
3707 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3708 int minHeight = 0;
3709 if (hasMinHeight) {
3710 // we need to set the minHeight of the notification
3711 minHeight = mContext.getResources().getDimensionPixelSize(
3712 com.android.internal.R.dimen.notification_min_content_height);
3713 }
3714 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3715 }
3716
Selim Cinek29603462015-11-17 19:04:39 -08003717 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003718 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3719 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3720 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3721 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003722 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003723 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003724 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003725 contentView.setProgressBackgroundTintList(
3726 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3727 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003728 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003729 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003730 contentView.setProgressTintList(R.id.progress, colorStateList);
3731 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003732 }
Selim Cinek29603462015-11-17 19:04:39 -08003733 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003734 } else {
3735 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003736 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003737 }
Joe Onorato561d3852010-11-20 18:09:34 -08003738 }
3739
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003740 private void bindLargeIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07003741 if (mN.mLargeIcon == null && mN.largeIcon != null) {
3742 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
3743 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003744 if (mN.mLargeIcon != null) {
3745 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3746 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3747 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003748 int endMargin = R.dimen.notification_content_picture_margin;
3749 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
3750 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
3751 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003752 }
3753 }
3754
Adrian Roos487374f2017-01-11 15:48:14 -08003755 private void bindNotificationHeader(RemoteViews contentView, boolean ambient) {
3756 bindSmallIcon(contentView, ambient);
3757 bindHeaderAppName(contentView, ambient);
3758 if (!ambient) {
3759 // Ambient view does not have these
3760 bindHeaderText(contentView);
3761 bindHeaderChronometerAndTime(contentView);
3762 bindExpandButton(contentView);
3763 bindProfileBadge(contentView);
3764 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003765 }
3766
3767 private void bindExpandButton(RemoteViews contentView) {
Selim Cinek99104832017-01-25 14:47:33 -08003768 int color = getPrimaryHighlightColor();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003769 contentView.setDrawableParameters(R.id.expand_button, false, -1, color,
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003770 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003771 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08003772 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003773 }
3774
Selim Cinek99104832017-01-25 14:47:33 -08003775 /**
3776 * @return the color that is used as the first primary highlight color. This is applied
3777 * in several places like the action buttons or the app name in the header.
3778 */
3779 private int getPrimaryHighlightColor() {
3780 return isColorized() ? getPrimaryTextColor() : resolveContrastColor();
3781 }
3782
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003783 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3784 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003785 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003786 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003787 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3788 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3789 contentView.setLong(R.id.chronometer, "setBase",
3790 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3791 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07003792 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07003793 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003794 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003795 } else {
3796 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3797 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003798 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003799 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003800 } else {
3801 // We still want a time to be set but gone, such that we can show and hide it
3802 // on demand in case it's a child notification without anything in the header
3803 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003804 }
3805 }
3806
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003807 private void bindHeaderText(RemoteViews contentView) {
3808 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3809 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05003810 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003811 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05003812 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003813 if (headerText == null
3814 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3815 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3816 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
3817 }
3818 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003819 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003820 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08003821 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003822 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
3823 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003824 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003825 }
3826 }
3827
Adrian Rooseba05822016-04-22 17:09:27 -07003828 /**
3829 * @hide
3830 */
3831 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04003832 CharSequence name = null;
3833 final PackageManager pm = mContext.getPackageManager();
3834 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
3835 // only system packages which lump together a bunch of unrelated stuff
3836 // may substitute a different name to make the purpose of the
3837 // notification more clear. the correct package label should always
3838 // be accessible via SystemUI.
3839 final String pkg = mContext.getPackageName();
3840 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
3841 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
3842 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
3843 name = subName;
3844 } else {
3845 Log.w(TAG, "warning: pkg "
3846 + pkg + " attempting to substitute app name '" + subName
3847 + "' without holding perm "
3848 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
3849 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003850 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04003851 if (TextUtils.isEmpty(name)) {
3852 name = pm.getApplicationLabel(mContext.getApplicationInfo());
3853 }
3854 if (TextUtils.isEmpty(name)) {
3855 // still nothing?
3856 return null;
3857 }
3858
3859 return String.valueOf(name);
3860 }
Adrian Roos487374f2017-01-11 15:48:14 -08003861 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04003862 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08003863 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08003864 setTextViewColorPrimary(contentView, R.id.app_name_text);
3865 } else {
3866 contentView.setTextColor(R.id.app_name_text,
3867 ambient ? resolveAmbientColor() : resolveContrastColor());
3868 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003869 }
3870
Adrian Roos487374f2017-01-11 15:48:14 -08003871 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07003872 if (mN.mSmallIcon == null && mN.icon != 0) {
3873 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
3874 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003875 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Adrian Roos9b45a152016-06-28 13:32:29 -07003876 contentView.setDrawableParameters(R.id.icon, false /* targetBackground */,
3877 -1 /* alpha */, -1 /* colorFilter */, null /* mode */, mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08003878 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003879 }
3880
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003881 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003882 * @return true if the built notification will show the time or the chronometer; false
3883 * otherwise
3884 */
3885 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07003886 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003887 }
3888
Christoph Studerfe718432014-09-01 18:21:18 +02003889 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003890 // actions_container is only reset when there are no actions to avoid focus issues with
3891 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02003892 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003893 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003894
3895 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3896 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3897
3898 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3899 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3900 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3901 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07003902
3903 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003904 }
3905
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003906 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003907 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07003908 }
3909
Adrian Roos70d7aa32017-01-11 15:39:06 -08003910 private RemoteViews applyStandardTemplateWithActions(int layoutId,
3911 StandardTemplateParams p) {
3912 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003913
Christoph Studerfe718432014-09-01 18:21:18 +02003914 resetStandardTemplateWithActions(big);
3915
Adrian Roose458aa82015-12-08 16:17:19 -08003916 boolean validRemoteInput = false;
3917
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003918 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08003919 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003920 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003921 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08003922 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003923 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roos487374f2017-01-11 15:48:14 -08003924 if (p.ambient) {
3925 big.setInt(R.id.actions, "setBackgroundColor", Color.TRANSPARENT);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003926 } else if (isColorized()) {
3927 big.setInt(R.id.actions, "setBackgroundColor", getActionBarColor());
3928 } else {
3929 big.setInt(R.id.actions, "setBackgroundColor", mContext.getColor(
3930 R.color.notification_action_list));
Adrian Roos487374f2017-01-11 15:48:14 -08003931 }
Adrian Roosf852a422016-06-03 13:33:43 -07003932 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
3933 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003934 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003935 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003936 Action action = mActions.get(i);
3937 validRemoteInput |= hasValidRemoteInput(action);
3938
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003939 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08003940 i % 2 != 0, p.ambient);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003941 big.addView(R.id.actions, button);
3942 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003943 } else {
3944 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003945 }
Adrian Roose458aa82015-12-08 16:17:19 -08003946
3947 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08003948 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08003949 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3950 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3951 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003952 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08003953
3954 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3955 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3956 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003957 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08003958
3959 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3960 big.setViewVisibility(
3961 R.id.notification_material_reply_text_3, View.VISIBLE);
3962 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003963 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08003964 }
3965 }
3966 }
3967
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003968 return big;
3969 }
3970
Adrian Roose458aa82015-12-08 16:17:19 -08003971 private boolean hasValidRemoteInput(Action action) {
3972 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
3973 // Weird actions
3974 return false;
3975 }
3976
3977 RemoteInput[] remoteInputs = action.getRemoteInputs();
3978 if (remoteInputs == null) {
3979 return false;
3980 }
3981
3982 for (RemoteInput r : remoteInputs) {
3983 CharSequence[] choices = r.getChoices();
3984 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
3985 return true;
3986 }
3987 }
3988 return false;
3989 }
3990
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003991 /**
3992 * Construct a RemoteViews for the final 1U notification layout. In order:
3993 * 1. Custom contentView from the caller
3994 * 2. Style's proposed content view
3995 * 3. Standard template view
3996 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003997 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08003998 return createContentView(false /* increasedheight */ );
3999 }
4000
4001 /**
4002 * Construct a RemoteViews for the smaller content view.
4003 *
4004 * @param increasedHeight true if this layout be created with an increased height. Some
4005 * styles may support showing more then just that basic 1U size
4006 * and the system may decide to render important notifications
4007 * slightly bigger even when collapsed.
4008 *
4009 * @hide
4010 */
4011 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08004012 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004013 return mN.contentView;
4014 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004015 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004016 if (styleView != null) {
4017 return styleView;
4018 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004019 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004020 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004021 }
4022
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004023 /**
4024 * Construct a RemoteViews for the final big notification layout.
4025 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004026 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004027 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08004028 if (mN.bigContentView != null
4029 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004030 return mN.bigContentView;
4031 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004032 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004033 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004034 } else if (mActions.size() != 0) {
4035 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004036 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004037 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004038 return result;
4039 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004040
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004041 /**
4042 * Construct a RemoteViews for the final notification header only
4043 *
4044 * @hide
4045 */
4046 public RemoteViews makeNotificationHeader() {
4047 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
4048 R.layout.notification_template_header);
4049 resetNotificationHeader(header);
Adrian Roos487374f2017-01-11 15:48:14 -08004050 bindNotificationHeader(header, false /* ambient */);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004051 return header;
4052 }
4053
Adrian Roos487374f2017-01-11 15:48:14 -08004054 /**
4055 * Construct a RemoteViews for the ambient version of the notification.
4056 *
4057 * @hide
4058 */
4059 public RemoteViews makeAmbientNotification() {
4060 RemoteViews ambient = applyStandardTemplateWithActions(
4061 R.layout.notification_template_material_ambient,
4062 mParams.reset().fillTextsFrom(this).hasProgress(false).ambient(true));
4063 return ambient;
4064 }
4065
Selim Cinek29603462015-11-17 19:04:39 -08004066 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004067 if (result != null) {
4068 result.setViewVisibility(R.id.text_line_1, View.GONE);
4069 }
Selim Cinek29603462015-11-17 19:04:39 -08004070 }
4071
Selim Cinek6743c0b2017-01-18 18:24:01 -08004072 /**
4073 * Adapt the Notification header if this view is used as an expanded view.
4074 *
4075 * @hide
4076 */
4077 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004078 if (result != null) {
4079 result.setBoolean(R.id.notification_header, "setExpanded", true);
4080 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004081 }
4082
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004083 /**
4084 * Construct a RemoteViews for the final heads-up notification layout.
4085 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004086 public RemoteViews createHeadsUpContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08004087 if (mN.headsUpContentView != null
4088 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004089 return mN.headsUpContentView;
4090 } else if (mStyle != null) {
4091 final RemoteViews styleView = mStyle.makeHeadsUpContentView();
4092 if (styleView != null) {
4093 return styleView;
4094 }
4095 } else if (mActions.size() == 0) {
4096 return null;
4097 }
4098
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004099 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004100 }
4101
Selim Cinek624c02db2015-12-14 21:00:02 -08004102 /**
4103 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4104 *
4105 * @hide
4106 */
4107 public RemoteViews makePublicContentView() {
4108 if (mN.publicVersion != null) {
4109 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Julia Reynolds3b848122016-02-26 10:45:32 -05004110 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004111 }
4112 Bundle savedBundle = mN.extras;
4113 Style style = mStyle;
4114 mStyle = null;
4115 Icon largeIcon = mN.mLargeIcon;
4116 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004117 Bitmap largeIconLegacy = mN.largeIcon;
4118 mN.largeIcon = null;
Selim Cinek624c02db2015-12-14 21:00:02 -08004119 Bundle publicExtras = new Bundle();
4120 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
4121 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
4122 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
4123 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07004124 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
4125 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08004126 publicExtras.putCharSequence(EXTRA_TITLE,
4127 mContext.getString(R.string.notification_hidden_text));
4128 mN.extras = publicExtras;
4129 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
4130 mN.extras = savedBundle;
4131 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07004132 mN.largeIcon = largeIconLegacy;
Selim Cinek624c02db2015-12-14 21:00:02 -08004133 mStyle = style;
4134 return publicView;
4135 }
4136
Selim Cinek6743c0b2017-01-18 18:24:01 -08004137 /**
4138 * Construct a content view for the display when low - priority
4139 *
4140 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
4141 * a new subtext is created consisting of the content of the
4142 * notification.
4143 * @hide
4144 */
4145 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
4146 int color = mN.color;
4147 mN.color = COLOR_DEFAULT;
4148 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4149 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
4150 CharSequence newSummary = createSummaryText();
4151 if (!TextUtils.isEmpty(newSummary)) {
4152 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
4153 }
4154 }
4155 RemoteViews header = makeNotificationHeader();
4156 if (summary != null) {
4157 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
4158 } else {
4159 mN.extras.remove(EXTRA_SUB_TEXT);
4160 }
4161 mN.color = color;
4162 return header;
4163 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004164
Selim Cinek6743c0b2017-01-18 18:24:01 -08004165 private CharSequence createSummaryText() {
4166 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
4167 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
4168 return titleText;
4169 }
4170 SpannableStringBuilder summary = new SpannableStringBuilder();
4171 if (titleText == null) {
4172 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
4173 }
4174 BidiFormatter bidi = BidiFormatter.getInstance();
4175 if (titleText != null) {
4176 summary.append(bidi.unicodeWrap(titleText));
4177 }
4178 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
4179 if (titleText != null && contentText != null) {
4180 summary.append(bidi.unicodeWrap(mContext.getText(
4181 R.string.notification_header_divider_symbol_with_spaces)));
4182 }
4183 if (contentText != null) {
4184 summary.append(bidi.unicodeWrap(contentText));
4185 }
4186 return summary;
4187 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05004188
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004189 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004190 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04004191 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004192 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004193 emphazisedMode ? getEmphasizedActionLayoutResource()
4194 : tombstone ? getActionTombstoneLayoutResource()
4195 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04004196 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04004197 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04004198 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004199 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004200 if (action.mRemoteInputs != null) {
4201 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
4202 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004203 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004204 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07004205 // change the background bgColor
4206 int bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
4207 : R.color.notification_action_list_dark);
4208 button.setDrawableParameters(R.id.button_holder, true, -1, bgColor,
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004209 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinek981962e2016-07-20 20:41:58 -07004210 CharSequence title = action.title;
4211 ColorStateList[] outResultColor = null;
4212 if (isLegacy()) {
4213 title = clearColorSpans(title);
4214 } else {
4215 outResultColor = new ColorStateList[1];
4216 title = ensureColorSpanContrast(title, bgColor, outResultColor);
4217 }
4218 button.setTextViewText(R.id.action0, title);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004219 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07004220 if (outResultColor != null && outResultColor[0] != null) {
4221 // We need to set the text color as well since changing a text to uppercase
4222 // clears its spans.
4223 button.setTextColor(R.id.action0, outResultColor[0]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004224 } else if (mN.color != COLOR_DEFAULT && !isColorized()) {
Selim Cinek981962e2016-07-20 20:41:58 -07004225 button.setTextColor(R.id.action0,resolveContrastColor());
4226 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004227 } else {
Selim Cinek981962e2016-07-20 20:41:58 -07004228 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Adrian Roos72171622017-01-27 10:32:06 -08004229 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004230 setTextViewColorPrimary(button, R.id.action0);
4231 } else if (mN.color != COLOR_DEFAULT) {
Adrian Roos487374f2017-01-11 15:48:14 -08004232 button.setTextColor(R.id.action0,
4233 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004234 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004235 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004236 return button;
4237 }
4238
Joe Onoratocb109a02011-01-18 17:57:41 -08004239 /**
Selim Cinek981962e2016-07-20 20:41:58 -07004240 * Clears all color spans of a text
4241 * @param charSequence the input text
4242 * @return the same text but without color spans
4243 */
4244 private CharSequence clearColorSpans(CharSequence charSequence) {
4245 if (charSequence instanceof Spanned) {
4246 Spanned ss = (Spanned) charSequence;
4247 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4248 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4249 for (Object span : spans) {
4250 Object resultSpan = span;
4251 if (resultSpan instanceof CharacterStyle) {
4252 resultSpan = ((CharacterStyle) span).getUnderlying();
4253 }
4254 if (resultSpan instanceof TextAppearanceSpan) {
4255 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4256 if (originalSpan.getTextColor() != null) {
4257 resultSpan = new TextAppearanceSpan(
4258 originalSpan.getFamily(),
4259 originalSpan.getTextStyle(),
4260 originalSpan.getTextSize(),
4261 null,
4262 originalSpan.getLinkTextColor());
4263 }
4264 } else if (resultSpan instanceof ForegroundColorSpan
4265 || (resultSpan instanceof BackgroundColorSpan)) {
4266 continue;
4267 } else {
4268 resultSpan = span;
4269 }
4270 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
4271 ss.getSpanFlags(span));
4272 }
4273 return builder;
4274 }
4275 return charSequence;
4276 }
4277
4278 /**
4279 * Ensures contrast on color spans against a background color. also returns the color of the
4280 * text if a span was found that spans over the whole text.
4281 *
4282 * @param charSequence the charSequence on which the spans are
4283 * @param background the background color to ensure the contrast against
4284 * @param outResultColor an array in which a color will be returned as the first element if
4285 * there exists a full length color span.
4286 * @return the contrasted charSequence
4287 */
4288 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
4289 ColorStateList[] outResultColor) {
4290 if (charSequence instanceof Spanned) {
4291 Spanned ss = (Spanned) charSequence;
4292 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4293 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4294 for (Object span : spans) {
4295 Object resultSpan = span;
4296 int spanStart = ss.getSpanStart(span);
4297 int spanEnd = ss.getSpanEnd(span);
4298 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
4299 if (resultSpan instanceof CharacterStyle) {
4300 resultSpan = ((CharacterStyle) span).getUnderlying();
4301 }
4302 if (resultSpan instanceof TextAppearanceSpan) {
4303 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4304 ColorStateList textColor = originalSpan.getTextColor();
4305 if (textColor != null) {
4306 int[] colors = textColor.getColors();
4307 int[] newColors = new int[colors.length];
4308 for (int i = 0; i < newColors.length; i++) {
4309 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
4310 colors[i], background);
4311 }
4312 textColor = new ColorStateList(textColor.getStates().clone(),
4313 newColors);
4314 resultSpan = new TextAppearanceSpan(
4315 originalSpan.getFamily(),
4316 originalSpan.getTextStyle(),
4317 originalSpan.getTextSize(),
4318 textColor,
4319 originalSpan.getLinkTextColor());
4320 if (fullLength) {
4321 outResultColor[0] = new ColorStateList(
4322 textColor.getStates().clone(), newColors);
4323 }
4324 }
4325 } else if (resultSpan instanceof ForegroundColorSpan) {
4326 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
4327 int foregroundColor = originalSpan.getForegroundColor();
4328 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
4329 foregroundColor, background);
4330 resultSpan = new ForegroundColorSpan(foregroundColor);
4331 if (fullLength) {
4332 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
4333 }
4334 } else {
4335 resultSpan = span;
4336 }
4337
4338 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
4339 }
4340 return builder;
4341 }
4342 return charSequence;
4343 }
4344
4345 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004346 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07004347 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004348 */
4349 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004350 if (!mIsLegacyInitialized) {
4351 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
4352 < Build.VERSION_CODES.LOLLIPOP;
4353 mIsLegacyInitialized = true;
4354 }
4355 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004356 }
4357
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004358 private CharSequence processLegacyText(CharSequence charSequence) {
Selim Cinek99104832017-01-25 14:47:33 -08004359 if (isLegacy() || textColorsNeedInversion()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004360 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004361 } else {
4362 return charSequence;
4363 }
4364 }
4365
Dan Sandler26e81cf2014-05-06 10:01:27 -04004366 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004367 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04004368 */
Adrian Roos487374f2017-01-11 15:48:14 -08004369 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
4370 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08004371 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinek99104832017-01-25 14:47:33 -08004372 int color = ambient ? resolveAmbientColor() : getPrimaryHighlightColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08004373 if (colorable) {
Adrian Roos487374f2017-01-11 15:48:14 -08004374 contentView.setDrawableParameters(R.id.icon, false, -1, color,
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004375 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08004376
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004377 }
Selim Cinekea4bef72015-12-02 15:51:10 -08004378 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08004379 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004380 }
4381
Dan Sandler26e81cf2014-05-06 10:01:27 -04004382 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004383 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04004384 * if it's grayscale).
4385 */
4386 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04004387 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
4388 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004389 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004390 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08004391 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04004392 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004393 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004394 }
4395
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004396 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004397 if (mN.color != COLOR_DEFAULT) {
4398 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02004399 }
Jorim Jaggi74419312014-06-10 20:57:21 +02004400 }
4401
Adrian Roos4ff3b122016-02-01 12:26:13 -08004402 int resolveContrastColor() {
4403 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
4404 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004405 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004406 final int contrasted = NotificationColorUtil.resolveContrastColor(mContext, mN.color);
4407
4408 mCachedContrastColorIsFor = mN.color;
4409 return mCachedContrastColor = contrasted;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004410 }
4411
Adrian Roos487374f2017-01-11 15:48:14 -08004412 int resolveAmbientColor() {
4413 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
4414 return mCachedAmbientColor;
4415 }
4416 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
4417
4418 mCachedAmbientColorIsFor = mN.color;
4419 return mCachedAmbientColor = contrasted;
4420 }
4421
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004422 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004423 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004424 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08004425 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004426 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004427 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004428 mN.actions = new Action[mActions.size()];
4429 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004430 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004431 if (!mPersonList.isEmpty()) {
4432 mN.extras.putStringArray(EXTRA_PEOPLE,
4433 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004434 }
Selim Cinek247fa012016-02-18 09:50:48 -08004435 if (mN.bigContentView != null || mN.contentView != null
4436 || mN.headsUpContentView != null) {
4437 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
4438 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004439 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08004440 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004441
Julia Reynolds3b848122016-02-26 10:45:32 -05004442 /**
4443 * Creates a Builder from an existing notification so further changes can be made.
4444 * @param context The context for your application / activity.
4445 * @param n The notification to create a Builder from.
4446 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004447 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004448 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004449 ApplicationInfo applicationInfo = n.extras.getParcelable(
4450 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004451 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05004452 if (applicationInfo != null) {
4453 try {
4454 builderContext = context.createApplicationContext(applicationInfo,
4455 Context.CONTEXT_RESTRICTED);
4456 } catch (NameNotFoundException e) {
4457 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
4458 builderContext = context; // try with our context
4459 }
4460 } else {
4461 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02004462 }
4463
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004464 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004465 }
4466
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004467 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004468 * @deprecated Use {@link #build()} instead.
4469 */
4470 @Deprecated
4471 public Notification getNotification() {
4472 return build();
4473 }
4474
4475 /**
4476 * Combine all of the options that have been set and return a new {@link Notification}
4477 * object.
4478 */
4479 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004480 // first, add any extras from the calling code
4481 if (mUserExtras != null) {
4482 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004483 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004484
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004485 mN.creationTime = System.currentTimeMillis();
4486
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004487 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05004488 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02004489
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004490 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004491
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004492 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004493 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004494 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004495
Adrian Roos5081c0d2016-02-26 16:04:19 -08004496 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4497 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004498 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004499 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004500 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
4501 mN.contentView.getSequenceNumber());
4502 }
4503 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004504 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004505 if (mN.bigContentView != null) {
4506 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
4507 mN.bigContentView.getSequenceNumber());
4508 }
4509 }
4510 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004511 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004512 if (mN.headsUpContentView != null) {
4513 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
4514 mN.headsUpContentView.getSequenceNumber());
4515 }
4516 }
4517 }
4518
Julia Reynolds4c0c2022016-02-02 15:11:59 -05004519 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
4520 mN.flags |= FLAG_SHOW_LIGHTS;
4521 }
4522
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004523 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004524 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004525
4526 /**
4527 * Apply this Builder to an existing {@link Notification} object.
4528 *
4529 * @hide
4530 */
4531 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04004532 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004533 return n;
4534 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004535
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004536 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08004537 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
4538 * change.
4539 *
4540 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
4541 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004542 * @hide
4543 */
Adrian Roos184bfe022016-03-03 13:41:44 -08004544 public static Notification maybeCloneStrippedForDelivery(Notification n) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004545 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08004546
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004547 // Only strip views for known Styles because we won't know how to
4548 // re-create them otherwise.
Adrian Roos184bfe022016-03-03 13:41:44 -08004549 if (!TextUtils.isEmpty(templateClass)
4550 && getNotificationStyleClass(templateClass) == null) {
4551 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004552 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004553
4554 // Only strip unmodified BuilderRemoteViews.
4555 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004556 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004557 n.contentView.getSequenceNumber();
4558 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004559 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004560 n.bigContentView.getSequenceNumber();
4561 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004562 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004563 n.headsUpContentView.getSequenceNumber();
4564
4565 // Nothing to do here, no need to clone.
4566 if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
4567 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004568 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004569
4570 Notification clone = n.clone();
4571 if (stripContentView) {
4572 clone.contentView = null;
4573 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
4574 }
4575 if (stripBigContentView) {
4576 clone.bigContentView = null;
4577 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
4578 }
4579 if (stripHeadsUpContentView) {
4580 clone.headsUpContentView = null;
4581 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
4582 }
4583 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004584 }
4585
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004586 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004587 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004588 }
4589
4590 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004591 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004592 }
4593
4594 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004595 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004596 }
4597
4598 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004599 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004600 }
4601
4602 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004603 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004604 }
4605
Adrian Roosc1a80b02016-04-05 14:54:55 -07004606 private int getMessagingLayoutResource() {
4607 return R.layout.notification_template_material_messaging;
4608 }
4609
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004610 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004611 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004612 }
4613
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004614 private int getEmphasizedActionLayoutResource() {
4615 return R.layout.notification_material_action_emphasized;
4616 }
4617
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004618 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004619 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004620 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004621
4622 private int getBackgroundColor() {
4623 if (isColorized()) {
4624 return mN.color;
4625 } else {
4626 return COLOR_DEFAULT;
4627 }
4628 }
4629
4630 private boolean isColorized() {
4631 return mN.isColorized();
4632 }
Selim Cinek99104832017-01-25 14:47:33 -08004633
4634 private boolean textColorsNeedInversion() {
4635 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
4636 return false;
4637 }
4638 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
4639 return targetSdkVersion > Build.VERSION_CODES.M
4640 && targetSdkVersion < Build.VERSION_CODES.O;
4641 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004642 }
4643
4644 /**
4645 * @return whether this notification is ongoing and can't be dismissed by the user.
4646 */
4647 private boolean isOngoing() {
4648 final int ongoingFlags = Notification.FLAG_FOREGROUND_SERVICE
4649 | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
4650 return (flags & ongoingFlags) != 0;
4651 }
4652
4653 /**
Selim Cinek99104832017-01-25 14:47:33 -08004654 * @return whether this notification has a media session attached
4655 * @hide
4656 */
4657 public boolean hasMediaSession() {
4658 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
4659 }
4660
4661 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08004662 * @return the style class of this notification
4663 * @hide
4664 */
4665 public Class<? extends Notification.Style> getNotificationStyle() {
4666 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
4667
4668 if (!TextUtils.isEmpty(templateClass)) {
4669 return Notification.getNotificationStyleClass(templateClass);
4670 }
4671 return null;
4672 }
4673
4674 /**
Selim Cinek99104832017-01-25 14:47:33 -08004675 * @return true if this notification is colorized. This also factors in whether the
Selim Cinek7b9605b2017-01-19 17:36:00 -08004676 * notification is ongoing.
4677 *
4678 * @hide
4679 */
4680 public boolean isColorized() {
Selim Cinek99104832017-01-25 14:47:33 -08004681 Class<? extends Style> style = getNotificationStyle();
4682 if (MediaStyle.class.equals(style)) {
4683 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
4684 if ((colorized == null || colorized) && hasMediaSession()) {
4685 return true;
4686 }
4687 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
4688 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
4689 return true;
4690 }
4691 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004692 return extras.getBoolean(EXTRA_COLORIZED) && isOngoing();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004693 }
4694
Selim Cinek279fa862016-06-14 10:57:25 -07004695 private boolean hasLargeIcon() {
4696 return mLargeIcon != null || largeIcon != null;
4697 }
4698
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004699 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004700 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004701 * @hide
4702 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07004703 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004704 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
4705 }
4706
4707 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004708 * @return true if the notification will show a chronometer; false otherwise
4709 * @hide
4710 */
4711 public boolean showsChronometer() {
4712 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
4713 }
4714
4715 /**
Julia Reynolds4a02afb2016-12-13 13:39:52 -05004716 * @hide
4717 */
4718 @SystemApi
4719 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
4720 Class<? extends Style>[] classes = new Class[] {
4721 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
4722 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
4723 MessagingStyle.class };
4724 for (Class<? extends Style> innerClass : classes) {
4725 if (templateClass.equals(innerClass.getName())) {
4726 return innerClass;
4727 }
4728 }
4729 return null;
4730 }
4731
4732 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004733 * An object that can apply a rich notification style to a {@link Notification.Builder}
4734 * object.
4735 */
Griff Hazendfcb0802014-02-11 12:00:00 -08004736 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04004737 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004738
4739 /**
4740 * @hide
4741 */
4742 protected CharSequence mSummaryText = null;
4743
4744 /**
4745 * @hide
4746 */
4747 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04004748
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004749 protected Builder mBuilder;
4750
Chris Wrend6297db2012-05-03 16:20:13 -04004751 /**
4752 * Overrides ContentTitle in the big form of the template.
4753 * This defaults to the value passed to setContentTitle().
4754 */
4755 protected void internalSetBigContentTitle(CharSequence title) {
4756 mBigContentTitle = title;
4757 }
4758
4759 /**
4760 * Set the first line of text after the detail section in the big form of the template.
4761 */
4762 protected void internalSetSummaryText(CharSequence cs) {
4763 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04004764 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04004765 }
4766
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004767 public void setBuilder(Builder builder) {
4768 if (mBuilder != builder) {
4769 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004770 if (mBuilder != null) {
4771 mBuilder.setStyle(this);
4772 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004773 }
4774 }
4775
Chris Wrend6297db2012-05-03 16:20:13 -04004776 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004777 if (mBuilder == null) {
4778 throw new IllegalArgumentException("Style requires a valid Builder object");
4779 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004780 }
Chris Wrend6297db2012-05-03 16:20:13 -04004781
4782 protected RemoteViews getStandardView(int layoutId) {
4783 checkBuilder();
4784
Christoph Studer4600f9b2014-07-22 22:44:43 +02004785 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004786 CharSequence oldBuilderContentTitle =
4787 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004788 if (mBigContentTitle != null) {
4789 mBuilder.setContentTitle(mBigContentTitle);
4790 }
4791
Chris Wrend6297db2012-05-03 16:20:13 -04004792 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
4793
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004794 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004795
Chris Wrend6297db2012-05-03 16:20:13 -04004796 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
4797 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04004798 } else {
4799 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004800 }
4801
Chris Wrend6297db2012-05-03 16:20:13 -04004802 return contentView;
4803 }
4804
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004805 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08004806 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004807 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08004808 *
4809 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004810 * @hide
4811 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08004812 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004813 return null;
4814 }
4815
4816 /**
4817 * Construct a Style-specific RemoteViews for the final big notification layout.
4818 * @hide
4819 */
4820 public RemoteViews makeBigContentView() {
4821 return null;
4822 }
4823
4824 /**
4825 * Construct a Style-specific RemoteViews for the final HUN layout.
4826 * @hide
4827 */
4828 public RemoteViews makeHeadsUpContentView() {
4829 return null;
4830 }
4831
4832 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004833 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004834 * @hide
4835 */
4836 public void addExtras(Bundle extras) {
4837 if (mSummaryTextSet) {
4838 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
4839 }
4840 if (mBigContentTitle != null) {
4841 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
4842 }
Chris Wren91ad5632013-06-05 15:05:57 -04004843 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004844 }
4845
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004846 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004847 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004848 * @hide
4849 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02004850 protected void restoreFromExtras(Bundle extras) {
4851 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
4852 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
4853 mSummaryTextSet = true;
4854 }
4855 if (extras.containsKey(EXTRA_TITLE_BIG)) {
4856 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
4857 }
4858 }
4859
4860
4861 /**
4862 * @hide
4863 */
4864 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004865 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004866 return wip;
4867 }
4868
Daniel Sandler0ec46202015-06-24 01:27:05 -04004869 /**
4870 * @hide
4871 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004872 public void purgeResources() {}
4873
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004874 /**
4875 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
4876 * attached to.
4877 *
4878 * @return the fully constructed Notification.
4879 */
4880 public Notification build() {
4881 checkBuilder();
4882 return mBuilder.build();
4883 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004884
4885 /**
4886 * @hide
4887 * @return true if the style positions the progress bar on the second line; false if the
4888 * style hides the progress bar
4889 */
4890 protected boolean hasProgress() {
4891 return true;
4892 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004893
4894 /**
4895 * @hide
4896 * @return Whether we should put the summary be put into the notification header
4897 */
4898 public boolean hasSummaryInHeader() {
4899 return true;
4900 }
Selim Cinek593610c2016-02-16 18:42:57 -08004901
4902 /**
4903 * @hide
4904 * @return Whether custom content views are displayed inline in the style
4905 */
4906 public boolean displayCustomViewInline() {
4907 return false;
4908 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004909 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004910
4911 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004912 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08004913 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004914 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004915 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004916 * Notification notif = new Notification.Builder(mContext)
4917 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
4918 * .setContentText(subject)
4919 * .setSmallIcon(R.drawable.new_post)
4920 * .setLargeIcon(aBitmap)
4921 * .setStyle(new Notification.BigPictureStyle()
4922 * .bigPicture(aBigBitmap))
4923 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004924 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004925 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004926 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004927 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004928 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004929 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004930 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004931 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004932
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004933 public BigPictureStyle() {
4934 }
4935
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004936 /**
4937 * @deprecated use {@code BigPictureStyle()}.
4938 */
4939 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004940 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004941 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004942 }
4943
Chris Wrend6297db2012-05-03 16:20:13 -04004944 /**
4945 * Overrides ContentTitle in the big form of the template.
4946 * This defaults to the value passed to setContentTitle().
4947 */
4948 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004949 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004950 return this;
4951 }
4952
4953 /**
4954 * Set the first line of text after the detail section in the big form of the template.
4955 */
4956 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004957 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004958 return this;
4959 }
4960
Chris Wren0bd664d2012-08-01 13:56:56 -04004961 /**
4962 * Provide the bitmap to be used as the payload for the BigPicture notification.
4963 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004964 public BigPictureStyle bigPicture(Bitmap b) {
4965 mPicture = b;
4966 return this;
4967 }
4968
Chris Wren3745a3d2012-05-22 15:11:52 -04004969 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04004970 * Override the large icon when the big notification is shown.
4971 */
4972 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04004973 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
4974 }
4975
4976 /**
4977 * Override the large icon when the big notification is shown.
4978 */
4979 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04004980 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004981 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004982 return this;
4983 }
4984
Riley Andrews0394a0c2015-11-03 23:36:52 -08004985 /** @hide */
4986 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
4987
Daniel Sandler0ec46202015-06-24 01:27:05 -04004988 /**
4989 * @hide
4990 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004991 @Override
4992 public void purgeResources() {
4993 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08004994 if (mPicture != null &&
4995 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08004996 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004997 mPicture = mPicture.createAshmemBitmap();
4998 }
4999 if (mBigLargeIcon != null) {
5000 mBigLargeIcon.convertToAshmem();
5001 }
5002 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01005003
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005004 /**
5005 * @hide
5006 */
5007 public RemoteViews makeBigContentView() {
5008 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01005009 // This covers the following cases:
5010 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005011 // mN.mLargeIcon
5012 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04005013 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07005014 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005015 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005016 oldLargeIcon = mBuilder.mN.mLargeIcon;
5017 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005018 // The legacy largeIcon might not allow us to clear the image, as it's taken in
5019 // replacement if the other one is null. Because we're restoring these legacy icons
5020 // for old listeners, this is in general non-null.
5021 largeIconLegacy = mBuilder.mN.largeIcon;
5022 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005023 }
5024
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005025 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05005026 if (mSummaryTextSet) {
5027 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005028 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005029 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05005030 }
Selim Cinek279fa862016-06-14 10:57:25 -07005031 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08005032
Christoph Studer5c510ee2014-12-15 16:32:27 +01005033 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005034 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005035 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005036 }
5037
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005038 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005039 return contentView;
5040 }
5041
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005042 /**
5043 * @hide
5044 */
5045 public void addExtras(Bundle extras) {
5046 super.addExtras(extras);
5047
5048 if (mBigLargeIconSet) {
5049 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
5050 }
5051 extras.putParcelable(EXTRA_PICTURE, mPicture);
5052 }
5053
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005054 /**
5055 * @hide
5056 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005057 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02005058 protected void restoreFromExtras(Bundle extras) {
5059 super.restoreFromExtras(extras);
5060
5061 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01005062 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02005063 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04005064 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005065 mPicture = extras.getParcelable(EXTRA_PICTURE);
5066 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005067
5068 /**
5069 * @hide
5070 */
5071 @Override
5072 public boolean hasSummaryInHeader() {
5073 return false;
5074 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005075 }
5076
5077 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005078 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08005079 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005080 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005081 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005082 * Notification notif = new Notification.Builder(mContext)
5083 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5084 * .setContentText(subject)
5085 * .setSmallIcon(R.drawable.new_mail)
5086 * .setLargeIcon(aBitmap)
5087 * .setStyle(new Notification.BigTextStyle()
5088 * .bigText(aVeryLongString))
5089 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005090 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005091 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005092 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005093 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005094 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005095
5096 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08005097 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005098
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005099 private CharSequence mBigText;
5100
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005101 public BigTextStyle() {
5102 }
5103
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005104 /**
5105 * @deprecated use {@code BigTextStyle()}.
5106 */
5107 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005108 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005109 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005110 }
5111
Chris Wrend6297db2012-05-03 16:20:13 -04005112 /**
5113 * Overrides ContentTitle in the big form of the template.
5114 * This defaults to the value passed to setContentTitle().
5115 */
5116 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005117 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005118 return this;
5119 }
5120
5121 /**
5122 * Set the first line of text after the detail section in the big form of the template.
5123 */
5124 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005125 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005126 return this;
5127 }
5128
Chris Wren0bd664d2012-08-01 13:56:56 -04005129 /**
5130 * Provide the longer text to be displayed in the big form of the
5131 * template in place of the content text.
5132 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005133 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005134 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005135 return this;
5136 }
5137
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005138 /**
5139 * @hide
5140 */
5141 public void addExtras(Bundle extras) {
5142 super.addExtras(extras);
5143
Christoph Studer4600f9b2014-07-22 22:44:43 +02005144 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
5145 }
5146
5147 /**
5148 * @hide
5149 */
5150 @Override
5151 protected void restoreFromExtras(Bundle extras) {
5152 super.restoreFromExtras(extras);
5153
5154 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005155 }
5156
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005157 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005158 * @param increasedHeight true if this layout be created with an increased height.
5159 *
5160 * @hide
5161 */
5162 @Override
5163 public RemoteViews makeContentView(boolean increasedHeight) {
5164 if (increasedHeight) {
5165 ArrayList<Action> actions = mBuilder.mActions;
5166 mBuilder.mActions = new ArrayList<>();
5167 RemoteViews remoteViews = makeBigContentView();
5168 mBuilder.mActions = actions;
5169 return remoteViews;
5170 }
5171 return super.makeContentView(increasedHeight);
5172 }
5173
5174 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005175 * @hide
5176 */
5177 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005178
5179 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07005180 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005181 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04005182
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005183 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08005184
Selim Cinek75998782016-04-26 10:39:17 -07005185 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005186
Selim Cinek3a2c4b92015-12-17 17:01:17 -08005187 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07005188 if (TextUtils.isEmpty(bigTextText)) {
5189 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
5190 // experience
5191 bigTextText = mBuilder.processLegacyText(text);
5192 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07005193 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08005194
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005195 return contentView;
5196 }
5197
Adrian Roosb1f427c2016-05-26 12:27:15 -07005198 static void applyBigTextContentView(Builder builder,
5199 RemoteViews contentView, CharSequence bigTextText) {
5200 contentView.setTextViewText(R.id.big_text, bigTextText);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005201 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07005202 contentView.setViewVisibility(R.id.big_text,
5203 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
5204 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines(builder));
Selim Cinek279fa862016-06-14 10:57:25 -07005205 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07005206 }
5207
5208 private static int calculateMaxLines(Builder builder) {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005209 int lineCount = MAX_LINES;
Adrian Roosb1f427c2016-05-26 12:27:15 -07005210 boolean hasActions = builder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005211 if (hasActions) {
5212 lineCount -= LINES_CONSUMED_BY_ACTIONS;
5213 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005214 return lineCount;
5215 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005216 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005217
5218 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005219 * Helper class for generating large-format notifications that include multiple back-and-forth
5220 * messages of varying types between any number of people.
5221 *
5222 * <br>
5223 * If the platform does not provide large-format notifications, this method has no effect. The
5224 * user will always see the normal notification view.
5225 * <br>
5226 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
5227 * so:
5228 * <pre class="prettyprint">
5229 *
5230 * Notification noti = new Notification.Builder()
5231 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
5232 * .setContentText(subject)
5233 * .setSmallIcon(R.drawable.new_message)
5234 * .setLargeIcon(aBitmap)
5235 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
5236 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
5237 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
5238 * .build();
5239 * </pre>
5240 */
5241 public static class MessagingStyle extends Style {
5242
5243 /**
5244 * The maximum number of messages that will be retained in the Notification itself (the
5245 * number displayed is up to the platform).
5246 */
5247 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
5248
5249 CharSequence mUserDisplayName;
5250 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04005251 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08005252 List<Message> mHistoricMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04005253
5254 MessagingStyle() {
5255 }
5256
5257 /**
Alex Hillsfd590442016-10-07 09:52:44 -04005258 * @param userDisplayName Required - the name to be displayed for any replies sent by the
5259 * user before the posting app reposts the notification with those messages after they've
5260 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04005261 * {@link #addMessage(Notification.MessagingStyle.Message)}
5262 */
Alex Hillsfd590442016-10-07 09:52:44 -04005263 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Alex Hillsfc737de2016-03-23 17:33:02 -04005264 mUserDisplayName = userDisplayName;
5265 }
5266
5267 /**
5268 * Returns the name to be displayed for any replies sent by the user
5269 */
5270 public CharSequence getUserDisplayName() {
5271 return mUserDisplayName;
5272 }
5273
5274 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005275 * Sets the title to be displayed on this conversation. This should only be used for
5276 * group messaging and left unset for one-on-one conversations.
5277 * @param conversationTitle
5278 * @return this object for method chaining.
5279 */
5280 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
5281 mConversationTitle = conversationTitle;
5282 return this;
5283 }
5284
5285 /**
5286 * Return the title to be displayed on this conversation. Can be <code>null</code> and
5287 * should be for one-on-one conversations
5288 */
5289 public CharSequence getConversationTitle() {
5290 return mConversationTitle;
5291 }
5292
5293 /**
5294 * Adds a message for display by this notification. Convenience call for a simple
5295 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
5296 * @param text A {@link CharSequence} to be displayed as the message content
5297 * @param timestamp Time at which the message arrived
5298 * @param sender A {@link CharSequence} to be used for displaying the name of the
5299 * sender. Should be <code>null</code> for messages by the current user, in which case
5300 * the platform will insert {@link #getUserDisplayName()}.
5301 * Should be unique amongst all individuals in the conversation, and should be
5302 * consistent during re-posts of the notification.
5303 *
5304 * @see Message#Message(CharSequence, long, CharSequence)
5305 *
5306 * @return this object for method chaining
5307 */
5308 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08005309 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04005310 }
5311
5312 /**
5313 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08005314 *
5315 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5316 * the newest last.
5317 *
Alex Hillsfc737de2016-03-23 17:33:02 -04005318 * @param message The {@link Message} to be displayed
5319 * @return this object for method chaining
5320 */
5321 public MessagingStyle addMessage(Message message) {
5322 mMessages.add(message);
5323 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5324 mMessages.remove(0);
5325 }
5326 return this;
5327 }
5328
5329 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005330 * Adds a {@link Message} for historic context in this notification.
5331 *
5332 * <p>Messages should be added as historic if they are not the main subject of the
5333 * notification but may give context to a conversation. The system may choose to present
5334 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
5335 *
5336 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5337 * the newest last.
5338 *
5339 * @param message The historic {@link Message} to be added
5340 * @return this object for method chaining
5341 */
5342 public MessagingStyle addHistoricMessage(Message message) {
5343 mHistoricMessages.add(message);
5344 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5345 mHistoricMessages.remove(0);
5346 }
5347 return this;
5348 }
5349
5350 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005351 * Gets the list of {@code Message} objects that represent the notification
5352 */
5353 public List<Message> getMessages() {
5354 return mMessages;
5355 }
5356
5357 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005358 * Gets the list of historic {@code Message}s in the notification.
5359 */
5360 public List<Message> getHistoricMessages() {
5361 return mHistoricMessages;
5362 }
5363
5364 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005365 * @hide
5366 */
5367 @Override
5368 public void addExtras(Bundle extras) {
5369 super.addExtras(extras);
5370 if (mUserDisplayName != null) {
5371 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
5372 }
5373 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005374 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04005375 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005376 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
5377 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04005378 }
Adrian Roos437cd562017-01-18 15:47:03 -08005379 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
5380 Message.getBundleArrayForMessages(mHistoricMessages));
5381 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07005382
5383 fixTitleAndTextExtras(extras);
5384 }
5385
5386 private void fixTitleAndTextExtras(Bundle extras) {
5387 Message m = findLatestIncomingMessage();
5388 CharSequence text = (m == null) ? null : m.mText;
5389 CharSequence sender = m == null ? null
5390 : TextUtils.isEmpty(m.mSender) ? mUserDisplayName : m.mSender;
5391 CharSequence title;
5392 if (!TextUtils.isEmpty(mConversationTitle)) {
5393 if (!TextUtils.isEmpty(sender)) {
5394 BidiFormatter bidi = BidiFormatter.getInstance();
5395 title = mBuilder.mContext.getString(
5396 com.android.internal.R.string.notification_messaging_title_template,
5397 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(m.mSender));
5398 } else {
5399 title = mConversationTitle;
5400 }
5401 } else {
5402 title = sender;
5403 }
5404
5405 if (title != null) {
5406 extras.putCharSequence(EXTRA_TITLE, title);
5407 }
5408 if (text != null) {
5409 extras.putCharSequence(EXTRA_TEXT, text);
5410 }
Alex Hillsfc737de2016-03-23 17:33:02 -04005411 }
5412
5413 /**
5414 * @hide
5415 */
5416 @Override
5417 protected void restoreFromExtras(Bundle extras) {
5418 super.restoreFromExtras(extras);
5419
5420 mMessages.clear();
Adrian Roos437cd562017-01-18 15:47:03 -08005421 mHistoricMessages.clear();
Adrian Roos96b7e202016-05-17 13:50:38 -07005422 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
5423 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08005424 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
5425 if (messages != null && messages instanceof Parcelable[]) {
5426 mMessages = Message.getMessagesFromBundleArray(messages);
5427 }
5428 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
5429 if (histMessages != null && histMessages instanceof Parcelable[]) {
5430 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Alex Hillsfc737de2016-03-23 17:33:02 -04005431 }
5432 }
5433
5434 /**
5435 * @hide
5436 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07005437 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08005438 public RemoteViews makeContentView(boolean increasedHeight) {
5439 if (!increasedHeight) {
5440 Message m = findLatestIncomingMessage();
5441 CharSequence title = mConversationTitle != null
5442 ? mConversationTitle
5443 : (m == null) ? null : m.mSender;
5444 CharSequence text = (m == null)
5445 ? null
5446 : mConversationTitle != null ? makeMessageLine(m, mBuilder) : m.mText;
Alex Hillsfc737de2016-03-23 17:33:02 -04005447
Selim Cinek7d1009b2017-01-25 15:28:28 -08005448 return mBuilder.applyStandardTemplate(mBuilder.getBaseLayoutResource(),
5449 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
5450 } else {
5451 ArrayList<Action> actions = mBuilder.mActions;
5452 mBuilder.mActions = new ArrayList<>();
5453 RemoteViews remoteViews = makeBigContentView();
5454 mBuilder.mActions = actions;
5455 return remoteViews;
5456 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07005457 }
5458
5459 private Message findLatestIncomingMessage() {
5460 for (int i = mMessages.size() - 1; i >= 0; i--) {
5461 Message m = mMessages.get(i);
5462 // Incoming messages have a non-empty sender.
5463 if (!TextUtils.isEmpty(m.mSender)) {
5464 return m;
5465 }
5466 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07005467 if (!mMessages.isEmpty()) {
5468 // No incoming messages, fall back to outgoing message
5469 return mMessages.get(mMessages.size() - 1);
5470 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07005471 return null;
5472 }
5473
5474 /**
5475 * @hide
5476 */
5477 @Override
5478 public RemoteViews makeBigContentView() {
5479 CharSequence title = !TextUtils.isEmpty(super.mBigContentTitle)
5480 ? super.mBigContentTitle
5481 : mConversationTitle;
5482 boolean hasTitle = !TextUtils.isEmpty(title);
5483
Adrian Roosfeafa052016-06-01 17:09:45 -07005484 if (mMessages.size() == 1) {
5485 // Special case for a single message: Use the big text style
5486 // so the collapsed and expanded versions match nicely.
5487 CharSequence bigTitle;
5488 CharSequence text;
5489 if (hasTitle) {
5490 bigTitle = title;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005491 text = makeMessageLine(mMessages.get(0), mBuilder);
Adrian Roosfeafa052016-06-01 17:09:45 -07005492 } else {
5493 bigTitle = mMessages.get(0).mSender;
5494 text = mMessages.get(0).mText;
5495 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07005496 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
5497 mBuilder.getBigTextLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08005498 mBuilder.mParams.reset().hasProgress(false).title(bigTitle).text(null));
Adrian Roosb1f427c2016-05-26 12:27:15 -07005499 BigTextStyle.applyBigTextContentView(mBuilder, contentView, text);
5500 return contentView;
5501 }
5502
Adrian Roos48d746a2016-04-12 14:57:28 -07005503 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07005504 mBuilder.getMessagingLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08005505 mBuilder.mParams.reset().hasProgress(false).title(title).text(null));
Adrian Roosc1a80b02016-04-05 14:54:55 -07005506
5507 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
5508 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
5509
5510 // Make sure all rows are gone in case we reuse a view.
5511 for (int rowId : rowIds) {
5512 contentView.setViewVisibility(rowId, View.GONE);
5513 }
5514
5515 int i=0;
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005516 contentView.setViewLayoutMarginBottomDimen(R.id.line1,
5517 hasTitle ? R.dimen.notification_messaging_spacing : 0);
Adrian Roosc1a80b02016-04-05 14:54:55 -07005518 contentView.setInt(R.id.notification_messaging, "setNumIndentLines",
Selim Cinek279fa862016-06-14 10:57:25 -07005519 !mBuilder.mN.hasLargeIcon() ? 0 : (hasTitle ? 1 : 2));
Adrian Roosc1a80b02016-04-05 14:54:55 -07005520
Adrian Roosfeafa052016-06-01 17:09:45 -07005521 int contractedChildId = View.NO_ID;
5522 Message contractedMessage = findLatestIncomingMessage();
Adrian Roos437cd562017-01-18 15:47:03 -08005523 int firstHistoricMessage = Math.max(0, mHistoricMessages.size()
5524 - (rowIds.length - mMessages.size()));
5525 while (firstHistoricMessage + i < mHistoricMessages.size() && i < rowIds.length) {
5526 Message m = mHistoricMessages.get(firstHistoricMessage + i);
5527 int rowId = rowIds[i];
5528
Selim Cinek7b9605b2017-01-19 17:36:00 -08005529 contentView.setTextViewText(rowId, makeMessageLine(m, mBuilder));
Adrian Roos437cd562017-01-18 15:47:03 -08005530
5531 if (contractedMessage == m) {
5532 contractedChildId = rowId;
5533 }
5534
5535 i++;
5536 }
5537
Adrian Roosc1a80b02016-04-05 14:54:55 -07005538 int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
5539 while (firstMessage + i < mMessages.size() && i < rowIds.length) {
5540 Message m = mMessages.get(firstMessage + i);
5541 int rowId = rowIds[i];
5542
5543 contentView.setViewVisibility(rowId, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005544 contentView.setTextViewText(rowId, makeMessageLine(m, mBuilder));
5545 mBuilder.setTextViewColorSecondary(contentView, rowId);
Adrian Roosc1a80b02016-04-05 14:54:55 -07005546
Adrian Roosfeafa052016-06-01 17:09:45 -07005547 if (contractedMessage == m) {
5548 contractedChildId = rowId;
5549 }
5550
Adrian Roosc1a80b02016-04-05 14:54:55 -07005551 i++;
5552 }
Adrian Roos437cd562017-01-18 15:47:03 -08005553 // Clear the remaining views for reapply. Ensures that historic message views can
5554 // reliably be identified as being GONE and having non-null text.
5555 while (i < rowIds.length) {
5556 int rowId = rowIds[i];
5557 contentView.setTextViewText(rowId, null);
5558 i++;
5559 }
5560
Adrian Roosfeafa052016-06-01 17:09:45 -07005561 // Record this here to allow transformation between the contracted and expanded views.
5562 contentView.setInt(R.id.notification_messaging, "setContractedChildId",
5563 contractedChildId);
Alex Hillsfc737de2016-03-23 17:33:02 -04005564 return contentView;
5565 }
5566
Selim Cinek7b9605b2017-01-19 17:36:00 -08005567 private CharSequence makeMessageLine(Message m, Builder builder) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07005568 BidiFormatter bidi = BidiFormatter.getInstance();
5569 SpannableStringBuilder sb = new SpannableStringBuilder();
Selim Cinek7b9605b2017-01-19 17:36:00 -08005570 boolean colorize = builder.isColorized();
Adrian Roosc1a80b02016-04-05 14:54:55 -07005571 if (TextUtils.isEmpty(m.mSender)) {
5572 CharSequence replyName = mUserDisplayName == null ? "" : mUserDisplayName;
5573 sb.append(bidi.unicodeWrap(replyName),
Selim Cinek7b9605b2017-01-19 17:36:00 -08005574 makeFontColorSpan(colorize
5575 ? builder.getPrimaryTextColor()
5576 : mBuilder.resolveContrastColor()),
Adrian Roosc1a80b02016-04-05 14:54:55 -07005577 0 /* flags */);
5578 } else {
5579 sb.append(bidi.unicodeWrap(m.mSender),
Selim Cinek7b9605b2017-01-19 17:36:00 -08005580 makeFontColorSpan(colorize
5581 ? builder.getPrimaryTextColor()
5582 : Color.BLACK),
Adrian Roosc1a80b02016-04-05 14:54:55 -07005583 0 /* flags */);
5584 }
5585 CharSequence text = m.mText == null ? "" : m.mText;
5586 sb.append(" ").append(bidi.unicodeWrap(text));
5587 return sb;
5588 }
5589
Adrian Roosdedd1df2016-04-26 16:38:47 -07005590 /**
5591 * @hide
5592 */
5593 @Override
5594 public RemoteViews makeHeadsUpContentView() {
5595 Message m = findLatestIncomingMessage();
5596 CharSequence title = mConversationTitle != null
5597 ? mConversationTitle
5598 : (m == null) ? null : m.mSender;
5599 CharSequence text = (m == null)
5600 ? null
Selim Cinek7b9605b2017-01-19 17:36:00 -08005601 : mConversationTitle != null ? makeMessageLine(m, mBuilder) : m.mText;
Adrian Roosdedd1df2016-04-26 16:38:47 -07005602
5603 return mBuilder.applyStandardTemplateWithActions(mBuilder.getBigBaseLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08005604 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
Adrian Roosdedd1df2016-04-26 16:38:47 -07005605 }
5606
Adrian Roosc1a80b02016-04-05 14:54:55 -07005607 private static TextAppearanceSpan makeFontColorSpan(int color) {
5608 return new TextAppearanceSpan(null, 0, 0,
5609 ColorStateList.valueOf(color), null);
5610 }
5611
Alex Hillsd9b04d92016-04-11 16:38:16 -04005612 public static final class Message {
5613
5614 static final String KEY_TEXT = "text";
5615 static final String KEY_TIMESTAMP = "time";
5616 static final String KEY_SENDER = "sender";
5617 static final String KEY_DATA_MIME_TYPE = "type";
5618 static final String KEY_DATA_URI= "uri";
Alex Hillsfc737de2016-03-23 17:33:02 -04005619
5620 private final CharSequence mText;
5621 private final long mTimestamp;
5622 private final CharSequence mSender;
5623
5624 private String mDataMimeType;
5625 private Uri mDataUri;
5626
5627 /**
5628 * Constructor
5629 * @param text A {@link CharSequence} to be displayed as the message content
5630 * @param timestamp Time at which the message arrived
5631 * @param sender A {@link CharSequence} to be used for displaying the name of the
5632 * sender. Should be <code>null</code> for messages by the current user, in which case
5633 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
5634 * Should be unique amongst all individuals in the conversation, and should be
5635 * consistent during re-posts of the notification.
5636 */
5637 public Message(CharSequence text, long timestamp, CharSequence sender){
5638 mText = text;
5639 mTimestamp = timestamp;
5640 mSender = sender;
5641 }
5642
5643 /**
5644 * Sets a binary blob of data and an associated MIME type for a message. In the case
5645 * where the platform doesn't support the MIME type, the original text provided in the
5646 * constructor will be used.
5647 * @param dataMimeType The MIME type of the content. See
5648 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
5649 * types on Android and Android Wear.
5650 * @param dataUri The uri containing the content whose type is given by the MIME type.
5651 * <p class="note">
5652 * <ol>
5653 * <li>Notification Listeners including the System UI need permission to access the
5654 * data the Uri points to. The recommended ways to do this are:</li>
5655 * <li>Store the data in your own ContentProvider, making sure that other apps have
5656 * the correct permission to access your provider. The preferred mechanism for
5657 * providing access is to use per-URI permissions which are temporary and only
5658 * grant access to the receiving application. An easy way to create a
5659 * ContentProvider like this is to use the FileProvider helper class.</li>
5660 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
5661 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
5662 * also store non-media types (see MediaStore.Files for more info). Files can be
5663 * inserted into the MediaStore using scanFile() after which a content:// style
5664 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
5665 * Note that once added to the system MediaStore the content is accessible to any
5666 * app on the device.</li>
5667 * </ol>
5668 * @return this object for method chaining
5669 */
5670 public Message setData(String dataMimeType, Uri dataUri) {
5671 mDataMimeType = dataMimeType;
5672 mDataUri = dataUri;
5673 return this;
5674 }
5675
Alex Hillsfc737de2016-03-23 17:33:02 -04005676 /**
5677 * Get the text to be used for this message, or the fallback text if a type and content
5678 * Uri have been set
5679 */
5680 public CharSequence getText() {
5681 return mText;
5682 }
5683
5684 /**
5685 * Get the time at which this message arrived
5686 */
5687 public long getTimestamp() {
5688 return mTimestamp;
5689 }
5690
5691 /**
5692 * Get the text used to display the contact's name in the messaging experience
5693 */
5694 public CharSequence getSender() {
5695 return mSender;
5696 }
5697
5698 /**
5699 * Get the MIME type of the data pointed to by the Uri
5700 */
5701 public String getDataMimeType() {
5702 return mDataMimeType;
5703 }
5704
5705 /**
5706 * Get the the Uri pointing to the content of the message. Can be null, in which case
5707 * {@see #getText()} is used.
5708 */
5709 public Uri getDataUri() {
5710 return mDataUri;
5711 }
5712
Alex Hillsd9b04d92016-04-11 16:38:16 -04005713 private Bundle toBundle() {
5714 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04005715 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005716 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04005717 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005718 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04005719 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005720 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04005721 }
5722 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005723 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04005724 }
5725 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005726 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04005727 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005728 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04005729 }
5730
Alex Hillsd9b04d92016-04-11 16:38:16 -04005731 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
5732 Bundle[] bundles = new Bundle[messages.size()];
5733 final int N = messages.size();
5734 for (int i = 0; i < N; i++) {
5735 bundles[i] = messages.get(i).toBundle();
5736 }
5737 return bundles;
5738 }
5739
Adrian Roosdedd1df2016-04-26 16:38:47 -07005740 static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005741 List<Message> messages = new ArrayList<>(bundles.length);
5742 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07005743 if (bundles[i] instanceof Bundle) {
5744 Message message = getMessageFromBundle((Bundle)bundles[i]);
5745 if (message != null) {
5746 messages.add(message);
5747 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005748 }
5749 }
5750 return messages;
5751 }
5752
5753 static Message getMessageFromBundle(Bundle bundle) {
5754 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07005755 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005756 return null;
5757 } else {
5758 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
5759 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
5760 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
5761 bundle.containsKey(KEY_DATA_URI)) {
5762
5763 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
5764 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04005765 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005766 return message;
5767 }
5768 } catch (ClassCastException e) {
5769 return null;
5770 }
5771 }
Alex Hillsfc737de2016-03-23 17:33:02 -04005772 }
5773 }
5774
5775 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04005776 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08005777 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005778 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04005779 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005780 * Notification notif = new Notification.Builder(mContext)
5781 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
5782 * .setContentText(subject)
5783 * .setSmallIcon(R.drawable.new_mail)
5784 * .setLargeIcon(aBitmap)
5785 * .setStyle(new Notification.InboxStyle()
5786 * .addLine(str1)
5787 * .addLine(str2)
5788 * .setContentTitle(&quot;&quot;)
5789 * .setSummaryText(&quot;+3 more&quot;))
5790 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04005791 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005792 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04005793 * @see Notification#bigContentView
5794 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005795 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005796 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
5797
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005798 public InboxStyle() {
5799 }
5800
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005801 /**
5802 * @deprecated use {@code InboxStyle()}.
5803 */
5804 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04005805 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005806 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04005807 }
5808
Chris Wrend6297db2012-05-03 16:20:13 -04005809 /**
5810 * Overrides ContentTitle in the big form of the template.
5811 * This defaults to the value passed to setContentTitle().
5812 */
5813 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005814 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005815 return this;
5816 }
5817
5818 /**
5819 * Set the first line of text after the detail section in the big form of the template.
5820 */
5821 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005822 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005823 return this;
5824 }
5825
Chris Wren0bd664d2012-08-01 13:56:56 -04005826 /**
5827 * Append a line to the digest section of the Inbox notification.
5828 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04005829 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005830 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04005831 return this;
5832 }
5833
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005834 /**
5835 * @hide
5836 */
5837 public void addExtras(Bundle extras) {
5838 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005839
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005840 CharSequence[] a = new CharSequence[mTexts.size()];
5841 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
5842 }
5843
Christoph Studer4600f9b2014-07-22 22:44:43 +02005844 /**
5845 * @hide
5846 */
5847 @Override
5848 protected void restoreFromExtras(Bundle extras) {
5849 super.restoreFromExtras(extras);
5850
5851 mTexts.clear();
5852 if (extras.containsKey(EXTRA_TEXT_LINES)) {
5853 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
5854 }
5855 }
5856
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005857 /**
5858 * @hide
5859 */
5860 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08005861 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02005862 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005863 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
5864 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005865
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005866 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04005867
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005868 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005869
Chris Wrend6297db2012-05-03 16:20:13 -04005870 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 -04005871 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04005872
Chris Wren4ed80d52012-05-17 09:30:03 -04005873 // Make sure all rows are gone in case we reuse a view.
5874 for (int rowId : rowIds) {
5875 contentView.setViewVisibility(rowId, View.GONE);
5876 }
5877
Daniel Sandler879c5e02012-04-17 16:46:51 -04005878 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07005879 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
5880 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08005881 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07005882 int onlyViewId = 0;
5883 int maxRows = rowIds.length;
5884 if (mBuilder.mActions.size() > 0) {
5885 maxRows--;
5886 }
5887 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005888 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07005889 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005890 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005891 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005892 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07005893 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08005894 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07005895 if (first) {
5896 onlyViewId = rowIds[i];
5897 } else {
5898 onlyViewId = 0;
5899 }
Selim Cinek247fa012016-02-18 09:50:48 -08005900 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04005901 }
5902 i++;
5903 }
Selim Cinek07c80172016-04-21 16:40:47 -07005904 if (onlyViewId != 0) {
5905 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
5906 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
5907 R.dimen.notification_text_margin_top);
5908 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
5909 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005910
Daniel Sandler879c5e02012-04-17 16:46:51 -04005911 return contentView;
5912 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005913
Selim Cinek247fa012016-02-18 09:50:48 -08005914 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08005915 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08005916 if (first) {
5917 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
5918 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
5919 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07005920 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005921 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08005922 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005923 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005924 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08005925 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005926 }
Dan Sandler842dd772014-05-15 09:36:47 -04005927
5928 /**
5929 * Notification style for media playback notifications.
5930 *
5931 * In the expanded form, {@link Notification#bigContentView}, up to 5
5932 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04005933 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04005934 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
5935 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
5936 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08005937 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04005938 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
5939 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01005940 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04005941 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08005942 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005943 * Notifications created with MediaStyle will have their category set to
5944 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
5945 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08005946 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07005947 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
5948 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04005949 * the System UI can identify this as a notification representing an active media session
5950 * and respond accordingly (by showing album artwork in the lockscreen, for example).
5951 *
Selim Cinek99104832017-01-25 14:47:33 -08005952 * <p>
5953 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
5954 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
5955 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
5956 * <p>
5957 *
Dan Sandler842dd772014-05-15 09:36:47 -04005958 * To use this style with your Notification, feed it to
5959 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5960 * <pre class="prettyprint">
5961 * Notification noti = new Notification.Builder()
5962 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01005963 * .setContentTitle(&quot;Track title&quot;)
5964 * .setContentText(&quot;Artist - Album&quot;)
5965 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005966 * .setStyle(<b>new Notification.MediaStyle()</b>
5967 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04005968 * .build();
5969 * </pre>
5970 *
5971 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08005972 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04005973 */
5974 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005975 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04005976 static final int MAX_MEDIA_BUTTONS = 5;
5977
5978 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07005979 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04005980
5981 public MediaStyle() {
5982 }
5983
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005984 /**
5985 * @deprecated use {@code MediaStyle()}.
5986 */
5987 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04005988 public MediaStyle(Builder builder) {
5989 setBuilder(builder);
5990 }
5991
5992 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005993 * 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 -04005994 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005995 *
5996 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04005997 */
5998 public MediaStyle setShowActionsInCompactView(int...actions) {
5999 mActionsToShowInCompact = actions;
6000 return this;
6001 }
6002
6003 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07006004 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
6005 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04006006 */
Jeff Browndba34ba2014-06-24 20:46:03 -07006007 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04006008 mToken = token;
6009 return this;
6010 }
6011
Christoph Studer4600f9b2014-07-22 22:44:43 +02006012 /**
6013 * @hide
6014 */
Dan Sandler842dd772014-05-15 09:36:47 -04006015 @Override
6016 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006017 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006018 if (wip.category == null) {
6019 wip.category = Notification.CATEGORY_TRANSPORT;
6020 }
Dan Sandler842dd772014-05-15 09:36:47 -04006021 return wip;
6022 }
6023
Christoph Studer4600f9b2014-07-22 22:44:43 +02006024 /**
6025 * @hide
6026 */
6027 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006028 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006029 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006030 }
6031
6032 /**
6033 * @hide
6034 */
6035 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006036 public RemoteViews makeBigContentView() {
6037 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006038 }
6039
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006040 /**
6041 * @hide
6042 */
6043 @Override
6044 public RemoteViews makeHeadsUpContentView() {
6045 RemoteViews expanded = makeMediaBigContentView();
6046 return expanded != null ? expanded : makeMediaContentView();
6047 }
6048
Dan Sandler842dd772014-05-15 09:36:47 -04006049 /** @hide */
6050 @Override
6051 public void addExtras(Bundle extras) {
6052 super.addExtras(extras);
6053
6054 if (mToken != null) {
6055 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
6056 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01006057 if (mActionsToShowInCompact != null) {
6058 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
6059 }
Dan Sandler842dd772014-05-15 09:36:47 -04006060 }
6061
Christoph Studer4600f9b2014-07-22 22:44:43 +02006062 /**
6063 * @hide
6064 */
6065 @Override
6066 protected void restoreFromExtras(Bundle extras) {
6067 super.restoreFromExtras(extras);
6068
6069 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
6070 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
6071 }
6072 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
6073 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
6074 }
6075 }
6076
Selim Cinek5bf069a2015-11-10 19:14:27 -05006077 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04006078 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07006079 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07006080 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04006081 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05006082 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
6083 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04006084 if (!tombstone) {
6085 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
6086 }
6087 button.setContentDescription(R.id.action0, action.title);
6088 return button;
6089 }
6090
6091 private RemoteViews makeMediaContentView() {
6092 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006093 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04006094
6095 final int numActions = mBuilder.mActions.size();
6096 final int N = mActionsToShowInCompact == null
6097 ? 0
6098 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
6099 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006100 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04006101 for (int i = 0; i < N; i++) {
6102 if (i >= numActions) {
6103 throw new IllegalArgumentException(String.format(
6104 "setShowActionsInCompactView: action %d out of bounds (max %d)",
6105 i, numActions - 1));
6106 }
6107
6108 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05006109 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08006110 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006111 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006112 }
6113 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08006114 handleImage(view);
6115 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006116 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006117 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006118 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08006119 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006120 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04006121 return view;
6122 }
6123
Selim Cinek99104832017-01-25 14:47:33 -08006124 private int getPrimaryHighlightColor() {
6125 return mBuilder.getPrimaryHighlightColor();
6126 }
6127
Dan Sandler842dd772014-05-15 09:36:47 -04006128 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006129 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006130 // Dont add an expanded view if there is no more content to be revealed
6131 int actionsInCompact = mActionsToShowInCompact == null
6132 ? 0
6133 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07006134 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006135 return null;
6136 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006137 RemoteViews big = mBuilder.applyStandardTemplate(
6138 R.layout.notification_template_material_big_media,
6139 false);
Dan Sandler842dd772014-05-15 09:36:47 -04006140
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006141 if (actionCount > 0) {
6142 big.removeAllViews(com.android.internal.R.id.media_actions);
6143 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05006144 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08006145 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006146 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006147 }
6148 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006149 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04006150 return big;
6151 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006152
Selim Cinek5bf069a2015-11-10 19:14:27 -05006153 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07006154 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006155 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
6156 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006157 }
6158 }
6159
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006160 /**
6161 * @hide
6162 */
6163 @Override
6164 protected boolean hasProgress() {
6165 return false;
6166 }
Dan Sandler842dd772014-05-15 09:36:47 -04006167 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006168
Selim Cinek593610c2016-02-16 18:42:57 -08006169 /**
6170 * Notification style for custom views that are decorated by the system
6171 *
6172 * <p>Instead of providing a notification that is completely custom, a developer can set this
6173 * style and still obtain system decorations like the notification header with the expand
6174 * affordance and actions.
6175 *
6176 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6177 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6178 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6179 * corresponding custom views to display.
6180 *
6181 * To use this style with your Notification, feed it to
6182 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6183 * <pre class="prettyprint">
6184 * Notification noti = new Notification.Builder()
6185 * .setSmallIcon(R.drawable.ic_stat_player)
6186 * .setLargeIcon(albumArtBitmap))
6187 * .setCustomContentView(contentView);
6188 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
6189 * .build();
6190 * </pre>
6191 */
6192 public static class DecoratedCustomViewStyle extends Style {
6193
6194 public DecoratedCustomViewStyle() {
6195 }
6196
Selim Cinek593610c2016-02-16 18:42:57 -08006197 /**
6198 * @hide
6199 */
6200 public boolean displayCustomViewInline() {
6201 return true;
6202 }
6203
6204 /**
6205 * @hide
6206 */
6207 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006208 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006209 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
6210 }
6211
6212 /**
6213 * @hide
6214 */
6215 @Override
6216 public RemoteViews makeBigContentView() {
6217 return makeDecoratedBigContentView();
6218 }
6219
6220 /**
6221 * @hide
6222 */
6223 @Override
6224 public RemoteViews makeHeadsUpContentView() {
6225 return makeDecoratedHeadsUpContentView();
6226 }
6227
Selim Cinek593610c2016-02-16 18:42:57 -08006228 private RemoteViews makeDecoratedHeadsUpContentView() {
6229 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
6230 ? mBuilder.mN.contentView
6231 : mBuilder.mN.headsUpContentView;
6232 if (mBuilder.mActions.size() == 0) {
6233 return makeStandardTemplateWithCustomContent(headsUpContentView);
6234 }
6235 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6236 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006237 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006238 return remoteViews;
6239 }
6240
Selim Cinek593610c2016-02-16 18:42:57 -08006241 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
6242 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
6243 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006244 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08006245 return remoteViews;
6246 }
6247
Selim Cinek593610c2016-02-16 18:42:57 -08006248 private RemoteViews makeDecoratedBigContentView() {
6249 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
6250 ? mBuilder.mN.contentView
6251 : mBuilder.mN.bigContentView;
6252 if (mBuilder.mActions.size() == 0) {
6253 return makeStandardTemplateWithCustomContent(bigContentView);
6254 }
6255 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6256 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006257 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006258 return remoteViews;
6259 }
Selim Cinek247fa012016-02-18 09:50:48 -08006260
6261 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
6262 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08006263 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07006264 // Need to clone customContent before adding, because otherwise it can no longer be
6265 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08006266 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07006267 remoteViews.removeAllViews(R.id.notification_main_column);
6268 remoteViews.addView(R.id.notification_main_column, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08006269 }
Selim Cinek247fa012016-02-18 09:50:48 -08006270 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006271 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006272 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006273 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08006274 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006275 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08006276 }
Selim Cinek593610c2016-02-16 18:42:57 -08006277 }
6278
Selim Cinek03eb3b72016-02-18 10:39:45 -08006279 /**
6280 * Notification style for media custom views that are decorated by the system
6281 *
6282 * <p>Instead of providing a media notification that is completely custom, a developer can set
6283 * this style and still obtain system decorations like the notification header with the expand
6284 * affordance and actions.
6285 *
6286 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6287 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6288 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6289 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08006290 * <p>
6291 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
6292 * notification by using {@link Notification.Builder#setColorized(boolean)}.
6293 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08006294 * To use this style with your Notification, feed it to
6295 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6296 * <pre class="prettyprint">
6297 * Notification noti = new Notification.Builder()
6298 * .setSmallIcon(R.drawable.ic_stat_player)
6299 * .setLargeIcon(albumArtBitmap))
6300 * .setCustomContentView(contentView);
6301 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
6302 * .setMediaSession(mySession))
6303 * .build();
6304 * </pre>
6305 *
6306 * @see android.app.Notification.DecoratedCustomViewStyle
6307 * @see android.app.Notification.MediaStyle
6308 */
6309 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
6310
6311 public DecoratedMediaCustomViewStyle() {
6312 }
6313
Selim Cinek03eb3b72016-02-18 10:39:45 -08006314 /**
6315 * @hide
6316 */
6317 public boolean displayCustomViewInline() {
6318 return true;
6319 }
6320
6321 /**
6322 * @hide
6323 */
6324 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006325 public RemoteViews makeContentView(boolean increasedHeight) {
6326 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006327 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6328 mBuilder.mN.contentView);
6329 }
6330
6331 /**
6332 * @hide
6333 */
6334 @Override
6335 public RemoteViews makeBigContentView() {
6336 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
6337 ? mBuilder.mN.bigContentView
6338 : mBuilder.mN.contentView;
6339 return makeBigContentViewWithCustomContent(customRemoteView);
6340 }
6341
6342 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
6343 RemoteViews remoteViews = super.makeBigContentView();
6344 if (remoteViews != null) {
6345 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
6346 customRemoteView);
6347 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08006348 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006349 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6350 customRemoteView);
6351 } else {
6352 return null;
6353 }
6354 }
6355
6356 /**
6357 * @hide
6358 */
6359 @Override
6360 public RemoteViews makeHeadsUpContentView() {
6361 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
6362 ? mBuilder.mN.headsUpContentView
6363 : mBuilder.mN.contentView;
6364 return makeBigContentViewWithCustomContent(customRemoteView);
6365 }
6366
6367 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
6368 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08006369 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07006370 // Need to clone customContent before adding, because otherwise it can no longer be
6371 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08006372 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07006373 remoteViews.removeAllViews(id);
6374 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08006375 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08006376 return remoteViews;
6377 }
6378 }
6379
Christoph Studer4600f9b2014-07-22 22:44:43 +02006380 // When adding a new Style subclass here, don't forget to update
6381 // Builder.getNotificationStyleClass.
6382
Griff Hazen61a9e862014-05-22 16:05:19 -07006383 /**
6384 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
6385 * metadata or change options on a notification builder.
6386 */
6387 public interface Extender {
6388 /**
6389 * Apply this extender to a notification builder.
6390 * @param builder the builder to be modified.
6391 * @return the build object for chaining.
6392 */
6393 public Builder extend(Builder builder);
6394 }
6395
6396 /**
6397 * Helper class to add wearable extensions to notifications.
6398 * <p class="note"> See
6399 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
6400 * for Android Wear</a> for more information on how to use this class.
6401 * <p>
6402 * To create a notification with wearable extensions:
6403 * <ol>
6404 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
6405 * properties.
6406 * <li>Create a {@link android.app.Notification.WearableExtender}.
6407 * <li>Set wearable-specific properties using the
6408 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
6409 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
6410 * notification.
6411 * <li>Post the notification to the notification system with the
6412 * {@code NotificationManager.notify(...)} methods.
6413 * </ol>
6414 *
6415 * <pre class="prettyprint">
6416 * Notification notif = new Notification.Builder(mContext)
6417 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6418 * .setContentText(subject)
6419 * .setSmallIcon(R.drawable.new_mail)
6420 * .extend(new Notification.WearableExtender()
6421 * .setContentIcon(R.drawable.new_mail))
6422 * .build();
6423 * NotificationManager notificationManger =
6424 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
6425 * notificationManger.notify(0, notif);</pre>
6426 *
6427 * <p>Wearable extensions can be accessed on an existing notification by using the
6428 * {@code WearableExtender(Notification)} constructor,
6429 * and then using the {@code get} methods to access values.
6430 *
6431 * <pre class="prettyprint">
6432 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
6433 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07006434 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07006435 */
6436 public static final class WearableExtender implements Extender {
6437 /**
6438 * Sentinel value for an action index that is unset.
6439 */
6440 public static final int UNSET_ACTION_INDEX = -1;
6441
6442 /**
6443 * Size value for use with {@link #setCustomSizePreset} to show this notification with
6444 * default sizing.
6445 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07006446 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07006447 * on their content.
6448 */
6449 public static final int SIZE_DEFAULT = 0;
6450
6451 /**
6452 * Size value for use with {@link #setCustomSizePreset} to show this notification
6453 * with an extra small size.
6454 * <p>This value is only applicable for custom display notifications created using
6455 * {@link #setDisplayIntent}.
6456 */
6457 public static final int SIZE_XSMALL = 1;
6458
6459 /**
6460 * Size value for use with {@link #setCustomSizePreset} to show this notification
6461 * with a small size.
6462 * <p>This value is only applicable for custom display notifications created using
6463 * {@link #setDisplayIntent}.
6464 */
6465 public static final int SIZE_SMALL = 2;
6466
6467 /**
6468 * Size value for use with {@link #setCustomSizePreset} to show this notification
6469 * with a medium size.
6470 * <p>This value is only applicable for custom display notifications created using
6471 * {@link #setDisplayIntent}.
6472 */
6473 public static final int SIZE_MEDIUM = 3;
6474
6475 /**
6476 * Size value for use with {@link #setCustomSizePreset} to show this notification
6477 * with a large size.
6478 * <p>This value is only applicable for custom display notifications created using
6479 * {@link #setDisplayIntent}.
6480 */
6481 public static final int SIZE_LARGE = 4;
6482
Griff Hazend5f11f92014-05-27 15:40:09 -07006483 /**
6484 * Size value for use with {@link #setCustomSizePreset} to show this notification
6485 * full screen.
6486 * <p>This value is only applicable for custom display notifications created using
6487 * {@link #setDisplayIntent}.
6488 */
6489 public static final int SIZE_FULL_SCREEN = 5;
6490
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006491 /**
6492 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
6493 * short amount of time when this notification is displayed on the screen. This
6494 * is the default value.
6495 */
6496 public static final int SCREEN_TIMEOUT_SHORT = 0;
6497
6498 /**
6499 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
6500 * for a longer amount of time when this notification is displayed on the screen.
6501 */
6502 public static final int SCREEN_TIMEOUT_LONG = -1;
6503
Griff Hazen61a9e862014-05-22 16:05:19 -07006504 /** Notification extra which contains wearable extensions */
6505 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
6506
Pete Gastaf6781d2014-10-07 15:17:05 -04006507 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07006508 private static final String KEY_ACTIONS = "actions";
6509 private static final String KEY_FLAGS = "flags";
6510 private static final String KEY_DISPLAY_INTENT = "displayIntent";
6511 private static final String KEY_PAGES = "pages";
6512 private static final String KEY_BACKGROUND = "background";
6513 private static final String KEY_CONTENT_ICON = "contentIcon";
6514 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
6515 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
6516 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
6517 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
6518 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006519 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04006520 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006521 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07006522
6523 // Flags bitwise-ored to mFlags
6524 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
6525 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
6526 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
6527 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006528 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04006529 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04006530 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07006531
6532 // Default value for flags integer
6533 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
6534
6535 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
6536 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
6537
6538 private ArrayList<Action> mActions = new ArrayList<Action>();
6539 private int mFlags = DEFAULT_FLAGS;
6540 private PendingIntent mDisplayIntent;
6541 private ArrayList<Notification> mPages = new ArrayList<Notification>();
6542 private Bitmap mBackground;
6543 private int mContentIcon;
6544 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
6545 private int mContentActionIndex = UNSET_ACTION_INDEX;
6546 private int mCustomSizePreset = SIZE_DEFAULT;
6547 private int mCustomContentHeight;
6548 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006549 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04006550 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006551 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07006552
6553 /**
6554 * Create a {@link android.app.Notification.WearableExtender} with default
6555 * options.
6556 */
6557 public WearableExtender() {
6558 }
6559
6560 public WearableExtender(Notification notif) {
6561 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
6562 if (wearableBundle != null) {
6563 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
6564 if (actions != null) {
6565 mActions.addAll(actions);
6566 }
6567
6568 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
6569 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
6570
6571 Notification[] pages = getNotificationArrayFromBundle(
6572 wearableBundle, KEY_PAGES);
6573 if (pages != null) {
6574 Collections.addAll(mPages, pages);
6575 }
6576
6577 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
6578 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
6579 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
6580 DEFAULT_CONTENT_ICON_GRAVITY);
6581 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
6582 UNSET_ACTION_INDEX);
6583 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
6584 SIZE_DEFAULT);
6585 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
6586 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006587 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04006588 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006589 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07006590 }
6591 }
6592
6593 /**
6594 * Apply wearable extensions to a notification that is being built. This is typically
6595 * called by the {@link android.app.Notification.Builder#extend} method of
6596 * {@link android.app.Notification.Builder}.
6597 */
6598 @Override
6599 public Notification.Builder extend(Notification.Builder builder) {
6600 Bundle wearableBundle = new Bundle();
6601
6602 if (!mActions.isEmpty()) {
6603 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
6604 }
6605 if (mFlags != DEFAULT_FLAGS) {
6606 wearableBundle.putInt(KEY_FLAGS, mFlags);
6607 }
6608 if (mDisplayIntent != null) {
6609 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
6610 }
6611 if (!mPages.isEmpty()) {
6612 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
6613 new Notification[mPages.size()]));
6614 }
6615 if (mBackground != null) {
6616 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
6617 }
6618 if (mContentIcon != 0) {
6619 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
6620 }
6621 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
6622 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
6623 }
6624 if (mContentActionIndex != UNSET_ACTION_INDEX) {
6625 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
6626 mContentActionIndex);
6627 }
6628 if (mCustomSizePreset != SIZE_DEFAULT) {
6629 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
6630 }
6631 if (mCustomContentHeight != 0) {
6632 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
6633 }
6634 if (mGravity != DEFAULT_GRAVITY) {
6635 wearableBundle.putInt(KEY_GRAVITY, mGravity);
6636 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006637 if (mHintScreenTimeout != 0) {
6638 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
6639 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04006640 if (mDismissalId != null) {
6641 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
6642 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006643 if (mBridgeTag != null) {
6644 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
6645 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006646
6647 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
6648 return builder;
6649 }
6650
6651 @Override
6652 public WearableExtender clone() {
6653 WearableExtender that = new WearableExtender();
6654 that.mActions = new ArrayList<Action>(this.mActions);
6655 that.mFlags = this.mFlags;
6656 that.mDisplayIntent = this.mDisplayIntent;
6657 that.mPages = new ArrayList<Notification>(this.mPages);
6658 that.mBackground = this.mBackground;
6659 that.mContentIcon = this.mContentIcon;
6660 that.mContentIconGravity = this.mContentIconGravity;
6661 that.mContentActionIndex = this.mContentActionIndex;
6662 that.mCustomSizePreset = this.mCustomSizePreset;
6663 that.mCustomContentHeight = this.mCustomContentHeight;
6664 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006665 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04006666 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006667 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07006668 return that;
6669 }
6670
6671 /**
6672 * Add a wearable action to this notification.
6673 *
6674 * <p>When wearable actions are added using this method, the set of actions that
6675 * show on a wearable device splits from devices that only show actions added
6676 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
6677 * of which actions display on different devices.
6678 *
6679 * @param action the action to add to this notification
6680 * @return this object for method chaining
6681 * @see android.app.Notification.Action
6682 */
6683 public WearableExtender addAction(Action action) {
6684 mActions.add(action);
6685 return this;
6686 }
6687
6688 /**
6689 * Adds wearable actions to this notification.
6690 *
6691 * <p>When wearable actions are added using this method, the set of actions that
6692 * show on a wearable device splits from devices that only show actions added
6693 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
6694 * of which actions display on different devices.
6695 *
6696 * @param actions the actions to add to this notification
6697 * @return this object for method chaining
6698 * @see android.app.Notification.Action
6699 */
6700 public WearableExtender addActions(List<Action> actions) {
6701 mActions.addAll(actions);
6702 return this;
6703 }
6704
6705 /**
6706 * Clear all wearable actions present on this builder.
6707 * @return this object for method chaining.
6708 * @see #addAction
6709 */
6710 public WearableExtender clearActions() {
6711 mActions.clear();
6712 return this;
6713 }
6714
6715 /**
6716 * Get the wearable actions present on this notification.
6717 */
6718 public List<Action> getActions() {
6719 return mActions;
6720 }
6721
6722 /**
6723 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07006724 * this notification. The {@link PendingIntent} provided should be for an activity.
6725 *
6726 * <pre class="prettyprint">
6727 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
6728 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
6729 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
6730 * Notification notif = new Notification.Builder(context)
6731 * .extend(new Notification.WearableExtender()
6732 * .setDisplayIntent(displayPendingIntent)
6733 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
6734 * .build();</pre>
6735 *
6736 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07006737 * should have an empty task affinity. It is also recommended to use the device
6738 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07006739 *
6740 * <p>Example AndroidManifest.xml entry:
6741 * <pre class="prettyprint">
6742 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
6743 * android:exported=&quot;true&quot;
6744 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07006745 * android:taskAffinity=&quot;&quot;
6746 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07006747 *
6748 * @param intent the {@link PendingIntent} for an activity
6749 * @return this object for method chaining
6750 * @see android.app.Notification.WearableExtender#getDisplayIntent
6751 */
6752 public WearableExtender setDisplayIntent(PendingIntent intent) {
6753 mDisplayIntent = intent;
6754 return this;
6755 }
6756
6757 /**
6758 * Get the intent to launch inside of an activity view when displaying this
6759 * notification. This {@code PendingIntent} should be for an activity.
6760 */
6761 public PendingIntent getDisplayIntent() {
6762 return mDisplayIntent;
6763 }
6764
6765 /**
6766 * Add an additional page of content to display with this notification. The current
6767 * notification forms the first page, and pages added using this function form
6768 * subsequent pages. This field can be used to separate a notification into multiple
6769 * sections.
6770 *
6771 * @param page the notification to add as another page
6772 * @return this object for method chaining
6773 * @see android.app.Notification.WearableExtender#getPages
6774 */
6775 public WearableExtender addPage(Notification page) {
6776 mPages.add(page);
6777 return this;
6778 }
6779
6780 /**
6781 * Add additional pages of content to display with this notification. The current
6782 * notification forms the first page, and pages added using this function form
6783 * subsequent pages. This field can be used to separate a notification into multiple
6784 * sections.
6785 *
6786 * @param pages a list of notifications
6787 * @return this object for method chaining
6788 * @see android.app.Notification.WearableExtender#getPages
6789 */
6790 public WearableExtender addPages(List<Notification> pages) {
6791 mPages.addAll(pages);
6792 return this;
6793 }
6794
6795 /**
6796 * Clear all additional pages present on this builder.
6797 * @return this object for method chaining.
6798 * @see #addPage
6799 */
6800 public WearableExtender clearPages() {
6801 mPages.clear();
6802 return this;
6803 }
6804
6805 /**
6806 * Get the array of additional pages of content for displaying this notification. The
6807 * current notification forms the first page, and elements within this array form
6808 * subsequent pages. This field can be used to separate a notification into multiple
6809 * sections.
6810 * @return the pages for this notification
6811 */
6812 public List<Notification> getPages() {
6813 return mPages;
6814 }
6815
6816 /**
6817 * Set a background image to be displayed behind the notification content.
6818 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
6819 * will work with any notification style.
6820 *
6821 * @param background the background bitmap
6822 * @return this object for method chaining
6823 * @see android.app.Notification.WearableExtender#getBackground
6824 */
6825 public WearableExtender setBackground(Bitmap background) {
6826 mBackground = background;
6827 return this;
6828 }
6829
6830 /**
6831 * Get a background image to be displayed behind the notification content.
6832 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
6833 * will work with any notification style.
6834 *
6835 * @return the background image
6836 * @see android.app.Notification.WearableExtender#setBackground
6837 */
6838 public Bitmap getBackground() {
6839 return mBackground;
6840 }
6841
6842 /**
6843 * Set an icon that goes with the content of this notification.
6844 */
6845 public WearableExtender setContentIcon(int icon) {
6846 mContentIcon = icon;
6847 return this;
6848 }
6849
6850 /**
6851 * Get an icon that goes with the content of this notification.
6852 */
6853 public int getContentIcon() {
6854 return mContentIcon;
6855 }
6856
6857 /**
6858 * Set the gravity that the content icon should have within the notification display.
6859 * Supported values include {@link android.view.Gravity#START} and
6860 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
6861 * @see #setContentIcon
6862 */
6863 public WearableExtender setContentIconGravity(int contentIconGravity) {
6864 mContentIconGravity = contentIconGravity;
6865 return this;
6866 }
6867
6868 /**
6869 * Get the gravity that the content icon should have within the notification display.
6870 * Supported values include {@link android.view.Gravity#START} and
6871 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
6872 * @see #getContentIcon
6873 */
6874 public int getContentIconGravity() {
6875 return mContentIconGravity;
6876 }
6877
6878 /**
6879 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07006880 * this notification. This action will no longer display separately from the
6881 * notification's content.
6882 *
Griff Hazenca48d352014-05-28 22:37:13 -07006883 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07006884 * set, although the list of available actions comes from the main notification and not
6885 * from the child page's notification.
6886 *
6887 * @param actionIndex The index of the action to hoist onto the current notification page.
6888 * If wearable actions were added to the main notification, this index
6889 * will apply to that list, otherwise it will apply to the regular
6890 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07006891 */
6892 public WearableExtender setContentAction(int actionIndex) {
6893 mContentActionIndex = actionIndex;
6894 return this;
6895 }
6896
6897 /**
Griff Hazenca48d352014-05-28 22:37:13 -07006898 * Get the index of the notification action, if any, that was specified as being clickable
6899 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07006900 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07006901 *
Griff Hazenca48d352014-05-28 22:37:13 -07006902 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07006903 * set, although the list of available actions comes from the main notification and not
6904 * from the child page's notification.
6905 *
6906 * <p>If wearable specific actions were added to the main notification, this index will
6907 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07006908 *
6909 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07006910 */
6911 public int getContentAction() {
6912 return mContentActionIndex;
6913 }
6914
6915 /**
6916 * Set the gravity that this notification should have within the available viewport space.
6917 * Supported values include {@link android.view.Gravity#TOP},
6918 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
6919 * The default value is {@link android.view.Gravity#BOTTOM}.
6920 */
6921 public WearableExtender setGravity(int gravity) {
6922 mGravity = gravity;
6923 return this;
6924 }
6925
6926 /**
6927 * Get the gravity that this notification should have within the available viewport space.
6928 * Supported values include {@link android.view.Gravity#TOP},
6929 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
6930 * The default value is {@link android.view.Gravity#BOTTOM}.
6931 */
6932 public int getGravity() {
6933 return mGravity;
6934 }
6935
6936 /**
6937 * Set the custom size preset for the display of this notification out of the available
6938 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
6939 * {@link #SIZE_LARGE}.
6940 * <p>Some custom size presets are only applicable for custom display notifications created
6941 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
6942 * documentation for the preset in question. See also
6943 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
6944 */
6945 public WearableExtender setCustomSizePreset(int sizePreset) {
6946 mCustomSizePreset = sizePreset;
6947 return this;
6948 }
6949
6950 /**
6951 * Get the custom size preset for the display of this notification out of the available
6952 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
6953 * {@link #SIZE_LARGE}.
6954 * <p>Some custom size presets are only applicable for custom display notifications created
6955 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
6956 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
6957 */
6958 public int getCustomSizePreset() {
6959 return mCustomSizePreset;
6960 }
6961
6962 /**
6963 * Set the custom height in pixels for the display of this notification's content.
6964 * <p>This option is only available for custom display notifications created
6965 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
6966 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
6967 * {@link #getCustomContentHeight}.
6968 */
6969 public WearableExtender setCustomContentHeight(int height) {
6970 mCustomContentHeight = height;
6971 return this;
6972 }
6973
6974 /**
6975 * Get the custom height in pixels for the display of this notification's content.
6976 * <p>This option is only available for custom display notifications created
6977 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
6978 * {@link #setCustomContentHeight}.
6979 */
6980 public int getCustomContentHeight() {
6981 return mCustomContentHeight;
6982 }
6983
6984 /**
6985 * Set whether the scrolling position for the contents of this notification should start
6986 * at the bottom of the contents instead of the top when the contents are too long to
6987 * display within the screen. Default is false (start scroll at the top).
6988 */
6989 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
6990 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
6991 return this;
6992 }
6993
6994 /**
6995 * Get whether the scrolling position for the contents of this notification should start
6996 * at the bottom of the contents instead of the top when the contents are too long to
6997 * display within the screen. Default is false (start scroll at the top).
6998 */
6999 public boolean getStartScrollBottom() {
7000 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
7001 }
7002
7003 /**
7004 * Set whether the content intent is available when the wearable device is not connected
7005 * to a companion device. The user can still trigger this intent when the wearable device
7006 * is offline, but a visual hint will indicate that the content intent may not be available.
7007 * Defaults to true.
7008 */
7009 public WearableExtender setContentIntentAvailableOffline(
7010 boolean contentIntentAvailableOffline) {
7011 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
7012 return this;
7013 }
7014
7015 /**
7016 * Get whether the content intent is available when the wearable device is not connected
7017 * to a companion device. The user can still trigger this intent when the wearable device
7018 * is offline, but a visual hint will indicate that the content intent may not be available.
7019 * Defaults to true.
7020 */
7021 public boolean getContentIntentAvailableOffline() {
7022 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
7023 }
7024
7025 /**
7026 * Set a hint that this notification's icon should not be displayed.
7027 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
7028 * @return this object for method chaining
7029 */
7030 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
7031 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
7032 return this;
7033 }
7034
7035 /**
7036 * Get a hint that this notification's icon should not be displayed.
7037 * @return {@code true} if this icon should not be displayed, false otherwise.
7038 * The default value is {@code false} if this was never set.
7039 */
7040 public boolean getHintHideIcon() {
7041 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
7042 }
7043
7044 /**
7045 * Set a visual hint that only the background image of this notification should be
7046 * displayed, and other semantic content should be hidden. This hint is only applicable
7047 * to sub-pages added using {@link #addPage}.
7048 */
7049 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
7050 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
7051 return this;
7052 }
7053
7054 /**
7055 * Get a visual hint that only the background image of this notification should be
7056 * displayed, and other semantic content should be hidden. This hint is only applicable
7057 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
7058 */
7059 public boolean getHintShowBackgroundOnly() {
7060 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
7061 }
7062
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007063 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007064 * Set a hint that this notification's background should not be clipped if possible,
7065 * and should instead be resized to fully display on the screen, retaining the aspect
7066 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007067 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
7068 * @return this object for method chaining
7069 */
7070 public WearableExtender setHintAvoidBackgroundClipping(
7071 boolean hintAvoidBackgroundClipping) {
7072 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
7073 return this;
7074 }
7075
7076 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007077 * Get a hint that this notification's background should not be clipped if possible,
7078 * and should instead be resized to fully display on the screen, retaining the aspect
7079 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007080 * @return {@code true} if it's ok if the background is clipped on the screen, false
7081 * otherwise. The default value is {@code false} if this was never set.
7082 */
7083 public boolean getHintAvoidBackgroundClipping() {
7084 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
7085 }
7086
7087 /**
7088 * Set a hint that the screen should remain on for at least this duration when
7089 * this notification is displayed on the screen.
7090 * @param timeout The requested screen timeout in milliseconds. Can also be either
7091 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7092 * @return this object for method chaining
7093 */
7094 public WearableExtender setHintScreenTimeout(int timeout) {
7095 mHintScreenTimeout = timeout;
7096 return this;
7097 }
7098
7099 /**
7100 * Get the duration, in milliseconds, that the screen should remain on for
7101 * when this notification is displayed.
7102 * @return the duration in milliseconds if > 0, or either one of the sentinel values
7103 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7104 */
7105 public int getHintScreenTimeout() {
7106 return mHintScreenTimeout;
7107 }
7108
Alex Hills9ab3a232016-04-05 14:54:56 -04007109 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04007110 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
7111 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7112 * qr codes, as well as other simple black-and-white tickets.
7113 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
7114 * @return this object for method chaining
7115 */
7116 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
7117 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
7118 return this;
7119 }
7120
7121 /**
7122 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
7123 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7124 * qr codes, as well as other simple black-and-white tickets.
7125 * @return {@code true} if it should be displayed in ambient, false otherwise
7126 * otherwise. The default value is {@code false} if this was never set.
7127 */
7128 public boolean getHintAmbientBigPicture() {
7129 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
7130 }
7131
7132 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04007133 * Set a hint that this notification's content intent will launch an {@link Activity}
7134 * directly, telling the platform that it can generate the appropriate transitions.
7135 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
7136 * an activity and transitions should be generated, false otherwise.
7137 * @return this object for method chaining
7138 */
7139 public WearableExtender setHintContentIntentLaunchesActivity(
7140 boolean hintContentIntentLaunchesActivity) {
7141 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
7142 return this;
7143 }
7144
7145 /**
7146 * Get a hint that this notification's content intent will launch an {@link Activity}
7147 * directly, telling the platform that it can generate the appropriate transitions
7148 * @return {@code true} if the content intent will launch an activity and transitions should
7149 * be generated, false otherwise. The default value is {@code false} if this was never set.
7150 */
7151 public boolean getHintContentIntentLaunchesActivity() {
7152 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
7153 }
7154
Nadia Benbernou948627e2016-04-14 14:41:08 -04007155 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007156 * Sets the dismissal id for this notification. If a notification is posted with a
7157 * dismissal id, then when that notification is canceled, notifications on other wearables
7158 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04007159 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007160 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04007161 * @param dismissalId the dismissal id of the notification.
7162 * @return this object for method chaining
7163 */
7164 public WearableExtender setDismissalId(String dismissalId) {
7165 mDismissalId = dismissalId;
7166 return this;
7167 }
7168
7169 /**
7170 * Returns the dismissal id of the notification.
7171 * @return the dismissal id of the notification or null if it has not been set.
7172 */
7173 public String getDismissalId() {
7174 return mDismissalId;
7175 }
7176
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007177 /**
7178 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
7179 * posted from a phone to provide finer-grained control on what notifications are bridged
7180 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
7181 * Features to Notifications</a> for more information.
7182 * @param bridgeTag the bridge tag of the notification.
7183 * @return this object for method chaining
7184 */
7185 public WearableExtender setBridgeTag(String bridgeTag) {
7186 mBridgeTag = bridgeTag;
7187 return this;
7188 }
7189
7190 /**
7191 * Returns the bridge tag of the notification.
7192 * @return the bridge tag or null if not present.
7193 */
7194 public String getBridgeTag() {
7195 return mBridgeTag;
7196 }
7197
Griff Hazen61a9e862014-05-22 16:05:19 -07007198 private void setFlag(int mask, boolean value) {
7199 if (value) {
7200 mFlags |= mask;
7201 } else {
7202 mFlags &= ~mask;
7203 }
7204 }
7205 }
7206
7207 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007208 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
7209 * with car extensions:
7210 *
7211 * <ol>
7212 * <li>Create an {@link Notification.Builder}, setting any desired
7213 * properties.
7214 * <li>Create a {@link CarExtender}.
7215 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
7216 * {@link CarExtender}.
7217 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
7218 * to apply the extensions to a notification.
7219 * </ol>
7220 *
7221 * <pre class="prettyprint">
7222 * Notification notification = new Notification.Builder(context)
7223 * ...
7224 * .extend(new CarExtender()
7225 * .set*(...))
7226 * .build();
7227 * </pre>
7228 *
7229 * <p>Car extensions can be accessed on an existing notification by using the
7230 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
7231 * to access values.
7232 */
7233 public static final class CarExtender implements Extender {
7234 private static final String TAG = "CarExtender";
7235
7236 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
7237 private static final String EXTRA_LARGE_ICON = "large_icon";
7238 private static final String EXTRA_CONVERSATION = "car_conversation";
7239 private static final String EXTRA_COLOR = "app_color";
7240
7241 private Bitmap mLargeIcon;
7242 private UnreadConversation mUnreadConversation;
7243 private int mColor = Notification.COLOR_DEFAULT;
7244
7245 /**
7246 * Create a {@link CarExtender} with default options.
7247 */
7248 public CarExtender() {
7249 }
7250
7251 /**
7252 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
7253 *
7254 * @param notif The notification from which to copy options.
7255 */
7256 public CarExtender(Notification notif) {
7257 Bundle carBundle = notif.extras == null ?
7258 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
7259 if (carBundle != null) {
7260 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
7261 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
7262
7263 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
7264 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
7265 }
7266 }
7267
7268 /**
7269 * Apply car extensions to a notification that is being built. This is typically called by
7270 * the {@link Notification.Builder#extend(Notification.Extender)}
7271 * method of {@link Notification.Builder}.
7272 */
7273 @Override
7274 public Notification.Builder extend(Notification.Builder builder) {
7275 Bundle carExtensions = new Bundle();
7276
7277 if (mLargeIcon != null) {
7278 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
7279 }
7280 if (mColor != Notification.COLOR_DEFAULT) {
7281 carExtensions.putInt(EXTRA_COLOR, mColor);
7282 }
7283
7284 if (mUnreadConversation != null) {
7285 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
7286 carExtensions.putBundle(EXTRA_CONVERSATION, b);
7287 }
7288
7289 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
7290 return builder;
7291 }
7292
7293 /**
7294 * Sets the accent color to use when Android Auto presents the notification.
7295 *
7296 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
7297 * to accent the displayed notification. However, not all colors are acceptable in an
7298 * automotive setting. This method can be used to override the color provided in the
7299 * notification in such a situation.
7300 */
Tor Norbye80756e32015-03-02 09:39:27 -08007301 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007302 mColor = color;
7303 return this;
7304 }
7305
7306 /**
7307 * Gets the accent color.
7308 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007309 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007310 */
Tor Norbye80756e32015-03-02 09:39:27 -08007311 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007312 public int getColor() {
7313 return mColor;
7314 }
7315
7316 /**
7317 * Sets the large icon of the car notification.
7318 *
7319 * If no large icon is set in the extender, Android Auto will display the icon
7320 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
7321 *
7322 * @param largeIcon The large icon to use in the car notification.
7323 * @return This object for method chaining.
7324 */
7325 public CarExtender setLargeIcon(Bitmap largeIcon) {
7326 mLargeIcon = largeIcon;
7327 return this;
7328 }
7329
7330 /**
7331 * Gets the large icon used in this car notification, or null if no icon has been set.
7332 *
7333 * @return The large icon for the car notification.
7334 * @see CarExtender#setLargeIcon
7335 */
7336 public Bitmap getLargeIcon() {
7337 return mLargeIcon;
7338 }
7339
7340 /**
7341 * Sets the unread conversation in a message notification.
7342 *
7343 * @param unreadConversation The unread part of the conversation this notification conveys.
7344 * @return This object for method chaining.
7345 */
7346 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
7347 mUnreadConversation = unreadConversation;
7348 return this;
7349 }
7350
7351 /**
7352 * Returns the unread conversation conveyed by this notification.
7353 * @see #setUnreadConversation(UnreadConversation)
7354 */
7355 public UnreadConversation getUnreadConversation() {
7356 return mUnreadConversation;
7357 }
7358
7359 /**
7360 * A class which holds the unread messages from a conversation.
7361 */
7362 public static class UnreadConversation {
7363 private static final String KEY_AUTHOR = "author";
7364 private static final String KEY_TEXT = "text";
7365 private static final String KEY_MESSAGES = "messages";
7366 private static final String KEY_REMOTE_INPUT = "remote_input";
7367 private static final String KEY_ON_REPLY = "on_reply";
7368 private static final String KEY_ON_READ = "on_read";
7369 private static final String KEY_PARTICIPANTS = "participants";
7370 private static final String KEY_TIMESTAMP = "timestamp";
7371
7372 private final String[] mMessages;
7373 private final RemoteInput mRemoteInput;
7374 private final PendingIntent mReplyPendingIntent;
7375 private final PendingIntent mReadPendingIntent;
7376 private final String[] mParticipants;
7377 private final long mLatestTimestamp;
7378
7379 UnreadConversation(String[] messages, RemoteInput remoteInput,
7380 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
7381 String[] participants, long latestTimestamp) {
7382 mMessages = messages;
7383 mRemoteInput = remoteInput;
7384 mReadPendingIntent = readPendingIntent;
7385 mReplyPendingIntent = replyPendingIntent;
7386 mParticipants = participants;
7387 mLatestTimestamp = latestTimestamp;
7388 }
7389
7390 /**
7391 * Gets the list of messages conveyed by this notification.
7392 */
7393 public String[] getMessages() {
7394 return mMessages;
7395 }
7396
7397 /**
7398 * Gets the remote input that will be used to convey the response to a message list, or
7399 * null if no such remote input exists.
7400 */
7401 public RemoteInput getRemoteInput() {
7402 return mRemoteInput;
7403 }
7404
7405 /**
7406 * Gets the pending intent that will be triggered when the user replies to this
7407 * notification.
7408 */
7409 public PendingIntent getReplyPendingIntent() {
7410 return mReplyPendingIntent;
7411 }
7412
7413 /**
7414 * Gets the pending intent that Android Auto will send after it reads aloud all messages
7415 * in this object's message list.
7416 */
7417 public PendingIntent getReadPendingIntent() {
7418 return mReadPendingIntent;
7419 }
7420
7421 /**
7422 * Gets the participants in the conversation.
7423 */
7424 public String[] getParticipants() {
7425 return mParticipants;
7426 }
7427
7428 /**
7429 * Gets the firs participant in the conversation.
7430 */
7431 public String getParticipant() {
7432 return mParticipants.length > 0 ? mParticipants[0] : null;
7433 }
7434
7435 /**
7436 * Gets the timestamp of the conversation.
7437 */
7438 public long getLatestTimestamp() {
7439 return mLatestTimestamp;
7440 }
7441
7442 Bundle getBundleForUnreadConversation() {
7443 Bundle b = new Bundle();
7444 String author = null;
7445 if (mParticipants != null && mParticipants.length > 1) {
7446 author = mParticipants[0];
7447 }
7448 Parcelable[] messages = new Parcelable[mMessages.length];
7449 for (int i = 0; i < messages.length; i++) {
7450 Bundle m = new Bundle();
7451 m.putString(KEY_TEXT, mMessages[i]);
7452 m.putString(KEY_AUTHOR, author);
7453 messages[i] = m;
7454 }
7455 b.putParcelableArray(KEY_MESSAGES, messages);
7456 if (mRemoteInput != null) {
7457 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
7458 }
7459 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
7460 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
7461 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
7462 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
7463 return b;
7464 }
7465
7466 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
7467 if (b == null) {
7468 return null;
7469 }
7470 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
7471 String[] messages = null;
7472 if (parcelableMessages != null) {
7473 String[] tmp = new String[parcelableMessages.length];
7474 boolean success = true;
7475 for (int i = 0; i < tmp.length; i++) {
7476 if (!(parcelableMessages[i] instanceof Bundle)) {
7477 success = false;
7478 break;
7479 }
7480 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
7481 if (tmp[i] == null) {
7482 success = false;
7483 break;
7484 }
7485 }
7486 if (success) {
7487 messages = tmp;
7488 } else {
7489 return null;
7490 }
7491 }
7492
7493 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
7494 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
7495
7496 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
7497
7498 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
7499 if (participants == null || participants.length != 1) {
7500 return null;
7501 }
7502
7503 return new UnreadConversation(messages,
7504 remoteInput,
7505 onReply,
7506 onRead,
7507 participants, b.getLong(KEY_TIMESTAMP));
7508 }
7509 };
7510
7511 /**
7512 * Builder class for {@link CarExtender.UnreadConversation} objects.
7513 */
7514 public static class Builder {
7515 private final List<String> mMessages = new ArrayList<String>();
7516 private final String mParticipant;
7517 private RemoteInput mRemoteInput;
7518 private PendingIntent mReadPendingIntent;
7519 private PendingIntent mReplyPendingIntent;
7520 private long mLatestTimestamp;
7521
7522 /**
7523 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
7524 *
7525 * @param name The name of the other participant in the conversation.
7526 */
7527 public Builder(String name) {
7528 mParticipant = name;
7529 }
7530
7531 /**
7532 * Appends a new unread message to the list of messages for this conversation.
7533 *
7534 * The messages should be added from oldest to newest.
7535 *
7536 * @param message The text of the new unread message.
7537 * @return This object for method chaining.
7538 */
7539 public Builder addMessage(String message) {
7540 mMessages.add(message);
7541 return this;
7542 }
7543
7544 /**
7545 * Sets the pending intent and remote input which will convey the reply to this
7546 * notification.
7547 *
7548 * @param pendingIntent The pending intent which will be triggered on a reply.
7549 * @param remoteInput The remote input parcelable which will carry the reply.
7550 * @return This object for method chaining.
7551 *
7552 * @see CarExtender.UnreadConversation#getRemoteInput
7553 * @see CarExtender.UnreadConversation#getReplyPendingIntent
7554 */
7555 public Builder setReplyAction(
7556 PendingIntent pendingIntent, RemoteInput remoteInput) {
7557 mRemoteInput = remoteInput;
7558 mReplyPendingIntent = pendingIntent;
7559
7560 return this;
7561 }
7562
7563 /**
7564 * Sets the pending intent that will be sent once the messages in this notification
7565 * are read.
7566 *
7567 * @param pendingIntent The pending intent to use.
7568 * @return This object for method chaining.
7569 */
7570 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
7571 mReadPendingIntent = pendingIntent;
7572 return this;
7573 }
7574
7575 /**
7576 * Sets the timestamp of the most recent message in an unread conversation.
7577 *
7578 * If a messaging notification has been posted by your application and has not
7579 * yet been cancelled, posting a later notification with the same id and tag
7580 * but without a newer timestamp may result in Android Auto not displaying a
7581 * heads up notification for the later notification.
7582 *
7583 * @param timestamp The timestamp of the most recent message in the conversation.
7584 * @return This object for method chaining.
7585 */
7586 public Builder setLatestTimestamp(long timestamp) {
7587 mLatestTimestamp = timestamp;
7588 return this;
7589 }
7590
7591 /**
7592 * Builds a new unread conversation object.
7593 *
7594 * @return The new unread conversation object.
7595 */
7596 public UnreadConversation build() {
7597 String[] messages = mMessages.toArray(new String[mMessages.size()]);
7598 String[] participants = { mParticipant };
7599 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
7600 mReadPendingIntent, participants, mLatestTimestamp);
7601 }
7602 }
7603 }
7604
7605 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007606 * <p>Helper class to add Android TV extensions to notifications. To create a notification
7607 * with a TV extension:
7608 *
7609 * <ol>
7610 * <li>Create an {@link Notification.Builder}, setting any desired properties.
7611 * <li>Create a {@link TvExtender}.
7612 * <li>Set TV-specific properties using the {@code set} methods of
7613 * {@link TvExtender}.
7614 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
7615 * to apply the extension to a notification.
7616 * </ol>
7617 *
7618 * <pre class="prettyprint">
7619 * Notification notification = new Notification.Builder(context)
7620 * ...
7621 * .extend(new TvExtender()
7622 * .set*(...))
7623 * .build();
7624 * </pre>
7625 *
7626 * <p>TV extensions can be accessed on an existing notification by using the
7627 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
7628 * to access values.
7629 *
7630 * @hide
7631 */
7632 @SystemApi
7633 public static final class TvExtender implements Extender {
7634 private static final String TAG = "TvExtender";
7635
7636 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
7637 private static final String EXTRA_FLAGS = "flags";
7638 private static final String EXTRA_CONTENT_INTENT = "content_intent";
7639 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007640 private static final String EXTRA_CHANNEL_ID = "channel_id";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007641
7642 // Flags bitwise-ored to mFlags
7643 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
7644
7645 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007646 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007647 private PendingIntent mContentIntent;
7648 private PendingIntent mDeleteIntent;
7649
7650 /**
7651 * Create a {@link TvExtender} with default options.
7652 */
7653 public TvExtender() {
7654 mFlags = FLAG_AVAILABLE_ON_TV;
7655 }
7656
7657 /**
7658 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
7659 *
7660 * @param notif The notification from which to copy options.
7661 */
7662 public TvExtender(Notification notif) {
7663 Bundle bundle = notif.extras == null ?
7664 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
7665 if (bundle != null) {
7666 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007667 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007668 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
7669 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
7670 }
7671 }
7672
7673 /**
7674 * Apply a TV extension to a notification that is being built. This is typically called by
7675 * the {@link Notification.Builder#extend(Notification.Extender)}
7676 * method of {@link Notification.Builder}.
7677 */
7678 @Override
7679 public Notification.Builder extend(Notification.Builder builder) {
7680 Bundle bundle = new Bundle();
7681
7682 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007683 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007684 if (mContentIntent != null) {
7685 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
7686 }
7687
7688 if (mDeleteIntent != null) {
7689 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
7690 }
7691
7692 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
7693 return builder;
7694 }
7695
7696 /**
7697 * Returns true if this notification should be shown on TV. This method return true
7698 * if the notification was extended with a TvExtender.
7699 */
7700 public boolean isAvailableOnTv() {
7701 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
7702 }
7703
7704 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007705 * Specifies the channel the notification should be delivered on when shown on TV.
7706 * It can be different from the channel that the notification is delivered to when
7707 * posting on a non-TV device.
7708 */
7709 public TvExtender setChannel(String channelId) {
7710 mChannelId = channelId;
7711 return this;
7712 }
7713
7714 /**
7715 * Returns the id of the channel this notification posts to on TV.
7716 */
7717 public String getChannel() {
7718 return mChannelId;
7719 }
7720
7721 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007722 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
7723 * If provided, it is used instead of the content intent specified
7724 * at the level of Notification.
7725 */
7726 public TvExtender setContentIntent(PendingIntent intent) {
7727 mContentIntent = intent;
7728 return this;
7729 }
7730
7731 /**
7732 * Returns the TV-specific content intent. If this method returns null, the
7733 * main content intent on the notification should be used.
7734 *
7735 * @see {@link Notification#contentIntent}
7736 */
7737 public PendingIntent getContentIntent() {
7738 return mContentIntent;
7739 }
7740
7741 /**
7742 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
7743 * by the user on TV. If provided, it is used instead of the delete intent specified
7744 * at the level of Notification.
7745 */
7746 public TvExtender setDeleteIntent(PendingIntent intent) {
7747 mDeleteIntent = intent;
7748 return this;
7749 }
7750
7751 /**
7752 * Returns the TV-specific delete intent. If this method returns null, the
7753 * main delete intent on the notification should be used.
7754 *
7755 * @see {@link Notification#deleteIntent}
7756 */
7757 public PendingIntent getDeleteIntent() {
7758 return mDeleteIntent;
7759 }
7760 }
7761
7762 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07007763 * Get an array of Notification objects from a parcelable array bundle field.
7764 * Update the bundle to have a typed array so fetches in the future don't need
7765 * to do an array copy.
7766 */
7767 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
7768 Parcelable[] array = bundle.getParcelableArray(key);
7769 if (array instanceof Notification[] || array == null) {
7770 return (Notification[]) array;
7771 }
7772 Notification[] typedArray = Arrays.copyOf(array, array.length,
7773 Notification[].class);
7774 bundle.putParcelableArray(key, typedArray);
7775 return typedArray;
7776 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02007777
7778 private static class BuilderRemoteViews extends RemoteViews {
7779 public BuilderRemoteViews(Parcel parcel) {
7780 super(parcel);
7781 }
7782
Kenny Guy77320062014-08-27 21:37:15 +01007783 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
7784 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007785 }
7786
7787 @Override
7788 public BuilderRemoteViews clone() {
7789 Parcel p = Parcel.obtain();
7790 writeToParcel(p, 0);
7791 p.setDataPosition(0);
7792 BuilderRemoteViews brv = new BuilderRemoteViews(p);
7793 p.recycle();
7794 return brv;
7795 }
7796 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08007797
7798 private static class StandardTemplateParams {
7799 boolean hasProgress = true;
7800 boolean ambient = false;
7801 CharSequence title;
7802 CharSequence text;
7803
7804 final StandardTemplateParams reset() {
7805 hasProgress = true;
7806 ambient = false;
7807 title = null;
7808 text = null;
7809 return this;
7810 }
7811
7812 final StandardTemplateParams hasProgress(boolean hasProgress) {
7813 this.hasProgress = hasProgress;
7814 return this;
7815 }
7816
7817 final StandardTemplateParams title(CharSequence title) {
7818 this.title = title;
7819 return this;
7820 }
7821
7822 final StandardTemplateParams text(CharSequence text) {
7823 this.text = text;
7824 return this;
7825 }
7826
7827 final StandardTemplateParams ambient(boolean ambient) {
7828 this.ambient = ambient;
7829 return this;
7830 }
7831
7832 final StandardTemplateParams fillTextsFrom(Builder b) {
7833 Bundle extras = b.mN.extras;
7834 title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE));
7835 text = b.processLegacyText(extras.getCharSequence(EXTRA_TEXT));
7836 return this;
7837 }
7838 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007839}