blob: ab0b68d61df5b8d5b830d7b991fbf5bf6d54a287 [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;
Jorim Jaggief72a192014-08-26 21:57:46 +020031import android.content.res.ColorStateList;
Joe Onoratoef1e7762010-09-17 18:38:38 -040032import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010033import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070034import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010035import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010036import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040037import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040038import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070039import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080040import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070041import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040043import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020044import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050045import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080046import android.os.Parcel;
47import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040048import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070049import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070050import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080051import android.text.SpannableStringBuilder;
52import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080054import android.text.style.AbsoluteSizeSpan;
Selim Cinek981962e2016-07-20 20:41:58 -070055import android.text.style.BackgroundColorSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080056import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070057import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080058import android.text.style.RelativeSizeSpan;
59import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070060import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040061import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050062import android.util.SparseArray;
Griff Hazen61a9e862014-05-22 16:05:19 -070063import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080064import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080065import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070066import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070067import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068import android.widget.RemoteViews;
69
Griff Hazen959591e2014-05-15 22:26:18 -070070import com.android.internal.R;
Svet Ganovddb94882016-06-23 19:55:24 -070071import com.android.internal.util.ArrayUtils;
Griff Hazenc091ba82014-05-16 10:13:26 -070072import com.android.internal.util.NotificationColorUtil;
Adrian Roos487374f2017-01-11 15:48:14 -080073import com.android.internal.util.Preconditions;
Griff Hazen959591e2014-05-15 22:26:18 -070074
Tor Norbyed9273d62013-05-30 15:59:53 -070075import java.lang.annotation.Retention;
76import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020077import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050078import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070079import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070080import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070081import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050082import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080083
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084/**
85 * A class that represents how a persistent notification is to be presented to
86 * the user using the {@link android.app.NotificationManager}.
87 *
Joe Onoratocb109a02011-01-18 17:57:41 -080088 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
89 * easier to construct Notifications.</p>
90 *
Joe Fernandez558459f2011-10-13 16:47:36 -070091 * <div class="special reference">
92 * <h3>Developer Guides</h3>
93 * <p>For a guide to creating notifications, read the
94 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
95 * developer guide.</p>
96 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 */
98public class Notification implements Parcelable
99{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400100 private static final String TAG = "Notification";
101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400103 * An activity that provides a user interface for adjusting notification preferences for its
104 * containing application. Optional but recommended for apps that post
105 * {@link android.app.Notification Notifications}.
106 */
107 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
108 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
109 = "android.intent.category.NOTIFICATION_PREFERENCES";
110
111 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 * Use all default values (where applicable).
113 */
114 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500115
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 /**
117 * Use the default notification sound. This will ignore any given
118 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500119 *
Chris Wren47c20a12014-06-18 17:27:29 -0400120 * <p>
121 * A notification that is noisy is more likely to be presented as a heads-up notification.
122 * </p>
123 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500125 */
126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 public static final int DEFAULT_SOUND = 1;
128
129 /**
130 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500131 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700132 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500133 *
Chris Wren47c20a12014-06-18 17:27:29 -0400134 * <p>
135 * A notification that vibrates is more likely to be presented as a heads-up notification.
136 * </p>
137 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500139 */
140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 /**
144 * Use the default notification lights. This will ignore the
145 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
146 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500147 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500149 */
150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200154 * Maximum length of CharSequences accepted by Builder and friends.
155 *
156 * <p>
157 * Avoids spamming the system with overly large strings such as full e-mails.
158 */
159 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
160
161 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800162 * Maximum entries of reply text that are accepted by Builder and friends.
163 */
164 private static final int MAX_REPLY_HISTORY = 5;
165
166 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500167 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800168 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500169 * Default value: {@link System#currentTimeMillis() Now}.
170 *
171 * Choose a timestamp that will be most relevant to the user. For most finite events, this
172 * corresponds to the time the event happened (or will happen, in the case of events that have
173 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800174 * timestamped according to when the activity began.
175 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500176 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800177 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500178 * <ul>
179 * <li>Notification of a new chat message should be stamped when the message was received.</li>
180 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
181 * <li>Notification of a completed file download should be stamped when the download finished.</li>
182 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
183 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
184 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800185 * </ul>
186 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700187 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
188 * anymore by default and must be opted into by using
189 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 */
191 public long when;
192
193 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700194 * The creation time of the notification
195 */
196 private long creationTime;
197
198 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400200 *
201 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 */
Dan Sandler86647982015-05-13 23:41:13 -0400203 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700204 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 public int icon;
206
207 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800208 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
209 * leave it at its default value of 0.
210 *
211 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700212 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800213 */
214 public int iconLevel;
215
216 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500217 * The number of events that this notification represents. For example, in a new mail
218 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800219 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500220 * The system may or may not use this field to modify the appearance of the notification. For
221 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
222 * superimposed over the icon in the status bar. Starting with
223 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
224 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800225 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500226 * If the number is 0 or negative, it is never shown.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -0700227 *
228 * @deprecated this number is not shown anymore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700230 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800231 public int number;
232
233 /**
234 * The intent to execute when the expanded status entry is clicked. If
235 * this is an activity, it must include the
236 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800237 * that you take care of task management as described in the
238 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800239 * Stack</a> document. In particular, make sure to read the notification section
240 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
241 * Notifications</a> for the correct ways to launch an application from a
242 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 */
244 public PendingIntent contentIntent;
245
246 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500247 * The intent to execute when the notification is explicitly dismissed by the user, either with
248 * the "Clear All" button or by swiping it away individually.
249 *
250 * This probably shouldn't be launching an activity since several of those will be sent
251 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 */
253 public PendingIntent deleteIntent;
254
255 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700256 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800257 *
Chris Wren47c20a12014-06-18 17:27:29 -0400258 * <p>
259 * The system UI may choose to display a heads-up notification, instead of
260 * launching this intent, while the user is using the device.
261 * </p>
262 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800263 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400264 */
265 public PendingIntent fullScreenIntent;
266
267 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400268 * Text that summarizes this notification for accessibility services.
269 *
270 * As of the L release, this text is no longer shown on screen, but it is still useful to
271 * accessibility services (where it serves as an audible announcement of the notification's
272 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400273 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800274 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 */
276 public CharSequence tickerText;
277
278 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400279 * Formerly, a view showing the {@link #tickerText}.
280 *
281 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400282 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400283 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800284 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400285
286 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400287 * The view that will represent this notification in the notification list (which is pulled
288 * down from the status bar).
289 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500290 * As of N, this field may be null. The notification view is determined by the inputs
291 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400292 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800293 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400294 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 public RemoteViews contentView;
296
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400297 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400298 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400299 * opportunity to show more detail. The system UI may choose to show this
300 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400301 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500302 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400303 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
304 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400305 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400306 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400307 public RemoteViews bigContentView;
308
Chris Wren8fd39ec2014-02-27 17:43:26 -0500309
310 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400311 * A medium-format version of {@link #contentView}, providing the Notification an
312 * opportunity to add action buttons to contentView. At its discretion, the system UI may
313 * choose to show this as a heads-up notification, which will pop up so the user can see
314 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400315 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500316 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400317 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
318 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500319 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400320 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500321 public RemoteViews headsUpContentView;
322
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400323 /**
Dan Sandler86647982015-05-13 23:41:13 -0400324 * A large bitmap to be shown in the notification content area.
325 *
326 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 */
Dan Sandler86647982015-05-13 23:41:13 -0400328 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800329 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330
331 /**
332 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500333 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800334 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400335 * A notification that is noisy is more likely to be presented as a heads-up notification.
336 * </p>
337 *
338 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500339 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 * </p>
341 */
342 public Uri sound;
343
344 /**
345 * Use this constant as the value for audioStreamType to request that
346 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700347 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400348 *
349 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700351 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 public static final int STREAM_DEFAULT = -1;
353
354 /**
355 * The audio stream type to use when playing the sound.
356 * Should be one of the STREAM_ constants from
357 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400358 *
359 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700361 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 public int audioStreamType = STREAM_DEFAULT;
363
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400365 * The default value of {@link #audioAttributes}.
366 */
367 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
368 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
369 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
370 .build();
371
372 /**
373 * The {@link AudioAttributes audio attributes} to use when playing the sound.
374 */
375 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
376
377 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500378 * The pattern with which to vibrate.
379 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 * <p>
381 * To vibrate the default pattern, see {@link #defaults}.
382 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500383 *
Chris Wren47c20a12014-06-18 17:27:29 -0400384 * <p>
385 * A notification that vibrates is more likely to be presented as a heads-up notification.
386 * </p>
387 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800388 * @see android.os.Vibrator#vibrate(long[],int)
389 */
390 public long[] vibrate;
391
392 /**
393 * The color of the led. The hardware will do its best approximation.
394 *
395 * @see #FLAG_SHOW_LIGHTS
396 * @see #flags
397 */
Tor Norbye80756e32015-03-02 09:39:27 -0800398 @ColorInt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399 public int ledARGB;
400
401 /**
402 * The number of milliseconds for the LED to be on while it's flashing.
403 * The hardware will do its best approximation.
404 *
405 * @see #FLAG_SHOW_LIGHTS
406 * @see #flags
407 */
408 public int ledOnMS;
409
410 /**
411 * The number of milliseconds for the LED to be off while it's flashing.
412 * The hardware will do its best approximation.
413 *
414 * @see #FLAG_SHOW_LIGHTS
415 * @see #flags
416 */
417 public int ledOffMS;
418
419 /**
420 * Specifies which values should be taken from the defaults.
421 * <p>
422 * To set, OR the desired from {@link #DEFAULT_SOUND},
423 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
424 * values, use {@link #DEFAULT_ALL}.
425 * </p>
426 */
427 public int defaults;
428
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 /**
430 * Bit to be bitwise-ored into the {@link #flags} field that should be
431 * set if you want the LED on for this notification.
432 * <ul>
433 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
434 * or 0 for both ledOnMS and ledOffMS.</li>
435 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
436 * <li>To flash the LED, pass the number of milliseconds that it should
437 * be on and off to ledOnMS and ledOffMS.</li>
438 * </ul>
439 * <p>
440 * Since hardware varies, you are not guaranteed that any of the values
441 * you pass are honored exactly. Use the system defaults (TODO) if possible
442 * because they will be set to values that work on any given hardware.
443 * <p>
444 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500445 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800446 */
447 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
448
449 /**
450 * Bit to be bitwise-ored into the {@link #flags} field that should be
451 * set if this notification is in reference to something that is ongoing,
452 * like a phone call. It should not be set if this notification is in
453 * reference to something that happened at a particular point in time,
454 * like a missed phone call.
455 */
456 public static final int FLAG_ONGOING_EVENT = 0x00000002;
457
458 /**
459 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700460 * the audio will be repeated until the notification is
461 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 */
463 public static final int FLAG_INSISTENT = 0x00000004;
464
465 /**
466 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700467 * set if you would only like the sound, vibrate and ticker to be played
468 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800469 */
470 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
471
472 /**
473 * Bit to be bitwise-ored into the {@link #flags} field that should be
474 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500475 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800476 */
477 public static final int FLAG_AUTO_CANCEL = 0x00000010;
478
479 /**
480 * Bit to be bitwise-ored into the {@link #flags} field that should be
481 * set if the notification should not be canceled when the user clicks
482 * the Clear all button.
483 */
484 public static final int FLAG_NO_CLEAR = 0x00000020;
485
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700486 /**
487 * Bit to be bitwise-ored into the {@link #flags} field that should be
488 * set if this notification represents a currently running service. This
489 * will normally be set for you by {@link Service#startForeground}.
490 */
491 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
492
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400493 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500494 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800495 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500496 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400497 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700498 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500499 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400500
Griff Hazendfcb0802014-02-11 12:00:00 -0800501 /**
502 * Bit to be bitswise-ored into the {@link #flags} field that should be
503 * set if this notification is relevant to the current device only
504 * and it is not recommended that it bridge to other devices.
505 */
506 public static final int FLAG_LOCAL_ONLY = 0x00000100;
507
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700508 /**
509 * Bit to be bitswise-ored into the {@link #flags} field that should be
510 * set if this notification is the group summary for a group of notifications.
511 * Grouped notifications may display in a cluster or stack on devices which
512 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
513 */
514 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
515
Julia Reynoldse46bb372016-03-17 11:05:58 -0400516 /**
517 * Bit to be bitswise-ored into the {@link #flags} field that should be
518 * set if this notification is the group summary for an auto-group of notifications.
519 *
520 * @hide
521 */
522 @SystemApi
523 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
524
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800525 public int flags;
526
Tor Norbyed9273d62013-05-30 15:59:53 -0700527 /** @hide */
528 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
529 @Retention(RetentionPolicy.SOURCE)
530 public @interface Priority {}
531
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800532 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500533 * Default notification {@link #priority}. If your application does not prioritize its own
534 * notifications, use this value for all notifications.
535 */
536 public static final int PRIORITY_DEFAULT = 0;
537
538 /**
539 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
540 * items smaller, or at a different position in the list, compared with your app's
541 * {@link #PRIORITY_DEFAULT} items.
542 */
543 public static final int PRIORITY_LOW = -1;
544
545 /**
546 * Lowest {@link #priority}; these items might not be shown to the user except under special
547 * circumstances, such as detailed notification logs.
548 */
549 public static final int PRIORITY_MIN = -2;
550
551 /**
552 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
553 * show these items larger, or at a different position in notification lists, compared with
554 * your app's {@link #PRIORITY_DEFAULT} items.
555 */
556 public static final int PRIORITY_HIGH = 1;
557
558 /**
559 * Highest {@link #priority}, for your application's most important items that require the
560 * user's prompt attention or input.
561 */
562 public static final int PRIORITY_MAX = 2;
563
564 /**
565 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800566 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500567 * Priority is an indication of how much of the user's valuable attention should be consumed by
568 * this notification. Low-priority notifications may be hidden from the user in certain
569 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500570 * system will make a determination about how to interpret this priority when presenting
571 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400572 *
573 * <p>
574 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
575 * as a heads-up notification.
576 * </p>
577 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500578 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700579 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500580 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800581
Dan Sandler26e81cf2014-05-06 10:01:27 -0400582 /**
583 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
584 * to be applied by the standard Style templates when presenting this notification.
585 *
586 * The current template design constructs a colorful header image by overlaying the
587 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
588 * ignored.
589 */
Tor Norbye80756e32015-03-02 09:39:27 -0800590 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400591 public int color = COLOR_DEFAULT;
592
593 /**
594 * Special value of {@link #color} telling the system not to decorate this notification with
595 * any special color but instead use default colors when presenting this notification.
596 */
Tor Norbye80756e32015-03-02 09:39:27 -0800597 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400598 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600599
600 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800601 * Special value of {@link #color} used as a place holder for an invalid color.
602 */
603 @ColorInt
604 private static final int COLOR_INVALID = 1;
605
606 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700607 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
608 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600609 * lockscreen).
610 *
611 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
612 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
613 * shown in all situations, but the contents are only available if the device is unlocked for
614 * the appropriate user.
615 *
616 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
617 * can be read even in an "insecure" context (that is, above a secure lockscreen).
618 * To modify the public version of this notification—for example, to redact some portions—see
619 * {@link Builder#setPublicVersion(Notification)}.
620 *
621 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
622 * and ticker until the user has bypassed the lockscreen.
623 */
624 public int visibility;
625
Griff Hazenfc3922d2014-08-20 11:56:44 -0700626 /**
627 * Notification visibility: Show this notification in its entirety on all lockscreens.
628 *
629 * {@see #visibility}
630 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600631 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700632
633 /**
634 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
635 * private information on secure lockscreens.
636 *
637 * {@see #visibility}
638 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600639 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700640
641 /**
642 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
643 *
644 * {@see #visibility}
645 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600646 public static final int VISIBILITY_SECRET = -1;
647
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500648 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400649 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500650 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400651 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500652
653 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400654 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500655 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400656 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500657
658 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400659 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500660 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400661 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500662
663 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400664 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500665 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400666 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500667
668 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400669 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500670 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400671 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500672
673 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400674 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500675 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400676 public static final String CATEGORY_ALARM = "alarm";
677
678 /**
679 * Notification category: progress of a long-running background operation.
680 */
681 public static final String CATEGORY_PROGRESS = "progress";
682
683 /**
684 * Notification category: social network or sharing update.
685 */
686 public static final String CATEGORY_SOCIAL = "social";
687
688 /**
689 * Notification category: error in background operation or authentication status.
690 */
691 public static final String CATEGORY_ERROR = "err";
692
693 /**
694 * Notification category: media transport control for playback.
695 */
696 public static final String CATEGORY_TRANSPORT = "transport";
697
698 /**
699 * Notification category: system or device status update. Reserved for system use.
700 */
701 public static final String CATEGORY_SYSTEM = "sys";
702
703 /**
704 * Notification category: indication of running background service.
705 */
706 public static final String CATEGORY_SERVICE = "service";
707
708 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400709 * Notification category: a specific, timely recommendation for a single thing.
710 * For example, a news app might want to recommend a news story it believes the user will
711 * want to read next.
712 */
713 public static final String CATEGORY_RECOMMENDATION = "recommendation";
714
715 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400716 * Notification category: ongoing information about device or contextual status.
717 */
718 public static final String CATEGORY_STATUS = "status";
719
720 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400721 * Notification category: user-scheduled reminder.
722 */
723 public static final String CATEGORY_REMINDER = "reminder";
724
725 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400726 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
727 * that best describes this Notification. May be used by the system for ranking and filtering.
728 */
729 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500730
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700731 private String mGroupKey;
732
733 /**
734 * Get the key used to group this notification into a cluster or stack
735 * with other notifications on devices which support such rendering.
736 */
737 public String getGroup() {
738 return mGroupKey;
739 }
740
741 private String mSortKey;
742
743 /**
744 * Get a sort key that orders this notification among other notifications from the
745 * same package. This can be useful if an external sort was already applied and an app
746 * would like to preserve this. Notifications will be sorted lexicographically using this
747 * value, although providing different priorities in addition to providing sort key may
748 * cause this value to be ignored.
749 *
750 * <p>This sort key can also be used to order members of a notification group. See
751 * {@link Builder#setGroup}.
752 *
753 * @see String#compareTo(String)
754 */
755 public String getSortKey() {
756 return mSortKey;
757 }
758
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500759 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400760 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400761 * <p>
762 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
763 * APIs, and are intended to be used by
764 * {@link android.service.notification.NotificationListenerService} implementations to extract
765 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500766 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400767 public Bundle extras = new Bundle();
768
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400769 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700770 * All pending intents in the notification as the system needs to be able to access them but
771 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700772 * custom parcelable objects.
773 *
774 * @hide
775 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700776 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700777
778 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400779 * {@link #extras} key: this is the title of the notification,
780 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
781 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500782 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400783
784 /**
785 * {@link #extras} key: this is the title of the notification when shown in expanded form,
786 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
787 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400788 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400789
790 /**
791 * {@link #extras} key: this is the main text payload, as supplied to
792 * {@link Builder#setContentText(CharSequence)}.
793 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500794 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400795
796 /**
797 * {@link #extras} key: this is a third line of text, as supplied to
798 * {@link Builder#setSubText(CharSequence)}.
799 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400800 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400801
802 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800803 * {@link #extras} key: this is the remote input history, as supplied to
804 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700805 *
806 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
807 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
808 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
809 * notifications once the other party has responded).
810 *
811 * The extra with this key is of type CharSequence[] and contains the most recent entry at
812 * the 0 index, the second most recent at the 1 index, etc.
813 *
814 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800815 */
816 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
817
818 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400819 * {@link #extras} key: this is a small piece of additional text as supplied to
820 * {@link Builder#setContentInfo(CharSequence)}.
821 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400822 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400823
824 /**
825 * {@link #extras} key: this is a line of summary information intended to be shown
826 * alongside expanded notifications, as supplied to (e.g.)
827 * {@link BigTextStyle#setSummaryText(CharSequence)}.
828 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400829 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400830
831 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200832 * {@link #extras} key: this is the longer text shown in the big form of a
833 * {@link BigTextStyle} notification, as supplied to
834 * {@link BigTextStyle#bigText(CharSequence)}.
835 */
836 public static final String EXTRA_BIG_TEXT = "android.bigText";
837
838 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400839 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
840 * supplied to {@link Builder#setSmallIcon(int)}.
841 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500842 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400843
844 /**
845 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
846 * notification payload, as
847 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
848 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400849 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400850
851 /**
852 * {@link #extras} key: this is a bitmap to be used instead of the one from
853 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
854 * shown in its expanded form, as supplied to
855 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
856 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400857 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400858
859 /**
860 * {@link #extras} key: this is the progress value supplied to
861 * {@link Builder#setProgress(int, int, boolean)}.
862 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400863 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400864
865 /**
866 * {@link #extras} key: this is the maximum value supplied to
867 * {@link Builder#setProgress(int, int, boolean)}.
868 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400869 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400870
871 /**
872 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
873 * {@link Builder#setProgress(int, int, boolean)}.
874 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400875 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400876
877 /**
878 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
879 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
880 * {@link Builder#setUsesChronometer(boolean)}.
881 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400882 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400883
884 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800885 * {@link #extras} key: whether the chronometer set on the notification should count down
886 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -0700887 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -0800888 */
Adrian Roos96b7e202016-05-17 13:50:38 -0700889 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -0800890
891 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400892 * {@link #extras} key: whether {@link #when} should be shown,
893 * as supplied to {@link Builder#setShowWhen(boolean)}.
894 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400895 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400896
897 /**
898 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
899 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
900 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400901 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400902
903 /**
904 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
905 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
906 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400907 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400908
909 /**
910 * {@link #extras} key: A string representing the name of the specific
911 * {@link android.app.Notification.Style} used to create this notification.
912 */
Chris Wren91ad5632013-06-05 15:05:57 -0400913 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400914
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400915 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400916 * {@link #extras} key: A String array containing the people that this notification relates to,
917 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400918 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400919 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500920
921 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400922 * Allow certain system-generated notifications to appear before the device is provisioned.
923 * Only available to notifications coming from the android package.
924 * @hide
925 */
926 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
927
928 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700929 * {@link #extras} key: A
930 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
931 * in the background when the notification is selected. The URI must point to an image stream
932 * suitable for passing into
933 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
934 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
935 * URI used for this purpose must require no permissions to read the image data.
936 */
937 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
938
939 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400940 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700941 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400942 * {@link android.app.Notification.MediaStyle} notification.
943 */
944 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
945
946 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100947 * {@link #extras} key: the indices of actions to be shown in the compact view,
948 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
949 */
950 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
951
Christoph Studer943aa672014-08-03 20:31:16 +0200952 /**
Alex Hillsfc737de2016-03-23 17:33:02 -0400953 * {@link #extras} key: the username to be displayed for all messages sent by the user including
954 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -0700955 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
956 * {@link CharSequence}
Alex Hillsfc737de2016-03-23 17:33:02 -0400957 */
958 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
959
960 /**
Adrian Roos96b7e202016-05-17 13:50:38 -0700961 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -0400962 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -0400963 */
Alex Hillsd9b04d92016-04-11 16:38:16 -0400964 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -0400965
966 /**
967 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
968 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -0700969 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
970 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -0400971 */
972 public static final String EXTRA_MESSAGES = "android.messages";
973
974 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100975 * {@link #extras} key: the user that built the notification.
976 *
977 * @hide
978 */
979 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
980
981 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400982 * @hide
983 */
984 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
985
Selim Cinek247fa012016-02-18 09:50:48 -0800986 /**
987 * @hide
988 */
989 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
990
Shane Brennan472a3b32016-12-12 15:28:10 -0800991 /**
992 * {@link #extras} key: the audio contents of this notification.
993 *
994 * This is for use when rendering the notification on an audio-focused interface;
995 * the audio contents are a complete sound sample that contains the contents/body of the
996 * notification. This may be used in substitute of a Text-to-Speech reading of the
997 * notification. For example if the notification represents a voice message this should point
998 * to the audio of that message.
999 *
1000 * The data stored under this key should be a String representation of a Uri that contains the
1001 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1002 *
1003 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1004 * has a field for holding data URI. That field can be used for audio.
1005 * See {@code Message#setData}.
1006 *
1007 * Example usage:
1008 * <pre>
1009 * {@code
1010 * Notification.Builder myBuilder = (build your Notification as normal);
1011 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1012 * }
1013 * </pre>
1014 */
1015 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1016
Dan Sandler80eaa592016-04-14 23:34:54 -04001017 /** @hide */
1018 @SystemApi
Dan Sandler732bd6c2016-04-12 14:20:32 -04001019 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1020
Dan Sandlerd63f9322015-05-06 15:18:49 -04001021 private Icon mSmallIcon;
1022 private Icon mLargeIcon;
1023
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001024 private String mChannelId;
1025
Chris Wren51c75102013-07-16 20:49:17 -04001026 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001027 * Structure to encapsulate a named action that can be shown as part of this notification.
1028 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1029 * selected by the user.
1030 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001031 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1032 * or {@link Notification.Builder#addAction(Notification.Action)}
1033 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001034 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001035 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001036 /**
1037 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1038 * {@link RemoteInput}s.
1039 *
1040 * This is intended for {@link RemoteInput}s that only accept data, meaning
1041 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1042 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1043 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1044 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1045 *
1046 * You can test if a RemoteInput matches these constraints using
1047 * {@link RemoteInput#isDataOnly}.
1048 */
1049 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1050
Griff Hazen959591e2014-05-15 22:26:18 -07001051 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001052 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001053 private final RemoteInput[] mRemoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001054 private boolean mAllowGeneratedReplies = false;
Griff Hazen959591e2014-05-15 22:26:18 -07001055
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001056 /**
1057 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001058 *
1059 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001060 */
Dan Sandler86647982015-05-13 23:41:13 -04001061 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001062 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001063
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001064 /**
1065 * Title of the action.
1066 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001067 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001068
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001069 /**
1070 * Intent to send when the user invokes this action. May be null, in which case the action
1071 * may be rendered in a disabled presentation by the system UI.
1072 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001073 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001074
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001075 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001076 if (in.readInt() != 0) {
1077 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001078 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1079 icon = mIcon.getResId();
1080 }
Dan Sandler86647982015-05-13 23:41:13 -04001081 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001082 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1083 if (in.readInt() == 1) {
1084 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1085 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001086 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001087 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001088 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001089 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001090
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001091 /**
Dan Sandler86647982015-05-13 23:41:13 -04001092 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001093 */
Dan Sandler86647982015-05-13 23:41:13 -04001094 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001095 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills42b0c4d2016-04-26 13:35:36 -04001096 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, false);
Griff Hazen959591e2014-05-15 22:26:18 -07001097 }
1098
Adrian Roos7af53622016-10-12 13:44:05 -07001099 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001100 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001101 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001102 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001103 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1104 this.icon = icon.getResId();
1105 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001106 this.title = title;
1107 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001108 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001109 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001110 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001111 }
1112
1113 /**
Dan Sandler86647982015-05-13 23:41:13 -04001114 * Return an icon representing the action.
1115 */
1116 public Icon getIcon() {
1117 if (mIcon == null && icon != 0) {
1118 // you snuck an icon in here without using the builder; let's try to keep it
1119 mIcon = Icon.createWithResource("", icon);
1120 }
1121 return mIcon;
1122 }
1123
1124 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001125 * Get additional metadata carried around with this Action.
1126 */
1127 public Bundle getExtras() {
1128 return mExtras;
1129 }
1130
1131 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001132 * Return whether the platform should automatically generate possible replies for this
1133 * {@link Action}
1134 */
1135 public boolean getAllowGeneratedReplies() {
1136 return mAllowGeneratedReplies;
1137 }
1138
1139 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001140 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001141 * May return null if no remote inputs were added. Only returns inputs which accept
1142 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001143 */
1144 public RemoteInput[] getRemoteInputs() {
1145 return mRemoteInputs;
1146 }
1147
1148 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001149 * Get the list of inputs to be collected from the user that ONLY accept data when this
1150 * action is sent. These remote inputs are guaranteed to return true on a call to
1151 * {@link RemoteInput#isDataOnly}.
1152 *
1153 * May return null if no data-only remote inputs were added.
1154 *
1155 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1156 * of non-textual RemoteInputs do not access these remote inputs.
1157 */
1158 public RemoteInput[] getDataOnlyRemoteInputs() {
1159 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1160 }
1161
1162 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001163 * Builder class for {@link Action} objects.
1164 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001165 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001166 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001167 private final CharSequence mTitle;
1168 private final PendingIntent mIntent;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001169 private boolean mAllowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001170 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001171 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001172
1173 /**
1174 * Construct a new builder for {@link Action} object.
1175 * @param icon icon to show for this action
1176 * @param title the title of the action
1177 * @param intent the {@link PendingIntent} to fire when users trigger this action
1178 */
Dan Sandler86647982015-05-13 23:41:13 -04001179 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001180 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001181 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001182 }
1183
1184 /**
1185 * Construct a new builder for {@link Action} object.
1186 * @param icon icon to show for this action
1187 * @param title the title of the action
1188 * @param intent the {@link PendingIntent} to fire when users trigger this action
1189 */
1190 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001191 this(icon, title, intent, new Bundle(), null, false);
Griff Hazen959591e2014-05-15 22:26:18 -07001192 }
1193
1194 /**
1195 * Construct a new builder for {@link Action} object using the fields from an
1196 * {@link Action}.
1197 * @param action the action to read fields from.
1198 */
1199 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001200 this(action.getIcon(), action.title, action.actionIntent,
1201 new Bundle(action.mExtras), action.getRemoteInputs(),
1202 action.getAllowGeneratedReplies());
Griff Hazen959591e2014-05-15 22:26:18 -07001203 }
1204
Dan Sandler86647982015-05-13 23:41:13 -04001205 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Adrian Roos7af53622016-10-12 13:44:05 -07001206 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Griff Hazen959591e2014-05-15 22:26:18 -07001207 mIcon = icon;
1208 mTitle = title;
1209 mIntent = intent;
1210 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001211 if (remoteInputs != null) {
1212 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1213 Collections.addAll(mRemoteInputs, remoteInputs);
1214 }
Adrian Roos7af53622016-10-12 13:44:05 -07001215 mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001216 }
1217
1218 /**
1219 * Merge additional metadata into this builder.
1220 *
1221 * <p>Values within the Bundle will replace existing extras values in this Builder.
1222 *
1223 * @see Notification.Action#extras
1224 */
1225 public Builder addExtras(Bundle extras) {
1226 if (extras != null) {
1227 mExtras.putAll(extras);
1228 }
1229 return this;
1230 }
1231
1232 /**
1233 * Get the metadata Bundle used by this Builder.
1234 *
1235 * <p>The returned Bundle is shared with this Builder.
1236 */
1237 public Bundle getExtras() {
1238 return mExtras;
1239 }
1240
1241 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001242 * Add an input to be collected from the user when this action is sent.
1243 * Response values can be retrieved from the fired intent by using the
1244 * {@link RemoteInput#getResultsFromIntent} function.
1245 * @param remoteInput a {@link RemoteInput} to add to the action
1246 * @return this object for method chaining
1247 */
1248 public Builder addRemoteInput(RemoteInput remoteInput) {
1249 if (mRemoteInputs == null) {
1250 mRemoteInputs = new ArrayList<RemoteInput>();
1251 }
1252 mRemoteInputs.add(remoteInput);
1253 return this;
1254 }
1255
1256 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001257 * Set whether the platform should automatically generate possible replies to add to
1258 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1259 * {@link RemoteInput}, this has no effect.
1260 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1261 * otherwise
1262 * @return this object for method chaining
1263 * The default value is {@code false}
1264 */
1265 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1266 mAllowGeneratedReplies = allowGeneratedReplies;
1267 return this;
1268 }
1269
1270 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001271 * Apply an extender to this action builder. Extenders may be used to add
1272 * metadata or change options on this builder.
1273 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001274 public Builder extend(Extender extender) {
1275 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001276 return this;
1277 }
1278
1279 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001280 * Combine all of the options that have been set and return a new {@link Action}
1281 * object.
1282 * @return the built action
1283 */
1284 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001285 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1286 RemoteInput[] previousDataInputs =
1287 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1288 if (previousDataInputs == null) {
1289 for (RemoteInput input : previousDataInputs) {
1290 dataOnlyInputs.add(input);
1291 }
1292 }
1293 List<RemoteInput> textInputs = new ArrayList<>();
1294 if (mRemoteInputs != null) {
1295 for (RemoteInput input : mRemoteInputs) {
1296 if (input.isDataOnly()) {
1297 dataOnlyInputs.add(input);
1298 } else {
1299 textInputs.add(input);
1300 }
1301 }
1302 }
1303 if (!dataOnlyInputs.isEmpty()) {
1304 RemoteInput[] dataInputsArr =
1305 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1306 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1307 }
1308 RemoteInput[] textInputsArr = textInputs.isEmpty()
1309 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1310 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001311 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001312 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001313 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001314
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001315 @Override
1316 public Action clone() {
1317 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001318 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001319 title,
1320 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001321 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001322 getRemoteInputs(),
1323 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001324 }
1325 @Override
1326 public int describeContents() {
1327 return 0;
1328 }
1329 @Override
1330 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001331 final Icon ic = getIcon();
1332 if (ic != null) {
1333 out.writeInt(1);
1334 ic.writeToParcel(out, 0);
1335 } else {
1336 out.writeInt(0);
1337 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001338 TextUtils.writeToParcel(title, out, flags);
1339 if (actionIntent != null) {
1340 out.writeInt(1);
1341 actionIntent.writeToParcel(out, flags);
1342 } else {
1343 out.writeInt(0);
1344 }
Griff Hazen959591e2014-05-15 22:26:18 -07001345 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001346 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001347 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001348 }
Griff Hazen959591e2014-05-15 22:26:18 -07001349 public static final Parcelable.Creator<Action> CREATOR =
1350 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001351 public Action createFromParcel(Parcel in) {
1352 return new Action(in);
1353 }
1354 public Action[] newArray(int size) {
1355 return new Action[size];
1356 }
1357 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001358
1359 /**
1360 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1361 * metadata or change options on an action builder.
1362 */
1363 public interface Extender {
1364 /**
1365 * Apply this extender to a notification action builder.
1366 * @param builder the builder to be modified.
1367 * @return the build object for chaining.
1368 */
1369 public Builder extend(Builder builder);
1370 }
1371
1372 /**
1373 * Wearable extender for notification actions. To add extensions to an action,
1374 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1375 * the {@code WearableExtender()} constructor and apply it to a
1376 * {@link android.app.Notification.Action.Builder} using
1377 * {@link android.app.Notification.Action.Builder#extend}.
1378 *
1379 * <pre class="prettyprint">
1380 * Notification.Action action = new Notification.Action.Builder(
1381 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001382 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001383 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001384 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001385 */
1386 public static final class WearableExtender implements Extender {
1387 /** Notification action extra which contains wearable extensions */
1388 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1389
Pete Gastaf6781d2014-10-07 15:17:05 -04001390 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001391 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001392 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1393 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1394 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001395
1396 // Flags bitwise-ored to mFlags
1397 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001398 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001399 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001400
1401 // Default value for flags integer
1402 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1403
1404 private int mFlags = DEFAULT_FLAGS;
1405
Pete Gastaf6781d2014-10-07 15:17:05 -04001406 private CharSequence mInProgressLabel;
1407 private CharSequence mConfirmLabel;
1408 private CharSequence mCancelLabel;
1409
Griff Hazen61a9e862014-05-22 16:05:19 -07001410 /**
1411 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1412 * options.
1413 */
1414 public WearableExtender() {
1415 }
1416
1417 /**
1418 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1419 * wearable options present in an existing notification action.
1420 * @param action the notification action to inspect.
1421 */
1422 public WearableExtender(Action action) {
1423 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1424 if (wearableBundle != null) {
1425 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001426 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1427 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1428 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001429 }
1430 }
1431
1432 /**
1433 * Apply wearable extensions to a notification action that is being built. This is
1434 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1435 * method of {@link android.app.Notification.Action.Builder}.
1436 */
1437 @Override
1438 public Action.Builder extend(Action.Builder builder) {
1439 Bundle wearableBundle = new Bundle();
1440
1441 if (mFlags != DEFAULT_FLAGS) {
1442 wearableBundle.putInt(KEY_FLAGS, mFlags);
1443 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001444 if (mInProgressLabel != null) {
1445 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1446 }
1447 if (mConfirmLabel != null) {
1448 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1449 }
1450 if (mCancelLabel != null) {
1451 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1452 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001453
1454 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1455 return builder;
1456 }
1457
1458 @Override
1459 public WearableExtender clone() {
1460 WearableExtender that = new WearableExtender();
1461 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001462 that.mInProgressLabel = this.mInProgressLabel;
1463 that.mConfirmLabel = this.mConfirmLabel;
1464 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001465 return that;
1466 }
1467
1468 /**
1469 * Set whether this action is available when the wearable device is not connected to
1470 * a companion device. The user can still trigger this action when the wearable device is
1471 * offline, but a visual hint will indicate that the action may not be available.
1472 * Defaults to true.
1473 */
1474 public WearableExtender setAvailableOffline(boolean availableOffline) {
1475 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1476 return this;
1477 }
1478
1479 /**
1480 * Get whether this action is available when the wearable device is not connected to
1481 * a companion device. The user can still trigger this action when the wearable device is
1482 * offline, but a visual hint will indicate that the action may not be available.
1483 * Defaults to true.
1484 */
1485 public boolean isAvailableOffline() {
1486 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1487 }
1488
1489 private void setFlag(int mask, boolean value) {
1490 if (value) {
1491 mFlags |= mask;
1492 } else {
1493 mFlags &= ~mask;
1494 }
1495 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001496
1497 /**
1498 * Set a label to display while the wearable is preparing to automatically execute the
1499 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1500 *
1501 * @param label the label to display while the action is being prepared to execute
1502 * @return this object for method chaining
1503 */
1504 public WearableExtender setInProgressLabel(CharSequence label) {
1505 mInProgressLabel = label;
1506 return this;
1507 }
1508
1509 /**
1510 * Get the label to display while the wearable is preparing to automatically execute
1511 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1512 *
1513 * @return the label to display while the action is being prepared to execute
1514 */
1515 public CharSequence getInProgressLabel() {
1516 return mInProgressLabel;
1517 }
1518
1519 /**
1520 * Set a label to display to confirm that the action should be executed.
1521 * This is usually an imperative verb like "Send".
1522 *
1523 * @param label the label to confirm the action should be executed
1524 * @return this object for method chaining
1525 */
1526 public WearableExtender setConfirmLabel(CharSequence label) {
1527 mConfirmLabel = label;
1528 return this;
1529 }
1530
1531 /**
1532 * Get the label to display to confirm that the action should be executed.
1533 * This is usually an imperative verb like "Send".
1534 *
1535 * @return the label to confirm the action should be executed
1536 */
1537 public CharSequence getConfirmLabel() {
1538 return mConfirmLabel;
1539 }
1540
1541 /**
1542 * Set a label to display to cancel the action.
1543 * This is usually an imperative verb, like "Cancel".
1544 *
1545 * @param label the label to display to cancel the action
1546 * @return this object for method chaining
1547 */
1548 public WearableExtender setCancelLabel(CharSequence label) {
1549 mCancelLabel = label;
1550 return this;
1551 }
1552
1553 /**
1554 * Get the label to display to cancel the action.
1555 * This is usually an imperative verb like "Cancel".
1556 *
1557 * @return the label to display to cancel the action
1558 */
1559 public CharSequence getCancelLabel() {
1560 return mCancelLabel;
1561 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001562
1563 /**
1564 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1565 * platform that it can generate the appropriate transitions.
1566 * @param hintLaunchesActivity {@code true} if the content intent will launch
1567 * an activity and transitions should be generated, false otherwise.
1568 * @return this object for method chaining
1569 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001570 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001571 boolean hintLaunchesActivity) {
1572 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1573 return this;
1574 }
1575
1576 /**
1577 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1578 * platform that it can generate the appropriate transitions
1579 * @return {@code true} if the content intent will launch an activity and transitions
1580 * should be generated, false otherwise. The default value is {@code false} if this was
1581 * never set.
1582 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001583 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001584 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1585 }
Alex Hills9f087612016-06-07 09:08:59 -04001586
1587 /**
1588 * Set a hint that this Action should be displayed inline.
1589 *
1590 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1591 * otherwise
1592 * @return this object for method chaining
1593 */
1594 public WearableExtender setHintDisplayActionInline(
1595 boolean hintDisplayInline) {
1596 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1597 return this;
1598 }
1599
1600 /**
1601 * Get a hint that this Action should be displayed inline.
1602 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08001603 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04001604 * otherwise. The default value is {@code false} if this was never set.
1605 */
1606 public boolean getHintDisplayActionInline() {
1607 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1608 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001609 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001610 }
1611
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001612 /**
1613 * Array of all {@link Action} structures attached to this notification by
1614 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1615 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1616 * interface for invoking actions.
1617 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001618 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001619
1620 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001621 * Replacement version of this notification whose content will be shown
1622 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1623 * and {@link #VISIBILITY_PUBLIC}.
1624 */
1625 public Notification publicVersion;
1626
1627 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001628 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001629 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 */
1631 public Notification()
1632 {
1633 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001634 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001635 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001636 }
1637
1638 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 * @hide
1640 */
1641 public Notification(Context context, int icon, CharSequence tickerText, long when,
1642 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1643 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001644 new Builder(context)
1645 .setWhen(when)
1646 .setSmallIcon(icon)
1647 .setTicker(tickerText)
1648 .setContentTitle(contentTitle)
1649 .setContentText(contentText)
1650 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1651 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001652 }
1653
1654 /**
1655 * Constructs a Notification object with the information needed to
1656 * have a status bar icon without the standard expanded view.
1657 *
1658 * @param icon The resource id of the icon to put in the status bar.
1659 * @param tickerText The text that flows by in the status bar when the notification first
1660 * activates.
1661 * @param when The time to show in the time field. In the System.currentTimeMillis
1662 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001663 *
1664 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001665 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001666 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001667 public Notification(int icon, CharSequence tickerText, long when)
1668 {
1669 this.icon = icon;
1670 this.tickerText = tickerText;
1671 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001672 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 }
1674
1675 /**
1676 * Unflatten the notification from a parcel.
1677 */
Svet Ganovddb94882016-06-23 19:55:24 -07001678 @SuppressWarnings("unchecked")
1679 public Notification(Parcel parcel) {
1680 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
1681 // intents in extras are always written as the last entry.
1682 readFromParcelImpl(parcel);
1683 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001684 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07001685 }
1686
1687 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001688 {
1689 int version = parcel.readInt();
1690
1691 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001692 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001693 if (parcel.readInt() != 0) {
1694 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001695 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1696 icon = mSmallIcon.getResId();
1697 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001698 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001699 number = parcel.readInt();
1700 if (parcel.readInt() != 0) {
1701 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1702 }
1703 if (parcel.readInt() != 0) {
1704 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1705 }
1706 if (parcel.readInt() != 0) {
1707 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1708 }
1709 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001710 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001711 }
1712 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001713 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1714 }
Joe Onorato561d3852010-11-20 18:09:34 -08001715 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001716 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001717 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001718 defaults = parcel.readInt();
1719 flags = parcel.readInt();
1720 if (parcel.readInt() != 0) {
1721 sound = Uri.CREATOR.createFromParcel(parcel);
1722 }
1723
1724 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001725 if (parcel.readInt() != 0) {
1726 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1727 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 vibrate = parcel.createLongArray();
1729 ledARGB = parcel.readInt();
1730 ledOnMS = parcel.readInt();
1731 ledOffMS = parcel.readInt();
1732 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001733
1734 if (parcel.readInt() != 0) {
1735 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1736 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001737
1738 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001739
John Spurlockfd7f1e02014-03-18 16:41:57 -04001740 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001741
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001742 mGroupKey = parcel.readString();
1743
1744 mSortKey = parcel.readString();
1745
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001746 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001747
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001748 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1749
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001750 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001751 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1752 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001753
Chris Wren8fd39ec2014-02-27 17:43:26 -05001754 if (parcel.readInt() != 0) {
1755 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1756 }
1757
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001758 visibility = parcel.readInt();
1759
1760 if (parcel.readInt() != 0) {
1761 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1762 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001763
1764 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001765
1766 if (parcel.readInt() != 0) {
1767 mChannelId = parcel.readString();
1768 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 }
1770
Andy Stadler110988c2010-12-03 14:29:16 -08001771 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001772 public Notification clone() {
1773 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001774 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001775 return that;
1776 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001777
Daniel Sandler1a497d32013-04-18 14:52:45 -04001778 /**
1779 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1780 * of this into that.
1781 * @hide
1782 */
1783 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001784 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001785 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001786 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001787 that.number = this.number;
1788
1789 // PendingIntents are global, so there's no reason (or way) to clone them.
1790 that.contentIntent = this.contentIntent;
1791 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001792 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001793
1794 if (this.tickerText != null) {
1795 that.tickerText = this.tickerText.toString();
1796 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001797 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001798 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001799 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001800 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001801 that.contentView = this.contentView.clone();
1802 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001803 if (heavy && this.mLargeIcon != null) {
1804 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001805 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001806 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001807 that.sound = this.sound; // android.net.Uri is immutable
1808 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001809 if (this.audioAttributes != null) {
1810 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1811 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001812
1813 final long[] vibrate = this.vibrate;
1814 if (vibrate != null) {
1815 final int N = vibrate.length;
1816 final long[] vib = that.vibrate = new long[N];
1817 System.arraycopy(vibrate, 0, vib, 0, N);
1818 }
1819
1820 that.ledARGB = this.ledARGB;
1821 that.ledOnMS = this.ledOnMS;
1822 that.ledOffMS = this.ledOffMS;
1823 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001824
Joe Onorato18e69df2010-05-17 22:26:12 -07001825 that.flags = this.flags;
1826
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001827 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001828
John Spurlockfd7f1e02014-03-18 16:41:57 -04001829 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001830
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001831 that.mGroupKey = this.mGroupKey;
1832
1833 that.mSortKey = this.mSortKey;
1834
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001835 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001836 try {
1837 that.extras = new Bundle(this.extras);
1838 // will unparcel
1839 that.extras.size();
1840 } catch (BadParcelableException e) {
1841 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1842 that.extras = null;
1843 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001844 }
1845
Felipe Lemedd85da62016-06-28 11:29:54 -07001846 if (!ArrayUtils.isEmpty(allPendingIntents)) {
1847 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07001848 }
1849
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001850 if (this.actions != null) {
1851 that.actions = new Action[this.actions.length];
1852 for(int i=0; i<this.actions.length; i++) {
1853 that.actions[i] = this.actions[i].clone();
1854 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001855 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001856
Daniel Sandler1a497d32013-04-18 14:52:45 -04001857 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001858 that.bigContentView = this.bigContentView.clone();
1859 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001860
Chris Wren8fd39ec2014-02-27 17:43:26 -05001861 if (heavy && this.headsUpContentView != null) {
1862 that.headsUpContentView = this.headsUpContentView.clone();
1863 }
1864
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001865 that.visibility = this.visibility;
1866
1867 if (this.publicVersion != null) {
1868 that.publicVersion = new Notification();
1869 this.publicVersion.cloneInto(that.publicVersion, heavy);
1870 }
1871
Dan Sandler26e81cf2014-05-06 10:01:27 -04001872 that.color = this.color;
1873
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001874 that.mChannelId = this.mChannelId;
1875
Daniel Sandler1a497d32013-04-18 14:52:45 -04001876 if (!heavy) {
1877 that.lightenPayload(); // will clean out extras
1878 }
1879 }
1880
1881 /**
1882 * Removes heavyweight parts of the Notification object for archival or for sending to
1883 * listeners when the full contents are not necessary.
1884 * @hide
1885 */
1886 public final void lightenPayload() {
1887 tickerView = null;
1888 contentView = null;
1889 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001890 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001891 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05001892 if (extras != null && !extras.isEmpty()) {
1893 final Set<String> keyset = extras.keySet();
1894 final int N = keyset.size();
1895 final String[] keys = keyset.toArray(new String[N]);
1896 for (int i=0; i<N; i++) {
1897 final String key = keys[i];
1898 final Object obj = extras.get(key);
1899 if (obj != null &&
1900 ( obj instanceof Parcelable
1901 || obj instanceof Parcelable[]
1902 || obj instanceof SparseArray
1903 || obj instanceof ArrayList)) {
1904 extras.remove(key);
1905 }
1906 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001907 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001908 }
1909
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001910 /**
1911 * Make sure this CharSequence is safe to put into a bundle, which basically
1912 * means it had better not be some custom Parcelable implementation.
1913 * @hide
1914 */
1915 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001916 if (cs == null) return cs;
1917 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1918 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1919 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001920 if (cs instanceof Parcelable) {
1921 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1922 + " instance is a custom Parcelable and not allowed in Notification");
1923 return cs.toString();
1924 }
Selim Cinek60a54252016-02-26 17:03:25 -08001925 return removeTextSizeSpans(cs);
1926 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001927
Selim Cinek60a54252016-02-26 17:03:25 -08001928 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
1929 if (charSequence instanceof Spanned) {
1930 Spanned ss = (Spanned) charSequence;
1931 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
1932 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
1933 for (Object span : spans) {
1934 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08001935 if (resultSpan instanceof CharacterStyle) {
1936 resultSpan = ((CharacterStyle) span).getUnderlying();
1937 }
1938 if (resultSpan instanceof TextAppearanceSpan) {
1939 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08001940 resultSpan = new TextAppearanceSpan(
1941 originalSpan.getFamily(),
1942 originalSpan.getTextStyle(),
1943 -1,
1944 originalSpan.getTextColor(),
1945 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08001946 } else if (resultSpan instanceof RelativeSizeSpan
1947 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08001948 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08001949 } else {
1950 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08001951 }
1952 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
1953 ss.getSpanFlags(span));
1954 }
1955 return builder;
1956 }
1957 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001958 }
1959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 public int describeContents() {
1961 return 0;
1962 }
1963
1964 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001965 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001966 */
Svet Ganovddb94882016-06-23 19:55:24 -07001967 public void writeToParcel(Parcel parcel, int flags) {
1968 // We need to mark all pending intents getting into the notification
1969 // system as being put there to later allow the notification ranker
1970 // to launch them and by doing so add the app to the battery saver white
1971 // list for a short period of time. The problem is that the system
1972 // cannot look into the extras as there may be parcelables there that
1973 // the platform does not know how to handle. To go around that we have
1974 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07001975 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07001976 if (collectPendingIntents) {
1977 PendingIntent.setOnMarshaledListener(
1978 (PendingIntent intent, Parcel out, int outFlags) -> {
1979 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07001980 if (allPendingIntents == null) {
1981 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07001982 }
Felipe Lemedd85da62016-06-28 11:29:54 -07001983 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07001984 }
1985 });
1986 }
1987 try {
1988 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
1989 // want to intercept all pending events written to the pacel.
1990 writeToParcelImpl(parcel, flags);
1991 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001992 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07001993 } finally {
1994 if (collectPendingIntents) {
1995 PendingIntent.setOnMarshaledListener(null);
1996 }
1997 }
1998 }
1999
2000 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002001 parcel.writeInt(1);
2002
2003 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002004 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002005 if (mSmallIcon == null && icon != 0) {
2006 // you snuck an icon in here without using the builder; let's try to keep it
2007 mSmallIcon = Icon.createWithResource("", icon);
2008 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002009 if (mSmallIcon != null) {
2010 parcel.writeInt(1);
2011 mSmallIcon.writeToParcel(parcel, 0);
2012 } else {
2013 parcel.writeInt(0);
2014 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 parcel.writeInt(number);
2016 if (contentIntent != null) {
2017 parcel.writeInt(1);
2018 contentIntent.writeToParcel(parcel, 0);
2019 } else {
2020 parcel.writeInt(0);
2021 }
2022 if (deleteIntent != null) {
2023 parcel.writeInt(1);
2024 deleteIntent.writeToParcel(parcel, 0);
2025 } else {
2026 parcel.writeInt(0);
2027 }
2028 if (tickerText != null) {
2029 parcel.writeInt(1);
2030 TextUtils.writeToParcel(tickerText, parcel, flags);
2031 } else {
2032 parcel.writeInt(0);
2033 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002034 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002035 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002036 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002037 } else {
2038 parcel.writeInt(0);
2039 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 if (contentView != null) {
2041 parcel.writeInt(1);
2042 contentView.writeToParcel(parcel, 0);
2043 } else {
2044 parcel.writeInt(0);
2045 }
Selim Cinek279fa862016-06-14 10:57:25 -07002046 if (mLargeIcon == null && largeIcon != null) {
2047 // you snuck an icon in here without using the builder; let's try to keep it
2048 mLargeIcon = Icon.createWithBitmap(largeIcon);
2049 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002050 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002051 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002052 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002053 } else {
2054 parcel.writeInt(0);
2055 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002056
2057 parcel.writeInt(defaults);
2058 parcel.writeInt(this.flags);
2059
2060 if (sound != null) {
2061 parcel.writeInt(1);
2062 sound.writeToParcel(parcel, 0);
2063 } else {
2064 parcel.writeInt(0);
2065 }
2066 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002067
2068 if (audioAttributes != null) {
2069 parcel.writeInt(1);
2070 audioAttributes.writeToParcel(parcel, 0);
2071 } else {
2072 parcel.writeInt(0);
2073 }
2074
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002075 parcel.writeLongArray(vibrate);
2076 parcel.writeInt(ledARGB);
2077 parcel.writeInt(ledOnMS);
2078 parcel.writeInt(ledOffMS);
2079 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002080
2081 if (fullScreenIntent != null) {
2082 parcel.writeInt(1);
2083 fullScreenIntent.writeToParcel(parcel, 0);
2084 } else {
2085 parcel.writeInt(0);
2086 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002087
2088 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002089
John Spurlockfd7f1e02014-03-18 16:41:57 -04002090 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002091
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002092 parcel.writeString(mGroupKey);
2093
2094 parcel.writeString(mSortKey);
2095
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002096 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002097
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002098 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002099
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002100 if (bigContentView != null) {
2101 parcel.writeInt(1);
2102 bigContentView.writeToParcel(parcel, 0);
2103 } else {
2104 parcel.writeInt(0);
2105 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002106
Chris Wren8fd39ec2014-02-27 17:43:26 -05002107 if (headsUpContentView != null) {
2108 parcel.writeInt(1);
2109 headsUpContentView.writeToParcel(parcel, 0);
2110 } else {
2111 parcel.writeInt(0);
2112 }
2113
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002114 parcel.writeInt(visibility);
2115
2116 if (publicVersion != null) {
2117 parcel.writeInt(1);
2118 publicVersion.writeToParcel(parcel, 0);
2119 } else {
2120 parcel.writeInt(0);
2121 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002122
2123 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002124
2125 if (mChannelId != null) {
2126 parcel.writeInt(1);
2127 parcel.writeString(mChannelId);
2128 } else {
2129 parcel.writeInt(0);
2130 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131 }
2132
2133 /**
2134 * Parcelable.Creator that instantiates Notification objects
2135 */
2136 public static final Parcelable.Creator<Notification> CREATOR
2137 = new Parcelable.Creator<Notification>()
2138 {
2139 public Notification createFromParcel(Parcel parcel)
2140 {
2141 return new Notification(parcel);
2142 }
2143
2144 public Notification[] newArray(int size)
2145 {
2146 return new Notification[size];
2147 }
2148 };
2149
2150 /**
2151 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2152 * layout.
2153 *
2154 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2155 * in the view.</p>
2156 * @param context The context for your application / activity.
2157 * @param contentTitle The title that goes in the expanded entry.
2158 * @param contentText The text that goes in the expanded entry.
2159 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2160 * If this is an activity, it must include the
2161 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002162 * that you take care of task management as described in the
2163 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2164 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002165 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002166 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002167 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002169 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002170 public void setLatestEventInfo(Context context,
2171 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002172 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2173 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2174 new Throwable());
2175 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002176
Selim Cinek4ac6f602016-06-13 15:47:03 -07002177 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2178 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2179 }
2180
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002181 // ensure that any information already set directly is preserved
2182 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002183
2184 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002185 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002186 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002187 }
2188 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002189 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002190 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002191 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002192
2193 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002194 }
2195
Julia Reynoldsda303542015-11-23 14:00:20 -05002196 /**
2197 * @hide
2198 */
2199 public static void addFieldsFromContext(Context context, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002200 addFieldsFromContext(context.getApplicationInfo(), context.getUserId(), notification);
2201 }
2202
2203 /**
2204 * @hide
2205 */
2206 public static void addFieldsFromContext(ApplicationInfo ai, int userId,
2207 Notification notification) {
2208 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
2209 notification.extras.putInt(EXTRA_ORIGINATING_USERID, userId);
Julia Reynoldsda303542015-11-23 14:00:20 -05002210 }
2211
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002212 @Override
2213 public String toString() {
2214 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002215 sb.append("Notification(pri=");
2216 sb.append(priority);
2217 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002218 if (contentView != null) {
2219 sb.append(contentView.getPackage());
2220 sb.append("/0x");
2221 sb.append(Integer.toHexString(contentView.getLayoutId()));
2222 } else {
2223 sb.append("null");
2224 }
2225 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002226 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2227 sb.append("default");
2228 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002229 int N = this.vibrate.length-1;
2230 sb.append("[");
2231 for (int i=0; i<N; i++) {
2232 sb.append(this.vibrate[i]);
2233 sb.append(',');
2234 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002235 if (N != -1) {
2236 sb.append(this.vibrate[N]);
2237 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002238 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002239 } else {
2240 sb.append("null");
2241 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002242 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002243 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002244 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002245 } else if (this.sound != null) {
2246 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002247 } else {
2248 sb.append("null");
2249 }
Chris Wren365b6d32015-07-16 10:39:26 -04002250 if (this.tickerText != null) {
2251 sb.append(" tick");
2252 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002253 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002254 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002255 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002256 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002257 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002258 if (this.category != null) {
2259 sb.append(" category=");
2260 sb.append(this.category);
2261 }
2262 if (this.mGroupKey != null) {
2263 sb.append(" groupKey=");
2264 sb.append(this.mGroupKey);
2265 }
2266 if (this.mSortKey != null) {
2267 sb.append(" sortKey=");
2268 sb.append(this.mSortKey);
2269 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002270 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002271 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002272 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002273 }
2274 sb.append(" vis=");
2275 sb.append(visibilityToString(this.visibility));
2276 if (this.publicVersion != null) {
2277 sb.append(" publicVersion=");
2278 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002279 }
2280 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002281 return sb.toString();
2282 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002283
Dan Sandler1b718782014-07-18 12:43:45 -04002284 /**
2285 * {@hide}
2286 */
2287 public static String visibilityToString(int vis) {
2288 switch (vis) {
2289 case VISIBILITY_PRIVATE:
2290 return "PRIVATE";
2291 case VISIBILITY_PUBLIC:
2292 return "PUBLIC";
2293 case VISIBILITY_SECRET:
2294 return "SECRET";
2295 default:
2296 return "UNKNOWN(" + String.valueOf(vis) + ")";
2297 }
2298 }
2299
Joe Onoratocb109a02011-01-18 17:57:41 -08002300 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002301 * {@hide}
2302 */
2303 public static String priorityToString(@Priority int pri) {
2304 switch (pri) {
2305 case PRIORITY_MIN:
2306 return "MIN";
2307 case PRIORITY_LOW:
2308 return "LOW";
2309 case PRIORITY_DEFAULT:
2310 return "DEFAULT";
2311 case PRIORITY_HIGH:
2312 return "HIGH";
2313 case PRIORITY_MAX:
2314 return "MAX";
2315 default:
2316 return "UNKNOWN(" + String.valueOf(pri) + ")";
2317 }
2318 }
2319
2320 /**
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002321 * Returns the id of the channel this notification posts to.
2322 */
Julia Reynolds37856052016-11-11 09:20:07 -05002323 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002324 return mChannelId;
2325 }
2326
2327 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002328 * The small icon representing this notification in the status bar and content view.
2329 *
2330 * @return the small icon representing this notification.
2331 *
2332 * @see Builder#getSmallIcon()
2333 * @see Builder#setSmallIcon(Icon)
2334 */
2335 public Icon getSmallIcon() {
2336 return mSmallIcon;
2337 }
2338
2339 /**
2340 * Used when notifying to clean up legacy small icons.
2341 * @hide
2342 */
2343 public void setSmallIcon(Icon icon) {
2344 mSmallIcon = icon;
2345 }
2346
2347 /**
2348 * The large icon shown in this notification's content view.
2349 * @see Builder#getLargeIcon()
2350 * @see Builder#setLargeIcon(Icon)
2351 */
2352 public Icon getLargeIcon() {
2353 return mLargeIcon;
2354 }
2355
2356 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002357 * @hide
2358 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002359 public boolean isGroupSummary() {
2360 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2361 }
2362
2363 /**
2364 * @hide
2365 */
2366 public boolean isGroupChild() {
2367 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2368 }
2369
2370 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002371 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002372 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002373 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002374 * content views using the platform's notification layout template. If your app supports
2375 * versions of Android as old as API level 4, you can instead use
2376 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2377 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2378 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002379 *
Scott Main183bf112012-08-13 19:12:13 -07002380 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002381 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002382 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002383 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002384 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2385 * .setContentText(subject)
2386 * .setSmallIcon(R.drawable.new_mail)
2387 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002388 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002389 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002390 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002391 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002392 /**
2393 * @hide
2394 */
2395 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2396 "android.rebuild.contentViewActionCount";
2397 /**
2398 * @hide
2399 */
2400 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2401 = "android.rebuild.bigViewActionCount";
2402 /**
2403 * @hide
2404 */
2405 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2406 = "android.rebuild.hudViewActionCount";
2407
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002408 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002409
Joe Onorato46439ce2010-11-19 13:56:21 -08002410 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002411 private Notification mN;
2412 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002413 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002414 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2415 private ArrayList<String> mPersonList = new ArrayList<String>();
2416 private NotificationColorUtil mColorUtil;
2417 private boolean mColorUtilInited = false;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002418
2419 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002420 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2421 */
2422 private int mCachedContrastColor = COLOR_INVALID;
2423 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08002424 /**
2425 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
2426 */
2427 private int mCachedAmbientColor = COLOR_INVALID;
2428 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08002429
2430 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08002431 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
2432 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
2433 */
2434 StandardTemplateParams mParams = new StandardTemplateParams();
2435
2436 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002437 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002438 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002439
2440 * <table>
2441 * <tr><th align=right>priority</th>
2442 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2443 * <tr><th align=right>when</th>
2444 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2445 * <tr><th align=right>audio stream</th>
2446 * <td>{@link #STREAM_DEFAULT}</td></tr>
2447 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002448 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002449
2450 * @param context
2451 * A {@link Context} that will be used by the Builder to construct the
2452 * RemoteViews. The Context will not be held past the lifetime of this Builder
2453 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002454 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002455 public Builder(Context context) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002456 this(context, null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002457 }
2458
Joe Onoratocb109a02011-01-18 17:57:41 -08002459 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002460 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002461 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002462 public Builder(Context context, Notification toAdopt) {
2463 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002464
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002465 if (toAdopt == null) {
2466 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002467 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2468 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2469 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002470 mN.priority = PRIORITY_DEFAULT;
2471 mN.visibility = VISIBILITY_PRIVATE;
2472 } else {
2473 mN = toAdopt;
2474 if (mN.actions != null) {
2475 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002476 }
2477
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002478 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2479 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2480 }
2481
Selim Cinek4ac6f602016-06-13 15:47:03 -07002482 if (mN.getSmallIcon() == null && mN.icon != 0) {
2483 setSmallIcon(mN.icon);
2484 }
2485
2486 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
2487 setLargeIcon(mN.largeIcon);
2488 }
2489
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002490 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2491 if (!TextUtils.isEmpty(templateClass)) {
2492 final Class<? extends Style> styleClass
2493 = getNotificationStyleClass(templateClass);
2494 if (styleClass == null) {
2495 Log.d(TAG, "Unknown style class: " + templateClass);
2496 } else {
2497 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002498 final Constructor<? extends Style> ctor =
2499 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002500 ctor.setAccessible(true);
2501 final Style style = ctor.newInstance();
2502 style.restoreFromExtras(mN.extras);
2503
2504 if (style != null) {
2505 setStyle(style);
2506 }
2507 } catch (Throwable t) {
2508 Log.e(TAG, "Could not create Style", t);
2509 }
2510 }
2511 }
2512
2513 }
2514 }
2515
2516 private NotificationColorUtil getColorUtil() {
2517 if (!mColorUtilInited) {
2518 mColorUtilInited = true;
2519 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
2520 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002521 }
2522 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002523 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002524 }
2525
2526 /**
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002527 * Specifies the channel the notification should be delivered on.
2528 */
2529 public Builder setChannel(String channelId) {
2530 mN.mChannelId = channelId;
2531 return this;
2532 }
2533
2534 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002535 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002536 *
2537 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2538 * shown anymore by default and must be opted into by using
2539 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002540 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002541 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002542 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002543 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002544 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002545 return this;
2546 }
2547
Joe Onoratocb109a02011-01-18 17:57:41 -08002548 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002549 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002550 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002551 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2552 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002553 */
2554 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002555 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002556 return this;
2557 }
2558
2559 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002560 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002561 *
2562 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002563 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002564 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002565 * Useful when showing an elapsed time (like an ongoing phone call).
2566 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002567 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07002568 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08002569 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002570 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002571 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07002572 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002573 */
2574 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002575 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002576 return this;
2577 }
2578
2579 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002580 * Sets the Chronometer to count down instead of counting up.
2581 *
2582 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2583 * If it isn't set the chronometer will count up.
2584 *
2585 * @see #setUsesChronometer(boolean)
2586 */
Adrian Roos96b7e202016-05-17 13:50:38 -07002587 public Builder setChronometerCountDown(boolean countDown) {
2588 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08002589 return this;
2590 }
2591
2592 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002593 * Set the small icon resource, which will be used to represent the notification in the
2594 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002595 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002596
2597 * The platform template for the expanded view will draw this icon in the left, unless a
2598 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2599 * icon will be moved to the right-hand side.
2600 *
2601
2602 * @param icon
2603 * A resource ID in the application's package of the drawable to use.
2604 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002605 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002606 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002607 return setSmallIcon(icon != 0
2608 ? Icon.createWithResource(mContext, icon)
2609 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002610 }
2611
Joe Onoratocb109a02011-01-18 17:57:41 -08002612 /**
2613 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2614 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2615 * LevelListDrawable}.
2616 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002617 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002618 * @param level The level to use for the icon.
2619 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002620 * @see Notification#icon
2621 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002622 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002623 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002624 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002625 return setSmallIcon(icon);
2626 }
2627
2628 /**
2629 * Set the small icon, which will be used to represent the notification in the
2630 * status bar and content view (unless overriden there by a
2631 * {@link #setLargeIcon(Bitmap) large icon}).
2632 *
2633 * @param icon An Icon object to use.
2634 * @see Notification#icon
2635 */
2636 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002637 mN.setSmallIcon(icon);
2638 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2639 mN.icon = icon.getResId();
2640 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002641 return this;
2642 }
2643
Joe Onoratocb109a02011-01-18 17:57:41 -08002644 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002645 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002646 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002647 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002648 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002649 return this;
2650 }
2651
Joe Onoratocb109a02011-01-18 17:57:41 -08002652 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002653 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002654 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002655 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002656 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002657 return this;
2658 }
2659
Joe Onoratocb109a02011-01-18 17:57:41 -08002660 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002661 * This provides some additional information that is displayed in the notification. No
2662 * guarantees are given where exactly it is displayed.
2663 *
2664 * <p>This information should only be provided if it provides an essential
2665 * benefit to the understanding of the notification. The more text you provide the
2666 * less readable it becomes. For example, an email client should only provide the account
2667 * name here if more than one email account has been added.</p>
2668 *
2669 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
2670 * notification header area.
2671 *
2672 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
2673 * this will be shown in the third line of text in the platform notification template.
2674 * You should not be using {@link #setProgress(int, int, boolean)} at the
2675 * same time on those versions; they occupy the same place.
2676 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002677 */
2678 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002679 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002680 return this;
2681 }
2682
2683 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002684 * Set the remote input history.
2685 *
2686 * This should be set to the most recent inputs that have been sent
2687 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2688 * longer relevant (e.g. for chat notifications once the other party has responded).
2689 *
2690 * The most recent input must be stored at the 0 index, the second most recent at the
2691 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2692 * and how much of each individual input is shown.
2693 *
2694 * <p>Note: The reply text will only be shown on notifications that have least one action
2695 * with a {@code RemoteInput}.</p>
2696 */
2697 public Builder setRemoteInputHistory(CharSequence[] text) {
2698 if (text == null) {
2699 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2700 } else {
2701 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2702 CharSequence[] safe = new CharSequence[N];
2703 for (int i = 0; i < N; i++) {
2704 safe[i] = safeCharSequence(text[i]);
2705 }
2706 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2707 }
2708 return this;
2709 }
2710
2711 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002712 * Set the large number at the right-hand side of the notification. This is
2713 * equivalent to setContentInfo, although it might show the number in a different
2714 * font size for readability.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002715 *
2716 * @deprecated this number is not shown anywhere anymore
Joe Onoratocb109a02011-01-18 17:57:41 -08002717 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002718 @Deprecated
Joe Onorato8595a3d2010-11-19 18:12:07 -08002719 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002720 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002721 return this;
2722 }
2723
Joe Onoratocb109a02011-01-18 17:57:41 -08002724 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002725 * A small piece of additional information pertaining to this notification.
2726 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002727 * The platform template will draw this on the last line of the notification, at the far
2728 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002729 *
2730 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
2731 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
2732 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08002733 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002734 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002735 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002736 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002737 return this;
2738 }
2739
Joe Onoratocb109a02011-01-18 17:57:41 -08002740 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002741 * Set the progress this notification represents.
2742 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002743 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002744 */
2745 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002746 mN.extras.putInt(EXTRA_PROGRESS, progress);
2747 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
2748 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002749 return this;
2750 }
2751
2752 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002753 * Supply a custom RemoteViews to use instead of the platform template.
2754 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002755 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08002756 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002757 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002758 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002759 return setCustomContentView(views);
2760 }
2761
2762 /**
2763 * Supply custom RemoteViews to use instead of the platform template.
2764 *
2765 * This will override the layout that would otherwise be constructed by this Builder
2766 * object.
2767 */
2768 public Builder setCustomContentView(RemoteViews contentView) {
2769 mN.contentView = contentView;
2770 return this;
2771 }
2772
2773 /**
2774 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
2775 *
2776 * This will override the expanded layout that would otherwise be constructed by this
2777 * Builder object.
2778 */
2779 public Builder setCustomBigContentView(RemoteViews contentView) {
2780 mN.bigContentView = contentView;
2781 return this;
2782 }
2783
2784 /**
2785 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
2786 *
2787 * This will override the heads-up layout that would otherwise be constructed by this
2788 * Builder object.
2789 */
2790 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
2791 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002792 return this;
2793 }
2794
Joe Onoratocb109a02011-01-18 17:57:41 -08002795 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002796 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2797 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002798 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2799 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2800 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002801 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002802 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002803 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002804 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002805 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002806 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002807 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002808 return this;
2809 }
2810
Joe Onoratocb109a02011-01-18 17:57:41 -08002811 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002812 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2813 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002814 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002815 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002816 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002817 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002818 return this;
2819 }
2820
Joe Onoratocb109a02011-01-18 17:57:41 -08002821 /**
2822 * An intent to launch instead of posting the notification to the status bar.
2823 * Only for use with extremely high-priority notifications demanding the user's
2824 * <strong>immediate</strong> attention, such as an incoming phone call or
2825 * alarm clock that the user has explicitly set to a particular time.
2826 * If this facility is used for something else, please give the user an option
2827 * to turn it off and use a normal notification, as this can be extremely
2828 * disruptive.
2829 *
Chris Wren47c20a12014-06-18 17:27:29 -04002830 * <p>
2831 * The system UI may choose to display a heads-up notification, instead of
2832 * launching this intent, while the user is using the device.
2833 * </p>
2834 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002835 * @param intent The pending intent to launch.
2836 * @param highPriority Passing true will cause this notification to be sent
2837 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002838 *
2839 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002840 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002841 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002842 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002843 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2844 return this;
2845 }
2846
Joe Onoratocb109a02011-01-18 17:57:41 -08002847 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002848 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002849 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002850 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002851 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002852 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002853 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002854 return this;
2855 }
2856
Joe Onoratocb109a02011-01-18 17:57:41 -08002857 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002858 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002859 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002860 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002861 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002862 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002863 setTicker(tickerText);
2864 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08002865 return this;
2866 }
2867
Joe Onoratocb109a02011-01-18 17:57:41 -08002868 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002869 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002870 *
2871 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002872 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2873 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002874 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002875 public Builder setLargeIcon(Bitmap b) {
2876 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2877 }
2878
2879 /**
2880 * Add a large icon to the notification content view.
2881 *
2882 * In the platform template, this image will be shown on the left of the notification view
2883 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2884 * badge atop the large icon).
2885 */
2886 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002887 mN.mLargeIcon = icon;
2888 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08002889 return this;
2890 }
2891
Joe Onoratocb109a02011-01-18 17:57:41 -08002892 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002893 * Set the sound to play.
2894 *
John Spurlockc0650f022014-07-19 13:22:39 -04002895 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2896 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002897 *
2898 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002899 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002900 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002901 mN.sound = sound;
2902 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002903 return this;
2904 }
2905
Joe Onoratocb109a02011-01-18 17:57:41 -08002906 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002907 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002908 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002909 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2910 *
John Spurlockc0650f022014-07-19 13:22:39 -04002911 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002912 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002913 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002914 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002915 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08002916 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002917 mN.sound = sound;
2918 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08002919 return this;
2920 }
2921
Joe Onoratocb109a02011-01-18 17:57:41 -08002922 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002923 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2924 * use during playback.
2925 *
John Spurlockc0650f022014-07-19 13:22:39 -04002926 * @see Notification#sound
2927 */
2928 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002929 mN.sound = sound;
2930 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04002931 return this;
2932 }
2933
2934 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002935 * Set the vibration pattern to use.
2936 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002937 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2938 * <code>pattern</code> parameter.
2939 *
Chris Wren47c20a12014-06-18 17:27:29 -04002940 * <p>
2941 * A notification that vibrates is more likely to be presented as a heads-up notification.
2942 * </p>
2943 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002944 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002945 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002946 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002947 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08002948 return this;
2949 }
2950
Joe Onoratocb109a02011-01-18 17:57:41 -08002951 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002952 * Set the desired color for the indicator LED on the device, as well as the
2953 * blink duty cycle (specified in milliseconds).
2954 *
2955
2956 * Not all devices will honor all (or even any) of these values.
2957 *
2958
2959 * @see Notification#ledARGB
2960 * @see Notification#ledOnMS
2961 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002962 */
Tor Norbye80756e32015-03-02 09:39:27 -08002963 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002964 mN.ledARGB = argb;
2965 mN.ledOnMS = onMs;
2966 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002967 if (onMs != 0 || offMs != 0) {
2968 mN.flags |= FLAG_SHOW_LIGHTS;
2969 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002970 return this;
2971 }
2972
Joe Onoratocb109a02011-01-18 17:57:41 -08002973 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002974 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002975 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002976
2977 * Ongoing notifications cannot be dismissed by the user, so your application or service
2978 * must take care of canceling them.
2979 *
2980
2981 * They are typically used to indicate a background task that the user is actively engaged
2982 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2983 * (e.g., a file download, sync operation, active network connection).
2984 *
2985
2986 * @see Notification#FLAG_ONGOING_EVENT
2987 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002988 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002989 public Builder setOngoing(boolean ongoing) {
2990 setFlag(FLAG_ONGOING_EVENT, ongoing);
2991 return this;
2992 }
2993
Joe Onoratocb109a02011-01-18 17:57:41 -08002994 /**
2995 * Set this flag if you would only like the sound, vibrate
2996 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002997 *
2998 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002999 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003000 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3001 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3002 return this;
3003 }
3004
Joe Onoratocb109a02011-01-18 17:57:41 -08003005 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003006 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003007 *
3008 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003009 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003010 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003011 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003012 return this;
3013 }
3014
Joe Onoratocb109a02011-01-18 17:57:41 -08003015 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003016 * Set whether or not this notification should not bridge to other devices.
3017 *
3018 * <p>Some notifications can be bridged to other devices for remote display.
3019 * This hint can be set to recommend this notification not be bridged.
3020 */
3021 public Builder setLocalOnly(boolean localOnly) {
3022 setFlag(FLAG_LOCAL_ONLY, localOnly);
3023 return this;
3024 }
3025
3026 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003027 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003028 * <p>
3029 * The value should be one or more of the following fields combined with
3030 * bitwise-or:
3031 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3032 * <p>
3033 * For all default values, use {@link #DEFAULT_ALL}.
3034 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003035 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003036 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003037 return this;
3038 }
3039
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003040 /**
3041 * Set the priority of this notification.
3042 *
3043 * @see Notification#priority
3044 */
Tor Norbyed9273d62013-05-30 15:59:53 -07003045 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003046 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003047 return this;
3048 }
Joe Malin8d40d042012-11-05 11:36:40 -08003049
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003050 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003051 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003052 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003053 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003054 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003055 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003056 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003057 return this;
3058 }
3059
3060 /**
Chris Wrendde75302014-03-26 17:24:15 -04003061 * Add a person that is relevant to this notification.
3062 *
Chris Wrene6c48932014-09-29 17:19:27 -04003063 * <P>
3064 * Depending on user preferences, this annotation may allow the notification to pass
3065 * through interruption filters, and to appear more prominently in the user interface.
3066 * </P>
3067 *
3068 * <P>
3069 * The person should be specified by the {@code String} representation of a
3070 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3071 * </P>
3072 *
3073 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3074 * URIs. The path part of these URIs must exist in the contacts database, in the
3075 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3076 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
3077 * </P>
3078 *
3079 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003080 * @see Notification#EXTRA_PEOPLE
3081 */
Chris Wrene6c48932014-09-29 17:19:27 -04003082 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003083 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04003084 return this;
3085 }
3086
3087 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003088 * Set this notification to be part of a group of notifications sharing the same key.
3089 * Grouped notifications may display in a cluster or stack on devices which
3090 * support such rendering.
3091 *
3092 * <p>To make this notification the summary for its group, also call
3093 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3094 * {@link #setSortKey}.
3095 * @param groupKey The group key of the group.
3096 * @return this object for method chaining
3097 */
3098 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003099 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003100 return this;
3101 }
3102
3103 /**
3104 * Set this notification to be the group summary for a group of notifications.
3105 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003106 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3107 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003108 * @param isGroupSummary Whether this notification should be a group summary.
3109 * @return this object for method chaining
3110 */
3111 public Builder setGroupSummary(boolean isGroupSummary) {
3112 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3113 return this;
3114 }
3115
3116 /**
3117 * Set a sort key that orders this notification among other notifications from the
3118 * same package. This can be useful if an external sort was already applied and an app
3119 * would like to preserve this. Notifications will be sorted lexicographically using this
3120 * value, although providing different priorities in addition to providing sort key may
3121 * cause this value to be ignored.
3122 *
3123 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003124 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003125 *
3126 * @see String#compareTo(String)
3127 */
3128 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003129 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003130 return this;
3131 }
3132
3133 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003134 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003135 *
Griff Hazen720042b2014-02-24 15:46:56 -08003136 * <p>Values within the Bundle will replace existing extras values in this Builder.
3137 *
3138 * @see Notification#extras
3139 */
Griff Hazen959591e2014-05-15 22:26:18 -07003140 public Builder addExtras(Bundle extras) {
3141 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003142 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003143 }
3144 return this;
3145 }
3146
3147 /**
3148 * Set metadata for this notification.
3149 *
3150 * <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 -04003151 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003152 * called.
3153 *
Griff Hazen720042b2014-02-24 15:46:56 -08003154 * <p>Replaces any existing extras values with those from the provided Bundle.
3155 * Use {@link #addExtras} to merge in metadata instead.
3156 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003157 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003158 */
Griff Hazen959591e2014-05-15 22:26:18 -07003159 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003160 if (extras != null) {
3161 mUserExtras = extras;
3162 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003163 return this;
3164 }
3165
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003166 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003167 * Get the current metadata Bundle used by this notification Builder.
3168 *
3169 * <p>The returned Bundle is shared with this Builder.
3170 *
3171 * <p>The current contents of this Bundle are copied into the Notification each time
3172 * {@link #build()} is called.
3173 *
3174 * @see Notification#extras
3175 */
3176 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003177 return mUserExtras;
3178 }
3179
3180 private Bundle getAllExtras() {
3181 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3182 saveExtras.putAll(mN.extras);
3183 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003184 }
3185
3186 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003187 * Add an action to this notification. Actions are typically displayed by
3188 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003189 * <p>
3190 * Every action must have an icon (32dp square and matching the
3191 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3192 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3193 * <p>
3194 * A notification in its expanded form can display up to 3 actions, from left to right in
3195 * the order they were added. Actions will not be displayed when the notification is
3196 * collapsed, however, so be sure that any essential functions may be accessed by the user
3197 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003198 *
3199 * @param icon Resource ID of a drawable that represents the action.
3200 * @param title Text describing the action.
3201 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003202 *
3203 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003204 */
Dan Sandler86647982015-05-13 23:41:13 -04003205 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003206 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003207 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003208 return this;
3209 }
3210
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003211 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003212 * Add an action to this notification. Actions are typically displayed by
3213 * the system as a button adjacent to the notification content.
3214 * <p>
3215 * Every action must have an icon (32dp square and matching the
3216 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3217 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3218 * <p>
3219 * A notification in its expanded form can display up to 3 actions, from left to right in
3220 * the order they were added. Actions will not be displayed when the notification is
3221 * collapsed, however, so be sure that any essential functions may be accessed by the user
3222 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3223 *
3224 * @param action The action to add.
3225 */
3226 public Builder addAction(Action action) {
3227 mActions.add(action);
3228 return this;
3229 }
3230
3231 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003232 * Alter the complete list of actions attached to this notification.
3233 * @see #addAction(Action).
3234 *
3235 * @param actions
3236 * @return
3237 */
3238 public Builder setActions(Action... actions) {
3239 mActions.clear();
3240 for (int i = 0; i < actions.length; i++) {
3241 mActions.add(actions[i]);
3242 }
3243 return this;
3244 }
3245
3246 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003247 * Add a rich notification style to be applied at build time.
3248 *
3249 * @param style Object responsible for modifying the notification style.
3250 */
3251 public Builder setStyle(Style style) {
3252 if (mStyle != style) {
3253 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003254 if (mStyle != null) {
3255 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003256 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3257 } else {
3258 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003259 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003260 }
3261 return this;
3262 }
3263
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003264 /**
3265 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003266 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003267 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
3268 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
3269 *
3270 * @return The same Builder.
3271 */
3272 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003273 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003274 return this;
3275 }
3276
3277 /**
3278 * Supply a replacement Notification whose contents should be shown in insecure contexts
3279 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3280 * @param n A replacement notification, presumably with some or all info redacted.
3281 * @return The same Builder.
3282 */
3283 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003284 if (n != null) {
3285 mN.publicVersion = new Notification();
3286 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3287 } else {
3288 mN.publicVersion = null;
3289 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003290 return this;
3291 }
3292
Griff Hazenb720abe2014-05-20 13:15:30 -07003293 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003294 * Apply an extender to this notification builder. Extenders may be used to add
3295 * metadata or change options on this builder.
3296 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003297 public Builder extend(Extender extender) {
3298 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003299 return this;
3300 }
3301
Dan Sandler4e787062015-06-17 15:09:48 -04003302 /**
3303 * @hide
3304 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003305 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003306 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003307 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003308 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003309 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003310 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003311 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003312 }
3313
Dan Sandler26e81cf2014-05-06 10:01:27 -04003314 /**
3315 * Sets {@link Notification#color}.
3316 *
3317 * @param argb The accent color to use
3318 *
3319 * @return The same Builder.
3320 */
Tor Norbye80756e32015-03-02 09:39:27 -08003321 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003322 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003323 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003324 return this;
3325 }
3326
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003327 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04003328 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
3329 // This user can never be a badged profile,
3330 // and also includes USER_ALL system notifications.
3331 return null;
3332 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02003333 // Note: This assumes that the current user can read the profile badge of the
3334 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003335 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003336 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003337 }
3338
3339 private Bitmap getProfileBadge() {
3340 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003341 if (badge == null) {
3342 return null;
3343 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003344 final int size = mContext.getResources().getDimensionPixelSize(
3345 R.dimen.notification_badge_size);
3346 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003347 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003348 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003349 badge.draw(canvas);
3350 return bitmap;
3351 }
3352
Selim Cinekc848c3a2016-01-13 15:27:30 -08003353 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003354 Bitmap profileBadge = getProfileBadge();
3355
Kenny Guy98193ea2014-07-24 19:54:37 +01003356 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003357 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3358 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Kenny Guy98193ea2014-07-24 19:54:37 +01003359 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003360 }
3361
Christoph Studerfe718432014-09-01 18:21:18 +02003362 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003363 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003364 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003365 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003366 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003367 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003368 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003369 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003370 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003371 contentView.setTextViewText(R.id.text_line_1, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003372 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003373 }
3374
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003375 /**
3376 * Resets the notification header to its original state
3377 */
3378 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07003379 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
3380 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08003381 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003382 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003383 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003384 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07003385 contentView.setTextViewText(R.id.header_text, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003386 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003387 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003388 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003389 contentView.setImageViewIcon(R.id.profile_badge, null);
3390 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003391 }
3392
3393 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003394 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
3395 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003396 }
3397
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003398 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003399 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003400 }
3401
3402 /**
3403 * @param hasProgress whether the progress bar should be shown and set
3404 */
3405 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003406 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
3407 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07003408 }
3409
Adrian Roos70d7aa32017-01-11 15:39:06 -08003410 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01003411 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003412
Christoph Studerfe718432014-09-01 18:21:18 +02003413 resetStandardTemplate(contentView);
3414
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003415 final Bundle ex = mN.extras;
Dan Sandler190d58d2014-05-15 09:33:39 -04003416
Adrian Roos487374f2017-01-11 15:48:14 -08003417 bindNotificationHeader(contentView, p.ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003418 bindLargeIcon(contentView);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003419 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
3420 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003421 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003422 contentView.setTextViewText(R.id.title, p.title);
Selim Cinek954cc232016-05-20 13:29:23 -07003423 contentView.setViewLayoutWidth(R.id.title, showProgress
3424 ? ViewGroup.LayoutParams.WRAP_CONTENT
3425 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08003426 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08003427 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003428 int textId = showProgress ? com.android.internal.R.id.text_line_1
3429 : com.android.internal.R.id.text;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003430 contentView.setTextViewText(textId, p.text);
Selim Cinek41598732016-01-11 16:58:37 -08003431 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003432 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003433
Selim Cinek279fa862016-06-14 10:57:25 -07003434 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003435
Selim Cinek29603462015-11-17 19:04:39 -08003436 return contentView;
3437 }
3438
Selim Cinek860b6da2015-12-16 19:02:19 -08003439 /**
3440 * @param remoteView the remote view to update the minheight in
3441 * @param hasMinHeight does it have a mimHeight
3442 * @hide
3443 */
3444 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3445 int minHeight = 0;
3446 if (hasMinHeight) {
3447 // we need to set the minHeight of the notification
3448 minHeight = mContext.getResources().getDimensionPixelSize(
3449 com.android.internal.R.dimen.notification_min_content_height);
3450 }
3451 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3452 }
3453
Selim Cinek29603462015-11-17 19:04:39 -08003454 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003455 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3456 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3457 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3458 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003459 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003460 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003461 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003462 contentView.setProgressBackgroundTintList(
3463 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3464 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003465 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003466 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003467 contentView.setProgressTintList(R.id.progress, colorStateList);
3468 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003469 }
Selim Cinek29603462015-11-17 19:04:39 -08003470 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003471 } else {
3472 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003473 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003474 }
Joe Onorato561d3852010-11-20 18:09:34 -08003475 }
3476
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003477 private void bindLargeIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07003478 if (mN.mLargeIcon == null && mN.largeIcon != null) {
3479 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
3480 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003481 if (mN.mLargeIcon != null) {
3482 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3483 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3484 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003485 int endMargin = R.dimen.notification_content_picture_margin;
3486 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
3487 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
3488 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003489 }
3490 }
3491
Adrian Roos487374f2017-01-11 15:48:14 -08003492 private void bindNotificationHeader(RemoteViews contentView, boolean ambient) {
3493 bindSmallIcon(contentView, ambient);
3494 bindHeaderAppName(contentView, ambient);
3495 if (!ambient) {
3496 // Ambient view does not have these
3497 bindHeaderText(contentView);
3498 bindHeaderChronometerAndTime(contentView);
3499 bindExpandButton(contentView);
3500 bindProfileBadge(contentView);
3501 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003502 }
3503
3504 private void bindExpandButton(RemoteViews contentView) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003505 contentView.setDrawableParameters(R.id.expand_button, false, -1, resolveContrastColor(),
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003506 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003507 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003508 resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003509 }
3510
3511 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3512 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003513 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003514 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3515 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3516 contentView.setLong(R.id.chronometer, "setBase",
3517 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3518 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07003519 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07003520 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003521 } else {
3522 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3523 contentView.setLong(R.id.time, "setTime", mN.when);
3524 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003525 } else {
3526 // We still want a time to be set but gone, such that we can show and hide it
3527 // on demand in case it's a child notification without anything in the header
3528 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003529 }
3530 }
3531
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003532 private void bindHeaderText(RemoteViews contentView) {
3533 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3534 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05003535 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003536 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05003537 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003538 if (headerText == null
3539 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3540 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3541 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
3542 }
3543 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003544 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003545 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
3546 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
3547 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003548 }
3549 }
3550
Adrian Rooseba05822016-04-22 17:09:27 -07003551 /**
3552 * @hide
3553 */
3554 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04003555 CharSequence name = null;
3556 final PackageManager pm = mContext.getPackageManager();
3557 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
3558 // only system packages which lump together a bunch of unrelated stuff
3559 // may substitute a different name to make the purpose of the
3560 // notification more clear. the correct package label should always
3561 // be accessible via SystemUI.
3562 final String pkg = mContext.getPackageName();
3563 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
3564 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
3565 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
3566 name = subName;
3567 } else {
3568 Log.w(TAG, "warning: pkg "
3569 + pkg + " attempting to substitute app name '" + subName
3570 + "' without holding perm "
3571 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
3572 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003573 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04003574 if (TextUtils.isEmpty(name)) {
3575 name = pm.getApplicationLabel(mContext.getApplicationInfo());
3576 }
3577 if (TextUtils.isEmpty(name)) {
3578 // still nothing?
3579 return null;
3580 }
3581
3582 return String.valueOf(name);
3583 }
Adrian Roos487374f2017-01-11 15:48:14 -08003584 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04003585 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos487374f2017-01-11 15:48:14 -08003586 contentView.setTextColor(R.id.app_name_text,
3587 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003588 }
3589
Adrian Roos487374f2017-01-11 15:48:14 -08003590 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07003591 if (mN.mSmallIcon == null && mN.icon != 0) {
3592 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
3593 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003594 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Adrian Roos9b45a152016-06-28 13:32:29 -07003595 contentView.setDrawableParameters(R.id.icon, false /* targetBackground */,
3596 -1 /* alpha */, -1 /* colorFilter */, null /* mode */, mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08003597 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003598 }
3599
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003600 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003601 * @return true if the built notification will show the time or the chronometer; false
3602 * otherwise
3603 */
3604 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07003605 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003606 }
3607
Christoph Studerfe718432014-09-01 18:21:18 +02003608 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003609 // actions_container is only reset when there are no actions to avoid focus issues with
3610 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02003611 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003612 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003613
3614 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3615 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3616
3617 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3618 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3619 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3620 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07003621
3622 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003623 }
3624
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003625 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003626 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07003627 }
3628
Adrian Roos70d7aa32017-01-11 15:39:06 -08003629 private RemoteViews applyStandardTemplateWithActions(int layoutId,
3630 StandardTemplateParams p) {
3631 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003632
Christoph Studerfe718432014-09-01 18:21:18 +02003633 resetStandardTemplateWithActions(big);
3634
Adrian Roose458aa82015-12-08 16:17:19 -08003635 boolean validRemoteInput = false;
3636
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003637 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08003638 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003639 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003640 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08003641 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003642 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roos487374f2017-01-11 15:48:14 -08003643 if (p.ambient) {
3644 big.setInt(R.id.actions, "setBackgroundColor", Color.TRANSPARENT);
3645 }
Adrian Roosf852a422016-06-03 13:33:43 -07003646 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
3647 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003648 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003649 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003650 Action action = mActions.get(i);
3651 validRemoteInput |= hasValidRemoteInput(action);
3652
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003653 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08003654 i % 2 != 0, p.ambient);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003655 big.addView(R.id.actions, button);
3656 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003657 } else {
3658 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003659 }
Adrian Roose458aa82015-12-08 16:17:19 -08003660
3661 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08003662 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08003663 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3664 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3665 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
3666
3667 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3668 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3669 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
3670
3671 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3672 big.setViewVisibility(
3673 R.id.notification_material_reply_text_3, View.VISIBLE);
3674 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
3675 }
3676 }
3677 }
3678
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003679 return big;
3680 }
3681
Adrian Roose458aa82015-12-08 16:17:19 -08003682 private boolean hasValidRemoteInput(Action action) {
3683 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
3684 // Weird actions
3685 return false;
3686 }
3687
3688 RemoteInput[] remoteInputs = action.getRemoteInputs();
3689 if (remoteInputs == null) {
3690 return false;
3691 }
3692
3693 for (RemoteInput r : remoteInputs) {
3694 CharSequence[] choices = r.getChoices();
3695 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
3696 return true;
3697 }
3698 }
3699 return false;
3700 }
3701
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003702 /**
3703 * Construct a RemoteViews for the final 1U notification layout. In order:
3704 * 1. Custom contentView from the caller
3705 * 2. Style's proposed content view
3706 * 3. Standard template view
3707 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003708 public RemoteViews createContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003709 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003710 return mN.contentView;
3711 } else if (mStyle != null) {
3712 final RemoteViews styleView = mStyle.makeContentView();
3713 if (styleView != null) {
3714 return styleView;
3715 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003716 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003717 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003718 }
3719
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003720 /**
3721 * Construct a RemoteViews for the final big notification layout.
3722 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003723 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05003724 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08003725 if (mN.bigContentView != null
3726 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003727 return mN.bigContentView;
3728 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05003729 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08003730 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003731 } else if (mActions.size() != 0) {
3732 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05003733 }
3734 adaptNotificationHeaderForBigContentView(result);
3735 return result;
3736 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003737
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003738 /**
3739 * Construct a RemoteViews for the final notification header only
3740 *
3741 * @hide
3742 */
3743 public RemoteViews makeNotificationHeader() {
3744 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
3745 R.layout.notification_template_header);
3746 resetNotificationHeader(header);
Adrian Roos487374f2017-01-11 15:48:14 -08003747 bindNotificationHeader(header, false /* ambient */);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003748 return header;
3749 }
3750
Adrian Roos487374f2017-01-11 15:48:14 -08003751 /**
3752 * Construct a RemoteViews for the ambient version of the notification.
3753 *
3754 * @hide
3755 */
3756 public RemoteViews makeAmbientNotification() {
3757 RemoteViews ambient = applyStandardTemplateWithActions(
3758 R.layout.notification_template_material_ambient,
3759 mParams.reset().fillTextsFrom(this).hasProgress(false).ambient(true));
3760 return ambient;
3761 }
3762
Selim Cinek29603462015-11-17 19:04:39 -08003763 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003764 if (result != null) {
3765 result.setViewVisibility(R.id.text_line_1, View.GONE);
3766 }
Selim Cinek29603462015-11-17 19:04:39 -08003767 }
3768
Selim Cinek850a8542015-11-11 11:48:36 -05003769 private void adaptNotificationHeaderForBigContentView(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003770 if (result != null) {
3771 result.setBoolean(R.id.notification_header, "setExpanded", true);
3772 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003773 }
3774
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003775 /**
3776 * Construct a RemoteViews for the final heads-up notification layout.
3777 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003778 public RemoteViews createHeadsUpContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003779 if (mN.headsUpContentView != null
3780 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003781 return mN.headsUpContentView;
3782 } else if (mStyle != null) {
3783 final RemoteViews styleView = mStyle.makeHeadsUpContentView();
3784 if (styleView != null) {
3785 return styleView;
3786 }
3787 } else if (mActions.size() == 0) {
3788 return null;
3789 }
3790
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003791 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003792 }
3793
Selim Cinek624c02db2015-12-14 21:00:02 -08003794 /**
3795 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
3796 *
3797 * @hide
3798 */
3799 public RemoteViews makePublicContentView() {
3800 if (mN.publicVersion != null) {
3801 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Julia Reynolds3b848122016-02-26 10:45:32 -05003802 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08003803 }
3804 Bundle savedBundle = mN.extras;
3805 Style style = mStyle;
3806 mStyle = null;
3807 Icon largeIcon = mN.mLargeIcon;
3808 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07003809 Bitmap largeIconLegacy = mN.largeIcon;
3810 mN.largeIcon = null;
Selim Cinek624c02db2015-12-14 21:00:02 -08003811 Bundle publicExtras = new Bundle();
3812 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
3813 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
3814 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
3815 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07003816 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
3817 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08003818 publicExtras.putCharSequence(EXTRA_TITLE,
3819 mContext.getString(R.string.notification_hidden_text));
3820 mN.extras = publicExtras;
3821 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
3822 mN.extras = savedBundle;
3823 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07003824 mN.largeIcon = largeIconLegacy;
Selim Cinek624c02db2015-12-14 21:00:02 -08003825 mStyle = style;
3826 return publicView;
3827 }
3828
3829
Chris Wren8fd39ec2014-02-27 17:43:26 -05003830
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003831 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08003832 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003833 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07003834 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003835 emphazisedMode ? getEmphasizedActionLayoutResource()
3836 : tombstone ? getActionTombstoneLayoutResource()
3837 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04003838 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003839 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003840 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003841 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003842 if (action.mRemoteInputs != null) {
3843 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
3844 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003845 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07003846 // change the background bgColor
3847 int bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
3848 : R.color.notification_action_list_dark);
3849 button.setDrawableParameters(R.id.button_holder, true, -1, bgColor,
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003850 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinek981962e2016-07-20 20:41:58 -07003851 CharSequence title = action.title;
3852 ColorStateList[] outResultColor = null;
3853 if (isLegacy()) {
3854 title = clearColorSpans(title);
3855 } else {
3856 outResultColor = new ColorStateList[1];
3857 title = ensureColorSpanContrast(title, bgColor, outResultColor);
3858 }
3859 button.setTextViewText(R.id.action0, title);
3860 if (outResultColor != null && outResultColor[0] != null) {
3861 // We need to set the text color as well since changing a text to uppercase
3862 // clears its spans.
3863 button.setTextColor(R.id.action0, outResultColor[0]);
3864 } else if (mN.color != COLOR_DEFAULT) {
3865 button.setTextColor(R.id.action0,resolveContrastColor());
3866 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003867 } else {
Selim Cinek981962e2016-07-20 20:41:58 -07003868 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003869 if (mN.color != COLOR_DEFAULT) {
Adrian Roos487374f2017-01-11 15:48:14 -08003870 button.setTextColor(R.id.action0,
3871 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003872 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003873 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003874 return button;
3875 }
3876
Joe Onoratocb109a02011-01-18 17:57:41 -08003877 /**
Selim Cinek981962e2016-07-20 20:41:58 -07003878 * Clears all color spans of a text
3879 * @param charSequence the input text
3880 * @return the same text but without color spans
3881 */
3882 private CharSequence clearColorSpans(CharSequence charSequence) {
3883 if (charSequence instanceof Spanned) {
3884 Spanned ss = (Spanned) charSequence;
3885 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
3886 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
3887 for (Object span : spans) {
3888 Object resultSpan = span;
3889 if (resultSpan instanceof CharacterStyle) {
3890 resultSpan = ((CharacterStyle) span).getUnderlying();
3891 }
3892 if (resultSpan instanceof TextAppearanceSpan) {
3893 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
3894 if (originalSpan.getTextColor() != null) {
3895 resultSpan = new TextAppearanceSpan(
3896 originalSpan.getFamily(),
3897 originalSpan.getTextStyle(),
3898 originalSpan.getTextSize(),
3899 null,
3900 originalSpan.getLinkTextColor());
3901 }
3902 } else if (resultSpan instanceof ForegroundColorSpan
3903 || (resultSpan instanceof BackgroundColorSpan)) {
3904 continue;
3905 } else {
3906 resultSpan = span;
3907 }
3908 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
3909 ss.getSpanFlags(span));
3910 }
3911 return builder;
3912 }
3913 return charSequence;
3914 }
3915
3916 /**
3917 * Ensures contrast on color spans against a background color. also returns the color of the
3918 * text if a span was found that spans over the whole text.
3919 *
3920 * @param charSequence the charSequence on which the spans are
3921 * @param background the background color to ensure the contrast against
3922 * @param outResultColor an array in which a color will be returned as the first element if
3923 * there exists a full length color span.
3924 * @return the contrasted charSequence
3925 */
3926 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
3927 ColorStateList[] outResultColor) {
3928 if (charSequence instanceof Spanned) {
3929 Spanned ss = (Spanned) charSequence;
3930 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
3931 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
3932 for (Object span : spans) {
3933 Object resultSpan = span;
3934 int spanStart = ss.getSpanStart(span);
3935 int spanEnd = ss.getSpanEnd(span);
3936 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
3937 if (resultSpan instanceof CharacterStyle) {
3938 resultSpan = ((CharacterStyle) span).getUnderlying();
3939 }
3940 if (resultSpan instanceof TextAppearanceSpan) {
3941 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
3942 ColorStateList textColor = originalSpan.getTextColor();
3943 if (textColor != null) {
3944 int[] colors = textColor.getColors();
3945 int[] newColors = new int[colors.length];
3946 for (int i = 0; i < newColors.length; i++) {
3947 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
3948 colors[i], background);
3949 }
3950 textColor = new ColorStateList(textColor.getStates().clone(),
3951 newColors);
3952 resultSpan = new TextAppearanceSpan(
3953 originalSpan.getFamily(),
3954 originalSpan.getTextStyle(),
3955 originalSpan.getTextSize(),
3956 textColor,
3957 originalSpan.getLinkTextColor());
3958 if (fullLength) {
3959 outResultColor[0] = new ColorStateList(
3960 textColor.getStates().clone(), newColors);
3961 }
3962 }
3963 } else if (resultSpan instanceof ForegroundColorSpan) {
3964 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
3965 int foregroundColor = originalSpan.getForegroundColor();
3966 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
3967 foregroundColor, background);
3968 resultSpan = new ForegroundColorSpan(foregroundColor);
3969 if (fullLength) {
3970 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
3971 }
3972 } else {
3973 resultSpan = span;
3974 }
3975
3976 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
3977 }
3978 return builder;
3979 }
3980 return charSequence;
3981 }
3982
3983 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003984 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003985 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003986 */
3987 private boolean isLegacy() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003988 return getColorUtil() != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003989 }
3990
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003991 private CharSequence processLegacyText(CharSequence charSequence) {
3992 if (isLegacy()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003993 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003994 } else {
3995 return charSequence;
3996 }
3997 }
3998
Dan Sandler26e81cf2014-05-06 10:01:27 -04003999 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004000 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04004001 */
Adrian Roos487374f2017-01-11 15:48:14 -08004002 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
4003 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08004004 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Adrian Roos487374f2017-01-11 15:48:14 -08004005 int color = ambient ? resolveAmbientColor() : resolveContrastColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08004006 if (colorable) {
Adrian Roos487374f2017-01-11 15:48:14 -08004007 contentView.setDrawableParameters(R.id.icon, false, -1, color,
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004008 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08004009
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004010 }
Selim Cinekea4bef72015-12-02 15:51:10 -08004011 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08004012 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004013 }
4014
Dan Sandler26e81cf2014-05-06 10:01:27 -04004015 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004016 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04004017 * if it's grayscale).
4018 */
4019 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04004020 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
4021 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004022 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004023 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08004024 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04004025 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004026 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004027 }
4028
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004029 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004030 if (mN.color != COLOR_DEFAULT) {
4031 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02004032 }
Jorim Jaggi74419312014-06-10 20:57:21 +02004033 }
4034
Adrian Roos4ff3b122016-02-01 12:26:13 -08004035 int resolveContrastColor() {
4036 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
4037 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004038 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004039 final int contrasted = NotificationColorUtil.resolveContrastColor(mContext, mN.color);
4040
4041 mCachedContrastColorIsFor = mN.color;
4042 return mCachedContrastColor = contrasted;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004043 }
4044
Adrian Roos487374f2017-01-11 15:48:14 -08004045 int resolveAmbientColor() {
4046 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
4047 return mCachedAmbientColor;
4048 }
4049 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
4050
4051 mCachedAmbientColorIsFor = mN.color;
4052 return mCachedAmbientColor = contrasted;
4053 }
4054
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004055 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004056 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004057 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08004058 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004059 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004060 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004061 mN.actions = new Action[mActions.size()];
4062 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004063 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004064 if (!mPersonList.isEmpty()) {
4065 mN.extras.putStringArray(EXTRA_PEOPLE,
4066 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004067 }
Selim Cinek247fa012016-02-18 09:50:48 -08004068 if (mN.bigContentView != null || mN.contentView != null
4069 || mN.headsUpContentView != null) {
4070 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
4071 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004072 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08004073 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004074
Julia Reynolds3b848122016-02-26 10:45:32 -05004075 /**
4076 * Creates a Builder from an existing notification so further changes can be made.
4077 * @param context The context for your application / activity.
4078 * @param n The notification to create a Builder from.
4079 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004080 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004081 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004082 ApplicationInfo applicationInfo = n.extras.getParcelable(
4083 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004084 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05004085 if (applicationInfo != null) {
4086 try {
4087 builderContext = context.createApplicationContext(applicationInfo,
4088 Context.CONTEXT_RESTRICTED);
4089 } catch (NameNotFoundException e) {
4090 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
4091 builderContext = context; // try with our context
4092 }
4093 } else {
4094 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02004095 }
4096
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004097 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004098 }
4099
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004100 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004101 * @deprecated Use {@link #build()} instead.
4102 */
4103 @Deprecated
4104 public Notification getNotification() {
4105 return build();
4106 }
4107
4108 /**
4109 * Combine all of the options that have been set and return a new {@link Notification}
4110 * object.
4111 */
4112 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004113 // first, add any extras from the calling code
4114 if (mUserExtras != null) {
4115 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004116 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004117
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004118 mN.creationTime = System.currentTimeMillis();
4119
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004120 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05004121 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02004122
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004123 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004124
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004125 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004126 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004127 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004128
Adrian Roos5081c0d2016-02-26 16:04:19 -08004129 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4130 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004131 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004132 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004133 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
4134 mN.contentView.getSequenceNumber());
4135 }
4136 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004137 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004138 if (mN.bigContentView != null) {
4139 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
4140 mN.bigContentView.getSequenceNumber());
4141 }
4142 }
4143 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004144 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004145 if (mN.headsUpContentView != null) {
4146 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
4147 mN.headsUpContentView.getSequenceNumber());
4148 }
4149 }
4150 }
4151
Julia Reynolds4c0c2022016-02-02 15:11:59 -05004152 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
4153 mN.flags |= FLAG_SHOW_LIGHTS;
4154 }
4155
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004156 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004157 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004158
4159 /**
4160 * Apply this Builder to an existing {@link Notification} object.
4161 *
4162 * @hide
4163 */
4164 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04004165 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004166 return n;
4167 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004168
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004169 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08004170 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
4171 * change.
4172 *
4173 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
4174 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004175 * @hide
4176 */
Adrian Roos184bfe022016-03-03 13:41:44 -08004177 public static Notification maybeCloneStrippedForDelivery(Notification n) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004178 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08004179
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004180 // Only strip views for known Styles because we won't know how to
4181 // re-create them otherwise.
Adrian Roos184bfe022016-03-03 13:41:44 -08004182 if (!TextUtils.isEmpty(templateClass)
4183 && getNotificationStyleClass(templateClass) == null) {
4184 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004185 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004186
4187 // Only strip unmodified BuilderRemoteViews.
4188 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004189 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004190 n.contentView.getSequenceNumber();
4191 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004192 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004193 n.bigContentView.getSequenceNumber();
4194 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004195 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004196 n.headsUpContentView.getSequenceNumber();
4197
4198 // Nothing to do here, no need to clone.
4199 if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
4200 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004201 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004202
4203 Notification clone = n.clone();
4204 if (stripContentView) {
4205 clone.contentView = null;
4206 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
4207 }
4208 if (stripBigContentView) {
4209 clone.bigContentView = null;
4210 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
4211 }
4212 if (stripHeadsUpContentView) {
4213 clone.headsUpContentView = null;
4214 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
4215 }
4216 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004217 }
4218
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004219 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004220 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004221 }
4222
4223 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004224 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004225 }
4226
4227 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004228 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004229 }
4230
4231 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004232 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004233 }
4234
4235 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004236 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004237 }
4238
Adrian Roosc1a80b02016-04-05 14:54:55 -07004239 private int getMessagingLayoutResource() {
4240 return R.layout.notification_template_material_messaging;
4241 }
4242
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004243 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004244 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004245 }
4246
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004247 private int getEmphasizedActionLayoutResource() {
4248 return R.layout.notification_material_action_emphasized;
4249 }
4250
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004251 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004252 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004253 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004254 }
4255
Selim Cinek279fa862016-06-14 10:57:25 -07004256 private boolean hasLargeIcon() {
4257 return mLargeIcon != null || largeIcon != null;
4258 }
4259
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004260 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004261 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004262 * @hide
4263 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07004264 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004265 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
4266 }
4267
4268 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004269 * @return true if the notification will show a chronometer; false otherwise
4270 * @hide
4271 */
4272 public boolean showsChronometer() {
4273 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
4274 }
4275
4276 /**
Julia Reynolds4a02afb2016-12-13 13:39:52 -05004277 * @hide
4278 */
4279 @SystemApi
4280 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
4281 Class<? extends Style>[] classes = new Class[] {
4282 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
4283 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
4284 MessagingStyle.class };
4285 for (Class<? extends Style> innerClass : classes) {
4286 if (templateClass.equals(innerClass.getName())) {
4287 return innerClass;
4288 }
4289 }
4290 return null;
4291 }
4292
4293 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004294 * An object that can apply a rich notification style to a {@link Notification.Builder}
4295 * object.
4296 */
Griff Hazendfcb0802014-02-11 12:00:00 -08004297 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04004298 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004299
4300 /**
4301 * @hide
4302 */
4303 protected CharSequence mSummaryText = null;
4304
4305 /**
4306 * @hide
4307 */
4308 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04004309
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004310 protected Builder mBuilder;
4311
Chris Wrend6297db2012-05-03 16:20:13 -04004312 /**
4313 * Overrides ContentTitle in the big form of the template.
4314 * This defaults to the value passed to setContentTitle().
4315 */
4316 protected void internalSetBigContentTitle(CharSequence title) {
4317 mBigContentTitle = title;
4318 }
4319
4320 /**
4321 * Set the first line of text after the detail section in the big form of the template.
4322 */
4323 protected void internalSetSummaryText(CharSequence cs) {
4324 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04004325 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04004326 }
4327
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004328 public void setBuilder(Builder builder) {
4329 if (mBuilder != builder) {
4330 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004331 if (mBuilder != null) {
4332 mBuilder.setStyle(this);
4333 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004334 }
4335 }
4336
Chris Wrend6297db2012-05-03 16:20:13 -04004337 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004338 if (mBuilder == null) {
4339 throw new IllegalArgumentException("Style requires a valid Builder object");
4340 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004341 }
Chris Wrend6297db2012-05-03 16:20:13 -04004342
4343 protected RemoteViews getStandardView(int layoutId) {
4344 checkBuilder();
4345
Christoph Studer4600f9b2014-07-22 22:44:43 +02004346 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004347 CharSequence oldBuilderContentTitle =
4348 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004349 if (mBigContentTitle != null) {
4350 mBuilder.setContentTitle(mBigContentTitle);
4351 }
4352
Chris Wrend6297db2012-05-03 16:20:13 -04004353 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
4354
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004355 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004356
Chris Wrend6297db2012-05-03 16:20:13 -04004357 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
4358 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04004359 } else {
4360 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004361 }
4362
Chris Wrend6297db2012-05-03 16:20:13 -04004363 return contentView;
4364 }
4365
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004366 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004367 * Construct a Style-specific RemoteViews for the final 1U notification layout.
4368 * The default implementation has nothing additional to add.
4369 * @hide
4370 */
4371 public RemoteViews makeContentView() {
4372 return null;
4373 }
4374
4375 /**
4376 * Construct a Style-specific RemoteViews for the final big notification layout.
4377 * @hide
4378 */
4379 public RemoteViews makeBigContentView() {
4380 return null;
4381 }
4382
4383 /**
4384 * Construct a Style-specific RemoteViews for the final HUN layout.
4385 * @hide
4386 */
4387 public RemoteViews makeHeadsUpContentView() {
4388 return null;
4389 }
4390
4391 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004392 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004393 * @hide
4394 */
4395 public void addExtras(Bundle extras) {
4396 if (mSummaryTextSet) {
4397 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
4398 }
4399 if (mBigContentTitle != null) {
4400 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
4401 }
Chris Wren91ad5632013-06-05 15:05:57 -04004402 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004403 }
4404
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004405 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004406 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004407 * @hide
4408 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02004409 protected void restoreFromExtras(Bundle extras) {
4410 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
4411 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
4412 mSummaryTextSet = true;
4413 }
4414 if (extras.containsKey(EXTRA_TITLE_BIG)) {
4415 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
4416 }
4417 }
4418
4419
4420 /**
4421 * @hide
4422 */
4423 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004424 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004425 return wip;
4426 }
4427
Daniel Sandler0ec46202015-06-24 01:27:05 -04004428 /**
4429 * @hide
4430 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004431 public void purgeResources() {}
4432
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004433 /**
4434 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
4435 * attached to.
4436 *
4437 * @return the fully constructed Notification.
4438 */
4439 public Notification build() {
4440 checkBuilder();
4441 return mBuilder.build();
4442 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004443
4444 /**
4445 * @hide
4446 * @return true if the style positions the progress bar on the second line; false if the
4447 * style hides the progress bar
4448 */
4449 protected boolean hasProgress() {
4450 return true;
4451 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004452
4453 /**
4454 * @hide
4455 * @return Whether we should put the summary be put into the notification header
4456 */
4457 public boolean hasSummaryInHeader() {
4458 return true;
4459 }
Selim Cinek593610c2016-02-16 18:42:57 -08004460
4461 /**
4462 * @hide
4463 * @return Whether custom content views are displayed inline in the style
4464 */
4465 public boolean displayCustomViewInline() {
4466 return false;
4467 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004468 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004469
4470 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004471 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08004472 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004473 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004474 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004475 * Notification notif = new Notification.Builder(mContext)
4476 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
4477 * .setContentText(subject)
4478 * .setSmallIcon(R.drawable.new_post)
4479 * .setLargeIcon(aBitmap)
4480 * .setStyle(new Notification.BigPictureStyle()
4481 * .bigPicture(aBigBitmap))
4482 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004483 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004484 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004485 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004486 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004487 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004488 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004489 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004490 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004491
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004492 public BigPictureStyle() {
4493 }
4494
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004495 /**
4496 * @deprecated use {@code BigPictureStyle()}.
4497 */
4498 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004499 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004500 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004501 }
4502
Chris Wrend6297db2012-05-03 16:20:13 -04004503 /**
4504 * Overrides ContentTitle in the big form of the template.
4505 * This defaults to the value passed to setContentTitle().
4506 */
4507 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004508 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004509 return this;
4510 }
4511
4512 /**
4513 * Set the first line of text after the detail section in the big form of the template.
4514 */
4515 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004516 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004517 return this;
4518 }
4519
Chris Wren0bd664d2012-08-01 13:56:56 -04004520 /**
4521 * Provide the bitmap to be used as the payload for the BigPicture notification.
4522 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004523 public BigPictureStyle bigPicture(Bitmap b) {
4524 mPicture = b;
4525 return this;
4526 }
4527
Chris Wren3745a3d2012-05-22 15:11:52 -04004528 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04004529 * Override the large icon when the big notification is shown.
4530 */
4531 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04004532 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
4533 }
4534
4535 /**
4536 * Override the large icon when the big notification is shown.
4537 */
4538 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04004539 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004540 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004541 return this;
4542 }
4543
Riley Andrews0394a0c2015-11-03 23:36:52 -08004544 /** @hide */
4545 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
4546
Daniel Sandler0ec46202015-06-24 01:27:05 -04004547 /**
4548 * @hide
4549 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004550 @Override
4551 public void purgeResources() {
4552 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08004553 if (mPicture != null &&
4554 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08004555 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004556 mPicture = mPicture.createAshmemBitmap();
4557 }
4558 if (mBigLargeIcon != null) {
4559 mBigLargeIcon.convertToAshmem();
4560 }
4561 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01004562
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004563 /**
4564 * @hide
4565 */
4566 public RemoteViews makeBigContentView() {
4567 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01004568 // This covers the following cases:
4569 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004570 // mN.mLargeIcon
4571 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04004572 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07004573 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004574 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004575 oldLargeIcon = mBuilder.mN.mLargeIcon;
4576 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07004577 // The legacy largeIcon might not allow us to clear the image, as it's taken in
4578 // replacement if the other one is null. Because we're restoring these legacy icons
4579 // for old listeners, this is in general non-null.
4580 largeIconLegacy = mBuilder.mN.largeIcon;
4581 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004582 }
4583
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004584 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05004585 if (mSummaryTextSet) {
4586 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinekc848c3a2016-01-13 15:27:30 -08004587 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05004588 }
Selim Cinek279fa862016-06-14 10:57:25 -07004589 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08004590
Christoph Studer5c510ee2014-12-15 16:32:27 +01004591 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004592 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07004593 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004594 }
4595
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004596 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004597 return contentView;
4598 }
4599
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004600 /**
4601 * @hide
4602 */
4603 public void addExtras(Bundle extras) {
4604 super.addExtras(extras);
4605
4606 if (mBigLargeIconSet) {
4607 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
4608 }
4609 extras.putParcelable(EXTRA_PICTURE, mPicture);
4610 }
4611
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004612 /**
4613 * @hide
4614 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004615 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004616 protected void restoreFromExtras(Bundle extras) {
4617 super.restoreFromExtras(extras);
4618
4619 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01004620 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02004621 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04004622 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02004623 mPicture = extras.getParcelable(EXTRA_PICTURE);
4624 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004625
4626 /**
4627 * @hide
4628 */
4629 @Override
4630 public boolean hasSummaryInHeader() {
4631 return false;
4632 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004633 }
4634
4635 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004636 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08004637 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004638 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004639 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004640 * Notification notif = new Notification.Builder(mContext)
4641 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4642 * .setContentText(subject)
4643 * .setSmallIcon(R.drawable.new_mail)
4644 * .setLargeIcon(aBitmap)
4645 * .setStyle(new Notification.BigTextStyle()
4646 * .bigText(aVeryLongString))
4647 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004648 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004649 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004650 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004651 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004652 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004653
4654 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004655 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004656
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004657 private CharSequence mBigText;
4658
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004659 public BigTextStyle() {
4660 }
4661
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004662 /**
4663 * @deprecated use {@code BigTextStyle()}.
4664 */
4665 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004666 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004667 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004668 }
4669
Chris Wrend6297db2012-05-03 16:20:13 -04004670 /**
4671 * Overrides ContentTitle in the big form of the template.
4672 * This defaults to the value passed to setContentTitle().
4673 */
4674 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004675 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004676 return this;
4677 }
4678
4679 /**
4680 * Set the first line of text after the detail section in the big form of the template.
4681 */
4682 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004683 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004684 return this;
4685 }
4686
Chris Wren0bd664d2012-08-01 13:56:56 -04004687 /**
4688 * Provide the longer text to be displayed in the big form of the
4689 * template in place of the content text.
4690 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004691 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004692 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004693 return this;
4694 }
4695
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004696 /**
4697 * @hide
4698 */
4699 public void addExtras(Bundle extras) {
4700 super.addExtras(extras);
4701
Christoph Studer4600f9b2014-07-22 22:44:43 +02004702 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4703 }
4704
4705 /**
4706 * @hide
4707 */
4708 @Override
4709 protected void restoreFromExtras(Bundle extras) {
4710 super.restoreFromExtras(extras);
4711
4712 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004713 }
4714
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004715 /**
4716 * @hide
4717 */
4718 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004719
4720 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07004721 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004722 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004723
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004724 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004725
Selim Cinek75998782016-04-26 10:39:17 -07004726 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004727
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004728 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07004729 if (TextUtils.isEmpty(bigTextText)) {
4730 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
4731 // experience
4732 bigTextText = mBuilder.processLegacyText(text);
4733 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07004734 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08004735
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004736 return contentView;
4737 }
4738
Adrian Roosb1f427c2016-05-26 12:27:15 -07004739 static void applyBigTextContentView(Builder builder,
4740 RemoteViews contentView, CharSequence bigTextText) {
4741 contentView.setTextViewText(R.id.big_text, bigTextText);
4742 contentView.setViewVisibility(R.id.big_text,
4743 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
4744 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines(builder));
Selim Cinek279fa862016-06-14 10:57:25 -07004745 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07004746 }
4747
4748 private static int calculateMaxLines(Builder builder) {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004749 int lineCount = MAX_LINES;
Adrian Roosb1f427c2016-05-26 12:27:15 -07004750 boolean hasActions = builder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004751 if (hasActions) {
4752 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4753 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004754 return lineCount;
4755 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004756 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004757
4758 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004759 * Helper class for generating large-format notifications that include multiple back-and-forth
4760 * messages of varying types between any number of people.
4761 *
4762 * <br>
4763 * If the platform does not provide large-format notifications, this method has no effect. The
4764 * user will always see the normal notification view.
4765 * <br>
4766 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
4767 * so:
4768 * <pre class="prettyprint">
4769 *
4770 * Notification noti = new Notification.Builder()
4771 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
4772 * .setContentText(subject)
4773 * .setSmallIcon(R.drawable.new_message)
4774 * .setLargeIcon(aBitmap)
4775 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
4776 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
4777 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
4778 * .build();
4779 * </pre>
4780 */
4781 public static class MessagingStyle extends Style {
4782
4783 /**
4784 * The maximum number of messages that will be retained in the Notification itself (the
4785 * number displayed is up to the platform).
4786 */
4787 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
4788
4789 CharSequence mUserDisplayName;
4790 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04004791 List<Message> mMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04004792
4793 MessagingStyle() {
4794 }
4795
4796 /**
Alex Hillsfd590442016-10-07 09:52:44 -04004797 * @param userDisplayName Required - the name to be displayed for any replies sent by the
4798 * user before the posting app reposts the notification with those messages after they've
4799 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04004800 * {@link #addMessage(Notification.MessagingStyle.Message)}
4801 */
Alex Hillsfd590442016-10-07 09:52:44 -04004802 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Alex Hillsfc737de2016-03-23 17:33:02 -04004803 mUserDisplayName = userDisplayName;
4804 }
4805
4806 /**
4807 * Returns the name to be displayed for any replies sent by the user
4808 */
4809 public CharSequence getUserDisplayName() {
4810 return mUserDisplayName;
4811 }
4812
4813 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004814 * Sets the title to be displayed on this conversation. This should only be used for
4815 * group messaging and left unset for one-on-one conversations.
4816 * @param conversationTitle
4817 * @return this object for method chaining.
4818 */
4819 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
4820 mConversationTitle = conversationTitle;
4821 return this;
4822 }
4823
4824 /**
4825 * Return the title to be displayed on this conversation. Can be <code>null</code> and
4826 * should be for one-on-one conversations
4827 */
4828 public CharSequence getConversationTitle() {
4829 return mConversationTitle;
4830 }
4831
4832 /**
4833 * Adds a message for display by this notification. Convenience call for a simple
4834 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
4835 * @param text A {@link CharSequence} to be displayed as the message content
4836 * @param timestamp Time at which the message arrived
4837 * @param sender A {@link CharSequence} to be used for displaying the name of the
4838 * sender. Should be <code>null</code> for messages by the current user, in which case
4839 * the platform will insert {@link #getUserDisplayName()}.
4840 * Should be unique amongst all individuals in the conversation, and should be
4841 * consistent during re-posts of the notification.
4842 *
4843 * @see Message#Message(CharSequence, long, CharSequence)
4844 *
4845 * @return this object for method chaining
4846 */
4847 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
4848 mMessages.add(new Message(text, timestamp, sender));
4849 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4850 mMessages.remove(0);
4851 }
4852 return this;
4853 }
4854
4855 /**
4856 * Adds a {@link Message} for display in this notification.
4857 * @param message The {@link Message} to be displayed
4858 * @return this object for method chaining
4859 */
4860 public MessagingStyle addMessage(Message message) {
4861 mMessages.add(message);
4862 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4863 mMessages.remove(0);
4864 }
4865 return this;
4866 }
4867
4868 /**
4869 * Gets the list of {@code Message} objects that represent the notification
4870 */
4871 public List<Message> getMessages() {
4872 return mMessages;
4873 }
4874
4875 /**
4876 * @hide
4877 */
4878 @Override
4879 public void addExtras(Bundle extras) {
4880 super.addExtras(extras);
4881 if (mUserDisplayName != null) {
4882 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
4883 }
4884 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004885 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04004886 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004887 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
4888 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04004889 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07004890
4891 fixTitleAndTextExtras(extras);
4892 }
4893
4894 private void fixTitleAndTextExtras(Bundle extras) {
4895 Message m = findLatestIncomingMessage();
4896 CharSequence text = (m == null) ? null : m.mText;
4897 CharSequence sender = m == null ? null
4898 : TextUtils.isEmpty(m.mSender) ? mUserDisplayName : m.mSender;
4899 CharSequence title;
4900 if (!TextUtils.isEmpty(mConversationTitle)) {
4901 if (!TextUtils.isEmpty(sender)) {
4902 BidiFormatter bidi = BidiFormatter.getInstance();
4903 title = mBuilder.mContext.getString(
4904 com.android.internal.R.string.notification_messaging_title_template,
4905 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(m.mSender));
4906 } else {
4907 title = mConversationTitle;
4908 }
4909 } else {
4910 title = sender;
4911 }
4912
4913 if (title != null) {
4914 extras.putCharSequence(EXTRA_TITLE, title);
4915 }
4916 if (text != null) {
4917 extras.putCharSequence(EXTRA_TEXT, text);
4918 }
Alex Hillsfc737de2016-03-23 17:33:02 -04004919 }
4920
4921 /**
4922 * @hide
4923 */
4924 @Override
4925 protected void restoreFromExtras(Bundle extras) {
4926 super.restoreFromExtras(extras);
4927
4928 mMessages.clear();
Adrian Roos96b7e202016-05-17 13:50:38 -07004929 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
4930 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Alex Hillsd9b04d92016-04-11 16:38:16 -04004931 Parcelable[] parcelables = extras.getParcelableArray(EXTRA_MESSAGES);
Adrian Roosdedd1df2016-04-26 16:38:47 -07004932 if (parcelables != null && parcelables instanceof Parcelable[]) {
4933 mMessages = Message.getMessagesFromBundleArray(parcelables);
Alex Hillsfc737de2016-03-23 17:33:02 -04004934 }
4935 }
4936
4937 /**
4938 * @hide
4939 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07004940 @Override
4941 public RemoteViews makeContentView() {
4942 Message m = findLatestIncomingMessage();
4943 CharSequence title = mConversationTitle != null
4944 ? mConversationTitle
4945 : (m == null) ? null : m.mSender;
4946 CharSequence text = (m == null)
4947 ? null
4948 : mConversationTitle != null ? makeMessageLine(m) : m.mText;
Alex Hillsfc737de2016-03-23 17:33:02 -04004949
Adrian Roos70d7aa32017-01-11 15:39:06 -08004950 return mBuilder.applyStandardTemplateWithActions(mBuilder.getBaseLayoutResource(),
4951 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
Adrian Roosc1a80b02016-04-05 14:54:55 -07004952 }
4953
4954 private Message findLatestIncomingMessage() {
4955 for (int i = mMessages.size() - 1; i >= 0; i--) {
4956 Message m = mMessages.get(i);
4957 // Incoming messages have a non-empty sender.
4958 if (!TextUtils.isEmpty(m.mSender)) {
4959 return m;
4960 }
4961 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07004962 if (!mMessages.isEmpty()) {
4963 // No incoming messages, fall back to outgoing message
4964 return mMessages.get(mMessages.size() - 1);
4965 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07004966 return null;
4967 }
4968
4969 /**
4970 * @hide
4971 */
4972 @Override
4973 public RemoteViews makeBigContentView() {
4974 CharSequence title = !TextUtils.isEmpty(super.mBigContentTitle)
4975 ? super.mBigContentTitle
4976 : mConversationTitle;
4977 boolean hasTitle = !TextUtils.isEmpty(title);
4978
Adrian Roosfeafa052016-06-01 17:09:45 -07004979 if (mMessages.size() == 1) {
4980 // Special case for a single message: Use the big text style
4981 // so the collapsed and expanded versions match nicely.
4982 CharSequence bigTitle;
4983 CharSequence text;
4984 if (hasTitle) {
4985 bigTitle = title;
4986 text = makeMessageLine(mMessages.get(0));
4987 } else {
4988 bigTitle = mMessages.get(0).mSender;
4989 text = mMessages.get(0).mText;
4990 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07004991 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
4992 mBuilder.getBigTextLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08004993 mBuilder.mParams.reset().hasProgress(false).title(bigTitle).text(null));
Adrian Roosb1f427c2016-05-26 12:27:15 -07004994 BigTextStyle.applyBigTextContentView(mBuilder, contentView, text);
4995 return contentView;
4996 }
4997
Adrian Roos48d746a2016-04-12 14:57:28 -07004998 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07004999 mBuilder.getMessagingLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08005000 mBuilder.mParams.reset().hasProgress(false).title(title).text(null));
Adrian Roosc1a80b02016-04-05 14:54:55 -07005001
5002 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
5003 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
5004
5005 // Make sure all rows are gone in case we reuse a view.
5006 for (int rowId : rowIds) {
5007 contentView.setViewVisibility(rowId, View.GONE);
5008 }
5009
5010 int i=0;
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005011 contentView.setViewLayoutMarginBottomDimen(R.id.line1,
5012 hasTitle ? R.dimen.notification_messaging_spacing : 0);
Adrian Roosc1a80b02016-04-05 14:54:55 -07005013 contentView.setInt(R.id.notification_messaging, "setNumIndentLines",
Selim Cinek279fa862016-06-14 10:57:25 -07005014 !mBuilder.mN.hasLargeIcon() ? 0 : (hasTitle ? 1 : 2));
Adrian Roosc1a80b02016-04-05 14:54:55 -07005015
Adrian Roosfeafa052016-06-01 17:09:45 -07005016 int contractedChildId = View.NO_ID;
5017 Message contractedMessage = findLatestIncomingMessage();
Adrian Roosc1a80b02016-04-05 14:54:55 -07005018 int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
5019 while (firstMessage + i < mMessages.size() && i < rowIds.length) {
5020 Message m = mMessages.get(firstMessage + i);
5021 int rowId = rowIds[i];
5022
5023 contentView.setViewVisibility(rowId, View.VISIBLE);
5024 contentView.setTextViewText(rowId, makeMessageLine(m));
5025
Adrian Roosfeafa052016-06-01 17:09:45 -07005026 if (contractedMessage == m) {
5027 contractedChildId = rowId;
5028 }
5029
Adrian Roosc1a80b02016-04-05 14:54:55 -07005030 i++;
5031 }
Adrian Roosfeafa052016-06-01 17:09:45 -07005032 // Record this here to allow transformation between the contracted and expanded views.
5033 contentView.setInt(R.id.notification_messaging, "setContractedChildId",
5034 contractedChildId);
Alex Hillsfc737de2016-03-23 17:33:02 -04005035 return contentView;
5036 }
5037
Adrian Roosc1a80b02016-04-05 14:54:55 -07005038 private CharSequence makeMessageLine(Message m) {
5039 BidiFormatter bidi = BidiFormatter.getInstance();
5040 SpannableStringBuilder sb = new SpannableStringBuilder();
5041 if (TextUtils.isEmpty(m.mSender)) {
5042 CharSequence replyName = mUserDisplayName == null ? "" : mUserDisplayName;
5043 sb.append(bidi.unicodeWrap(replyName),
5044 makeFontColorSpan(mBuilder.resolveContrastColor()),
5045 0 /* flags */);
5046 } else {
5047 sb.append(bidi.unicodeWrap(m.mSender),
5048 makeFontColorSpan(Color.BLACK),
5049 0 /* flags */);
5050 }
5051 CharSequence text = m.mText == null ? "" : m.mText;
5052 sb.append(" ").append(bidi.unicodeWrap(text));
5053 return sb;
5054 }
5055
Adrian Roosdedd1df2016-04-26 16:38:47 -07005056 /**
5057 * @hide
5058 */
5059 @Override
5060 public RemoteViews makeHeadsUpContentView() {
5061 Message m = findLatestIncomingMessage();
5062 CharSequence title = mConversationTitle != null
5063 ? mConversationTitle
5064 : (m == null) ? null : m.mSender;
5065 CharSequence text = (m == null)
5066 ? null
5067 : mConversationTitle != null ? makeMessageLine(m) : m.mText;
5068
5069 return mBuilder.applyStandardTemplateWithActions(mBuilder.getBigBaseLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08005070 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
Adrian Roosdedd1df2016-04-26 16:38:47 -07005071 }
5072
Adrian Roosc1a80b02016-04-05 14:54:55 -07005073 private static TextAppearanceSpan makeFontColorSpan(int color) {
5074 return new TextAppearanceSpan(null, 0, 0,
5075 ColorStateList.valueOf(color), null);
5076 }
5077
Alex Hillsd9b04d92016-04-11 16:38:16 -04005078 public static final class Message {
5079
5080 static final String KEY_TEXT = "text";
5081 static final String KEY_TIMESTAMP = "time";
5082 static final String KEY_SENDER = "sender";
5083 static final String KEY_DATA_MIME_TYPE = "type";
5084 static final String KEY_DATA_URI= "uri";
Alex Hillsfc737de2016-03-23 17:33:02 -04005085
5086 private final CharSequence mText;
5087 private final long mTimestamp;
5088 private final CharSequence mSender;
5089
5090 private String mDataMimeType;
5091 private Uri mDataUri;
5092
5093 /**
5094 * Constructor
5095 * @param text A {@link CharSequence} to be displayed as the message content
5096 * @param timestamp Time at which the message arrived
5097 * @param sender A {@link CharSequence} to be used for displaying the name of the
5098 * sender. Should be <code>null</code> for messages by the current user, in which case
5099 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
5100 * Should be unique amongst all individuals in the conversation, and should be
5101 * consistent during re-posts of the notification.
5102 */
5103 public Message(CharSequence text, long timestamp, CharSequence sender){
5104 mText = text;
5105 mTimestamp = timestamp;
5106 mSender = sender;
5107 }
5108
5109 /**
5110 * Sets a binary blob of data and an associated MIME type for a message. In the case
5111 * where the platform doesn't support the MIME type, the original text provided in the
5112 * constructor will be used.
5113 * @param dataMimeType The MIME type of the content. See
5114 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
5115 * types on Android and Android Wear.
5116 * @param dataUri The uri containing the content whose type is given by the MIME type.
5117 * <p class="note">
5118 * <ol>
5119 * <li>Notification Listeners including the System UI need permission to access the
5120 * data the Uri points to. The recommended ways to do this are:</li>
5121 * <li>Store the data in your own ContentProvider, making sure that other apps have
5122 * the correct permission to access your provider. The preferred mechanism for
5123 * providing access is to use per-URI permissions which are temporary and only
5124 * grant access to the receiving application. An easy way to create a
5125 * ContentProvider like this is to use the FileProvider helper class.</li>
5126 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
5127 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
5128 * also store non-media types (see MediaStore.Files for more info). Files can be
5129 * inserted into the MediaStore using scanFile() after which a content:// style
5130 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
5131 * Note that once added to the system MediaStore the content is accessible to any
5132 * app on the device.</li>
5133 * </ol>
5134 * @return this object for method chaining
5135 */
5136 public Message setData(String dataMimeType, Uri dataUri) {
5137 mDataMimeType = dataMimeType;
5138 mDataUri = dataUri;
5139 return this;
5140 }
5141
Alex Hillsfc737de2016-03-23 17:33:02 -04005142 /**
5143 * Get the text to be used for this message, or the fallback text if a type and content
5144 * Uri have been set
5145 */
5146 public CharSequence getText() {
5147 return mText;
5148 }
5149
5150 /**
5151 * Get the time at which this message arrived
5152 */
5153 public long getTimestamp() {
5154 return mTimestamp;
5155 }
5156
5157 /**
5158 * Get the text used to display the contact's name in the messaging experience
5159 */
5160 public CharSequence getSender() {
5161 return mSender;
5162 }
5163
5164 /**
5165 * Get the MIME type of the data pointed to by the Uri
5166 */
5167 public String getDataMimeType() {
5168 return mDataMimeType;
5169 }
5170
5171 /**
5172 * Get the the Uri pointing to the content of the message. Can be null, in which case
5173 * {@see #getText()} is used.
5174 */
5175 public Uri getDataUri() {
5176 return mDataUri;
5177 }
5178
Alex Hillsd9b04d92016-04-11 16:38:16 -04005179 private Bundle toBundle() {
5180 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04005181 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005182 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04005183 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005184 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04005185 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005186 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04005187 }
5188 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005189 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04005190 }
5191 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005192 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04005193 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005194 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04005195 }
5196
Alex Hillsd9b04d92016-04-11 16:38:16 -04005197 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
5198 Bundle[] bundles = new Bundle[messages.size()];
5199 final int N = messages.size();
5200 for (int i = 0; i < N; i++) {
5201 bundles[i] = messages.get(i).toBundle();
5202 }
5203 return bundles;
5204 }
5205
Adrian Roosdedd1df2016-04-26 16:38:47 -07005206 static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005207 List<Message> messages = new ArrayList<>(bundles.length);
5208 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07005209 if (bundles[i] instanceof Bundle) {
5210 Message message = getMessageFromBundle((Bundle)bundles[i]);
5211 if (message != null) {
5212 messages.add(message);
5213 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005214 }
5215 }
5216 return messages;
5217 }
5218
5219 static Message getMessageFromBundle(Bundle bundle) {
5220 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07005221 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005222 return null;
5223 } else {
5224 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
5225 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
5226 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
5227 bundle.containsKey(KEY_DATA_URI)) {
5228
5229 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
5230 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04005231 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005232 return message;
5233 }
5234 } catch (ClassCastException e) {
5235 return null;
5236 }
5237 }
Alex Hillsfc737de2016-03-23 17:33:02 -04005238 }
5239 }
5240
5241 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04005242 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08005243 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005244 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04005245 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005246 * Notification notif = new Notification.Builder(mContext)
5247 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
5248 * .setContentText(subject)
5249 * .setSmallIcon(R.drawable.new_mail)
5250 * .setLargeIcon(aBitmap)
5251 * .setStyle(new Notification.InboxStyle()
5252 * .addLine(str1)
5253 * .addLine(str2)
5254 * .setContentTitle(&quot;&quot;)
5255 * .setSummaryText(&quot;+3 more&quot;))
5256 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04005257 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005258 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04005259 * @see Notification#bigContentView
5260 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005261 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005262 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
5263
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005264 public InboxStyle() {
5265 }
5266
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005267 /**
5268 * @deprecated use {@code InboxStyle()}.
5269 */
5270 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04005271 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005272 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04005273 }
5274
Chris Wrend6297db2012-05-03 16:20:13 -04005275 /**
5276 * Overrides ContentTitle in the big form of the template.
5277 * This defaults to the value passed to setContentTitle().
5278 */
5279 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005280 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005281 return this;
5282 }
5283
5284 /**
5285 * Set the first line of text after the detail section in the big form of the template.
5286 */
5287 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005288 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005289 return this;
5290 }
5291
Chris Wren0bd664d2012-08-01 13:56:56 -04005292 /**
5293 * Append a line to the digest section of the Inbox notification.
5294 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04005295 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005296 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04005297 return this;
5298 }
5299
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005300 /**
5301 * @hide
5302 */
5303 public void addExtras(Bundle extras) {
5304 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005305
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005306 CharSequence[] a = new CharSequence[mTexts.size()];
5307 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
5308 }
5309
Christoph Studer4600f9b2014-07-22 22:44:43 +02005310 /**
5311 * @hide
5312 */
5313 @Override
5314 protected void restoreFromExtras(Bundle extras) {
5315 super.restoreFromExtras(extras);
5316
5317 mTexts.clear();
5318 if (extras.containsKey(EXTRA_TEXT_LINES)) {
5319 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
5320 }
5321 }
5322
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005323 /**
5324 * @hide
5325 */
5326 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08005327 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02005328 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005329 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
5330 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005331
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005332 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04005333
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005334 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005335
Chris Wrend6297db2012-05-03 16:20:13 -04005336 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 -04005337 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04005338
Chris Wren4ed80d52012-05-17 09:30:03 -04005339 // Make sure all rows are gone in case we reuse a view.
5340 for (int rowId : rowIds) {
5341 contentView.setViewVisibility(rowId, View.GONE);
5342 }
5343
Daniel Sandler879c5e02012-04-17 16:46:51 -04005344 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07005345 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
5346 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08005347 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07005348 int onlyViewId = 0;
5349 int maxRows = rowIds.length;
5350 if (mBuilder.mActions.size() > 0) {
5351 maxRows--;
5352 }
5353 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005354 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07005355 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005356 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005357 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Selim Cinek07c80172016-04-21 16:40:47 -07005358 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08005359 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07005360 if (first) {
5361 onlyViewId = rowIds[i];
5362 } else {
5363 onlyViewId = 0;
5364 }
Selim Cinek247fa012016-02-18 09:50:48 -08005365 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04005366 }
5367 i++;
5368 }
Selim Cinek07c80172016-04-21 16:40:47 -07005369 if (onlyViewId != 0) {
5370 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
5371 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
5372 R.dimen.notification_text_margin_top);
5373 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
5374 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005375
Daniel Sandler879c5e02012-04-17 16:46:51 -04005376 return contentView;
5377 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005378
Selim Cinek247fa012016-02-18 09:50:48 -08005379 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08005380 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08005381 if (first) {
5382 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
5383 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
5384 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07005385 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005386 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08005387 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005388 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005389 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08005390 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005391 }
Dan Sandler842dd772014-05-15 09:36:47 -04005392
5393 /**
5394 * Notification style for media playback notifications.
5395 *
5396 * In the expanded form, {@link Notification#bigContentView}, up to 5
5397 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04005398 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04005399 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
5400 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
5401 * treated as album artwork.
5402 *
5403 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
5404 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01005405 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04005406 * in the standard view alongside the usual content.
5407 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005408 * Notifications created with MediaStyle will have their category set to
5409 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
5410 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
5411 *
Jeff Browndba34ba2014-06-24 20:46:03 -07005412 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
5413 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04005414 * the System UI can identify this as a notification representing an active media session
5415 * and respond accordingly (by showing album artwork in the lockscreen, for example).
5416 *
5417 * To use this style with your Notification, feed it to
5418 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5419 * <pre class="prettyprint">
5420 * Notification noti = new Notification.Builder()
5421 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01005422 * .setContentTitle(&quot;Track title&quot;)
5423 * .setContentText(&quot;Artist - Album&quot;)
5424 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005425 * .setStyle(<b>new Notification.MediaStyle()</b>
5426 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04005427 * .build();
5428 * </pre>
5429 *
5430 * @see Notification#bigContentView
5431 */
5432 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005433 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04005434 static final int MAX_MEDIA_BUTTONS = 5;
5435
5436 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07005437 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04005438
5439 public MediaStyle() {
5440 }
5441
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005442 /**
5443 * @deprecated use {@code MediaStyle()}.
5444 */
5445 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04005446 public MediaStyle(Builder builder) {
5447 setBuilder(builder);
5448 }
5449
5450 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005451 * 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 -04005452 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005453 *
5454 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04005455 */
5456 public MediaStyle setShowActionsInCompactView(int...actions) {
5457 mActionsToShowInCompact = actions;
5458 return this;
5459 }
5460
5461 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07005462 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
5463 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04005464 */
Jeff Browndba34ba2014-06-24 20:46:03 -07005465 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04005466 mToken = token;
5467 return this;
5468 }
5469
Christoph Studer4600f9b2014-07-22 22:44:43 +02005470 /**
5471 * @hide
5472 */
Dan Sandler842dd772014-05-15 09:36:47 -04005473 @Override
5474 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005475 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005476 if (wip.category == null) {
5477 wip.category = Notification.CATEGORY_TRANSPORT;
5478 }
Dan Sandler842dd772014-05-15 09:36:47 -04005479 return wip;
5480 }
5481
Christoph Studer4600f9b2014-07-22 22:44:43 +02005482 /**
5483 * @hide
5484 */
5485 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005486 public RemoteViews makeContentView() {
5487 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02005488 }
5489
5490 /**
5491 * @hide
5492 */
5493 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005494 public RemoteViews makeBigContentView() {
5495 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02005496 }
5497
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005498 /**
5499 * @hide
5500 */
5501 @Override
5502 public RemoteViews makeHeadsUpContentView() {
5503 RemoteViews expanded = makeMediaBigContentView();
5504 return expanded != null ? expanded : makeMediaContentView();
5505 }
5506
Dan Sandler842dd772014-05-15 09:36:47 -04005507 /** @hide */
5508 @Override
5509 public void addExtras(Bundle extras) {
5510 super.addExtras(extras);
5511
5512 if (mToken != null) {
5513 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
5514 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01005515 if (mActionsToShowInCompact != null) {
5516 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
5517 }
Dan Sandler842dd772014-05-15 09:36:47 -04005518 }
5519
Christoph Studer4600f9b2014-07-22 22:44:43 +02005520 /**
5521 * @hide
5522 */
5523 @Override
5524 protected void restoreFromExtras(Bundle extras) {
5525 super.restoreFromExtras(extras);
5526
5527 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
5528 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
5529 }
5530 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
5531 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
5532 }
5533 }
5534
Selim Cinek5bf069a2015-11-10 19:14:27 -05005535 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04005536 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005537 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07005538 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04005539 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05005540 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
5541 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04005542 if (!tombstone) {
5543 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
5544 }
5545 button.setContentDescription(R.id.action0, action.title);
5546 return button;
5547 }
5548
5549 private RemoteViews makeMediaContentView() {
5550 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005551 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04005552
5553 final int numActions = mBuilder.mActions.size();
5554 final int N = mActionsToShowInCompact == null
5555 ? 0
5556 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
5557 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005558 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04005559 for (int i = 0; i < N; i++) {
5560 if (i >= numActions) {
5561 throw new IllegalArgumentException(String.format(
5562 "setShowActionsInCompactView: action %d out of bounds (max %d)",
5563 i, numActions - 1));
5564 }
5565
5566 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05005567 final RemoteViews button = generateMediaActionButton(action,
Adrian Roos4ff3b122016-02-01 12:26:13 -08005568 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005569 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04005570 }
5571 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08005572 handleImage(view);
5573 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005574 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07005575 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005576 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08005577 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005578 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04005579 return view;
5580 }
5581
5582 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005583 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005584 // Dont add an expanded view if there is no more content to be revealed
5585 int actionsInCompact = mActionsToShowInCompact == null
5586 ? 0
5587 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07005588 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005589 return null;
5590 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05005591 RemoteViews big = mBuilder.applyStandardTemplate(
5592 R.layout.notification_template_material_big_media,
5593 false);
Dan Sandler842dd772014-05-15 09:36:47 -04005594
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005595 if (actionCount > 0) {
5596 big.removeAllViews(com.android.internal.R.id.media_actions);
5597 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05005598 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Adrian Roos4ff3b122016-02-01 12:26:13 -08005599 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005600 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04005601 }
5602 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05005603 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04005604 return big;
5605 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005606
Selim Cinek5bf069a2015-11-10 19:14:27 -05005607 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07005608 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005609 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
5610 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005611 }
5612 }
5613
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005614 /**
5615 * @hide
5616 */
5617 @Override
5618 protected boolean hasProgress() {
5619 return false;
5620 }
Dan Sandler842dd772014-05-15 09:36:47 -04005621 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005622
Selim Cinek593610c2016-02-16 18:42:57 -08005623 /**
5624 * Notification style for custom views that are decorated by the system
5625 *
5626 * <p>Instead of providing a notification that is completely custom, a developer can set this
5627 * style and still obtain system decorations like the notification header with the expand
5628 * affordance and actions.
5629 *
5630 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5631 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5632 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5633 * corresponding custom views to display.
5634 *
5635 * To use this style with your Notification, feed it to
5636 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5637 * <pre class="prettyprint">
5638 * Notification noti = new Notification.Builder()
5639 * .setSmallIcon(R.drawable.ic_stat_player)
5640 * .setLargeIcon(albumArtBitmap))
5641 * .setCustomContentView(contentView);
5642 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
5643 * .build();
5644 * </pre>
5645 */
5646 public static class DecoratedCustomViewStyle extends Style {
5647
5648 public DecoratedCustomViewStyle() {
5649 }
5650
Selim Cinek593610c2016-02-16 18:42:57 -08005651 /**
5652 * @hide
5653 */
5654 public boolean displayCustomViewInline() {
5655 return true;
5656 }
5657
5658 /**
5659 * @hide
5660 */
5661 @Override
5662 public RemoteViews makeContentView() {
5663 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
5664 }
5665
5666 /**
5667 * @hide
5668 */
5669 @Override
5670 public RemoteViews makeBigContentView() {
5671 return makeDecoratedBigContentView();
5672 }
5673
5674 /**
5675 * @hide
5676 */
5677 @Override
5678 public RemoteViews makeHeadsUpContentView() {
5679 return makeDecoratedHeadsUpContentView();
5680 }
5681
Selim Cinek593610c2016-02-16 18:42:57 -08005682 private RemoteViews makeDecoratedHeadsUpContentView() {
5683 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
5684 ? mBuilder.mN.contentView
5685 : mBuilder.mN.headsUpContentView;
5686 if (mBuilder.mActions.size() == 0) {
5687 return makeStandardTemplateWithCustomContent(headsUpContentView);
5688 }
5689 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5690 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005691 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005692 return remoteViews;
5693 }
5694
Selim Cinek593610c2016-02-16 18:42:57 -08005695 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
5696 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
5697 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005698 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08005699 return remoteViews;
5700 }
5701
Selim Cinek593610c2016-02-16 18:42:57 -08005702 private RemoteViews makeDecoratedBigContentView() {
5703 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
5704 ? mBuilder.mN.contentView
5705 : mBuilder.mN.bigContentView;
5706 if (mBuilder.mActions.size() == 0) {
5707 return makeStandardTemplateWithCustomContent(bigContentView);
5708 }
5709 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5710 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005711 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005712 return remoteViews;
5713 }
Selim Cinek247fa012016-02-18 09:50:48 -08005714
5715 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
5716 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005717 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005718 // Need to clone customContent before adding, because otherwise it can no longer be
5719 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005720 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005721 remoteViews.removeAllViews(R.id.notification_main_column);
5722 remoteViews.addView(R.id.notification_main_column, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005723 }
Selim Cinek247fa012016-02-18 09:50:48 -08005724 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005725 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07005726 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005727 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08005728 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005729 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08005730 }
Selim Cinek593610c2016-02-16 18:42:57 -08005731 }
5732
Selim Cinek03eb3b72016-02-18 10:39:45 -08005733 /**
5734 * Notification style for media custom views that are decorated by the system
5735 *
5736 * <p>Instead of providing a media notification that is completely custom, a developer can set
5737 * this style and still obtain system decorations like the notification header with the expand
5738 * affordance and actions.
5739 *
5740 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5741 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5742 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5743 * corresponding custom views to display.
5744 *
5745 * To use this style with your Notification, feed it to
5746 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5747 * <pre class="prettyprint">
5748 * Notification noti = new Notification.Builder()
5749 * .setSmallIcon(R.drawable.ic_stat_player)
5750 * .setLargeIcon(albumArtBitmap))
5751 * .setCustomContentView(contentView);
5752 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
5753 * .setMediaSession(mySession))
5754 * .build();
5755 * </pre>
5756 *
5757 * @see android.app.Notification.DecoratedCustomViewStyle
5758 * @see android.app.Notification.MediaStyle
5759 */
5760 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
5761
5762 public DecoratedMediaCustomViewStyle() {
5763 }
5764
Selim Cinek03eb3b72016-02-18 10:39:45 -08005765 /**
5766 * @hide
5767 */
5768 public boolean displayCustomViewInline() {
5769 return true;
5770 }
5771
5772 /**
5773 * @hide
5774 */
5775 @Override
5776 public RemoteViews makeContentView() {
5777 RemoteViews remoteViews = super.makeContentView();
5778 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5779 mBuilder.mN.contentView);
5780 }
5781
5782 /**
5783 * @hide
5784 */
5785 @Override
5786 public RemoteViews makeBigContentView() {
5787 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
5788 ? mBuilder.mN.bigContentView
5789 : mBuilder.mN.contentView;
5790 return makeBigContentViewWithCustomContent(customRemoteView);
5791 }
5792
5793 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
5794 RemoteViews remoteViews = super.makeBigContentView();
5795 if (remoteViews != null) {
5796 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
5797 customRemoteView);
5798 } else if (customRemoteView != mBuilder.mN.contentView){
5799 remoteViews = super.makeContentView();
5800 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5801 customRemoteView);
5802 } else {
5803 return null;
5804 }
5805 }
5806
5807 /**
5808 * @hide
5809 */
5810 @Override
5811 public RemoteViews makeHeadsUpContentView() {
5812 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
5813 ? mBuilder.mN.headsUpContentView
5814 : mBuilder.mN.contentView;
5815 return makeBigContentViewWithCustomContent(customRemoteView);
5816 }
5817
5818 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
5819 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005820 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005821 // Need to clone customContent before adding, because otherwise it can no longer be
5822 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005823 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005824 remoteViews.removeAllViews(id);
5825 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005826 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08005827 return remoteViews;
5828 }
5829 }
5830
Christoph Studer4600f9b2014-07-22 22:44:43 +02005831 // When adding a new Style subclass here, don't forget to update
5832 // Builder.getNotificationStyleClass.
5833
Griff Hazen61a9e862014-05-22 16:05:19 -07005834 /**
5835 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
5836 * metadata or change options on a notification builder.
5837 */
5838 public interface Extender {
5839 /**
5840 * Apply this extender to a notification builder.
5841 * @param builder the builder to be modified.
5842 * @return the build object for chaining.
5843 */
5844 public Builder extend(Builder builder);
5845 }
5846
5847 /**
5848 * Helper class to add wearable extensions to notifications.
5849 * <p class="note"> See
5850 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
5851 * for Android Wear</a> for more information on how to use this class.
5852 * <p>
5853 * To create a notification with wearable extensions:
5854 * <ol>
5855 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
5856 * properties.
5857 * <li>Create a {@link android.app.Notification.WearableExtender}.
5858 * <li>Set wearable-specific properties using the
5859 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
5860 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
5861 * notification.
5862 * <li>Post the notification to the notification system with the
5863 * {@code NotificationManager.notify(...)} methods.
5864 * </ol>
5865 *
5866 * <pre class="prettyprint">
5867 * Notification notif = new Notification.Builder(mContext)
5868 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5869 * .setContentText(subject)
5870 * .setSmallIcon(R.drawable.new_mail)
5871 * .extend(new Notification.WearableExtender()
5872 * .setContentIcon(R.drawable.new_mail))
5873 * .build();
5874 * NotificationManager notificationManger =
5875 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
5876 * notificationManger.notify(0, notif);</pre>
5877 *
5878 * <p>Wearable extensions can be accessed on an existing notification by using the
5879 * {@code WearableExtender(Notification)} constructor,
5880 * and then using the {@code get} methods to access values.
5881 *
5882 * <pre class="prettyprint">
5883 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
5884 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07005885 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005886 */
5887 public static final class WearableExtender implements Extender {
5888 /**
5889 * Sentinel value for an action index that is unset.
5890 */
5891 public static final int UNSET_ACTION_INDEX = -1;
5892
5893 /**
5894 * Size value for use with {@link #setCustomSizePreset} to show this notification with
5895 * default sizing.
5896 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07005897 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07005898 * on their content.
5899 */
5900 public static final int SIZE_DEFAULT = 0;
5901
5902 /**
5903 * Size value for use with {@link #setCustomSizePreset} to show this notification
5904 * with an extra small size.
5905 * <p>This value is only applicable for custom display notifications created using
5906 * {@link #setDisplayIntent}.
5907 */
5908 public static final int SIZE_XSMALL = 1;
5909
5910 /**
5911 * Size value for use with {@link #setCustomSizePreset} to show this notification
5912 * with a small size.
5913 * <p>This value is only applicable for custom display notifications created using
5914 * {@link #setDisplayIntent}.
5915 */
5916 public static final int SIZE_SMALL = 2;
5917
5918 /**
5919 * Size value for use with {@link #setCustomSizePreset} to show this notification
5920 * with a medium size.
5921 * <p>This value is only applicable for custom display notifications created using
5922 * {@link #setDisplayIntent}.
5923 */
5924 public static final int SIZE_MEDIUM = 3;
5925
5926 /**
5927 * Size value for use with {@link #setCustomSizePreset} to show this notification
5928 * with a large size.
5929 * <p>This value is only applicable for custom display notifications created using
5930 * {@link #setDisplayIntent}.
5931 */
5932 public static final int SIZE_LARGE = 4;
5933
Griff Hazend5f11f92014-05-27 15:40:09 -07005934 /**
5935 * Size value for use with {@link #setCustomSizePreset} to show this notification
5936 * full screen.
5937 * <p>This value is only applicable for custom display notifications created using
5938 * {@link #setDisplayIntent}.
5939 */
5940 public static final int SIZE_FULL_SCREEN = 5;
5941
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005942 /**
5943 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
5944 * short amount of time when this notification is displayed on the screen. This
5945 * is the default value.
5946 */
5947 public static final int SCREEN_TIMEOUT_SHORT = 0;
5948
5949 /**
5950 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
5951 * for a longer amount of time when this notification is displayed on the screen.
5952 */
5953 public static final int SCREEN_TIMEOUT_LONG = -1;
5954
Griff Hazen61a9e862014-05-22 16:05:19 -07005955 /** Notification extra which contains wearable extensions */
5956 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
5957
Pete Gastaf6781d2014-10-07 15:17:05 -04005958 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07005959 private static final String KEY_ACTIONS = "actions";
5960 private static final String KEY_FLAGS = "flags";
5961 private static final String KEY_DISPLAY_INTENT = "displayIntent";
5962 private static final String KEY_PAGES = "pages";
5963 private static final String KEY_BACKGROUND = "background";
5964 private static final String KEY_CONTENT_ICON = "contentIcon";
5965 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
5966 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
5967 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
5968 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
5969 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005970 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04005971 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04005972 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07005973
5974 // Flags bitwise-ored to mFlags
5975 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
5976 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
5977 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
5978 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005979 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04005980 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04005981 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07005982
5983 // Default value for flags integer
5984 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
5985
5986 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
5987 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
5988
5989 private ArrayList<Action> mActions = new ArrayList<Action>();
5990 private int mFlags = DEFAULT_FLAGS;
5991 private PendingIntent mDisplayIntent;
5992 private ArrayList<Notification> mPages = new ArrayList<Notification>();
5993 private Bitmap mBackground;
5994 private int mContentIcon;
5995 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
5996 private int mContentActionIndex = UNSET_ACTION_INDEX;
5997 private int mCustomSizePreset = SIZE_DEFAULT;
5998 private int mCustomContentHeight;
5999 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006000 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04006001 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006002 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07006003
6004 /**
6005 * Create a {@link android.app.Notification.WearableExtender} with default
6006 * options.
6007 */
6008 public WearableExtender() {
6009 }
6010
6011 public WearableExtender(Notification notif) {
6012 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
6013 if (wearableBundle != null) {
6014 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
6015 if (actions != null) {
6016 mActions.addAll(actions);
6017 }
6018
6019 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
6020 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
6021
6022 Notification[] pages = getNotificationArrayFromBundle(
6023 wearableBundle, KEY_PAGES);
6024 if (pages != null) {
6025 Collections.addAll(mPages, pages);
6026 }
6027
6028 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
6029 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
6030 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
6031 DEFAULT_CONTENT_ICON_GRAVITY);
6032 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
6033 UNSET_ACTION_INDEX);
6034 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
6035 SIZE_DEFAULT);
6036 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
6037 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006038 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04006039 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006040 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07006041 }
6042 }
6043
6044 /**
6045 * Apply wearable extensions to a notification that is being built. This is typically
6046 * called by the {@link android.app.Notification.Builder#extend} method of
6047 * {@link android.app.Notification.Builder}.
6048 */
6049 @Override
6050 public Notification.Builder extend(Notification.Builder builder) {
6051 Bundle wearableBundle = new Bundle();
6052
6053 if (!mActions.isEmpty()) {
6054 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
6055 }
6056 if (mFlags != DEFAULT_FLAGS) {
6057 wearableBundle.putInt(KEY_FLAGS, mFlags);
6058 }
6059 if (mDisplayIntent != null) {
6060 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
6061 }
6062 if (!mPages.isEmpty()) {
6063 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
6064 new Notification[mPages.size()]));
6065 }
6066 if (mBackground != null) {
6067 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
6068 }
6069 if (mContentIcon != 0) {
6070 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
6071 }
6072 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
6073 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
6074 }
6075 if (mContentActionIndex != UNSET_ACTION_INDEX) {
6076 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
6077 mContentActionIndex);
6078 }
6079 if (mCustomSizePreset != SIZE_DEFAULT) {
6080 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
6081 }
6082 if (mCustomContentHeight != 0) {
6083 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
6084 }
6085 if (mGravity != DEFAULT_GRAVITY) {
6086 wearableBundle.putInt(KEY_GRAVITY, mGravity);
6087 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006088 if (mHintScreenTimeout != 0) {
6089 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
6090 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04006091 if (mDismissalId != null) {
6092 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
6093 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006094 if (mBridgeTag != null) {
6095 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
6096 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006097
6098 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
6099 return builder;
6100 }
6101
6102 @Override
6103 public WearableExtender clone() {
6104 WearableExtender that = new WearableExtender();
6105 that.mActions = new ArrayList<Action>(this.mActions);
6106 that.mFlags = this.mFlags;
6107 that.mDisplayIntent = this.mDisplayIntent;
6108 that.mPages = new ArrayList<Notification>(this.mPages);
6109 that.mBackground = this.mBackground;
6110 that.mContentIcon = this.mContentIcon;
6111 that.mContentIconGravity = this.mContentIconGravity;
6112 that.mContentActionIndex = this.mContentActionIndex;
6113 that.mCustomSizePreset = this.mCustomSizePreset;
6114 that.mCustomContentHeight = this.mCustomContentHeight;
6115 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006116 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04006117 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006118 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07006119 return that;
6120 }
6121
6122 /**
6123 * Add a wearable action to this notification.
6124 *
6125 * <p>When wearable actions are added using this method, the set of actions that
6126 * show on a wearable device splits from devices that only show actions added
6127 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
6128 * of which actions display on different devices.
6129 *
6130 * @param action the action to add to this notification
6131 * @return this object for method chaining
6132 * @see android.app.Notification.Action
6133 */
6134 public WearableExtender addAction(Action action) {
6135 mActions.add(action);
6136 return this;
6137 }
6138
6139 /**
6140 * Adds wearable actions to this notification.
6141 *
6142 * <p>When wearable actions are added using this method, the set of actions that
6143 * show on a wearable device splits from devices that only show actions added
6144 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
6145 * of which actions display on different devices.
6146 *
6147 * @param actions the actions to add to this notification
6148 * @return this object for method chaining
6149 * @see android.app.Notification.Action
6150 */
6151 public WearableExtender addActions(List<Action> actions) {
6152 mActions.addAll(actions);
6153 return this;
6154 }
6155
6156 /**
6157 * Clear all wearable actions present on this builder.
6158 * @return this object for method chaining.
6159 * @see #addAction
6160 */
6161 public WearableExtender clearActions() {
6162 mActions.clear();
6163 return this;
6164 }
6165
6166 /**
6167 * Get the wearable actions present on this notification.
6168 */
6169 public List<Action> getActions() {
6170 return mActions;
6171 }
6172
6173 /**
6174 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07006175 * this notification. The {@link PendingIntent} provided should be for an activity.
6176 *
6177 * <pre class="prettyprint">
6178 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
6179 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
6180 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
6181 * Notification notif = new Notification.Builder(context)
6182 * .extend(new Notification.WearableExtender()
6183 * .setDisplayIntent(displayPendingIntent)
6184 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
6185 * .build();</pre>
6186 *
6187 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07006188 * should have an empty task affinity. It is also recommended to use the device
6189 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07006190 *
6191 * <p>Example AndroidManifest.xml entry:
6192 * <pre class="prettyprint">
6193 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
6194 * android:exported=&quot;true&quot;
6195 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07006196 * android:taskAffinity=&quot;&quot;
6197 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07006198 *
6199 * @param intent the {@link PendingIntent} for an activity
6200 * @return this object for method chaining
6201 * @see android.app.Notification.WearableExtender#getDisplayIntent
6202 */
6203 public WearableExtender setDisplayIntent(PendingIntent intent) {
6204 mDisplayIntent = intent;
6205 return this;
6206 }
6207
6208 /**
6209 * Get the intent to launch inside of an activity view when displaying this
6210 * notification. This {@code PendingIntent} should be for an activity.
6211 */
6212 public PendingIntent getDisplayIntent() {
6213 return mDisplayIntent;
6214 }
6215
6216 /**
6217 * Add an additional page of content to display with this notification. The current
6218 * notification forms the first page, and pages added using this function form
6219 * subsequent pages. This field can be used to separate a notification into multiple
6220 * sections.
6221 *
6222 * @param page the notification to add as another page
6223 * @return this object for method chaining
6224 * @see android.app.Notification.WearableExtender#getPages
6225 */
6226 public WearableExtender addPage(Notification page) {
6227 mPages.add(page);
6228 return this;
6229 }
6230
6231 /**
6232 * Add additional pages of content to display with this notification. The current
6233 * notification forms the first page, and pages added using this function form
6234 * subsequent pages. This field can be used to separate a notification into multiple
6235 * sections.
6236 *
6237 * @param pages a list of notifications
6238 * @return this object for method chaining
6239 * @see android.app.Notification.WearableExtender#getPages
6240 */
6241 public WearableExtender addPages(List<Notification> pages) {
6242 mPages.addAll(pages);
6243 return this;
6244 }
6245
6246 /**
6247 * Clear all additional pages present on this builder.
6248 * @return this object for method chaining.
6249 * @see #addPage
6250 */
6251 public WearableExtender clearPages() {
6252 mPages.clear();
6253 return this;
6254 }
6255
6256 /**
6257 * Get the array of additional pages of content for displaying this notification. The
6258 * current notification forms the first page, and elements within this array form
6259 * subsequent pages. This field can be used to separate a notification into multiple
6260 * sections.
6261 * @return the pages for this notification
6262 */
6263 public List<Notification> getPages() {
6264 return mPages;
6265 }
6266
6267 /**
6268 * Set a background image to be displayed behind the notification content.
6269 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
6270 * will work with any notification style.
6271 *
6272 * @param background the background bitmap
6273 * @return this object for method chaining
6274 * @see android.app.Notification.WearableExtender#getBackground
6275 */
6276 public WearableExtender setBackground(Bitmap background) {
6277 mBackground = background;
6278 return this;
6279 }
6280
6281 /**
6282 * Get a background image to be displayed behind the notification content.
6283 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
6284 * will work with any notification style.
6285 *
6286 * @return the background image
6287 * @see android.app.Notification.WearableExtender#setBackground
6288 */
6289 public Bitmap getBackground() {
6290 return mBackground;
6291 }
6292
6293 /**
6294 * Set an icon that goes with the content of this notification.
6295 */
6296 public WearableExtender setContentIcon(int icon) {
6297 mContentIcon = icon;
6298 return this;
6299 }
6300
6301 /**
6302 * Get an icon that goes with the content of this notification.
6303 */
6304 public int getContentIcon() {
6305 return mContentIcon;
6306 }
6307
6308 /**
6309 * Set the gravity that the content icon should have within the notification display.
6310 * Supported values include {@link android.view.Gravity#START} and
6311 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
6312 * @see #setContentIcon
6313 */
6314 public WearableExtender setContentIconGravity(int contentIconGravity) {
6315 mContentIconGravity = contentIconGravity;
6316 return this;
6317 }
6318
6319 /**
6320 * Get the gravity that the content icon should have within the notification display.
6321 * Supported values include {@link android.view.Gravity#START} and
6322 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
6323 * @see #getContentIcon
6324 */
6325 public int getContentIconGravity() {
6326 return mContentIconGravity;
6327 }
6328
6329 /**
6330 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07006331 * this notification. This action will no longer display separately from the
6332 * notification's content.
6333 *
Griff Hazenca48d352014-05-28 22:37:13 -07006334 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07006335 * set, although the list of available actions comes from the main notification and not
6336 * from the child page's notification.
6337 *
6338 * @param actionIndex The index of the action to hoist onto the current notification page.
6339 * If wearable actions were added to the main notification, this index
6340 * will apply to that list, otherwise it will apply to the regular
6341 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07006342 */
6343 public WearableExtender setContentAction(int actionIndex) {
6344 mContentActionIndex = actionIndex;
6345 return this;
6346 }
6347
6348 /**
Griff Hazenca48d352014-05-28 22:37:13 -07006349 * Get the index of the notification action, if any, that was specified as being clickable
6350 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07006351 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07006352 *
Griff Hazenca48d352014-05-28 22:37:13 -07006353 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07006354 * set, although the list of available actions comes from the main notification and not
6355 * from the child page's notification.
6356 *
6357 * <p>If wearable specific actions were added to the main notification, this index will
6358 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07006359 *
6360 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07006361 */
6362 public int getContentAction() {
6363 return mContentActionIndex;
6364 }
6365
6366 /**
6367 * Set the gravity that this notification should have within the available viewport space.
6368 * Supported values include {@link android.view.Gravity#TOP},
6369 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
6370 * The default value is {@link android.view.Gravity#BOTTOM}.
6371 */
6372 public WearableExtender setGravity(int gravity) {
6373 mGravity = gravity;
6374 return this;
6375 }
6376
6377 /**
6378 * Get the gravity that this notification should have within the available viewport space.
6379 * Supported values include {@link android.view.Gravity#TOP},
6380 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
6381 * The default value is {@link android.view.Gravity#BOTTOM}.
6382 */
6383 public int getGravity() {
6384 return mGravity;
6385 }
6386
6387 /**
6388 * Set the custom size preset for the display of this notification out of the available
6389 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
6390 * {@link #SIZE_LARGE}.
6391 * <p>Some custom size presets are only applicable for custom display notifications created
6392 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
6393 * documentation for the preset in question. See also
6394 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
6395 */
6396 public WearableExtender setCustomSizePreset(int sizePreset) {
6397 mCustomSizePreset = sizePreset;
6398 return this;
6399 }
6400
6401 /**
6402 * Get the custom size preset for the display of this notification out of the available
6403 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
6404 * {@link #SIZE_LARGE}.
6405 * <p>Some custom size presets are only applicable for custom display notifications created
6406 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
6407 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
6408 */
6409 public int getCustomSizePreset() {
6410 return mCustomSizePreset;
6411 }
6412
6413 /**
6414 * Set the custom height in pixels for the display of this notification's content.
6415 * <p>This option is only available for custom display notifications created
6416 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
6417 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
6418 * {@link #getCustomContentHeight}.
6419 */
6420 public WearableExtender setCustomContentHeight(int height) {
6421 mCustomContentHeight = height;
6422 return this;
6423 }
6424
6425 /**
6426 * Get the custom height in pixels for the display of this notification's content.
6427 * <p>This option is only available for custom display notifications created
6428 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
6429 * {@link #setCustomContentHeight}.
6430 */
6431 public int getCustomContentHeight() {
6432 return mCustomContentHeight;
6433 }
6434
6435 /**
6436 * Set whether the scrolling position for the contents of this notification should start
6437 * at the bottom of the contents instead of the top when the contents are too long to
6438 * display within the screen. Default is false (start scroll at the top).
6439 */
6440 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
6441 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
6442 return this;
6443 }
6444
6445 /**
6446 * Get whether the scrolling position for the contents of this notification should start
6447 * at the bottom of the contents instead of the top when the contents are too long to
6448 * display within the screen. Default is false (start scroll at the top).
6449 */
6450 public boolean getStartScrollBottom() {
6451 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
6452 }
6453
6454 /**
6455 * Set whether the content intent is available when the wearable device is not connected
6456 * to a companion device. The user can still trigger this intent when the wearable device
6457 * is offline, but a visual hint will indicate that the content intent may not be available.
6458 * Defaults to true.
6459 */
6460 public WearableExtender setContentIntentAvailableOffline(
6461 boolean contentIntentAvailableOffline) {
6462 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
6463 return this;
6464 }
6465
6466 /**
6467 * Get whether the content intent is available when the wearable device is not connected
6468 * to a companion device. The user can still trigger this intent when the wearable device
6469 * is offline, but a visual hint will indicate that the content intent may not be available.
6470 * Defaults to true.
6471 */
6472 public boolean getContentIntentAvailableOffline() {
6473 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
6474 }
6475
6476 /**
6477 * Set a hint that this notification's icon should not be displayed.
6478 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
6479 * @return this object for method chaining
6480 */
6481 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
6482 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
6483 return this;
6484 }
6485
6486 /**
6487 * Get a hint that this notification's icon should not be displayed.
6488 * @return {@code true} if this icon should not be displayed, false otherwise.
6489 * The default value is {@code false} if this was never set.
6490 */
6491 public boolean getHintHideIcon() {
6492 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
6493 }
6494
6495 /**
6496 * Set a visual hint that only the background image of this notification should be
6497 * displayed, and other semantic content should be hidden. This hint is only applicable
6498 * to sub-pages added using {@link #addPage}.
6499 */
6500 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
6501 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
6502 return this;
6503 }
6504
6505 /**
6506 * Get a visual hint that only the background image of this notification should be
6507 * displayed, and other semantic content should be hidden. This hint is only applicable
6508 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
6509 */
6510 public boolean getHintShowBackgroundOnly() {
6511 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
6512 }
6513
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006514 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08006515 * Set a hint that this notification's background should not be clipped if possible,
6516 * and should instead be resized to fully display on the screen, retaining the aspect
6517 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006518 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
6519 * @return this object for method chaining
6520 */
6521 public WearableExtender setHintAvoidBackgroundClipping(
6522 boolean hintAvoidBackgroundClipping) {
6523 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
6524 return this;
6525 }
6526
6527 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08006528 * Get a hint that this notification's background should not be clipped if possible,
6529 * and should instead be resized to fully display on the screen, retaining the aspect
6530 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006531 * @return {@code true} if it's ok if the background is clipped on the screen, false
6532 * otherwise. The default value is {@code false} if this was never set.
6533 */
6534 public boolean getHintAvoidBackgroundClipping() {
6535 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
6536 }
6537
6538 /**
6539 * Set a hint that the screen should remain on for at least this duration when
6540 * this notification is displayed on the screen.
6541 * @param timeout The requested screen timeout in milliseconds. Can also be either
6542 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
6543 * @return this object for method chaining
6544 */
6545 public WearableExtender setHintScreenTimeout(int timeout) {
6546 mHintScreenTimeout = timeout;
6547 return this;
6548 }
6549
6550 /**
6551 * Get the duration, in milliseconds, that the screen should remain on for
6552 * when this notification is displayed.
6553 * @return the duration in milliseconds if > 0, or either one of the sentinel values
6554 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
6555 */
6556 public int getHintScreenTimeout() {
6557 return mHintScreenTimeout;
6558 }
6559
Alex Hills9ab3a232016-04-05 14:54:56 -04006560 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04006561 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
6562 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
6563 * qr codes, as well as other simple black-and-white tickets.
6564 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
6565 * @return this object for method chaining
6566 */
6567 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
6568 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
6569 return this;
6570 }
6571
6572 /**
6573 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
6574 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
6575 * qr codes, as well as other simple black-and-white tickets.
6576 * @return {@code true} if it should be displayed in ambient, false otherwise
6577 * otherwise. The default value is {@code false} if this was never set.
6578 */
6579 public boolean getHintAmbientBigPicture() {
6580 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
6581 }
6582
6583 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04006584 * Set a hint that this notification's content intent will launch an {@link Activity}
6585 * directly, telling the platform that it can generate the appropriate transitions.
6586 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
6587 * an activity and transitions should be generated, false otherwise.
6588 * @return this object for method chaining
6589 */
6590 public WearableExtender setHintContentIntentLaunchesActivity(
6591 boolean hintContentIntentLaunchesActivity) {
6592 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
6593 return this;
6594 }
6595
6596 /**
6597 * Get a hint that this notification's content intent will launch an {@link Activity}
6598 * directly, telling the platform that it can generate the appropriate transitions
6599 * @return {@code true} if the content intent will launch an activity and transitions should
6600 * be generated, false otherwise. The default value is {@code false} if this was never set.
6601 */
6602 public boolean getHintContentIntentLaunchesActivity() {
6603 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
6604 }
6605
Nadia Benbernou948627e2016-04-14 14:41:08 -04006606 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006607 * Sets the dismissal id for this notification. If a notification is posted with a
6608 * dismissal id, then when that notification is canceled, notifications on other wearables
6609 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04006610 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006611 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04006612 * @param dismissalId the dismissal id of the notification.
6613 * @return this object for method chaining
6614 */
6615 public WearableExtender setDismissalId(String dismissalId) {
6616 mDismissalId = dismissalId;
6617 return this;
6618 }
6619
6620 /**
6621 * Returns the dismissal id of the notification.
6622 * @return the dismissal id of the notification or null if it has not been set.
6623 */
6624 public String getDismissalId() {
6625 return mDismissalId;
6626 }
6627
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006628 /**
6629 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
6630 * posted from a phone to provide finer-grained control on what notifications are bridged
6631 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
6632 * Features to Notifications</a> for more information.
6633 * @param bridgeTag the bridge tag of the notification.
6634 * @return this object for method chaining
6635 */
6636 public WearableExtender setBridgeTag(String bridgeTag) {
6637 mBridgeTag = bridgeTag;
6638 return this;
6639 }
6640
6641 /**
6642 * Returns the bridge tag of the notification.
6643 * @return the bridge tag or null if not present.
6644 */
6645 public String getBridgeTag() {
6646 return mBridgeTag;
6647 }
6648
Griff Hazen61a9e862014-05-22 16:05:19 -07006649 private void setFlag(int mask, boolean value) {
6650 if (value) {
6651 mFlags |= mask;
6652 } else {
6653 mFlags &= ~mask;
6654 }
6655 }
6656 }
6657
6658 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006659 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
6660 * with car extensions:
6661 *
6662 * <ol>
6663 * <li>Create an {@link Notification.Builder}, setting any desired
6664 * properties.
6665 * <li>Create a {@link CarExtender}.
6666 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
6667 * {@link CarExtender}.
6668 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
6669 * to apply the extensions to a notification.
6670 * </ol>
6671 *
6672 * <pre class="prettyprint">
6673 * Notification notification = new Notification.Builder(context)
6674 * ...
6675 * .extend(new CarExtender()
6676 * .set*(...))
6677 * .build();
6678 * </pre>
6679 *
6680 * <p>Car extensions can be accessed on an existing notification by using the
6681 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
6682 * to access values.
6683 */
6684 public static final class CarExtender implements Extender {
6685 private static final String TAG = "CarExtender";
6686
6687 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
6688 private static final String EXTRA_LARGE_ICON = "large_icon";
6689 private static final String EXTRA_CONVERSATION = "car_conversation";
6690 private static final String EXTRA_COLOR = "app_color";
6691
6692 private Bitmap mLargeIcon;
6693 private UnreadConversation mUnreadConversation;
6694 private int mColor = Notification.COLOR_DEFAULT;
6695
6696 /**
6697 * Create a {@link CarExtender} with default options.
6698 */
6699 public CarExtender() {
6700 }
6701
6702 /**
6703 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
6704 *
6705 * @param notif The notification from which to copy options.
6706 */
6707 public CarExtender(Notification notif) {
6708 Bundle carBundle = notif.extras == null ?
6709 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
6710 if (carBundle != null) {
6711 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
6712 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
6713
6714 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
6715 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
6716 }
6717 }
6718
6719 /**
6720 * Apply car extensions to a notification that is being built. This is typically called by
6721 * the {@link Notification.Builder#extend(Notification.Extender)}
6722 * method of {@link Notification.Builder}.
6723 */
6724 @Override
6725 public Notification.Builder extend(Notification.Builder builder) {
6726 Bundle carExtensions = new Bundle();
6727
6728 if (mLargeIcon != null) {
6729 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
6730 }
6731 if (mColor != Notification.COLOR_DEFAULT) {
6732 carExtensions.putInt(EXTRA_COLOR, mColor);
6733 }
6734
6735 if (mUnreadConversation != null) {
6736 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
6737 carExtensions.putBundle(EXTRA_CONVERSATION, b);
6738 }
6739
6740 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
6741 return builder;
6742 }
6743
6744 /**
6745 * Sets the accent color to use when Android Auto presents the notification.
6746 *
6747 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
6748 * to accent the displayed notification. However, not all colors are acceptable in an
6749 * automotive setting. This method can be used to override the color provided in the
6750 * notification in such a situation.
6751 */
Tor Norbye80756e32015-03-02 09:39:27 -08006752 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006753 mColor = color;
6754 return this;
6755 }
6756
6757 /**
6758 * Gets the accent color.
6759 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006760 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006761 */
Tor Norbye80756e32015-03-02 09:39:27 -08006762 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006763 public int getColor() {
6764 return mColor;
6765 }
6766
6767 /**
6768 * Sets the large icon of the car notification.
6769 *
6770 * If no large icon is set in the extender, Android Auto will display the icon
6771 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
6772 *
6773 * @param largeIcon The large icon to use in the car notification.
6774 * @return This object for method chaining.
6775 */
6776 public CarExtender setLargeIcon(Bitmap largeIcon) {
6777 mLargeIcon = largeIcon;
6778 return this;
6779 }
6780
6781 /**
6782 * Gets the large icon used in this car notification, or null if no icon has been set.
6783 *
6784 * @return The large icon for the car notification.
6785 * @see CarExtender#setLargeIcon
6786 */
6787 public Bitmap getLargeIcon() {
6788 return mLargeIcon;
6789 }
6790
6791 /**
6792 * Sets the unread conversation in a message notification.
6793 *
6794 * @param unreadConversation The unread part of the conversation this notification conveys.
6795 * @return This object for method chaining.
6796 */
6797 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
6798 mUnreadConversation = unreadConversation;
6799 return this;
6800 }
6801
6802 /**
6803 * Returns the unread conversation conveyed by this notification.
6804 * @see #setUnreadConversation(UnreadConversation)
6805 */
6806 public UnreadConversation getUnreadConversation() {
6807 return mUnreadConversation;
6808 }
6809
6810 /**
6811 * A class which holds the unread messages from a conversation.
6812 */
6813 public static class UnreadConversation {
6814 private static final String KEY_AUTHOR = "author";
6815 private static final String KEY_TEXT = "text";
6816 private static final String KEY_MESSAGES = "messages";
6817 private static final String KEY_REMOTE_INPUT = "remote_input";
6818 private static final String KEY_ON_REPLY = "on_reply";
6819 private static final String KEY_ON_READ = "on_read";
6820 private static final String KEY_PARTICIPANTS = "participants";
6821 private static final String KEY_TIMESTAMP = "timestamp";
6822
6823 private final String[] mMessages;
6824 private final RemoteInput mRemoteInput;
6825 private final PendingIntent mReplyPendingIntent;
6826 private final PendingIntent mReadPendingIntent;
6827 private final String[] mParticipants;
6828 private final long mLatestTimestamp;
6829
6830 UnreadConversation(String[] messages, RemoteInput remoteInput,
6831 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
6832 String[] participants, long latestTimestamp) {
6833 mMessages = messages;
6834 mRemoteInput = remoteInput;
6835 mReadPendingIntent = readPendingIntent;
6836 mReplyPendingIntent = replyPendingIntent;
6837 mParticipants = participants;
6838 mLatestTimestamp = latestTimestamp;
6839 }
6840
6841 /**
6842 * Gets the list of messages conveyed by this notification.
6843 */
6844 public String[] getMessages() {
6845 return mMessages;
6846 }
6847
6848 /**
6849 * Gets the remote input that will be used to convey the response to a message list, or
6850 * null if no such remote input exists.
6851 */
6852 public RemoteInput getRemoteInput() {
6853 return mRemoteInput;
6854 }
6855
6856 /**
6857 * Gets the pending intent that will be triggered when the user replies to this
6858 * notification.
6859 */
6860 public PendingIntent getReplyPendingIntent() {
6861 return mReplyPendingIntent;
6862 }
6863
6864 /**
6865 * Gets the pending intent that Android Auto will send after it reads aloud all messages
6866 * in this object's message list.
6867 */
6868 public PendingIntent getReadPendingIntent() {
6869 return mReadPendingIntent;
6870 }
6871
6872 /**
6873 * Gets the participants in the conversation.
6874 */
6875 public String[] getParticipants() {
6876 return mParticipants;
6877 }
6878
6879 /**
6880 * Gets the firs participant in the conversation.
6881 */
6882 public String getParticipant() {
6883 return mParticipants.length > 0 ? mParticipants[0] : null;
6884 }
6885
6886 /**
6887 * Gets the timestamp of the conversation.
6888 */
6889 public long getLatestTimestamp() {
6890 return mLatestTimestamp;
6891 }
6892
6893 Bundle getBundleForUnreadConversation() {
6894 Bundle b = new Bundle();
6895 String author = null;
6896 if (mParticipants != null && mParticipants.length > 1) {
6897 author = mParticipants[0];
6898 }
6899 Parcelable[] messages = new Parcelable[mMessages.length];
6900 for (int i = 0; i < messages.length; i++) {
6901 Bundle m = new Bundle();
6902 m.putString(KEY_TEXT, mMessages[i]);
6903 m.putString(KEY_AUTHOR, author);
6904 messages[i] = m;
6905 }
6906 b.putParcelableArray(KEY_MESSAGES, messages);
6907 if (mRemoteInput != null) {
6908 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
6909 }
6910 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
6911 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
6912 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
6913 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
6914 return b;
6915 }
6916
6917 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
6918 if (b == null) {
6919 return null;
6920 }
6921 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
6922 String[] messages = null;
6923 if (parcelableMessages != null) {
6924 String[] tmp = new String[parcelableMessages.length];
6925 boolean success = true;
6926 for (int i = 0; i < tmp.length; i++) {
6927 if (!(parcelableMessages[i] instanceof Bundle)) {
6928 success = false;
6929 break;
6930 }
6931 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
6932 if (tmp[i] == null) {
6933 success = false;
6934 break;
6935 }
6936 }
6937 if (success) {
6938 messages = tmp;
6939 } else {
6940 return null;
6941 }
6942 }
6943
6944 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
6945 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
6946
6947 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
6948
6949 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
6950 if (participants == null || participants.length != 1) {
6951 return null;
6952 }
6953
6954 return new UnreadConversation(messages,
6955 remoteInput,
6956 onReply,
6957 onRead,
6958 participants, b.getLong(KEY_TIMESTAMP));
6959 }
6960 };
6961
6962 /**
6963 * Builder class for {@link CarExtender.UnreadConversation} objects.
6964 */
6965 public static class Builder {
6966 private final List<String> mMessages = new ArrayList<String>();
6967 private final String mParticipant;
6968 private RemoteInput mRemoteInput;
6969 private PendingIntent mReadPendingIntent;
6970 private PendingIntent mReplyPendingIntent;
6971 private long mLatestTimestamp;
6972
6973 /**
6974 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
6975 *
6976 * @param name The name of the other participant in the conversation.
6977 */
6978 public Builder(String name) {
6979 mParticipant = name;
6980 }
6981
6982 /**
6983 * Appends a new unread message to the list of messages for this conversation.
6984 *
6985 * The messages should be added from oldest to newest.
6986 *
6987 * @param message The text of the new unread message.
6988 * @return This object for method chaining.
6989 */
6990 public Builder addMessage(String message) {
6991 mMessages.add(message);
6992 return this;
6993 }
6994
6995 /**
6996 * Sets the pending intent and remote input which will convey the reply to this
6997 * notification.
6998 *
6999 * @param pendingIntent The pending intent which will be triggered on a reply.
7000 * @param remoteInput The remote input parcelable which will carry the reply.
7001 * @return This object for method chaining.
7002 *
7003 * @see CarExtender.UnreadConversation#getRemoteInput
7004 * @see CarExtender.UnreadConversation#getReplyPendingIntent
7005 */
7006 public Builder setReplyAction(
7007 PendingIntent pendingIntent, RemoteInput remoteInput) {
7008 mRemoteInput = remoteInput;
7009 mReplyPendingIntent = pendingIntent;
7010
7011 return this;
7012 }
7013
7014 /**
7015 * Sets the pending intent that will be sent once the messages in this notification
7016 * are read.
7017 *
7018 * @param pendingIntent The pending intent to use.
7019 * @return This object for method chaining.
7020 */
7021 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
7022 mReadPendingIntent = pendingIntent;
7023 return this;
7024 }
7025
7026 /**
7027 * Sets the timestamp of the most recent message in an unread conversation.
7028 *
7029 * If a messaging notification has been posted by your application and has not
7030 * yet been cancelled, posting a later notification with the same id and tag
7031 * but without a newer timestamp may result in Android Auto not displaying a
7032 * heads up notification for the later notification.
7033 *
7034 * @param timestamp The timestamp of the most recent message in the conversation.
7035 * @return This object for method chaining.
7036 */
7037 public Builder setLatestTimestamp(long timestamp) {
7038 mLatestTimestamp = timestamp;
7039 return this;
7040 }
7041
7042 /**
7043 * Builds a new unread conversation object.
7044 *
7045 * @return The new unread conversation object.
7046 */
7047 public UnreadConversation build() {
7048 String[] messages = mMessages.toArray(new String[mMessages.size()]);
7049 String[] participants = { mParticipant };
7050 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
7051 mReadPendingIntent, participants, mLatestTimestamp);
7052 }
7053 }
7054 }
7055
7056 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007057 * <p>Helper class to add Android TV extensions to notifications. To create a notification
7058 * with a TV extension:
7059 *
7060 * <ol>
7061 * <li>Create an {@link Notification.Builder}, setting any desired properties.
7062 * <li>Create a {@link TvExtender}.
7063 * <li>Set TV-specific properties using the {@code set} methods of
7064 * {@link TvExtender}.
7065 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
7066 * to apply the extension to a notification.
7067 * </ol>
7068 *
7069 * <pre class="prettyprint">
7070 * Notification notification = new Notification.Builder(context)
7071 * ...
7072 * .extend(new TvExtender()
7073 * .set*(...))
7074 * .build();
7075 * </pre>
7076 *
7077 * <p>TV extensions can be accessed on an existing notification by using the
7078 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
7079 * to access values.
7080 *
7081 * @hide
7082 */
7083 @SystemApi
7084 public static final class TvExtender implements Extender {
7085 private static final String TAG = "TvExtender";
7086
7087 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
7088 private static final String EXTRA_FLAGS = "flags";
7089 private static final String EXTRA_CONTENT_INTENT = "content_intent";
7090 private static final String EXTRA_DELETE_INTENT = "delete_intent";
7091
7092 // Flags bitwise-ored to mFlags
7093 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
7094
7095 private int mFlags;
7096 private PendingIntent mContentIntent;
7097 private PendingIntent mDeleteIntent;
7098
7099 /**
7100 * Create a {@link TvExtender} with default options.
7101 */
7102 public TvExtender() {
7103 mFlags = FLAG_AVAILABLE_ON_TV;
7104 }
7105
7106 /**
7107 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
7108 *
7109 * @param notif The notification from which to copy options.
7110 */
7111 public TvExtender(Notification notif) {
7112 Bundle bundle = notif.extras == null ?
7113 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
7114 if (bundle != null) {
7115 mFlags = bundle.getInt(EXTRA_FLAGS);
7116 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
7117 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
7118 }
7119 }
7120
7121 /**
7122 * Apply a TV extension to a notification that is being built. This is typically called by
7123 * the {@link Notification.Builder#extend(Notification.Extender)}
7124 * method of {@link Notification.Builder}.
7125 */
7126 @Override
7127 public Notification.Builder extend(Notification.Builder builder) {
7128 Bundle bundle = new Bundle();
7129
7130 bundle.putInt(EXTRA_FLAGS, mFlags);
7131 if (mContentIntent != null) {
7132 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
7133 }
7134
7135 if (mDeleteIntent != null) {
7136 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
7137 }
7138
7139 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
7140 return builder;
7141 }
7142
7143 /**
7144 * Returns true if this notification should be shown on TV. This method return true
7145 * if the notification was extended with a TvExtender.
7146 */
7147 public boolean isAvailableOnTv() {
7148 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
7149 }
7150
7151 /**
7152 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
7153 * If provided, it is used instead of the content intent specified
7154 * at the level of Notification.
7155 */
7156 public TvExtender setContentIntent(PendingIntent intent) {
7157 mContentIntent = intent;
7158 return this;
7159 }
7160
7161 /**
7162 * Returns the TV-specific content intent. If this method returns null, the
7163 * main content intent on the notification should be used.
7164 *
7165 * @see {@link Notification#contentIntent}
7166 */
7167 public PendingIntent getContentIntent() {
7168 return mContentIntent;
7169 }
7170
7171 /**
7172 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
7173 * by the user on TV. If provided, it is used instead of the delete intent specified
7174 * at the level of Notification.
7175 */
7176 public TvExtender setDeleteIntent(PendingIntent intent) {
7177 mDeleteIntent = intent;
7178 return this;
7179 }
7180
7181 /**
7182 * Returns the TV-specific delete intent. If this method returns null, the
7183 * main delete intent on the notification should be used.
7184 *
7185 * @see {@link Notification#deleteIntent}
7186 */
7187 public PendingIntent getDeleteIntent() {
7188 return mDeleteIntent;
7189 }
7190 }
7191
7192 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07007193 * Get an array of Notification objects from a parcelable array bundle field.
7194 * Update the bundle to have a typed array so fetches in the future don't need
7195 * to do an array copy.
7196 */
7197 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
7198 Parcelable[] array = bundle.getParcelableArray(key);
7199 if (array instanceof Notification[] || array == null) {
7200 return (Notification[]) array;
7201 }
7202 Notification[] typedArray = Arrays.copyOf(array, array.length,
7203 Notification[].class);
7204 bundle.putParcelableArray(key, typedArray);
7205 return typedArray;
7206 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02007207
7208 private static class BuilderRemoteViews extends RemoteViews {
7209 public BuilderRemoteViews(Parcel parcel) {
7210 super(parcel);
7211 }
7212
Kenny Guy77320062014-08-27 21:37:15 +01007213 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
7214 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007215 }
7216
7217 @Override
7218 public BuilderRemoteViews clone() {
7219 Parcel p = Parcel.obtain();
7220 writeToParcel(p, 0);
7221 p.setDataPosition(0);
7222 BuilderRemoteViews brv = new BuilderRemoteViews(p);
7223 p.recycle();
7224 return brv;
7225 }
7226 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08007227
7228 private static class StandardTemplateParams {
7229 boolean hasProgress = true;
7230 boolean ambient = false;
7231 CharSequence title;
7232 CharSequence text;
7233
7234 final StandardTemplateParams reset() {
7235 hasProgress = true;
7236 ambient = false;
7237 title = null;
7238 text = null;
7239 return this;
7240 }
7241
7242 final StandardTemplateParams hasProgress(boolean hasProgress) {
7243 this.hasProgress = hasProgress;
7244 return this;
7245 }
7246
7247 final StandardTemplateParams title(CharSequence title) {
7248 this.title = title;
7249 return this;
7250 }
7251
7252 final StandardTemplateParams text(CharSequence text) {
7253 this.text = text;
7254 return this;
7255 }
7256
7257 final StandardTemplateParams ambient(boolean ambient) {
7258 this.ambient = ambient;
7259 return this;
7260 }
7261
7262 final StandardTemplateParams fillTextsFrom(Builder b) {
7263 Bundle extras = b.mN.extras;
7264 title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE));
7265 text = b.processLegacyText(extras.getCharSequence(EXTRA_TEXT));
7266 return this;
7267 }
7268 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007269}