blob: da57873bf68c12f138dff23997f2940ee9f98a70 [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
Dan Sandler80eaa592016-04-14 23:34:54 -0400991 /** @hide */
992 @SystemApi
Dan Sandler732bd6c2016-04-12 14:20:32 -0400993 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
994
Dan Sandlerd63f9322015-05-06 15:18:49 -0400995 private Icon mSmallIcon;
996 private Icon mLargeIcon;
997
Julia Reynoldsb5e44b72016-08-16 15:00:25 -0400998 private String mChannelId;
999
Chris Wren51c75102013-07-16 20:49:17 -04001000 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001001 * Structure to encapsulate a named action that can be shown as part of this notification.
1002 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1003 * selected by the user.
1004 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001005 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1006 * or {@link Notification.Builder#addAction(Notification.Action)}
1007 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001008 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001009 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -07001010 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001011 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001012 private final RemoteInput[] mRemoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001013 private boolean mAllowGeneratedReplies = false;
Griff Hazen959591e2014-05-15 22:26:18 -07001014
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001015 /**
1016 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001017 *
1018 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001019 */
Dan Sandler86647982015-05-13 23:41:13 -04001020 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001021 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001022
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001023 /**
1024 * Title of the action.
1025 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001026 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001027
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001028 /**
1029 * Intent to send when the user invokes this action. May be null, in which case the action
1030 * may be rendered in a disabled presentation by the system UI.
1031 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001032 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001033
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001034 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001035 if (in.readInt() != 0) {
1036 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001037 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1038 icon = mIcon.getResId();
1039 }
Dan Sandler86647982015-05-13 23:41:13 -04001040 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001041 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1042 if (in.readInt() == 1) {
1043 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1044 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001045 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001046 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001047 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001048 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001049
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001050 /**
Dan Sandler86647982015-05-13 23:41:13 -04001051 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001052 */
Dan Sandler86647982015-05-13 23:41:13 -04001053 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001054 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills42b0c4d2016-04-26 13:35:36 -04001055 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, false);
Griff Hazen959591e2014-05-15 22:26:18 -07001056 }
1057
Adrian Roos7af53622016-10-12 13:44:05 -07001058 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001059 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001060 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001061 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001062 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1063 this.icon = icon.getResId();
1064 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001065 this.title = title;
1066 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001067 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001068 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001069 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001070 }
1071
1072 /**
Dan Sandler86647982015-05-13 23:41:13 -04001073 * Return an icon representing the action.
1074 */
1075 public Icon getIcon() {
1076 if (mIcon == null && icon != 0) {
1077 // you snuck an icon in here without using the builder; let's try to keep it
1078 mIcon = Icon.createWithResource("", icon);
1079 }
1080 return mIcon;
1081 }
1082
1083 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001084 * Get additional metadata carried around with this Action.
1085 */
1086 public Bundle getExtras() {
1087 return mExtras;
1088 }
1089
1090 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001091 * Return whether the platform should automatically generate possible replies for this
1092 * {@link Action}
1093 */
1094 public boolean getAllowGeneratedReplies() {
1095 return mAllowGeneratedReplies;
1096 }
1097
1098 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001099 * Get the list of inputs to be collected from the user when this action is sent.
1100 * May return null if no remote inputs were added.
1101 */
1102 public RemoteInput[] getRemoteInputs() {
1103 return mRemoteInputs;
1104 }
1105
1106 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001107 * Builder class for {@link Action} objects.
1108 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001109 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001110 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001111 private final CharSequence mTitle;
1112 private final PendingIntent mIntent;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001113 private boolean mAllowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001114 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001115 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001116
1117 /**
1118 * Construct a new builder for {@link Action} object.
1119 * @param icon icon to show for this action
1120 * @param title the title of the action
1121 * @param intent the {@link PendingIntent} to fire when users trigger this action
1122 */
Dan Sandler86647982015-05-13 23:41:13 -04001123 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001124 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001125 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001126 }
1127
1128 /**
1129 * Construct a new builder for {@link Action} object.
1130 * @param icon icon to show for this action
1131 * @param title the title of the action
1132 * @param intent the {@link PendingIntent} to fire when users trigger this action
1133 */
1134 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001135 this(icon, title, intent, new Bundle(), null, false);
Griff Hazen959591e2014-05-15 22:26:18 -07001136 }
1137
1138 /**
1139 * Construct a new builder for {@link Action} object using the fields from an
1140 * {@link Action}.
1141 * @param action the action to read fields from.
1142 */
1143 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001144 this(action.getIcon(), action.title, action.actionIntent,
1145 new Bundle(action.mExtras), action.getRemoteInputs(),
1146 action.getAllowGeneratedReplies());
Griff Hazen959591e2014-05-15 22:26:18 -07001147 }
1148
Dan Sandler86647982015-05-13 23:41:13 -04001149 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Adrian Roos7af53622016-10-12 13:44:05 -07001150 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Griff Hazen959591e2014-05-15 22:26:18 -07001151 mIcon = icon;
1152 mTitle = title;
1153 mIntent = intent;
1154 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001155 if (remoteInputs != null) {
1156 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1157 Collections.addAll(mRemoteInputs, remoteInputs);
1158 }
Adrian Roos7af53622016-10-12 13:44:05 -07001159 mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001160 }
1161
1162 /**
1163 * Merge additional metadata into this builder.
1164 *
1165 * <p>Values within the Bundle will replace existing extras values in this Builder.
1166 *
1167 * @see Notification.Action#extras
1168 */
1169 public Builder addExtras(Bundle extras) {
1170 if (extras != null) {
1171 mExtras.putAll(extras);
1172 }
1173 return this;
1174 }
1175
1176 /**
1177 * Get the metadata Bundle used by this Builder.
1178 *
1179 * <p>The returned Bundle is shared with this Builder.
1180 */
1181 public Bundle getExtras() {
1182 return mExtras;
1183 }
1184
1185 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001186 * Add an input to be collected from the user when this action is sent.
1187 * Response values can be retrieved from the fired intent by using the
1188 * {@link RemoteInput#getResultsFromIntent} function.
1189 * @param remoteInput a {@link RemoteInput} to add to the action
1190 * @return this object for method chaining
1191 */
1192 public Builder addRemoteInput(RemoteInput remoteInput) {
1193 if (mRemoteInputs == null) {
1194 mRemoteInputs = new ArrayList<RemoteInput>();
1195 }
1196 mRemoteInputs.add(remoteInput);
1197 return this;
1198 }
1199
1200 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001201 * Set whether the platform should automatically generate possible replies to add to
1202 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1203 * {@link RemoteInput}, this has no effect.
1204 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1205 * otherwise
1206 * @return this object for method chaining
1207 * The default value is {@code false}
1208 */
1209 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1210 mAllowGeneratedReplies = allowGeneratedReplies;
1211 return this;
1212 }
1213
1214 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001215 * Apply an extender to this action builder. Extenders may be used to add
1216 * metadata or change options on this builder.
1217 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001218 public Builder extend(Extender extender) {
1219 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001220 return this;
1221 }
1222
1223 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001224 * Combine all of the options that have been set and return a new {@link Action}
1225 * object.
1226 * @return the built action
1227 */
1228 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001229 RemoteInput[] remoteInputs = mRemoteInputs != null
1230 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001231 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs,
1232 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001233 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001234 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001235
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001236 @Override
1237 public Action clone() {
1238 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001239 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001240 title,
1241 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001242 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001243 getRemoteInputs(),
1244 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001245 }
1246 @Override
1247 public int describeContents() {
1248 return 0;
1249 }
1250 @Override
1251 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001252 final Icon ic = getIcon();
1253 if (ic != null) {
1254 out.writeInt(1);
1255 ic.writeToParcel(out, 0);
1256 } else {
1257 out.writeInt(0);
1258 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001259 TextUtils.writeToParcel(title, out, flags);
1260 if (actionIntent != null) {
1261 out.writeInt(1);
1262 actionIntent.writeToParcel(out, flags);
1263 } else {
1264 out.writeInt(0);
1265 }
Griff Hazen959591e2014-05-15 22:26:18 -07001266 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001267 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001268 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001269 }
Griff Hazen959591e2014-05-15 22:26:18 -07001270 public static final Parcelable.Creator<Action> CREATOR =
1271 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001272 public Action createFromParcel(Parcel in) {
1273 return new Action(in);
1274 }
1275 public Action[] newArray(int size) {
1276 return new Action[size];
1277 }
1278 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001279
1280 /**
1281 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1282 * metadata or change options on an action builder.
1283 */
1284 public interface Extender {
1285 /**
1286 * Apply this extender to a notification action builder.
1287 * @param builder the builder to be modified.
1288 * @return the build object for chaining.
1289 */
1290 public Builder extend(Builder builder);
1291 }
1292
1293 /**
1294 * Wearable extender for notification actions. To add extensions to an action,
1295 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1296 * the {@code WearableExtender()} constructor and apply it to a
1297 * {@link android.app.Notification.Action.Builder} using
1298 * {@link android.app.Notification.Action.Builder#extend}.
1299 *
1300 * <pre class="prettyprint">
1301 * Notification.Action action = new Notification.Action.Builder(
1302 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001303 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001304 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001305 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001306 */
1307 public static final class WearableExtender implements Extender {
1308 /** Notification action extra which contains wearable extensions */
1309 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1310
Pete Gastaf6781d2014-10-07 15:17:05 -04001311 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001312 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001313 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1314 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1315 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001316
1317 // Flags bitwise-ored to mFlags
1318 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001319 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001320 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001321
1322 // Default value for flags integer
1323 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1324
1325 private int mFlags = DEFAULT_FLAGS;
1326
Pete Gastaf6781d2014-10-07 15:17:05 -04001327 private CharSequence mInProgressLabel;
1328 private CharSequence mConfirmLabel;
1329 private CharSequence mCancelLabel;
1330
Griff Hazen61a9e862014-05-22 16:05:19 -07001331 /**
1332 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1333 * options.
1334 */
1335 public WearableExtender() {
1336 }
1337
1338 /**
1339 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1340 * wearable options present in an existing notification action.
1341 * @param action the notification action to inspect.
1342 */
1343 public WearableExtender(Action action) {
1344 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1345 if (wearableBundle != null) {
1346 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001347 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1348 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1349 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001350 }
1351 }
1352
1353 /**
1354 * Apply wearable extensions to a notification action that is being built. This is
1355 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1356 * method of {@link android.app.Notification.Action.Builder}.
1357 */
1358 @Override
1359 public Action.Builder extend(Action.Builder builder) {
1360 Bundle wearableBundle = new Bundle();
1361
1362 if (mFlags != DEFAULT_FLAGS) {
1363 wearableBundle.putInt(KEY_FLAGS, mFlags);
1364 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001365 if (mInProgressLabel != null) {
1366 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1367 }
1368 if (mConfirmLabel != null) {
1369 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1370 }
1371 if (mCancelLabel != null) {
1372 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1373 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001374
1375 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1376 return builder;
1377 }
1378
1379 @Override
1380 public WearableExtender clone() {
1381 WearableExtender that = new WearableExtender();
1382 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001383 that.mInProgressLabel = this.mInProgressLabel;
1384 that.mConfirmLabel = this.mConfirmLabel;
1385 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001386 return that;
1387 }
1388
1389 /**
1390 * Set whether this action is available when the wearable device is not connected to
1391 * a companion device. The user can still trigger this action when the wearable device is
1392 * offline, but a visual hint will indicate that the action may not be available.
1393 * Defaults to true.
1394 */
1395 public WearableExtender setAvailableOffline(boolean availableOffline) {
1396 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1397 return this;
1398 }
1399
1400 /**
1401 * Get whether this action is available when the wearable device is not connected to
1402 * a companion device. The user can still trigger this action when the wearable device is
1403 * offline, but a visual hint will indicate that the action may not be available.
1404 * Defaults to true.
1405 */
1406 public boolean isAvailableOffline() {
1407 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1408 }
1409
1410 private void setFlag(int mask, boolean value) {
1411 if (value) {
1412 mFlags |= mask;
1413 } else {
1414 mFlags &= ~mask;
1415 }
1416 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001417
1418 /**
1419 * Set a label to display while the wearable is preparing to automatically execute the
1420 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1421 *
1422 * @param label the label to display while the action is being prepared to execute
1423 * @return this object for method chaining
1424 */
1425 public WearableExtender setInProgressLabel(CharSequence label) {
1426 mInProgressLabel = label;
1427 return this;
1428 }
1429
1430 /**
1431 * Get the label to display while the wearable is preparing to automatically execute
1432 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1433 *
1434 * @return the label to display while the action is being prepared to execute
1435 */
1436 public CharSequence getInProgressLabel() {
1437 return mInProgressLabel;
1438 }
1439
1440 /**
1441 * Set a label to display to confirm that the action should be executed.
1442 * This is usually an imperative verb like "Send".
1443 *
1444 * @param label the label to confirm the action should be executed
1445 * @return this object for method chaining
1446 */
1447 public WearableExtender setConfirmLabel(CharSequence label) {
1448 mConfirmLabel = label;
1449 return this;
1450 }
1451
1452 /**
1453 * Get the label to display to confirm that the action should be executed.
1454 * This is usually an imperative verb like "Send".
1455 *
1456 * @return the label to confirm the action should be executed
1457 */
1458 public CharSequence getConfirmLabel() {
1459 return mConfirmLabel;
1460 }
1461
1462 /**
1463 * Set a label to display to cancel the action.
1464 * This is usually an imperative verb, like "Cancel".
1465 *
1466 * @param label the label to display to cancel the action
1467 * @return this object for method chaining
1468 */
1469 public WearableExtender setCancelLabel(CharSequence label) {
1470 mCancelLabel = label;
1471 return this;
1472 }
1473
1474 /**
1475 * Get the label to display to cancel the action.
1476 * This is usually an imperative verb like "Cancel".
1477 *
1478 * @return the label to display to cancel the action
1479 */
1480 public CharSequence getCancelLabel() {
1481 return mCancelLabel;
1482 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001483
1484 /**
1485 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1486 * platform that it can generate the appropriate transitions.
1487 * @param hintLaunchesActivity {@code true} if the content intent will launch
1488 * an activity and transitions should be generated, false otherwise.
1489 * @return this object for method chaining
1490 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001491 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001492 boolean hintLaunchesActivity) {
1493 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1494 return this;
1495 }
1496
1497 /**
1498 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1499 * platform that it can generate the appropriate transitions
1500 * @return {@code true} if the content intent will launch an activity and transitions
1501 * should be generated, false otherwise. The default value is {@code false} if this was
1502 * never set.
1503 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001504 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001505 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1506 }
Alex Hills9f087612016-06-07 09:08:59 -04001507
1508 /**
1509 * Set a hint that this Action should be displayed inline.
1510 *
1511 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1512 * otherwise
1513 * @return this object for method chaining
1514 */
1515 public WearableExtender setHintDisplayActionInline(
1516 boolean hintDisplayInline) {
1517 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1518 return this;
1519 }
1520
1521 /**
1522 * Get a hint that this Action should be displayed inline.
1523 *
1524 * @return {@code true} if the Action should be displayed inline, {@code false}
1525 * otherwise. The default value is {@code false} if this was never set.
1526 */
1527 public boolean getHintDisplayActionInline() {
1528 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1529 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001530 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001531 }
1532
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001533 /**
1534 * Array of all {@link Action} structures attached to this notification by
1535 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1536 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1537 * interface for invoking actions.
1538 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001539 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001540
1541 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001542 * Replacement version of this notification whose content will be shown
1543 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1544 * and {@link #VISIBILITY_PUBLIC}.
1545 */
1546 public Notification publicVersion;
1547
1548 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001549 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001550 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 */
1552 public Notification()
1553 {
1554 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001555 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001556 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 }
1558
1559 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 * @hide
1561 */
1562 public Notification(Context context, int icon, CharSequence tickerText, long when,
1563 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1564 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001565 new Builder(context)
1566 .setWhen(when)
1567 .setSmallIcon(icon)
1568 .setTicker(tickerText)
1569 .setContentTitle(contentTitle)
1570 .setContentText(contentText)
1571 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1572 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001573 }
1574
1575 /**
1576 * Constructs a Notification object with the information needed to
1577 * have a status bar icon without the standard expanded view.
1578 *
1579 * @param icon The resource id of the icon to put in the status bar.
1580 * @param tickerText The text that flows by in the status bar when the notification first
1581 * activates.
1582 * @param when The time to show in the time field. In the System.currentTimeMillis
1583 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001584 *
1585 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001587 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 public Notification(int icon, CharSequence tickerText, long when)
1589 {
1590 this.icon = icon;
1591 this.tickerText = tickerText;
1592 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001593 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 }
1595
1596 /**
1597 * Unflatten the notification from a parcel.
1598 */
Svet Ganovddb94882016-06-23 19:55:24 -07001599 @SuppressWarnings("unchecked")
1600 public Notification(Parcel parcel) {
1601 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
1602 // intents in extras are always written as the last entry.
1603 readFromParcelImpl(parcel);
1604 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001605 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07001606 }
1607
1608 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001609 {
1610 int version = parcel.readInt();
1611
1612 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001613 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001614 if (parcel.readInt() != 0) {
1615 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001616 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1617 icon = mSmallIcon.getResId();
1618 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001619 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001620 number = parcel.readInt();
1621 if (parcel.readInt() != 0) {
1622 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1623 }
1624 if (parcel.readInt() != 0) {
1625 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1626 }
1627 if (parcel.readInt() != 0) {
1628 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1629 }
1630 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001631 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001632 }
1633 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001634 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1635 }
Joe Onorato561d3852010-11-20 18:09:34 -08001636 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001637 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001638 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 defaults = parcel.readInt();
1640 flags = parcel.readInt();
1641 if (parcel.readInt() != 0) {
1642 sound = Uri.CREATOR.createFromParcel(parcel);
1643 }
1644
1645 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001646 if (parcel.readInt() != 0) {
1647 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1648 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001649 vibrate = parcel.createLongArray();
1650 ledARGB = parcel.readInt();
1651 ledOnMS = parcel.readInt();
1652 ledOffMS = parcel.readInt();
1653 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001654
1655 if (parcel.readInt() != 0) {
1656 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1657 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001658
1659 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001660
John Spurlockfd7f1e02014-03-18 16:41:57 -04001661 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001662
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001663 mGroupKey = parcel.readString();
1664
1665 mSortKey = parcel.readString();
1666
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001667 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001668
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001669 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1670
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001671 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001672 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1673 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001674
Chris Wren8fd39ec2014-02-27 17:43:26 -05001675 if (parcel.readInt() != 0) {
1676 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1677 }
1678
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001679 visibility = parcel.readInt();
1680
1681 if (parcel.readInt() != 0) {
1682 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1683 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001684
1685 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001686
1687 if (parcel.readInt() != 0) {
1688 mChannelId = parcel.readString();
1689 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001690 }
1691
Andy Stadler110988c2010-12-03 14:29:16 -08001692 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001693 public Notification clone() {
1694 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001695 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001696 return that;
1697 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001698
Daniel Sandler1a497d32013-04-18 14:52:45 -04001699 /**
1700 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1701 * of this into that.
1702 * @hide
1703 */
1704 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001705 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001706 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001707 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001708 that.number = this.number;
1709
1710 // PendingIntents are global, so there's no reason (or way) to clone them.
1711 that.contentIntent = this.contentIntent;
1712 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001713 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001714
1715 if (this.tickerText != null) {
1716 that.tickerText = this.tickerText.toString();
1717 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001718 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001719 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001720 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001721 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001722 that.contentView = this.contentView.clone();
1723 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001724 if (heavy && this.mLargeIcon != null) {
1725 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001726 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001727 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001728 that.sound = this.sound; // android.net.Uri is immutable
1729 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001730 if (this.audioAttributes != null) {
1731 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1732 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001733
1734 final long[] vibrate = this.vibrate;
1735 if (vibrate != null) {
1736 final int N = vibrate.length;
1737 final long[] vib = that.vibrate = new long[N];
1738 System.arraycopy(vibrate, 0, vib, 0, N);
1739 }
1740
1741 that.ledARGB = this.ledARGB;
1742 that.ledOnMS = this.ledOnMS;
1743 that.ledOffMS = this.ledOffMS;
1744 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001745
Joe Onorato18e69df2010-05-17 22:26:12 -07001746 that.flags = this.flags;
1747
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001748 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001749
John Spurlockfd7f1e02014-03-18 16:41:57 -04001750 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001751
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001752 that.mGroupKey = this.mGroupKey;
1753
1754 that.mSortKey = this.mSortKey;
1755
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001756 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001757 try {
1758 that.extras = new Bundle(this.extras);
1759 // will unparcel
1760 that.extras.size();
1761 } catch (BadParcelableException e) {
1762 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1763 that.extras = null;
1764 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001765 }
1766
Felipe Lemedd85da62016-06-28 11:29:54 -07001767 if (!ArrayUtils.isEmpty(allPendingIntents)) {
1768 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07001769 }
1770
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001771 if (this.actions != null) {
1772 that.actions = new Action[this.actions.length];
1773 for(int i=0; i<this.actions.length; i++) {
1774 that.actions[i] = this.actions[i].clone();
1775 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001776 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001777
Daniel Sandler1a497d32013-04-18 14:52:45 -04001778 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001779 that.bigContentView = this.bigContentView.clone();
1780 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001781
Chris Wren8fd39ec2014-02-27 17:43:26 -05001782 if (heavy && this.headsUpContentView != null) {
1783 that.headsUpContentView = this.headsUpContentView.clone();
1784 }
1785
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001786 that.visibility = this.visibility;
1787
1788 if (this.publicVersion != null) {
1789 that.publicVersion = new Notification();
1790 this.publicVersion.cloneInto(that.publicVersion, heavy);
1791 }
1792
Dan Sandler26e81cf2014-05-06 10:01:27 -04001793 that.color = this.color;
1794
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001795 that.mChannelId = this.mChannelId;
1796
Daniel Sandler1a497d32013-04-18 14:52:45 -04001797 if (!heavy) {
1798 that.lightenPayload(); // will clean out extras
1799 }
1800 }
1801
1802 /**
1803 * Removes heavyweight parts of the Notification object for archival or for sending to
1804 * listeners when the full contents are not necessary.
1805 * @hide
1806 */
1807 public final void lightenPayload() {
1808 tickerView = null;
1809 contentView = null;
1810 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001811 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001812 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05001813 if (extras != null && !extras.isEmpty()) {
1814 final Set<String> keyset = extras.keySet();
1815 final int N = keyset.size();
1816 final String[] keys = keyset.toArray(new String[N]);
1817 for (int i=0; i<N; i++) {
1818 final String key = keys[i];
1819 final Object obj = extras.get(key);
1820 if (obj != null &&
1821 ( obj instanceof Parcelable
1822 || obj instanceof Parcelable[]
1823 || obj instanceof SparseArray
1824 || obj instanceof ArrayList)) {
1825 extras.remove(key);
1826 }
1827 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001828 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001829 }
1830
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001831 /**
1832 * Make sure this CharSequence is safe to put into a bundle, which basically
1833 * means it had better not be some custom Parcelable implementation.
1834 * @hide
1835 */
1836 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001837 if (cs == null) return cs;
1838 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1839 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1840 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001841 if (cs instanceof Parcelable) {
1842 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1843 + " instance is a custom Parcelable and not allowed in Notification");
1844 return cs.toString();
1845 }
Selim Cinek60a54252016-02-26 17:03:25 -08001846 return removeTextSizeSpans(cs);
1847 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001848
Selim Cinek60a54252016-02-26 17:03:25 -08001849 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
1850 if (charSequence instanceof Spanned) {
1851 Spanned ss = (Spanned) charSequence;
1852 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
1853 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
1854 for (Object span : spans) {
1855 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08001856 if (resultSpan instanceof CharacterStyle) {
1857 resultSpan = ((CharacterStyle) span).getUnderlying();
1858 }
1859 if (resultSpan instanceof TextAppearanceSpan) {
1860 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08001861 resultSpan = new TextAppearanceSpan(
1862 originalSpan.getFamily(),
1863 originalSpan.getTextStyle(),
1864 -1,
1865 originalSpan.getTextColor(),
1866 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08001867 } else if (resultSpan instanceof RelativeSizeSpan
1868 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08001869 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08001870 } else {
1871 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08001872 }
1873 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
1874 ss.getSpanFlags(span));
1875 }
1876 return builder;
1877 }
1878 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001879 }
1880
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 public int describeContents() {
1882 return 0;
1883 }
1884
1885 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001886 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 */
Svet Ganovddb94882016-06-23 19:55:24 -07001888 public void writeToParcel(Parcel parcel, int flags) {
1889 // We need to mark all pending intents getting into the notification
1890 // system as being put there to later allow the notification ranker
1891 // to launch them and by doing so add the app to the battery saver white
1892 // list for a short period of time. The problem is that the system
1893 // cannot look into the extras as there may be parcelables there that
1894 // the platform does not know how to handle. To go around that we have
1895 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07001896 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07001897 if (collectPendingIntents) {
1898 PendingIntent.setOnMarshaledListener(
1899 (PendingIntent intent, Parcel out, int outFlags) -> {
1900 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07001901 if (allPendingIntents == null) {
1902 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07001903 }
Felipe Lemedd85da62016-06-28 11:29:54 -07001904 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07001905 }
1906 });
1907 }
1908 try {
1909 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
1910 // want to intercept all pending events written to the pacel.
1911 writeToParcelImpl(parcel, flags);
1912 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001913 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07001914 } finally {
1915 if (collectPendingIntents) {
1916 PendingIntent.setOnMarshaledListener(null);
1917 }
1918 }
1919 }
1920
1921 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001922 parcel.writeInt(1);
1923
1924 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001925 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04001926 if (mSmallIcon == null && icon != 0) {
1927 // you snuck an icon in here without using the builder; let's try to keep it
1928 mSmallIcon = Icon.createWithResource("", icon);
1929 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001930 if (mSmallIcon != null) {
1931 parcel.writeInt(1);
1932 mSmallIcon.writeToParcel(parcel, 0);
1933 } else {
1934 parcel.writeInt(0);
1935 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 parcel.writeInt(number);
1937 if (contentIntent != null) {
1938 parcel.writeInt(1);
1939 contentIntent.writeToParcel(parcel, 0);
1940 } else {
1941 parcel.writeInt(0);
1942 }
1943 if (deleteIntent != null) {
1944 parcel.writeInt(1);
1945 deleteIntent.writeToParcel(parcel, 0);
1946 } else {
1947 parcel.writeInt(0);
1948 }
1949 if (tickerText != null) {
1950 parcel.writeInt(1);
1951 TextUtils.writeToParcel(tickerText, parcel, flags);
1952 } else {
1953 parcel.writeInt(0);
1954 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001955 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001956 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001957 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001958 } else {
1959 parcel.writeInt(0);
1960 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001961 if (contentView != null) {
1962 parcel.writeInt(1);
1963 contentView.writeToParcel(parcel, 0);
1964 } else {
1965 parcel.writeInt(0);
1966 }
Selim Cinek279fa862016-06-14 10:57:25 -07001967 if (mLargeIcon == null && largeIcon != null) {
1968 // you snuck an icon in here without using the builder; let's try to keep it
1969 mLargeIcon = Icon.createWithBitmap(largeIcon);
1970 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001971 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001972 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001973 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001974 } else {
1975 parcel.writeInt(0);
1976 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977
1978 parcel.writeInt(defaults);
1979 parcel.writeInt(this.flags);
1980
1981 if (sound != null) {
1982 parcel.writeInt(1);
1983 sound.writeToParcel(parcel, 0);
1984 } else {
1985 parcel.writeInt(0);
1986 }
1987 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001988
1989 if (audioAttributes != null) {
1990 parcel.writeInt(1);
1991 audioAttributes.writeToParcel(parcel, 0);
1992 } else {
1993 parcel.writeInt(0);
1994 }
1995
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 parcel.writeLongArray(vibrate);
1997 parcel.writeInt(ledARGB);
1998 parcel.writeInt(ledOnMS);
1999 parcel.writeInt(ledOffMS);
2000 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002001
2002 if (fullScreenIntent != null) {
2003 parcel.writeInt(1);
2004 fullScreenIntent.writeToParcel(parcel, 0);
2005 } else {
2006 parcel.writeInt(0);
2007 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002008
2009 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002010
John Spurlockfd7f1e02014-03-18 16:41:57 -04002011 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002012
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002013 parcel.writeString(mGroupKey);
2014
2015 parcel.writeString(mSortKey);
2016
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002017 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002018
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002019 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002020
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002021 if (bigContentView != null) {
2022 parcel.writeInt(1);
2023 bigContentView.writeToParcel(parcel, 0);
2024 } else {
2025 parcel.writeInt(0);
2026 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002027
Chris Wren8fd39ec2014-02-27 17:43:26 -05002028 if (headsUpContentView != null) {
2029 parcel.writeInt(1);
2030 headsUpContentView.writeToParcel(parcel, 0);
2031 } else {
2032 parcel.writeInt(0);
2033 }
2034
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002035 parcel.writeInt(visibility);
2036
2037 if (publicVersion != null) {
2038 parcel.writeInt(1);
2039 publicVersion.writeToParcel(parcel, 0);
2040 } else {
2041 parcel.writeInt(0);
2042 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002043
2044 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002045
2046 if (mChannelId != null) {
2047 parcel.writeInt(1);
2048 parcel.writeString(mChannelId);
2049 } else {
2050 parcel.writeInt(0);
2051 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002052 }
2053
2054 /**
2055 * Parcelable.Creator that instantiates Notification objects
2056 */
2057 public static final Parcelable.Creator<Notification> CREATOR
2058 = new Parcelable.Creator<Notification>()
2059 {
2060 public Notification createFromParcel(Parcel parcel)
2061 {
2062 return new Notification(parcel);
2063 }
2064
2065 public Notification[] newArray(int size)
2066 {
2067 return new Notification[size];
2068 }
2069 };
2070
2071 /**
2072 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2073 * layout.
2074 *
2075 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2076 * in the view.</p>
2077 * @param context The context for your application / activity.
2078 * @param contentTitle The title that goes in the expanded entry.
2079 * @param contentText The text that goes in the expanded entry.
2080 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2081 * If this is an activity, it must include the
2082 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002083 * that you take care of task management as described in the
2084 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2085 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002086 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002087 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002088 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002090 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002091 public void setLatestEventInfo(Context context,
2092 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002093 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2094 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2095 new Throwable());
2096 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002097
Selim Cinek4ac6f602016-06-13 15:47:03 -07002098 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2099 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2100 }
2101
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002102 // ensure that any information already set directly is preserved
2103 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002104
2105 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002106 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002107 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002108 }
2109 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002110 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002112 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002113
2114 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002115 }
2116
Julia Reynoldsda303542015-11-23 14:00:20 -05002117 /**
2118 * @hide
2119 */
2120 public static void addFieldsFromContext(Context context, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002121 addFieldsFromContext(context.getApplicationInfo(), context.getUserId(), notification);
2122 }
2123
2124 /**
2125 * @hide
2126 */
2127 public static void addFieldsFromContext(ApplicationInfo ai, int userId,
2128 Notification notification) {
2129 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
2130 notification.extras.putInt(EXTRA_ORIGINATING_USERID, userId);
Julia Reynoldsda303542015-11-23 14:00:20 -05002131 }
2132
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002133 @Override
2134 public String toString() {
2135 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002136 sb.append("Notification(pri=");
2137 sb.append(priority);
2138 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002139 if (contentView != null) {
2140 sb.append(contentView.getPackage());
2141 sb.append("/0x");
2142 sb.append(Integer.toHexString(contentView.getLayoutId()));
2143 } else {
2144 sb.append("null");
2145 }
2146 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002147 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2148 sb.append("default");
2149 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002150 int N = this.vibrate.length-1;
2151 sb.append("[");
2152 for (int i=0; i<N; i++) {
2153 sb.append(this.vibrate[i]);
2154 sb.append(',');
2155 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002156 if (N != -1) {
2157 sb.append(this.vibrate[N]);
2158 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002159 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002160 } else {
2161 sb.append("null");
2162 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002163 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002164 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002165 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002166 } else if (this.sound != null) {
2167 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168 } else {
2169 sb.append("null");
2170 }
Chris Wren365b6d32015-07-16 10:39:26 -04002171 if (this.tickerText != null) {
2172 sb.append(" tick");
2173 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002174 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002175 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002176 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002177 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002178 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002179 if (this.category != null) {
2180 sb.append(" category=");
2181 sb.append(this.category);
2182 }
2183 if (this.mGroupKey != null) {
2184 sb.append(" groupKey=");
2185 sb.append(this.mGroupKey);
2186 }
2187 if (this.mSortKey != null) {
2188 sb.append(" sortKey=");
2189 sb.append(this.mSortKey);
2190 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002191 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002192 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002193 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002194 }
2195 sb.append(" vis=");
2196 sb.append(visibilityToString(this.visibility));
2197 if (this.publicVersion != null) {
2198 sb.append(" publicVersion=");
2199 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002200 }
2201 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002202 return sb.toString();
2203 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002204
Dan Sandler1b718782014-07-18 12:43:45 -04002205 /**
2206 * {@hide}
2207 */
2208 public static String visibilityToString(int vis) {
2209 switch (vis) {
2210 case VISIBILITY_PRIVATE:
2211 return "PRIVATE";
2212 case VISIBILITY_PUBLIC:
2213 return "PUBLIC";
2214 case VISIBILITY_SECRET:
2215 return "SECRET";
2216 default:
2217 return "UNKNOWN(" + String.valueOf(vis) + ")";
2218 }
2219 }
2220
Joe Onoratocb109a02011-01-18 17:57:41 -08002221 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002222 * {@hide}
2223 */
2224 public static String priorityToString(@Priority int pri) {
2225 switch (pri) {
2226 case PRIORITY_MIN:
2227 return "MIN";
2228 case PRIORITY_LOW:
2229 return "LOW";
2230 case PRIORITY_DEFAULT:
2231 return "DEFAULT";
2232 case PRIORITY_HIGH:
2233 return "HIGH";
2234 case PRIORITY_MAX:
2235 return "MAX";
2236 default:
2237 return "UNKNOWN(" + String.valueOf(pri) + ")";
2238 }
2239 }
2240
2241 /**
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002242 * Returns the id of the channel this notification posts to.
2243 */
Julia Reynolds37856052016-11-11 09:20:07 -05002244 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002245 return mChannelId;
2246 }
2247
2248 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002249 * The small icon representing this notification in the status bar and content view.
2250 *
2251 * @return the small icon representing this notification.
2252 *
2253 * @see Builder#getSmallIcon()
2254 * @see Builder#setSmallIcon(Icon)
2255 */
2256 public Icon getSmallIcon() {
2257 return mSmallIcon;
2258 }
2259
2260 /**
2261 * Used when notifying to clean up legacy small icons.
2262 * @hide
2263 */
2264 public void setSmallIcon(Icon icon) {
2265 mSmallIcon = icon;
2266 }
2267
2268 /**
2269 * The large icon shown in this notification's content view.
2270 * @see Builder#getLargeIcon()
2271 * @see Builder#setLargeIcon(Icon)
2272 */
2273 public Icon getLargeIcon() {
2274 return mLargeIcon;
2275 }
2276
2277 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002278 * @hide
2279 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002280 public boolean isGroupSummary() {
2281 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2282 }
2283
2284 /**
2285 * @hide
2286 */
2287 public boolean isGroupChild() {
2288 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2289 }
2290
2291 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002292 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002293 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002294 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002295 * content views using the platform's notification layout template. If your app supports
2296 * versions of Android as old as API level 4, you can instead use
2297 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2298 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2299 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002300 *
Scott Main183bf112012-08-13 19:12:13 -07002301 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002302 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002303 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002304 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002305 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2306 * .setContentText(subject)
2307 * .setSmallIcon(R.drawable.new_mail)
2308 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002309 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002310 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002311 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002312 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002313 /**
2314 * @hide
2315 */
2316 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2317 "android.rebuild.contentViewActionCount";
2318 /**
2319 * @hide
2320 */
2321 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2322 = "android.rebuild.bigViewActionCount";
2323 /**
2324 * @hide
2325 */
2326 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2327 = "android.rebuild.hudViewActionCount";
2328
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002329 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002330
Joe Onorato46439ce2010-11-19 13:56:21 -08002331 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002332 private Notification mN;
2333 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002334 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002335 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2336 private ArrayList<String> mPersonList = new ArrayList<String>();
2337 private NotificationColorUtil mColorUtil;
2338 private boolean mColorUtilInited = false;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002339
2340 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002341 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2342 */
2343 private int mCachedContrastColor = COLOR_INVALID;
2344 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08002345 /**
2346 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
2347 */
2348 private int mCachedAmbientColor = COLOR_INVALID;
2349 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08002350
2351 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08002352 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
2353 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
2354 */
2355 StandardTemplateParams mParams = new StandardTemplateParams();
2356
2357 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002358 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002359 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002360
2361 * <table>
2362 * <tr><th align=right>priority</th>
2363 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2364 * <tr><th align=right>when</th>
2365 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2366 * <tr><th align=right>audio stream</th>
2367 * <td>{@link #STREAM_DEFAULT}</td></tr>
2368 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002369 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002370
2371 * @param context
2372 * A {@link Context} that will be used by the Builder to construct the
2373 * RemoteViews. The Context will not be held past the lifetime of this Builder
2374 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002375 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002376 public Builder(Context context) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002377 this(context, null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002378 }
2379
Joe Onoratocb109a02011-01-18 17:57:41 -08002380 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002381 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002382 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002383 public Builder(Context context, Notification toAdopt) {
2384 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002385
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002386 if (toAdopt == null) {
2387 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002388 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2389 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2390 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002391 mN.priority = PRIORITY_DEFAULT;
2392 mN.visibility = VISIBILITY_PRIVATE;
2393 } else {
2394 mN = toAdopt;
2395 if (mN.actions != null) {
2396 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002397 }
2398
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002399 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2400 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2401 }
2402
Selim Cinek4ac6f602016-06-13 15:47:03 -07002403 if (mN.getSmallIcon() == null && mN.icon != 0) {
2404 setSmallIcon(mN.icon);
2405 }
2406
2407 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
2408 setLargeIcon(mN.largeIcon);
2409 }
2410
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002411 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2412 if (!TextUtils.isEmpty(templateClass)) {
2413 final Class<? extends Style> styleClass
2414 = getNotificationStyleClass(templateClass);
2415 if (styleClass == null) {
2416 Log.d(TAG, "Unknown style class: " + templateClass);
2417 } else {
2418 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002419 final Constructor<? extends Style> ctor =
2420 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002421 ctor.setAccessible(true);
2422 final Style style = ctor.newInstance();
2423 style.restoreFromExtras(mN.extras);
2424
2425 if (style != null) {
2426 setStyle(style);
2427 }
2428 } catch (Throwable t) {
2429 Log.e(TAG, "Could not create Style", t);
2430 }
2431 }
2432 }
2433
2434 }
2435 }
2436
2437 private NotificationColorUtil getColorUtil() {
2438 if (!mColorUtilInited) {
2439 mColorUtilInited = true;
2440 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
2441 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002442 }
2443 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002444 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002445 }
2446
2447 /**
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002448 * Specifies the channel the notification should be delivered on.
2449 */
2450 public Builder setChannel(String channelId) {
2451 mN.mChannelId = channelId;
2452 return this;
2453 }
2454
2455 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002456 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002457 *
2458 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2459 * shown anymore by default and must be opted into by using
2460 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002461 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002462 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002463 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002464 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002465 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002466 return this;
2467 }
2468
Joe Onoratocb109a02011-01-18 17:57:41 -08002469 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002470 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002471 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002472 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2473 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002474 */
2475 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002476 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002477 return this;
2478 }
2479
2480 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002481 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002482 *
2483 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002484 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002485 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002486 * Useful when showing an elapsed time (like an ongoing phone call).
2487 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002488 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07002489 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08002490 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002491 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002492 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07002493 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002494 */
2495 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002496 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002497 return this;
2498 }
2499
2500 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002501 * Sets the Chronometer to count down instead of counting up.
2502 *
2503 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2504 * If it isn't set the chronometer will count up.
2505 *
2506 * @see #setUsesChronometer(boolean)
2507 */
Adrian Roos96b7e202016-05-17 13:50:38 -07002508 public Builder setChronometerCountDown(boolean countDown) {
2509 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08002510 return this;
2511 }
2512
2513 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002514 * Set the small icon resource, which will be used to represent the notification in the
2515 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002516 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002517
2518 * The platform template for the expanded view will draw this icon in the left, unless a
2519 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2520 * icon will be moved to the right-hand side.
2521 *
2522
2523 * @param icon
2524 * A resource ID in the application's package of the drawable to use.
2525 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002526 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002527 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002528 return setSmallIcon(icon != 0
2529 ? Icon.createWithResource(mContext, icon)
2530 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002531 }
2532
Joe Onoratocb109a02011-01-18 17:57:41 -08002533 /**
2534 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2535 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2536 * LevelListDrawable}.
2537 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002538 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002539 * @param level The level to use for the icon.
2540 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002541 * @see Notification#icon
2542 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002543 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002544 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002545 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002546 return setSmallIcon(icon);
2547 }
2548
2549 /**
2550 * Set the small icon, which will be used to represent the notification in the
2551 * status bar and content view (unless overriden there by a
2552 * {@link #setLargeIcon(Bitmap) large icon}).
2553 *
2554 * @param icon An Icon object to use.
2555 * @see Notification#icon
2556 */
2557 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002558 mN.setSmallIcon(icon);
2559 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2560 mN.icon = icon.getResId();
2561 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002562 return this;
2563 }
2564
Joe Onoratocb109a02011-01-18 17:57:41 -08002565 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002566 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002567 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002568 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002569 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002570 return this;
2571 }
2572
Joe Onoratocb109a02011-01-18 17:57:41 -08002573 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002574 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002575 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002576 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002577 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002578 return this;
2579 }
2580
Joe Onoratocb109a02011-01-18 17:57:41 -08002581 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002582 * This provides some additional information that is displayed in the notification. No
2583 * guarantees are given where exactly it is displayed.
2584 *
2585 * <p>This information should only be provided if it provides an essential
2586 * benefit to the understanding of the notification. The more text you provide the
2587 * less readable it becomes. For example, an email client should only provide the account
2588 * name here if more than one email account has been added.</p>
2589 *
2590 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
2591 * notification header area.
2592 *
2593 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
2594 * this will be shown in the third line of text in the platform notification template.
2595 * You should not be using {@link #setProgress(int, int, boolean)} at the
2596 * same time on those versions; they occupy the same place.
2597 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002598 */
2599 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002600 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002601 return this;
2602 }
2603
2604 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002605 * Set the remote input history.
2606 *
2607 * This should be set to the most recent inputs that have been sent
2608 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2609 * longer relevant (e.g. for chat notifications once the other party has responded).
2610 *
2611 * The most recent input must be stored at the 0 index, the second most recent at the
2612 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2613 * and how much of each individual input is shown.
2614 *
2615 * <p>Note: The reply text will only be shown on notifications that have least one action
2616 * with a {@code RemoteInput}.</p>
2617 */
2618 public Builder setRemoteInputHistory(CharSequence[] text) {
2619 if (text == null) {
2620 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2621 } else {
2622 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2623 CharSequence[] safe = new CharSequence[N];
2624 for (int i = 0; i < N; i++) {
2625 safe[i] = safeCharSequence(text[i]);
2626 }
2627 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2628 }
2629 return this;
2630 }
2631
2632 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002633 * Set the large number at the right-hand side of the notification. This is
2634 * equivalent to setContentInfo, although it might show the number in a different
2635 * font size for readability.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002636 *
2637 * @deprecated this number is not shown anywhere anymore
Joe Onoratocb109a02011-01-18 17:57:41 -08002638 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002639 @Deprecated
Joe Onorato8595a3d2010-11-19 18:12:07 -08002640 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002641 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002642 return this;
2643 }
2644
Joe Onoratocb109a02011-01-18 17:57:41 -08002645 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002646 * A small piece of additional information pertaining to this notification.
2647 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002648 * The platform template will draw this on the last line of the notification, at the far
2649 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002650 *
2651 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
2652 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
2653 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08002654 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002655 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002656 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002657 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002658 return this;
2659 }
2660
Joe Onoratocb109a02011-01-18 17:57:41 -08002661 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002662 * Set the progress this notification represents.
2663 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002664 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002665 */
2666 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002667 mN.extras.putInt(EXTRA_PROGRESS, progress);
2668 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
2669 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002670 return this;
2671 }
2672
2673 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002674 * Supply a custom RemoteViews to use instead of the platform template.
2675 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002676 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08002677 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002678 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002679 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002680 return setCustomContentView(views);
2681 }
2682
2683 /**
2684 * Supply custom RemoteViews to use instead of the platform template.
2685 *
2686 * This will override the layout that would otherwise be constructed by this Builder
2687 * object.
2688 */
2689 public Builder setCustomContentView(RemoteViews contentView) {
2690 mN.contentView = contentView;
2691 return this;
2692 }
2693
2694 /**
2695 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
2696 *
2697 * This will override the expanded layout that would otherwise be constructed by this
2698 * Builder object.
2699 */
2700 public Builder setCustomBigContentView(RemoteViews contentView) {
2701 mN.bigContentView = contentView;
2702 return this;
2703 }
2704
2705 /**
2706 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
2707 *
2708 * This will override the heads-up layout that would otherwise be constructed by this
2709 * Builder object.
2710 */
2711 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
2712 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002713 return this;
2714 }
2715
Joe Onoratocb109a02011-01-18 17:57:41 -08002716 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002717 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2718 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002719 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2720 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2721 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002722 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002723 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002724 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002725 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002726 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002727 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002728 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002729 return this;
2730 }
2731
Joe Onoratocb109a02011-01-18 17:57:41 -08002732 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002733 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2734 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002735 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002736 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002737 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002738 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002739 return this;
2740 }
2741
Joe Onoratocb109a02011-01-18 17:57:41 -08002742 /**
2743 * An intent to launch instead of posting the notification to the status bar.
2744 * Only for use with extremely high-priority notifications demanding the user's
2745 * <strong>immediate</strong> attention, such as an incoming phone call or
2746 * alarm clock that the user has explicitly set to a particular time.
2747 * If this facility is used for something else, please give the user an option
2748 * to turn it off and use a normal notification, as this can be extremely
2749 * disruptive.
2750 *
Chris Wren47c20a12014-06-18 17:27:29 -04002751 * <p>
2752 * The system UI may choose to display a heads-up notification, instead of
2753 * launching this intent, while the user is using the device.
2754 * </p>
2755 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002756 * @param intent The pending intent to launch.
2757 * @param highPriority Passing true will cause this notification to be sent
2758 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002759 *
2760 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002761 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002762 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002763 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002764 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2765 return this;
2766 }
2767
Joe Onoratocb109a02011-01-18 17:57:41 -08002768 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002769 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002770 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002771 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002772 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002773 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002774 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002775 return this;
2776 }
2777
Joe Onoratocb109a02011-01-18 17:57:41 -08002778 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002779 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002780 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002781 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002782 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002783 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002784 setTicker(tickerText);
2785 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08002786 return this;
2787 }
2788
Joe Onoratocb109a02011-01-18 17:57:41 -08002789 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002790 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002791 *
2792 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002793 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2794 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002795 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002796 public Builder setLargeIcon(Bitmap b) {
2797 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2798 }
2799
2800 /**
2801 * Add a large icon to the notification content view.
2802 *
2803 * In the platform template, this image will be shown on the left of the notification view
2804 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2805 * badge atop the large icon).
2806 */
2807 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002808 mN.mLargeIcon = icon;
2809 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08002810 return this;
2811 }
2812
Joe Onoratocb109a02011-01-18 17:57:41 -08002813 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002814 * Set the sound to play.
2815 *
John Spurlockc0650f022014-07-19 13:22:39 -04002816 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2817 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002818 *
2819 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002820 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002821 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002822 mN.sound = sound;
2823 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002824 return this;
2825 }
2826
Joe Onoratocb109a02011-01-18 17:57:41 -08002827 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002828 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002829 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002830 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2831 *
John Spurlockc0650f022014-07-19 13:22:39 -04002832 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002833 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002834 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002835 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002836 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08002837 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002838 mN.sound = sound;
2839 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08002840 return this;
2841 }
2842
Joe Onoratocb109a02011-01-18 17:57:41 -08002843 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002844 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2845 * use during playback.
2846 *
John Spurlockc0650f022014-07-19 13:22:39 -04002847 * @see Notification#sound
2848 */
2849 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002850 mN.sound = sound;
2851 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04002852 return this;
2853 }
2854
2855 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002856 * Set the vibration pattern to use.
2857 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002858 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2859 * <code>pattern</code> parameter.
2860 *
Chris Wren47c20a12014-06-18 17:27:29 -04002861 * <p>
2862 * A notification that vibrates is more likely to be presented as a heads-up notification.
2863 * </p>
2864 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002865 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002866 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002867 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002868 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08002869 return this;
2870 }
2871
Joe Onoratocb109a02011-01-18 17:57:41 -08002872 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002873 * Set the desired color for the indicator LED on the device, as well as the
2874 * blink duty cycle (specified in milliseconds).
2875 *
2876
2877 * Not all devices will honor all (or even any) of these values.
2878 *
2879
2880 * @see Notification#ledARGB
2881 * @see Notification#ledOnMS
2882 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002883 */
Tor Norbye80756e32015-03-02 09:39:27 -08002884 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002885 mN.ledARGB = argb;
2886 mN.ledOnMS = onMs;
2887 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002888 if (onMs != 0 || offMs != 0) {
2889 mN.flags |= FLAG_SHOW_LIGHTS;
2890 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002891 return this;
2892 }
2893
Joe Onoratocb109a02011-01-18 17:57:41 -08002894 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002895 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002896 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002897
2898 * Ongoing notifications cannot be dismissed by the user, so your application or service
2899 * must take care of canceling them.
2900 *
2901
2902 * They are typically used to indicate a background task that the user is actively engaged
2903 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2904 * (e.g., a file download, sync operation, active network connection).
2905 *
2906
2907 * @see Notification#FLAG_ONGOING_EVENT
2908 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002909 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002910 public Builder setOngoing(boolean ongoing) {
2911 setFlag(FLAG_ONGOING_EVENT, ongoing);
2912 return this;
2913 }
2914
Joe Onoratocb109a02011-01-18 17:57:41 -08002915 /**
2916 * Set this flag if you would only like the sound, vibrate
2917 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002918 *
2919 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002920 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002921 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2922 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2923 return this;
2924 }
2925
Joe Onoratocb109a02011-01-18 17:57:41 -08002926 /**
Julia Reynolds04499532016-09-13 14:04:53 -04002927 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002928 *
2929 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002930 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002931 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002932 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002933 return this;
2934 }
2935
Joe Onoratocb109a02011-01-18 17:57:41 -08002936 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002937 * Set whether or not this notification should not bridge to other devices.
2938 *
2939 * <p>Some notifications can be bridged to other devices for remote display.
2940 * This hint can be set to recommend this notification not be bridged.
2941 */
2942 public Builder setLocalOnly(boolean localOnly) {
2943 setFlag(FLAG_LOCAL_ONLY, localOnly);
2944 return this;
2945 }
2946
2947 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002948 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002949 * <p>
2950 * The value should be one or more of the following fields combined with
2951 * bitwise-or:
2952 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2953 * <p>
2954 * For all default values, use {@link #DEFAULT_ALL}.
2955 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002956 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002957 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08002958 return this;
2959 }
2960
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002961 /**
2962 * Set the priority of this notification.
2963 *
2964 * @see Notification#priority
2965 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002966 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002967 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002968 return this;
2969 }
Joe Malin8d40d042012-11-05 11:36:40 -08002970
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002971 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002972 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002973 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002974 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002975 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002976 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002977 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002978 return this;
2979 }
2980
2981 /**
Chris Wrendde75302014-03-26 17:24:15 -04002982 * Add a person that is relevant to this notification.
2983 *
Chris Wrene6c48932014-09-29 17:19:27 -04002984 * <P>
2985 * Depending on user preferences, this annotation may allow the notification to pass
2986 * through interruption filters, and to appear more prominently in the user interface.
2987 * </P>
2988 *
2989 * <P>
2990 * The person should be specified by the {@code String} representation of a
2991 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2992 * </P>
2993 *
2994 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2995 * URIs. The path part of these URIs must exist in the contacts database, in the
2996 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2997 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2998 * </P>
2999 *
3000 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003001 * @see Notification#EXTRA_PEOPLE
3002 */
Chris Wrene6c48932014-09-29 17:19:27 -04003003 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003004 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04003005 return this;
3006 }
3007
3008 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003009 * Set this notification to be part of a group of notifications sharing the same key.
3010 * Grouped notifications may display in a cluster or stack on devices which
3011 * support such rendering.
3012 *
3013 * <p>To make this notification the summary for its group, also call
3014 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3015 * {@link #setSortKey}.
3016 * @param groupKey The group key of the group.
3017 * @return this object for method chaining
3018 */
3019 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003020 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003021 return this;
3022 }
3023
3024 /**
3025 * Set this notification to be the group summary for a group of notifications.
3026 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003027 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3028 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003029 * @param isGroupSummary Whether this notification should be a group summary.
3030 * @return this object for method chaining
3031 */
3032 public Builder setGroupSummary(boolean isGroupSummary) {
3033 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3034 return this;
3035 }
3036
3037 /**
3038 * Set a sort key that orders this notification among other notifications from the
3039 * same package. This can be useful if an external sort was already applied and an app
3040 * would like to preserve this. Notifications will be sorted lexicographically using this
3041 * value, although providing different priorities in addition to providing sort key may
3042 * cause this value to be ignored.
3043 *
3044 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003045 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003046 *
3047 * @see String#compareTo(String)
3048 */
3049 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003050 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003051 return this;
3052 }
3053
3054 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003055 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003056 *
Griff Hazen720042b2014-02-24 15:46:56 -08003057 * <p>Values within the Bundle will replace existing extras values in this Builder.
3058 *
3059 * @see Notification#extras
3060 */
Griff Hazen959591e2014-05-15 22:26:18 -07003061 public Builder addExtras(Bundle extras) {
3062 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003063 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003064 }
3065 return this;
3066 }
3067
3068 /**
3069 * Set metadata for this notification.
3070 *
3071 * <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 -04003072 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003073 * called.
3074 *
Griff Hazen720042b2014-02-24 15:46:56 -08003075 * <p>Replaces any existing extras values with those from the provided Bundle.
3076 * Use {@link #addExtras} to merge in metadata instead.
3077 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003078 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003079 */
Griff Hazen959591e2014-05-15 22:26:18 -07003080 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003081 if (extras != null) {
3082 mUserExtras = extras;
3083 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003084 return this;
3085 }
3086
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003087 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003088 * Get the current metadata Bundle used by this notification Builder.
3089 *
3090 * <p>The returned Bundle is shared with this Builder.
3091 *
3092 * <p>The current contents of this Bundle are copied into the Notification each time
3093 * {@link #build()} is called.
3094 *
3095 * @see Notification#extras
3096 */
3097 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003098 return mUserExtras;
3099 }
3100
3101 private Bundle getAllExtras() {
3102 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3103 saveExtras.putAll(mN.extras);
3104 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003105 }
3106
3107 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003108 * Add an action to this notification. Actions are typically displayed by
3109 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003110 * <p>
3111 * Every action must have an icon (32dp square and matching the
3112 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3113 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3114 * <p>
3115 * A notification in its expanded form can display up to 3 actions, from left to right in
3116 * the order they were added. Actions will not be displayed when the notification is
3117 * collapsed, however, so be sure that any essential functions may be accessed by the user
3118 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003119 *
3120 * @param icon Resource ID of a drawable that represents the action.
3121 * @param title Text describing the action.
3122 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003123 *
3124 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003125 */
Dan Sandler86647982015-05-13 23:41:13 -04003126 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003127 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003128 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003129 return this;
3130 }
3131
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003132 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003133 * Add an action to this notification. Actions are typically displayed by
3134 * the system as a button adjacent to the notification content.
3135 * <p>
3136 * Every action must have an icon (32dp square and matching the
3137 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3138 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3139 * <p>
3140 * A notification in its expanded form can display up to 3 actions, from left to right in
3141 * the order they were added. Actions will not be displayed when the notification is
3142 * collapsed, however, so be sure that any essential functions may be accessed by the user
3143 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3144 *
3145 * @param action The action to add.
3146 */
3147 public Builder addAction(Action action) {
3148 mActions.add(action);
3149 return this;
3150 }
3151
3152 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003153 * Alter the complete list of actions attached to this notification.
3154 * @see #addAction(Action).
3155 *
3156 * @param actions
3157 * @return
3158 */
3159 public Builder setActions(Action... actions) {
3160 mActions.clear();
3161 for (int i = 0; i < actions.length; i++) {
3162 mActions.add(actions[i]);
3163 }
3164 return this;
3165 }
3166
3167 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003168 * Add a rich notification style to be applied at build time.
3169 *
3170 * @param style Object responsible for modifying the notification style.
3171 */
3172 public Builder setStyle(Style style) {
3173 if (mStyle != style) {
3174 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003175 if (mStyle != null) {
3176 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003177 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3178 } else {
3179 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003180 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003181 }
3182 return this;
3183 }
3184
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003185 /**
3186 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003187 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003188 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
3189 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
3190 *
3191 * @return The same Builder.
3192 */
3193 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003194 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003195 return this;
3196 }
3197
3198 /**
3199 * Supply a replacement Notification whose contents should be shown in insecure contexts
3200 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3201 * @param n A replacement notification, presumably with some or all info redacted.
3202 * @return The same Builder.
3203 */
3204 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003205 if (n != null) {
3206 mN.publicVersion = new Notification();
3207 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3208 } else {
3209 mN.publicVersion = null;
3210 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003211 return this;
3212 }
3213
Griff Hazenb720abe2014-05-20 13:15:30 -07003214 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003215 * Apply an extender to this notification builder. Extenders may be used to add
3216 * metadata or change options on this builder.
3217 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003218 public Builder extend(Extender extender) {
3219 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003220 return this;
3221 }
3222
Dan Sandler4e787062015-06-17 15:09:48 -04003223 /**
3224 * @hide
3225 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003226 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003227 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003228 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003229 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003230 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003231 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003232 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003233 }
3234
Dan Sandler26e81cf2014-05-06 10:01:27 -04003235 /**
3236 * Sets {@link Notification#color}.
3237 *
3238 * @param argb The accent color to use
3239 *
3240 * @return The same Builder.
3241 */
Tor Norbye80756e32015-03-02 09:39:27 -08003242 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003243 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003244 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003245 return this;
3246 }
3247
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003248 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04003249 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
3250 // This user can never be a badged profile,
3251 // and also includes USER_ALL system notifications.
3252 return null;
3253 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02003254 // Note: This assumes that the current user can read the profile badge of the
3255 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003256 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003257 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003258 }
3259
3260 private Bitmap getProfileBadge() {
3261 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003262 if (badge == null) {
3263 return null;
3264 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003265 final int size = mContext.getResources().getDimensionPixelSize(
3266 R.dimen.notification_badge_size);
3267 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003268 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003269 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003270 badge.draw(canvas);
3271 return bitmap;
3272 }
3273
Selim Cinekc848c3a2016-01-13 15:27:30 -08003274 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003275 Bitmap profileBadge = getProfileBadge();
3276
Kenny Guy98193ea2014-07-24 19:54:37 +01003277 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003278 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3279 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Kenny Guy98193ea2014-07-24 19:54:37 +01003280 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003281 }
3282
Christoph Studerfe718432014-09-01 18:21:18 +02003283 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003284 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003285 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003286 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003287 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003288 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003289 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003290 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003291 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003292 contentView.setTextViewText(R.id.text_line_1, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003293 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003294 }
3295
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003296 /**
3297 * Resets the notification header to its original state
3298 */
3299 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07003300 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
3301 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08003302 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003303 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003304 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003305 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07003306 contentView.setTextViewText(R.id.header_text, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003307 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003308 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003309 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003310 contentView.setImageViewIcon(R.id.profile_badge, null);
3311 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003312 }
3313
3314 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003315 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
3316 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003317 }
3318
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003319 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003320 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003321 }
3322
3323 /**
3324 * @param hasProgress whether the progress bar should be shown and set
3325 */
3326 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003327 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
3328 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07003329 }
3330
Adrian Roos70d7aa32017-01-11 15:39:06 -08003331 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01003332 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003333
Christoph Studerfe718432014-09-01 18:21:18 +02003334 resetStandardTemplate(contentView);
3335
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003336 final Bundle ex = mN.extras;
Dan Sandler190d58d2014-05-15 09:33:39 -04003337
Adrian Roos487374f2017-01-11 15:48:14 -08003338 bindNotificationHeader(contentView, p.ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003339 bindLargeIcon(contentView);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003340 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
3341 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003342 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003343 contentView.setTextViewText(R.id.title, p.title);
Selim Cinek954cc232016-05-20 13:29:23 -07003344 contentView.setViewLayoutWidth(R.id.title, showProgress
3345 ? ViewGroup.LayoutParams.WRAP_CONTENT
3346 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08003347 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08003348 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003349 int textId = showProgress ? com.android.internal.R.id.text_line_1
3350 : com.android.internal.R.id.text;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003351 contentView.setTextViewText(textId, p.text);
Selim Cinek41598732016-01-11 16:58:37 -08003352 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003353 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003354
Selim Cinek279fa862016-06-14 10:57:25 -07003355 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003356
Selim Cinek29603462015-11-17 19:04:39 -08003357 return contentView;
3358 }
3359
Selim Cinek860b6da2015-12-16 19:02:19 -08003360 /**
3361 * @param remoteView the remote view to update the minheight in
3362 * @param hasMinHeight does it have a mimHeight
3363 * @hide
3364 */
3365 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3366 int minHeight = 0;
3367 if (hasMinHeight) {
3368 // we need to set the minHeight of the notification
3369 minHeight = mContext.getResources().getDimensionPixelSize(
3370 com.android.internal.R.dimen.notification_min_content_height);
3371 }
3372 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3373 }
3374
Selim Cinek29603462015-11-17 19:04:39 -08003375 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003376 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3377 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3378 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3379 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003380 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003381 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003382 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003383 contentView.setProgressBackgroundTintList(
3384 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3385 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003386 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003387 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003388 contentView.setProgressTintList(R.id.progress, colorStateList);
3389 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003390 }
Selim Cinek29603462015-11-17 19:04:39 -08003391 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003392 } else {
3393 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003394 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003395 }
Joe Onorato561d3852010-11-20 18:09:34 -08003396 }
3397
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003398 private void bindLargeIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07003399 if (mN.mLargeIcon == null && mN.largeIcon != null) {
3400 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
3401 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003402 if (mN.mLargeIcon != null) {
3403 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3404 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3405 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003406 int endMargin = R.dimen.notification_content_picture_margin;
3407 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
3408 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
3409 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003410 }
3411 }
3412
Adrian Roos487374f2017-01-11 15:48:14 -08003413 private void bindNotificationHeader(RemoteViews contentView, boolean ambient) {
3414 bindSmallIcon(contentView, ambient);
3415 bindHeaderAppName(contentView, ambient);
3416 if (!ambient) {
3417 // Ambient view does not have these
3418 bindHeaderText(contentView);
3419 bindHeaderChronometerAndTime(contentView);
3420 bindExpandButton(contentView);
3421 bindProfileBadge(contentView);
3422 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003423 }
3424
3425 private void bindExpandButton(RemoteViews contentView) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003426 contentView.setDrawableParameters(R.id.expand_button, false, -1, resolveContrastColor(),
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003427 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003428 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003429 resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003430 }
3431
3432 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3433 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003434 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003435 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3436 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3437 contentView.setLong(R.id.chronometer, "setBase",
3438 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3439 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07003440 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07003441 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003442 } else {
3443 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3444 contentView.setLong(R.id.time, "setTime", mN.when);
3445 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003446 } else {
3447 // We still want a time to be set but gone, such that we can show and hide it
3448 // on demand in case it's a child notification without anything in the header
3449 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003450 }
3451 }
3452
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003453 private void bindHeaderText(RemoteViews contentView) {
3454 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3455 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05003456 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003457 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05003458 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003459 if (headerText == null
3460 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3461 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3462 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
3463 }
3464 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003465 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003466 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
3467 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
3468 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003469 }
3470 }
3471
Adrian Rooseba05822016-04-22 17:09:27 -07003472 /**
3473 * @hide
3474 */
3475 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04003476 CharSequence name = null;
3477 final PackageManager pm = mContext.getPackageManager();
3478 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
3479 // only system packages which lump together a bunch of unrelated stuff
3480 // may substitute a different name to make the purpose of the
3481 // notification more clear. the correct package label should always
3482 // be accessible via SystemUI.
3483 final String pkg = mContext.getPackageName();
3484 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
3485 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
3486 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
3487 name = subName;
3488 } else {
3489 Log.w(TAG, "warning: pkg "
3490 + pkg + " attempting to substitute app name '" + subName
3491 + "' without holding perm "
3492 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
3493 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003494 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04003495 if (TextUtils.isEmpty(name)) {
3496 name = pm.getApplicationLabel(mContext.getApplicationInfo());
3497 }
3498 if (TextUtils.isEmpty(name)) {
3499 // still nothing?
3500 return null;
3501 }
3502
3503 return String.valueOf(name);
3504 }
Adrian Roos487374f2017-01-11 15:48:14 -08003505 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04003506 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos487374f2017-01-11 15:48:14 -08003507 contentView.setTextColor(R.id.app_name_text,
3508 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003509 }
3510
Adrian Roos487374f2017-01-11 15:48:14 -08003511 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07003512 if (mN.mSmallIcon == null && mN.icon != 0) {
3513 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
3514 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003515 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Adrian Roos9b45a152016-06-28 13:32:29 -07003516 contentView.setDrawableParameters(R.id.icon, false /* targetBackground */,
3517 -1 /* alpha */, -1 /* colorFilter */, null /* mode */, mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08003518 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003519 }
3520
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003521 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003522 * @return true if the built notification will show the time or the chronometer; false
3523 * otherwise
3524 */
3525 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07003526 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003527 }
3528
Christoph Studerfe718432014-09-01 18:21:18 +02003529 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003530 // actions_container is only reset when there are no actions to avoid focus issues with
3531 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02003532 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003533 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003534
3535 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3536 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3537
3538 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3539 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3540 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3541 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07003542
3543 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003544 }
3545
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003546 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003547 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07003548 }
3549
Adrian Roos70d7aa32017-01-11 15:39:06 -08003550 private RemoteViews applyStandardTemplateWithActions(int layoutId,
3551 StandardTemplateParams p) {
3552 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003553
Christoph Studerfe718432014-09-01 18:21:18 +02003554 resetStandardTemplateWithActions(big);
3555
Adrian Roose458aa82015-12-08 16:17:19 -08003556 boolean validRemoteInput = false;
3557
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003558 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08003559 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003560 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003561 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08003562 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003563 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roos487374f2017-01-11 15:48:14 -08003564 if (p.ambient) {
3565 big.setInt(R.id.actions, "setBackgroundColor", Color.TRANSPARENT);
3566 }
Adrian Roosf852a422016-06-03 13:33:43 -07003567 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
3568 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003569 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003570 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003571 Action action = mActions.get(i);
3572 validRemoteInput |= hasValidRemoteInput(action);
3573
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003574 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08003575 i % 2 != 0, p.ambient);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003576 big.addView(R.id.actions, button);
3577 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003578 } else {
3579 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003580 }
Adrian Roose458aa82015-12-08 16:17:19 -08003581
3582 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08003583 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08003584 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3585 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3586 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
3587
3588 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3589 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3590 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
3591
3592 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3593 big.setViewVisibility(
3594 R.id.notification_material_reply_text_3, View.VISIBLE);
3595 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
3596 }
3597 }
3598 }
3599
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003600 return big;
3601 }
3602
Adrian Roose458aa82015-12-08 16:17:19 -08003603 private boolean hasValidRemoteInput(Action action) {
3604 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
3605 // Weird actions
3606 return false;
3607 }
3608
3609 RemoteInput[] remoteInputs = action.getRemoteInputs();
3610 if (remoteInputs == null) {
3611 return false;
3612 }
3613
3614 for (RemoteInput r : remoteInputs) {
3615 CharSequence[] choices = r.getChoices();
3616 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
3617 return true;
3618 }
3619 }
3620 return false;
3621 }
3622
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003623 /**
3624 * Construct a RemoteViews for the final 1U notification layout. In order:
3625 * 1. Custom contentView from the caller
3626 * 2. Style's proposed content view
3627 * 3. Standard template view
3628 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003629 public RemoteViews createContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003630 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003631 return mN.contentView;
3632 } else if (mStyle != null) {
3633 final RemoteViews styleView = mStyle.makeContentView();
3634 if (styleView != null) {
3635 return styleView;
3636 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003637 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003638 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003639 }
3640
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003641 /**
3642 * Construct a RemoteViews for the final big notification layout.
3643 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003644 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05003645 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08003646 if (mN.bigContentView != null
3647 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003648 return mN.bigContentView;
3649 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05003650 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08003651 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003652 } else if (mActions.size() != 0) {
3653 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05003654 }
3655 adaptNotificationHeaderForBigContentView(result);
3656 return result;
3657 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003658
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003659 /**
3660 * Construct a RemoteViews for the final notification header only
3661 *
3662 * @hide
3663 */
3664 public RemoteViews makeNotificationHeader() {
3665 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
3666 R.layout.notification_template_header);
3667 resetNotificationHeader(header);
Adrian Roos487374f2017-01-11 15:48:14 -08003668 bindNotificationHeader(header, false /* ambient */);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003669 return header;
3670 }
3671
Adrian Roos487374f2017-01-11 15:48:14 -08003672 /**
3673 * Construct a RemoteViews for the ambient version of the notification.
3674 *
3675 * @hide
3676 */
3677 public RemoteViews makeAmbientNotification() {
3678 RemoteViews ambient = applyStandardTemplateWithActions(
3679 R.layout.notification_template_material_ambient,
3680 mParams.reset().fillTextsFrom(this).hasProgress(false).ambient(true));
3681 return ambient;
3682 }
3683
Selim Cinek29603462015-11-17 19:04:39 -08003684 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003685 if (result != null) {
3686 result.setViewVisibility(R.id.text_line_1, View.GONE);
3687 }
Selim Cinek29603462015-11-17 19:04:39 -08003688 }
3689
Selim Cinek850a8542015-11-11 11:48:36 -05003690 private void adaptNotificationHeaderForBigContentView(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003691 if (result != null) {
3692 result.setBoolean(R.id.notification_header, "setExpanded", true);
3693 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003694 }
3695
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003696 /**
3697 * Construct a RemoteViews for the final heads-up notification layout.
3698 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003699 public RemoteViews createHeadsUpContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003700 if (mN.headsUpContentView != null
3701 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003702 return mN.headsUpContentView;
3703 } else if (mStyle != null) {
3704 final RemoteViews styleView = mStyle.makeHeadsUpContentView();
3705 if (styleView != null) {
3706 return styleView;
3707 }
3708 } else if (mActions.size() == 0) {
3709 return null;
3710 }
3711
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003712 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003713 }
3714
Selim Cinek624c02db2015-12-14 21:00:02 -08003715 /**
3716 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
3717 *
3718 * @hide
3719 */
3720 public RemoteViews makePublicContentView() {
3721 if (mN.publicVersion != null) {
3722 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Julia Reynolds3b848122016-02-26 10:45:32 -05003723 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08003724 }
3725 Bundle savedBundle = mN.extras;
3726 Style style = mStyle;
3727 mStyle = null;
3728 Icon largeIcon = mN.mLargeIcon;
3729 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07003730 Bitmap largeIconLegacy = mN.largeIcon;
3731 mN.largeIcon = null;
Selim Cinek624c02db2015-12-14 21:00:02 -08003732 Bundle publicExtras = new Bundle();
3733 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
3734 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
3735 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
3736 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07003737 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
3738 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08003739 publicExtras.putCharSequence(EXTRA_TITLE,
3740 mContext.getString(R.string.notification_hidden_text));
3741 mN.extras = publicExtras;
3742 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
3743 mN.extras = savedBundle;
3744 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07003745 mN.largeIcon = largeIconLegacy;
Selim Cinek624c02db2015-12-14 21:00:02 -08003746 mStyle = style;
3747 return publicView;
3748 }
3749
3750
Chris Wren8fd39ec2014-02-27 17:43:26 -05003751
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003752 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08003753 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003754 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07003755 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003756 emphazisedMode ? getEmphasizedActionLayoutResource()
3757 : tombstone ? getActionTombstoneLayoutResource()
3758 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04003759 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003760 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003761 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003762 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003763 if (action.mRemoteInputs != null) {
3764 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
3765 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003766 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07003767 // change the background bgColor
3768 int bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
3769 : R.color.notification_action_list_dark);
3770 button.setDrawableParameters(R.id.button_holder, true, -1, bgColor,
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003771 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinek981962e2016-07-20 20:41:58 -07003772 CharSequence title = action.title;
3773 ColorStateList[] outResultColor = null;
3774 if (isLegacy()) {
3775 title = clearColorSpans(title);
3776 } else {
3777 outResultColor = new ColorStateList[1];
3778 title = ensureColorSpanContrast(title, bgColor, outResultColor);
3779 }
3780 button.setTextViewText(R.id.action0, title);
3781 if (outResultColor != null && outResultColor[0] != null) {
3782 // We need to set the text color as well since changing a text to uppercase
3783 // clears its spans.
3784 button.setTextColor(R.id.action0, outResultColor[0]);
3785 } else if (mN.color != COLOR_DEFAULT) {
3786 button.setTextColor(R.id.action0,resolveContrastColor());
3787 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003788 } else {
Selim Cinek981962e2016-07-20 20:41:58 -07003789 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003790 if (mN.color != COLOR_DEFAULT) {
Adrian Roos487374f2017-01-11 15:48:14 -08003791 button.setTextColor(R.id.action0,
3792 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003793 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003794 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003795 return button;
3796 }
3797
Joe Onoratocb109a02011-01-18 17:57:41 -08003798 /**
Selim Cinek981962e2016-07-20 20:41:58 -07003799 * Clears all color spans of a text
3800 * @param charSequence the input text
3801 * @return the same text but without color spans
3802 */
3803 private CharSequence clearColorSpans(CharSequence charSequence) {
3804 if (charSequence instanceof Spanned) {
3805 Spanned ss = (Spanned) charSequence;
3806 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
3807 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
3808 for (Object span : spans) {
3809 Object resultSpan = span;
3810 if (resultSpan instanceof CharacterStyle) {
3811 resultSpan = ((CharacterStyle) span).getUnderlying();
3812 }
3813 if (resultSpan instanceof TextAppearanceSpan) {
3814 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
3815 if (originalSpan.getTextColor() != null) {
3816 resultSpan = new TextAppearanceSpan(
3817 originalSpan.getFamily(),
3818 originalSpan.getTextStyle(),
3819 originalSpan.getTextSize(),
3820 null,
3821 originalSpan.getLinkTextColor());
3822 }
3823 } else if (resultSpan instanceof ForegroundColorSpan
3824 || (resultSpan instanceof BackgroundColorSpan)) {
3825 continue;
3826 } else {
3827 resultSpan = span;
3828 }
3829 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
3830 ss.getSpanFlags(span));
3831 }
3832 return builder;
3833 }
3834 return charSequence;
3835 }
3836
3837 /**
3838 * Ensures contrast on color spans against a background color. also returns the color of the
3839 * text if a span was found that spans over the whole text.
3840 *
3841 * @param charSequence the charSequence on which the spans are
3842 * @param background the background color to ensure the contrast against
3843 * @param outResultColor an array in which a color will be returned as the first element if
3844 * there exists a full length color span.
3845 * @return the contrasted charSequence
3846 */
3847 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
3848 ColorStateList[] outResultColor) {
3849 if (charSequence instanceof Spanned) {
3850 Spanned ss = (Spanned) charSequence;
3851 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
3852 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
3853 for (Object span : spans) {
3854 Object resultSpan = span;
3855 int spanStart = ss.getSpanStart(span);
3856 int spanEnd = ss.getSpanEnd(span);
3857 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
3858 if (resultSpan instanceof CharacterStyle) {
3859 resultSpan = ((CharacterStyle) span).getUnderlying();
3860 }
3861 if (resultSpan instanceof TextAppearanceSpan) {
3862 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
3863 ColorStateList textColor = originalSpan.getTextColor();
3864 if (textColor != null) {
3865 int[] colors = textColor.getColors();
3866 int[] newColors = new int[colors.length];
3867 for (int i = 0; i < newColors.length; i++) {
3868 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
3869 colors[i], background);
3870 }
3871 textColor = new ColorStateList(textColor.getStates().clone(),
3872 newColors);
3873 resultSpan = new TextAppearanceSpan(
3874 originalSpan.getFamily(),
3875 originalSpan.getTextStyle(),
3876 originalSpan.getTextSize(),
3877 textColor,
3878 originalSpan.getLinkTextColor());
3879 if (fullLength) {
3880 outResultColor[0] = new ColorStateList(
3881 textColor.getStates().clone(), newColors);
3882 }
3883 }
3884 } else if (resultSpan instanceof ForegroundColorSpan) {
3885 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
3886 int foregroundColor = originalSpan.getForegroundColor();
3887 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
3888 foregroundColor, background);
3889 resultSpan = new ForegroundColorSpan(foregroundColor);
3890 if (fullLength) {
3891 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
3892 }
3893 } else {
3894 resultSpan = span;
3895 }
3896
3897 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
3898 }
3899 return builder;
3900 }
3901 return charSequence;
3902 }
3903
3904 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003905 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003906 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003907 */
3908 private boolean isLegacy() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003909 return getColorUtil() != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003910 }
3911
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003912 private CharSequence processLegacyText(CharSequence charSequence) {
3913 if (isLegacy()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003914 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003915 } else {
3916 return charSequence;
3917 }
3918 }
3919
Dan Sandler26e81cf2014-05-06 10:01:27 -04003920 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003921 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04003922 */
Adrian Roos487374f2017-01-11 15:48:14 -08003923 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
3924 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08003925 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Adrian Roos487374f2017-01-11 15:48:14 -08003926 int color = ambient ? resolveAmbientColor() : resolveContrastColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08003927 if (colorable) {
Adrian Roos487374f2017-01-11 15:48:14 -08003928 contentView.setDrawableParameters(R.id.icon, false, -1, color,
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003929 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003930
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003931 }
Selim Cinekea4bef72015-12-02 15:51:10 -08003932 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08003933 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003934 }
3935
Dan Sandler26e81cf2014-05-06 10:01:27 -04003936 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003937 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04003938 * if it's grayscale).
3939 */
3940 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003941 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3942 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003943 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003944 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08003945 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003946 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003947 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003948 }
3949
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003950 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003951 if (mN.color != COLOR_DEFAULT) {
3952 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02003953 }
Jorim Jaggi74419312014-06-10 20:57:21 +02003954 }
3955
Adrian Roos4ff3b122016-02-01 12:26:13 -08003956 int resolveContrastColor() {
3957 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
3958 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003959 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08003960 final int contrasted = NotificationColorUtil.resolveContrastColor(mContext, mN.color);
3961
3962 mCachedContrastColorIsFor = mN.color;
3963 return mCachedContrastColor = contrasted;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003964 }
3965
Adrian Roos487374f2017-01-11 15:48:14 -08003966 int resolveAmbientColor() {
3967 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
3968 return mCachedAmbientColor;
3969 }
3970 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
3971
3972 mCachedAmbientColorIsFor = mN.color;
3973 return mCachedAmbientColor = contrasted;
3974 }
3975
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003976 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003977 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003978 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003979 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003980 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003981 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003982 mN.actions = new Action[mActions.size()];
3983 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003984 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003985 if (!mPersonList.isEmpty()) {
3986 mN.extras.putStringArray(EXTRA_PEOPLE,
3987 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003988 }
Selim Cinek247fa012016-02-18 09:50:48 -08003989 if (mN.bigContentView != null || mN.contentView != null
3990 || mN.headsUpContentView != null) {
3991 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
3992 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003993 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08003994 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003995
Julia Reynolds3b848122016-02-26 10:45:32 -05003996 /**
3997 * Creates a Builder from an existing notification so further changes can be made.
3998 * @param context The context for your application / activity.
3999 * @param n The notification to create a Builder from.
4000 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004001 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004002 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004003 ApplicationInfo applicationInfo = n.extras.getParcelable(
4004 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004005 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05004006 if (applicationInfo != null) {
4007 try {
4008 builderContext = context.createApplicationContext(applicationInfo,
4009 Context.CONTEXT_RESTRICTED);
4010 } catch (NameNotFoundException e) {
4011 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
4012 builderContext = context; // try with our context
4013 }
4014 } else {
4015 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02004016 }
4017
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004018 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004019 }
4020
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004021 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004022 * @deprecated Use {@link #build()} instead.
4023 */
4024 @Deprecated
4025 public Notification getNotification() {
4026 return build();
4027 }
4028
4029 /**
4030 * Combine all of the options that have been set and return a new {@link Notification}
4031 * object.
4032 */
4033 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004034 // first, add any extras from the calling code
4035 if (mUserExtras != null) {
4036 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004037 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004038
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004039 mN.creationTime = System.currentTimeMillis();
4040
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004041 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05004042 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02004043
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004044 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004045
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004046 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004047 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004048 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004049
Adrian Roos5081c0d2016-02-26 16:04:19 -08004050 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4051 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004052 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004053 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004054 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
4055 mN.contentView.getSequenceNumber());
4056 }
4057 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004058 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004059 if (mN.bigContentView != null) {
4060 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
4061 mN.bigContentView.getSequenceNumber());
4062 }
4063 }
4064 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004065 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004066 if (mN.headsUpContentView != null) {
4067 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
4068 mN.headsUpContentView.getSequenceNumber());
4069 }
4070 }
4071 }
4072
Julia Reynolds4c0c2022016-02-02 15:11:59 -05004073 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
4074 mN.flags |= FLAG_SHOW_LIGHTS;
4075 }
4076
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004077 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004078 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004079
4080 /**
4081 * Apply this Builder to an existing {@link Notification} object.
4082 *
4083 * @hide
4084 */
4085 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04004086 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004087 return n;
4088 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004089
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004090 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08004091 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
4092 * change.
4093 *
4094 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
4095 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004096 * @hide
4097 */
Adrian Roos184bfe022016-03-03 13:41:44 -08004098 public static Notification maybeCloneStrippedForDelivery(Notification n) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004099 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08004100
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004101 // Only strip views for known Styles because we won't know how to
4102 // re-create them otherwise.
Adrian Roos184bfe022016-03-03 13:41:44 -08004103 if (!TextUtils.isEmpty(templateClass)
4104 && getNotificationStyleClass(templateClass) == null) {
4105 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004106 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004107
4108 // Only strip unmodified BuilderRemoteViews.
4109 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004110 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004111 n.contentView.getSequenceNumber();
4112 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004113 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004114 n.bigContentView.getSequenceNumber();
4115 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004116 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004117 n.headsUpContentView.getSequenceNumber();
4118
4119 // Nothing to do here, no need to clone.
4120 if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
4121 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004122 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004123
4124 Notification clone = n.clone();
4125 if (stripContentView) {
4126 clone.contentView = null;
4127 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
4128 }
4129 if (stripBigContentView) {
4130 clone.bigContentView = null;
4131 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
4132 }
4133 if (stripHeadsUpContentView) {
4134 clone.headsUpContentView = null;
4135 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
4136 }
4137 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004138 }
4139
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004140 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004141 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004142 }
4143
4144 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004145 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004146 }
4147
4148 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004149 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004150 }
4151
4152 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004153 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004154 }
4155
4156 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004157 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004158 }
4159
Adrian Roosc1a80b02016-04-05 14:54:55 -07004160 private int getMessagingLayoutResource() {
4161 return R.layout.notification_template_material_messaging;
4162 }
4163
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004164 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004165 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004166 }
4167
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004168 private int getEmphasizedActionLayoutResource() {
4169 return R.layout.notification_material_action_emphasized;
4170 }
4171
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004172 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004173 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004174 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004175 }
4176
Selim Cinek279fa862016-06-14 10:57:25 -07004177 private boolean hasLargeIcon() {
4178 return mLargeIcon != null || largeIcon != null;
4179 }
4180
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004181 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004182 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004183 * @hide
4184 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07004185 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004186 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
4187 }
4188
4189 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004190 * @return true if the notification will show a chronometer; false otherwise
4191 * @hide
4192 */
4193 public boolean showsChronometer() {
4194 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
4195 }
4196
4197 /**
Julia Reynolds4a02afb2016-12-13 13:39:52 -05004198 * @hide
4199 */
4200 @SystemApi
4201 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
4202 Class<? extends Style>[] classes = new Class[] {
4203 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
4204 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
4205 MessagingStyle.class };
4206 for (Class<? extends Style> innerClass : classes) {
4207 if (templateClass.equals(innerClass.getName())) {
4208 return innerClass;
4209 }
4210 }
4211 return null;
4212 }
4213
4214 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004215 * An object that can apply a rich notification style to a {@link Notification.Builder}
4216 * object.
4217 */
Griff Hazendfcb0802014-02-11 12:00:00 -08004218 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04004219 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004220
4221 /**
4222 * @hide
4223 */
4224 protected CharSequence mSummaryText = null;
4225
4226 /**
4227 * @hide
4228 */
4229 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04004230
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004231 protected Builder mBuilder;
4232
Chris Wrend6297db2012-05-03 16:20:13 -04004233 /**
4234 * Overrides ContentTitle in the big form of the template.
4235 * This defaults to the value passed to setContentTitle().
4236 */
4237 protected void internalSetBigContentTitle(CharSequence title) {
4238 mBigContentTitle = title;
4239 }
4240
4241 /**
4242 * Set the first line of text after the detail section in the big form of the template.
4243 */
4244 protected void internalSetSummaryText(CharSequence cs) {
4245 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04004246 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04004247 }
4248
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004249 public void setBuilder(Builder builder) {
4250 if (mBuilder != builder) {
4251 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004252 if (mBuilder != null) {
4253 mBuilder.setStyle(this);
4254 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004255 }
4256 }
4257
Chris Wrend6297db2012-05-03 16:20:13 -04004258 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004259 if (mBuilder == null) {
4260 throw new IllegalArgumentException("Style requires a valid Builder object");
4261 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004262 }
Chris Wrend6297db2012-05-03 16:20:13 -04004263
4264 protected RemoteViews getStandardView(int layoutId) {
4265 checkBuilder();
4266
Christoph Studer4600f9b2014-07-22 22:44:43 +02004267 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004268 CharSequence oldBuilderContentTitle =
4269 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004270 if (mBigContentTitle != null) {
4271 mBuilder.setContentTitle(mBigContentTitle);
4272 }
4273
Chris Wrend6297db2012-05-03 16:20:13 -04004274 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
4275
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004276 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004277
Chris Wrend6297db2012-05-03 16:20:13 -04004278 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
4279 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04004280 } else {
4281 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004282 }
4283
Chris Wrend6297db2012-05-03 16:20:13 -04004284 return contentView;
4285 }
4286
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004287 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004288 * Construct a Style-specific RemoteViews for the final 1U notification layout.
4289 * The default implementation has nothing additional to add.
4290 * @hide
4291 */
4292 public RemoteViews makeContentView() {
4293 return null;
4294 }
4295
4296 /**
4297 * Construct a Style-specific RemoteViews for the final big notification layout.
4298 * @hide
4299 */
4300 public RemoteViews makeBigContentView() {
4301 return null;
4302 }
4303
4304 /**
4305 * Construct a Style-specific RemoteViews for the final HUN layout.
4306 * @hide
4307 */
4308 public RemoteViews makeHeadsUpContentView() {
4309 return null;
4310 }
4311
4312 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004313 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004314 * @hide
4315 */
4316 public void addExtras(Bundle extras) {
4317 if (mSummaryTextSet) {
4318 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
4319 }
4320 if (mBigContentTitle != null) {
4321 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
4322 }
Chris Wren91ad5632013-06-05 15:05:57 -04004323 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004324 }
4325
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004326 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004327 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004328 * @hide
4329 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02004330 protected void restoreFromExtras(Bundle extras) {
4331 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
4332 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
4333 mSummaryTextSet = true;
4334 }
4335 if (extras.containsKey(EXTRA_TITLE_BIG)) {
4336 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
4337 }
4338 }
4339
4340
4341 /**
4342 * @hide
4343 */
4344 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004345 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004346 return wip;
4347 }
4348
Daniel Sandler0ec46202015-06-24 01:27:05 -04004349 /**
4350 * @hide
4351 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004352 public void purgeResources() {}
4353
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004354 /**
4355 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
4356 * attached to.
4357 *
4358 * @return the fully constructed Notification.
4359 */
4360 public Notification build() {
4361 checkBuilder();
4362 return mBuilder.build();
4363 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004364
4365 /**
4366 * @hide
4367 * @return true if the style positions the progress bar on the second line; false if the
4368 * style hides the progress bar
4369 */
4370 protected boolean hasProgress() {
4371 return true;
4372 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004373
4374 /**
4375 * @hide
4376 * @return Whether we should put the summary be put into the notification header
4377 */
4378 public boolean hasSummaryInHeader() {
4379 return true;
4380 }
Selim Cinek593610c2016-02-16 18:42:57 -08004381
4382 /**
4383 * @hide
4384 * @return Whether custom content views are displayed inline in the style
4385 */
4386 public boolean displayCustomViewInline() {
4387 return false;
4388 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004389 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004390
4391 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004392 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08004393 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004394 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004395 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004396 * Notification notif = new Notification.Builder(mContext)
4397 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
4398 * .setContentText(subject)
4399 * .setSmallIcon(R.drawable.new_post)
4400 * .setLargeIcon(aBitmap)
4401 * .setStyle(new Notification.BigPictureStyle()
4402 * .bigPicture(aBigBitmap))
4403 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004404 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004405 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004406 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004407 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004408 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004409 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004410 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004411 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004412
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004413 public BigPictureStyle() {
4414 }
4415
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004416 /**
4417 * @deprecated use {@code BigPictureStyle()}.
4418 */
4419 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004420 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004421 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004422 }
4423
Chris Wrend6297db2012-05-03 16:20:13 -04004424 /**
4425 * Overrides ContentTitle in the big form of the template.
4426 * This defaults to the value passed to setContentTitle().
4427 */
4428 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004429 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004430 return this;
4431 }
4432
4433 /**
4434 * Set the first line of text after the detail section in the big form of the template.
4435 */
4436 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004437 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004438 return this;
4439 }
4440
Chris Wren0bd664d2012-08-01 13:56:56 -04004441 /**
4442 * Provide the bitmap to be used as the payload for the BigPicture notification.
4443 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004444 public BigPictureStyle bigPicture(Bitmap b) {
4445 mPicture = b;
4446 return this;
4447 }
4448
Chris Wren3745a3d2012-05-22 15:11:52 -04004449 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04004450 * Override the large icon when the big notification is shown.
4451 */
4452 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04004453 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
4454 }
4455
4456 /**
4457 * Override the large icon when the big notification is shown.
4458 */
4459 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04004460 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004461 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004462 return this;
4463 }
4464
Riley Andrews0394a0c2015-11-03 23:36:52 -08004465 /** @hide */
4466 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
4467
Daniel Sandler0ec46202015-06-24 01:27:05 -04004468 /**
4469 * @hide
4470 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004471 @Override
4472 public void purgeResources() {
4473 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08004474 if (mPicture != null &&
4475 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08004476 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004477 mPicture = mPicture.createAshmemBitmap();
4478 }
4479 if (mBigLargeIcon != null) {
4480 mBigLargeIcon.convertToAshmem();
4481 }
4482 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01004483
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004484 /**
4485 * @hide
4486 */
4487 public RemoteViews makeBigContentView() {
4488 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01004489 // This covers the following cases:
4490 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004491 // mN.mLargeIcon
4492 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04004493 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07004494 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004495 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004496 oldLargeIcon = mBuilder.mN.mLargeIcon;
4497 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07004498 // The legacy largeIcon might not allow us to clear the image, as it's taken in
4499 // replacement if the other one is null. Because we're restoring these legacy icons
4500 // for old listeners, this is in general non-null.
4501 largeIconLegacy = mBuilder.mN.largeIcon;
4502 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004503 }
4504
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004505 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05004506 if (mSummaryTextSet) {
4507 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinekc848c3a2016-01-13 15:27:30 -08004508 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05004509 }
Selim Cinek279fa862016-06-14 10:57:25 -07004510 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08004511
Christoph Studer5c510ee2014-12-15 16:32:27 +01004512 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004513 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07004514 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004515 }
4516
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004517 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004518 return contentView;
4519 }
4520
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004521 /**
4522 * @hide
4523 */
4524 public void addExtras(Bundle extras) {
4525 super.addExtras(extras);
4526
4527 if (mBigLargeIconSet) {
4528 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
4529 }
4530 extras.putParcelable(EXTRA_PICTURE, mPicture);
4531 }
4532
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004533 /**
4534 * @hide
4535 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004536 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004537 protected void restoreFromExtras(Bundle extras) {
4538 super.restoreFromExtras(extras);
4539
4540 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01004541 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02004542 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04004543 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02004544 mPicture = extras.getParcelable(EXTRA_PICTURE);
4545 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004546
4547 /**
4548 * @hide
4549 */
4550 @Override
4551 public boolean hasSummaryInHeader() {
4552 return false;
4553 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004554 }
4555
4556 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004557 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08004558 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004559 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004560 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004561 * Notification notif = new Notification.Builder(mContext)
4562 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4563 * .setContentText(subject)
4564 * .setSmallIcon(R.drawable.new_mail)
4565 * .setLargeIcon(aBitmap)
4566 * .setStyle(new Notification.BigTextStyle()
4567 * .bigText(aVeryLongString))
4568 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004569 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004570 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004571 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004572 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004573 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004574
4575 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004576 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004577
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004578 private CharSequence mBigText;
4579
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004580 public BigTextStyle() {
4581 }
4582
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004583 /**
4584 * @deprecated use {@code BigTextStyle()}.
4585 */
4586 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004587 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004588 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004589 }
4590
Chris Wrend6297db2012-05-03 16:20:13 -04004591 /**
4592 * Overrides ContentTitle in the big form of the template.
4593 * This defaults to the value passed to setContentTitle().
4594 */
4595 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004596 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004597 return this;
4598 }
4599
4600 /**
4601 * Set the first line of text after the detail section in the big form of the template.
4602 */
4603 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004604 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004605 return this;
4606 }
4607
Chris Wren0bd664d2012-08-01 13:56:56 -04004608 /**
4609 * Provide the longer text to be displayed in the big form of the
4610 * template in place of the content text.
4611 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004612 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004613 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004614 return this;
4615 }
4616
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004617 /**
4618 * @hide
4619 */
4620 public void addExtras(Bundle extras) {
4621 super.addExtras(extras);
4622
Christoph Studer4600f9b2014-07-22 22:44:43 +02004623 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4624 }
4625
4626 /**
4627 * @hide
4628 */
4629 @Override
4630 protected void restoreFromExtras(Bundle extras) {
4631 super.restoreFromExtras(extras);
4632
4633 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004634 }
4635
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004636 /**
4637 * @hide
4638 */
4639 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004640
4641 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07004642 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004643 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004644
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004645 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004646
Selim Cinek75998782016-04-26 10:39:17 -07004647 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004648
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004649 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07004650 if (TextUtils.isEmpty(bigTextText)) {
4651 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
4652 // experience
4653 bigTextText = mBuilder.processLegacyText(text);
4654 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07004655 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08004656
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004657 return contentView;
4658 }
4659
Adrian Roosb1f427c2016-05-26 12:27:15 -07004660 static void applyBigTextContentView(Builder builder,
4661 RemoteViews contentView, CharSequence bigTextText) {
4662 contentView.setTextViewText(R.id.big_text, bigTextText);
4663 contentView.setViewVisibility(R.id.big_text,
4664 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
4665 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines(builder));
Selim Cinek279fa862016-06-14 10:57:25 -07004666 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07004667 }
4668
4669 private static int calculateMaxLines(Builder builder) {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004670 int lineCount = MAX_LINES;
Adrian Roosb1f427c2016-05-26 12:27:15 -07004671 boolean hasActions = builder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004672 if (hasActions) {
4673 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4674 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004675 return lineCount;
4676 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004677 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004678
4679 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004680 * Helper class for generating large-format notifications that include multiple back-and-forth
4681 * messages of varying types between any number of people.
4682 *
4683 * <br>
4684 * If the platform does not provide large-format notifications, this method has no effect. The
4685 * user will always see the normal notification view.
4686 * <br>
4687 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
4688 * so:
4689 * <pre class="prettyprint">
4690 *
4691 * Notification noti = new Notification.Builder()
4692 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
4693 * .setContentText(subject)
4694 * .setSmallIcon(R.drawable.new_message)
4695 * .setLargeIcon(aBitmap)
4696 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
4697 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
4698 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
4699 * .build();
4700 * </pre>
4701 */
4702 public static class MessagingStyle extends Style {
4703
4704 /**
4705 * The maximum number of messages that will be retained in the Notification itself (the
4706 * number displayed is up to the platform).
4707 */
4708 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
4709
4710 CharSequence mUserDisplayName;
4711 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04004712 List<Message> mMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04004713
4714 MessagingStyle() {
4715 }
4716
4717 /**
Alex Hillsfd590442016-10-07 09:52:44 -04004718 * @param userDisplayName Required - the name to be displayed for any replies sent by the
4719 * user before the posting app reposts the notification with those messages after they've
4720 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04004721 * {@link #addMessage(Notification.MessagingStyle.Message)}
4722 */
Alex Hillsfd590442016-10-07 09:52:44 -04004723 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Alex Hillsfc737de2016-03-23 17:33:02 -04004724 mUserDisplayName = userDisplayName;
4725 }
4726
4727 /**
4728 * Returns the name to be displayed for any replies sent by the user
4729 */
4730 public CharSequence getUserDisplayName() {
4731 return mUserDisplayName;
4732 }
4733
4734 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004735 * Sets the title to be displayed on this conversation. This should only be used for
4736 * group messaging and left unset for one-on-one conversations.
4737 * @param conversationTitle
4738 * @return this object for method chaining.
4739 */
4740 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
4741 mConversationTitle = conversationTitle;
4742 return this;
4743 }
4744
4745 /**
4746 * Return the title to be displayed on this conversation. Can be <code>null</code> and
4747 * should be for one-on-one conversations
4748 */
4749 public CharSequence getConversationTitle() {
4750 return mConversationTitle;
4751 }
4752
4753 /**
4754 * Adds a message for display by this notification. Convenience call for a simple
4755 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
4756 * @param text A {@link CharSequence} to be displayed as the message content
4757 * @param timestamp Time at which the message arrived
4758 * @param sender A {@link CharSequence} to be used for displaying the name of the
4759 * sender. Should be <code>null</code> for messages by the current user, in which case
4760 * the platform will insert {@link #getUserDisplayName()}.
4761 * Should be unique amongst all individuals in the conversation, and should be
4762 * consistent during re-posts of the notification.
4763 *
4764 * @see Message#Message(CharSequence, long, CharSequence)
4765 *
4766 * @return this object for method chaining
4767 */
4768 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
4769 mMessages.add(new Message(text, timestamp, sender));
4770 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4771 mMessages.remove(0);
4772 }
4773 return this;
4774 }
4775
4776 /**
4777 * Adds a {@link Message} for display in this notification.
4778 * @param message The {@link Message} to be displayed
4779 * @return this object for method chaining
4780 */
4781 public MessagingStyle addMessage(Message message) {
4782 mMessages.add(message);
4783 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4784 mMessages.remove(0);
4785 }
4786 return this;
4787 }
4788
4789 /**
4790 * Gets the list of {@code Message} objects that represent the notification
4791 */
4792 public List<Message> getMessages() {
4793 return mMessages;
4794 }
4795
4796 /**
4797 * @hide
4798 */
4799 @Override
4800 public void addExtras(Bundle extras) {
4801 super.addExtras(extras);
4802 if (mUserDisplayName != null) {
4803 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
4804 }
4805 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004806 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04004807 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004808 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
4809 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04004810 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07004811
4812 fixTitleAndTextExtras(extras);
4813 }
4814
4815 private void fixTitleAndTextExtras(Bundle extras) {
4816 Message m = findLatestIncomingMessage();
4817 CharSequence text = (m == null) ? null : m.mText;
4818 CharSequence sender = m == null ? null
4819 : TextUtils.isEmpty(m.mSender) ? mUserDisplayName : m.mSender;
4820 CharSequence title;
4821 if (!TextUtils.isEmpty(mConversationTitle)) {
4822 if (!TextUtils.isEmpty(sender)) {
4823 BidiFormatter bidi = BidiFormatter.getInstance();
4824 title = mBuilder.mContext.getString(
4825 com.android.internal.R.string.notification_messaging_title_template,
4826 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(m.mSender));
4827 } else {
4828 title = mConversationTitle;
4829 }
4830 } else {
4831 title = sender;
4832 }
4833
4834 if (title != null) {
4835 extras.putCharSequence(EXTRA_TITLE, title);
4836 }
4837 if (text != null) {
4838 extras.putCharSequence(EXTRA_TEXT, text);
4839 }
Alex Hillsfc737de2016-03-23 17:33:02 -04004840 }
4841
4842 /**
4843 * @hide
4844 */
4845 @Override
4846 protected void restoreFromExtras(Bundle extras) {
4847 super.restoreFromExtras(extras);
4848
4849 mMessages.clear();
Adrian Roos96b7e202016-05-17 13:50:38 -07004850 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
4851 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Alex Hillsd9b04d92016-04-11 16:38:16 -04004852 Parcelable[] parcelables = extras.getParcelableArray(EXTRA_MESSAGES);
Adrian Roosdedd1df2016-04-26 16:38:47 -07004853 if (parcelables != null && parcelables instanceof Parcelable[]) {
4854 mMessages = Message.getMessagesFromBundleArray(parcelables);
Alex Hillsfc737de2016-03-23 17:33:02 -04004855 }
4856 }
4857
4858 /**
4859 * @hide
4860 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07004861 @Override
4862 public RemoteViews makeContentView() {
4863 Message m = findLatestIncomingMessage();
4864 CharSequence title = mConversationTitle != null
4865 ? mConversationTitle
4866 : (m == null) ? null : m.mSender;
4867 CharSequence text = (m == null)
4868 ? null
4869 : mConversationTitle != null ? makeMessageLine(m) : m.mText;
Alex Hillsfc737de2016-03-23 17:33:02 -04004870
Adrian Roos70d7aa32017-01-11 15:39:06 -08004871 return mBuilder.applyStandardTemplateWithActions(mBuilder.getBaseLayoutResource(),
4872 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
Adrian Roosc1a80b02016-04-05 14:54:55 -07004873 }
4874
4875 private Message findLatestIncomingMessage() {
4876 for (int i = mMessages.size() - 1; i >= 0; i--) {
4877 Message m = mMessages.get(i);
4878 // Incoming messages have a non-empty sender.
4879 if (!TextUtils.isEmpty(m.mSender)) {
4880 return m;
4881 }
4882 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07004883 if (!mMessages.isEmpty()) {
4884 // No incoming messages, fall back to outgoing message
4885 return mMessages.get(mMessages.size() - 1);
4886 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07004887 return null;
4888 }
4889
4890 /**
4891 * @hide
4892 */
4893 @Override
4894 public RemoteViews makeBigContentView() {
4895 CharSequence title = !TextUtils.isEmpty(super.mBigContentTitle)
4896 ? super.mBigContentTitle
4897 : mConversationTitle;
4898 boolean hasTitle = !TextUtils.isEmpty(title);
4899
Adrian Roosfeafa052016-06-01 17:09:45 -07004900 if (mMessages.size() == 1) {
4901 // Special case for a single message: Use the big text style
4902 // so the collapsed and expanded versions match nicely.
4903 CharSequence bigTitle;
4904 CharSequence text;
4905 if (hasTitle) {
4906 bigTitle = title;
4907 text = makeMessageLine(mMessages.get(0));
4908 } else {
4909 bigTitle = mMessages.get(0).mSender;
4910 text = mMessages.get(0).mText;
4911 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07004912 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
4913 mBuilder.getBigTextLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08004914 mBuilder.mParams.reset().hasProgress(false).title(bigTitle).text(null));
Adrian Roosb1f427c2016-05-26 12:27:15 -07004915 BigTextStyle.applyBigTextContentView(mBuilder, contentView, text);
4916 return contentView;
4917 }
4918
Adrian Roos48d746a2016-04-12 14:57:28 -07004919 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07004920 mBuilder.getMessagingLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08004921 mBuilder.mParams.reset().hasProgress(false).title(title).text(null));
Adrian Roosc1a80b02016-04-05 14:54:55 -07004922
4923 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
4924 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
4925
4926 // Make sure all rows are gone in case we reuse a view.
4927 for (int rowId : rowIds) {
4928 contentView.setViewVisibility(rowId, View.GONE);
4929 }
4930
4931 int i=0;
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004932 contentView.setViewLayoutMarginBottomDimen(R.id.line1,
4933 hasTitle ? R.dimen.notification_messaging_spacing : 0);
Adrian Roosc1a80b02016-04-05 14:54:55 -07004934 contentView.setInt(R.id.notification_messaging, "setNumIndentLines",
Selim Cinek279fa862016-06-14 10:57:25 -07004935 !mBuilder.mN.hasLargeIcon() ? 0 : (hasTitle ? 1 : 2));
Adrian Roosc1a80b02016-04-05 14:54:55 -07004936
Adrian Roosfeafa052016-06-01 17:09:45 -07004937 int contractedChildId = View.NO_ID;
4938 Message contractedMessage = findLatestIncomingMessage();
Adrian Roosc1a80b02016-04-05 14:54:55 -07004939 int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
4940 while (firstMessage + i < mMessages.size() && i < rowIds.length) {
4941 Message m = mMessages.get(firstMessage + i);
4942 int rowId = rowIds[i];
4943
4944 contentView.setViewVisibility(rowId, View.VISIBLE);
4945 contentView.setTextViewText(rowId, makeMessageLine(m));
4946
Adrian Roosfeafa052016-06-01 17:09:45 -07004947 if (contractedMessage == m) {
4948 contractedChildId = rowId;
4949 }
4950
Adrian Roosc1a80b02016-04-05 14:54:55 -07004951 i++;
4952 }
Adrian Roosfeafa052016-06-01 17:09:45 -07004953 // Record this here to allow transformation between the contracted and expanded views.
4954 contentView.setInt(R.id.notification_messaging, "setContractedChildId",
4955 contractedChildId);
Alex Hillsfc737de2016-03-23 17:33:02 -04004956 return contentView;
4957 }
4958
Adrian Roosc1a80b02016-04-05 14:54:55 -07004959 private CharSequence makeMessageLine(Message m) {
4960 BidiFormatter bidi = BidiFormatter.getInstance();
4961 SpannableStringBuilder sb = new SpannableStringBuilder();
4962 if (TextUtils.isEmpty(m.mSender)) {
4963 CharSequence replyName = mUserDisplayName == null ? "" : mUserDisplayName;
4964 sb.append(bidi.unicodeWrap(replyName),
4965 makeFontColorSpan(mBuilder.resolveContrastColor()),
4966 0 /* flags */);
4967 } else {
4968 sb.append(bidi.unicodeWrap(m.mSender),
4969 makeFontColorSpan(Color.BLACK),
4970 0 /* flags */);
4971 }
4972 CharSequence text = m.mText == null ? "" : m.mText;
4973 sb.append(" ").append(bidi.unicodeWrap(text));
4974 return sb;
4975 }
4976
Adrian Roosdedd1df2016-04-26 16:38:47 -07004977 /**
4978 * @hide
4979 */
4980 @Override
4981 public RemoteViews makeHeadsUpContentView() {
4982 Message m = findLatestIncomingMessage();
4983 CharSequence title = mConversationTitle != null
4984 ? mConversationTitle
4985 : (m == null) ? null : m.mSender;
4986 CharSequence text = (m == null)
4987 ? null
4988 : mConversationTitle != null ? makeMessageLine(m) : m.mText;
4989
4990 return mBuilder.applyStandardTemplateWithActions(mBuilder.getBigBaseLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08004991 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
Adrian Roosdedd1df2016-04-26 16:38:47 -07004992 }
4993
Adrian Roosc1a80b02016-04-05 14:54:55 -07004994 private static TextAppearanceSpan makeFontColorSpan(int color) {
4995 return new TextAppearanceSpan(null, 0, 0,
4996 ColorStateList.valueOf(color), null);
4997 }
4998
Alex Hillsd9b04d92016-04-11 16:38:16 -04004999 public static final class Message {
5000
5001 static final String KEY_TEXT = "text";
5002 static final String KEY_TIMESTAMP = "time";
5003 static final String KEY_SENDER = "sender";
5004 static final String KEY_DATA_MIME_TYPE = "type";
5005 static final String KEY_DATA_URI= "uri";
Alex Hillsfc737de2016-03-23 17:33:02 -04005006
5007 private final CharSequence mText;
5008 private final long mTimestamp;
5009 private final CharSequence mSender;
5010
5011 private String mDataMimeType;
5012 private Uri mDataUri;
5013
5014 /**
5015 * Constructor
5016 * @param text A {@link CharSequence} to be displayed as the message content
5017 * @param timestamp Time at which the message arrived
5018 * @param sender A {@link CharSequence} to be used for displaying the name of the
5019 * sender. Should be <code>null</code> for messages by the current user, in which case
5020 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
5021 * Should be unique amongst all individuals in the conversation, and should be
5022 * consistent during re-posts of the notification.
5023 */
5024 public Message(CharSequence text, long timestamp, CharSequence sender){
5025 mText = text;
5026 mTimestamp = timestamp;
5027 mSender = sender;
5028 }
5029
5030 /**
5031 * Sets a binary blob of data and an associated MIME type for a message. In the case
5032 * where the platform doesn't support the MIME type, the original text provided in the
5033 * constructor will be used.
5034 * @param dataMimeType The MIME type of the content. See
5035 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
5036 * types on Android and Android Wear.
5037 * @param dataUri The uri containing the content whose type is given by the MIME type.
5038 * <p class="note">
5039 * <ol>
5040 * <li>Notification Listeners including the System UI need permission to access the
5041 * data the Uri points to. The recommended ways to do this are:</li>
5042 * <li>Store the data in your own ContentProvider, making sure that other apps have
5043 * the correct permission to access your provider. The preferred mechanism for
5044 * providing access is to use per-URI permissions which are temporary and only
5045 * grant access to the receiving application. An easy way to create a
5046 * ContentProvider like this is to use the FileProvider helper class.</li>
5047 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
5048 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
5049 * also store non-media types (see MediaStore.Files for more info). Files can be
5050 * inserted into the MediaStore using scanFile() after which a content:// style
5051 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
5052 * Note that once added to the system MediaStore the content is accessible to any
5053 * app on the device.</li>
5054 * </ol>
5055 * @return this object for method chaining
5056 */
5057 public Message setData(String dataMimeType, Uri dataUri) {
5058 mDataMimeType = dataMimeType;
5059 mDataUri = dataUri;
5060 return this;
5061 }
5062
Alex Hillsfc737de2016-03-23 17:33:02 -04005063 /**
5064 * Get the text to be used for this message, or the fallback text if a type and content
5065 * Uri have been set
5066 */
5067 public CharSequence getText() {
5068 return mText;
5069 }
5070
5071 /**
5072 * Get the time at which this message arrived
5073 */
5074 public long getTimestamp() {
5075 return mTimestamp;
5076 }
5077
5078 /**
5079 * Get the text used to display the contact's name in the messaging experience
5080 */
5081 public CharSequence getSender() {
5082 return mSender;
5083 }
5084
5085 /**
5086 * Get the MIME type of the data pointed to by the Uri
5087 */
5088 public String getDataMimeType() {
5089 return mDataMimeType;
5090 }
5091
5092 /**
5093 * Get the the Uri pointing to the content of the message. Can be null, in which case
5094 * {@see #getText()} is used.
5095 */
5096 public Uri getDataUri() {
5097 return mDataUri;
5098 }
5099
Alex Hillsd9b04d92016-04-11 16:38:16 -04005100 private Bundle toBundle() {
5101 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04005102 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005103 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04005104 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005105 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04005106 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005107 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04005108 }
5109 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005110 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04005111 }
5112 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005113 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04005114 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005115 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04005116 }
5117
Alex Hillsd9b04d92016-04-11 16:38:16 -04005118 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
5119 Bundle[] bundles = new Bundle[messages.size()];
5120 final int N = messages.size();
5121 for (int i = 0; i < N; i++) {
5122 bundles[i] = messages.get(i).toBundle();
5123 }
5124 return bundles;
5125 }
5126
Adrian Roosdedd1df2016-04-26 16:38:47 -07005127 static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005128 List<Message> messages = new ArrayList<>(bundles.length);
5129 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07005130 if (bundles[i] instanceof Bundle) {
5131 Message message = getMessageFromBundle((Bundle)bundles[i]);
5132 if (message != null) {
5133 messages.add(message);
5134 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005135 }
5136 }
5137 return messages;
5138 }
5139
5140 static Message getMessageFromBundle(Bundle bundle) {
5141 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07005142 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005143 return null;
5144 } else {
5145 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
5146 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
5147 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
5148 bundle.containsKey(KEY_DATA_URI)) {
5149
5150 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
5151 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04005152 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005153 return message;
5154 }
5155 } catch (ClassCastException e) {
5156 return null;
5157 }
5158 }
Alex Hillsfc737de2016-03-23 17:33:02 -04005159 }
5160 }
5161
5162 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04005163 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08005164 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005165 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04005166 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005167 * Notification notif = new Notification.Builder(mContext)
5168 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
5169 * .setContentText(subject)
5170 * .setSmallIcon(R.drawable.new_mail)
5171 * .setLargeIcon(aBitmap)
5172 * .setStyle(new Notification.InboxStyle()
5173 * .addLine(str1)
5174 * .addLine(str2)
5175 * .setContentTitle(&quot;&quot;)
5176 * .setSummaryText(&quot;+3 more&quot;))
5177 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04005178 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005179 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04005180 * @see Notification#bigContentView
5181 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005182 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005183 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
5184
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005185 public InboxStyle() {
5186 }
5187
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005188 /**
5189 * @deprecated use {@code InboxStyle()}.
5190 */
5191 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04005192 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005193 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04005194 }
5195
Chris Wrend6297db2012-05-03 16:20:13 -04005196 /**
5197 * Overrides ContentTitle in the big form of the template.
5198 * This defaults to the value passed to setContentTitle().
5199 */
5200 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005201 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005202 return this;
5203 }
5204
5205 /**
5206 * Set the first line of text after the detail section in the big form of the template.
5207 */
5208 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005209 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005210 return this;
5211 }
5212
Chris Wren0bd664d2012-08-01 13:56:56 -04005213 /**
5214 * Append a line to the digest section of the Inbox notification.
5215 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04005216 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005217 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04005218 return this;
5219 }
5220
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005221 /**
5222 * @hide
5223 */
5224 public void addExtras(Bundle extras) {
5225 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005226
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005227 CharSequence[] a = new CharSequence[mTexts.size()];
5228 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
5229 }
5230
Christoph Studer4600f9b2014-07-22 22:44:43 +02005231 /**
5232 * @hide
5233 */
5234 @Override
5235 protected void restoreFromExtras(Bundle extras) {
5236 super.restoreFromExtras(extras);
5237
5238 mTexts.clear();
5239 if (extras.containsKey(EXTRA_TEXT_LINES)) {
5240 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
5241 }
5242 }
5243
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005244 /**
5245 * @hide
5246 */
5247 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08005248 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02005249 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005250 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
5251 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005252
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005253 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04005254
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005255 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005256
Chris Wrend6297db2012-05-03 16:20:13 -04005257 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 -04005258 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04005259
Chris Wren4ed80d52012-05-17 09:30:03 -04005260 // Make sure all rows are gone in case we reuse a view.
5261 for (int rowId : rowIds) {
5262 contentView.setViewVisibility(rowId, View.GONE);
5263 }
5264
Daniel Sandler879c5e02012-04-17 16:46:51 -04005265 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07005266 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
5267 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08005268 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07005269 int onlyViewId = 0;
5270 int maxRows = rowIds.length;
5271 if (mBuilder.mActions.size() > 0) {
5272 maxRows--;
5273 }
5274 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005275 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07005276 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005277 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005278 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Selim Cinek07c80172016-04-21 16:40:47 -07005279 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08005280 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07005281 if (first) {
5282 onlyViewId = rowIds[i];
5283 } else {
5284 onlyViewId = 0;
5285 }
Selim Cinek247fa012016-02-18 09:50:48 -08005286 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04005287 }
5288 i++;
5289 }
Selim Cinek07c80172016-04-21 16:40:47 -07005290 if (onlyViewId != 0) {
5291 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
5292 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
5293 R.dimen.notification_text_margin_top);
5294 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
5295 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005296
Daniel Sandler879c5e02012-04-17 16:46:51 -04005297 return contentView;
5298 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005299
Selim Cinek247fa012016-02-18 09:50:48 -08005300 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08005301 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08005302 if (first) {
5303 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
5304 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
5305 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07005306 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005307 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08005308 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005309 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005310 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08005311 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005312 }
Dan Sandler842dd772014-05-15 09:36:47 -04005313
5314 /**
5315 * Notification style for media playback notifications.
5316 *
5317 * In the expanded form, {@link Notification#bigContentView}, up to 5
5318 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04005319 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04005320 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
5321 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
5322 * treated as album artwork.
5323 *
5324 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
5325 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01005326 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04005327 * in the standard view alongside the usual content.
5328 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005329 * Notifications created with MediaStyle will have their category set to
5330 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
5331 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
5332 *
Jeff Browndba34ba2014-06-24 20:46:03 -07005333 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
5334 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04005335 * the System UI can identify this as a notification representing an active media session
5336 * and respond accordingly (by showing album artwork in the lockscreen, for example).
5337 *
5338 * To use this style with your Notification, feed it to
5339 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5340 * <pre class="prettyprint">
5341 * Notification noti = new Notification.Builder()
5342 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01005343 * .setContentTitle(&quot;Track title&quot;)
5344 * .setContentText(&quot;Artist - Album&quot;)
5345 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005346 * .setStyle(<b>new Notification.MediaStyle()</b>
5347 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04005348 * .build();
5349 * </pre>
5350 *
5351 * @see Notification#bigContentView
5352 */
5353 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005354 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04005355 static final int MAX_MEDIA_BUTTONS = 5;
5356
5357 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07005358 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04005359
5360 public MediaStyle() {
5361 }
5362
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005363 /**
5364 * @deprecated use {@code MediaStyle()}.
5365 */
5366 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04005367 public MediaStyle(Builder builder) {
5368 setBuilder(builder);
5369 }
5370
5371 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005372 * 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 -04005373 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005374 *
5375 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04005376 */
5377 public MediaStyle setShowActionsInCompactView(int...actions) {
5378 mActionsToShowInCompact = actions;
5379 return this;
5380 }
5381
5382 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07005383 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
5384 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04005385 */
Jeff Browndba34ba2014-06-24 20:46:03 -07005386 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04005387 mToken = token;
5388 return this;
5389 }
5390
Christoph Studer4600f9b2014-07-22 22:44:43 +02005391 /**
5392 * @hide
5393 */
Dan Sandler842dd772014-05-15 09:36:47 -04005394 @Override
5395 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005396 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005397 if (wip.category == null) {
5398 wip.category = Notification.CATEGORY_TRANSPORT;
5399 }
Dan Sandler842dd772014-05-15 09:36:47 -04005400 return wip;
5401 }
5402
Christoph Studer4600f9b2014-07-22 22:44:43 +02005403 /**
5404 * @hide
5405 */
5406 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005407 public RemoteViews makeContentView() {
5408 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02005409 }
5410
5411 /**
5412 * @hide
5413 */
5414 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005415 public RemoteViews makeBigContentView() {
5416 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02005417 }
5418
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005419 /**
5420 * @hide
5421 */
5422 @Override
5423 public RemoteViews makeHeadsUpContentView() {
5424 RemoteViews expanded = makeMediaBigContentView();
5425 return expanded != null ? expanded : makeMediaContentView();
5426 }
5427
Dan Sandler842dd772014-05-15 09:36:47 -04005428 /** @hide */
5429 @Override
5430 public void addExtras(Bundle extras) {
5431 super.addExtras(extras);
5432
5433 if (mToken != null) {
5434 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
5435 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01005436 if (mActionsToShowInCompact != null) {
5437 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
5438 }
Dan Sandler842dd772014-05-15 09:36:47 -04005439 }
5440
Christoph Studer4600f9b2014-07-22 22:44:43 +02005441 /**
5442 * @hide
5443 */
5444 @Override
5445 protected void restoreFromExtras(Bundle extras) {
5446 super.restoreFromExtras(extras);
5447
5448 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
5449 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
5450 }
5451 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
5452 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
5453 }
5454 }
5455
Selim Cinek5bf069a2015-11-10 19:14:27 -05005456 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04005457 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005458 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07005459 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04005460 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05005461 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
5462 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04005463 if (!tombstone) {
5464 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
5465 }
5466 button.setContentDescription(R.id.action0, action.title);
5467 return button;
5468 }
5469
5470 private RemoteViews makeMediaContentView() {
5471 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005472 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04005473
5474 final int numActions = mBuilder.mActions.size();
5475 final int N = mActionsToShowInCompact == null
5476 ? 0
5477 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
5478 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005479 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04005480 for (int i = 0; i < N; i++) {
5481 if (i >= numActions) {
5482 throw new IllegalArgumentException(String.format(
5483 "setShowActionsInCompactView: action %d out of bounds (max %d)",
5484 i, numActions - 1));
5485 }
5486
5487 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05005488 final RemoteViews button = generateMediaActionButton(action,
Adrian Roos4ff3b122016-02-01 12:26:13 -08005489 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005490 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04005491 }
5492 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08005493 handleImage(view);
5494 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005495 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07005496 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005497 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08005498 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005499 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04005500 return view;
5501 }
5502
5503 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005504 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005505 // Dont add an expanded view if there is no more content to be revealed
5506 int actionsInCompact = mActionsToShowInCompact == null
5507 ? 0
5508 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07005509 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005510 return null;
5511 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05005512 RemoteViews big = mBuilder.applyStandardTemplate(
5513 R.layout.notification_template_material_big_media,
5514 false);
Dan Sandler842dd772014-05-15 09:36:47 -04005515
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005516 if (actionCount > 0) {
5517 big.removeAllViews(com.android.internal.R.id.media_actions);
5518 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05005519 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Adrian Roos4ff3b122016-02-01 12:26:13 -08005520 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005521 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04005522 }
5523 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05005524 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04005525 return big;
5526 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005527
Selim Cinek5bf069a2015-11-10 19:14:27 -05005528 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07005529 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005530 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
5531 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005532 }
5533 }
5534
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005535 /**
5536 * @hide
5537 */
5538 @Override
5539 protected boolean hasProgress() {
5540 return false;
5541 }
Dan Sandler842dd772014-05-15 09:36:47 -04005542 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005543
Selim Cinek593610c2016-02-16 18:42:57 -08005544 /**
5545 * Notification style for custom views that are decorated by the system
5546 *
5547 * <p>Instead of providing a notification that is completely custom, a developer can set this
5548 * style and still obtain system decorations like the notification header with the expand
5549 * affordance and actions.
5550 *
5551 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5552 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5553 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5554 * corresponding custom views to display.
5555 *
5556 * To use this style with your Notification, feed it to
5557 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5558 * <pre class="prettyprint">
5559 * Notification noti = new Notification.Builder()
5560 * .setSmallIcon(R.drawable.ic_stat_player)
5561 * .setLargeIcon(albumArtBitmap))
5562 * .setCustomContentView(contentView);
5563 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
5564 * .build();
5565 * </pre>
5566 */
5567 public static class DecoratedCustomViewStyle extends Style {
5568
5569 public DecoratedCustomViewStyle() {
5570 }
5571
Selim Cinek593610c2016-02-16 18:42:57 -08005572 /**
5573 * @hide
5574 */
5575 public boolean displayCustomViewInline() {
5576 return true;
5577 }
5578
5579 /**
5580 * @hide
5581 */
5582 @Override
5583 public RemoteViews makeContentView() {
5584 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
5585 }
5586
5587 /**
5588 * @hide
5589 */
5590 @Override
5591 public RemoteViews makeBigContentView() {
5592 return makeDecoratedBigContentView();
5593 }
5594
5595 /**
5596 * @hide
5597 */
5598 @Override
5599 public RemoteViews makeHeadsUpContentView() {
5600 return makeDecoratedHeadsUpContentView();
5601 }
5602
Selim Cinek593610c2016-02-16 18:42:57 -08005603 private RemoteViews makeDecoratedHeadsUpContentView() {
5604 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
5605 ? mBuilder.mN.contentView
5606 : mBuilder.mN.headsUpContentView;
5607 if (mBuilder.mActions.size() == 0) {
5608 return makeStandardTemplateWithCustomContent(headsUpContentView);
5609 }
5610 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5611 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005612 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005613 return remoteViews;
5614 }
5615
Selim Cinek593610c2016-02-16 18:42:57 -08005616 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
5617 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
5618 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005619 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08005620 return remoteViews;
5621 }
5622
Selim Cinek593610c2016-02-16 18:42:57 -08005623 private RemoteViews makeDecoratedBigContentView() {
5624 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
5625 ? mBuilder.mN.contentView
5626 : mBuilder.mN.bigContentView;
5627 if (mBuilder.mActions.size() == 0) {
5628 return makeStandardTemplateWithCustomContent(bigContentView);
5629 }
5630 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5631 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005632 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005633 return remoteViews;
5634 }
Selim Cinek247fa012016-02-18 09:50:48 -08005635
5636 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
5637 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005638 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005639 // Need to clone customContent before adding, because otherwise it can no longer be
5640 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005641 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005642 remoteViews.removeAllViews(R.id.notification_main_column);
5643 remoteViews.addView(R.id.notification_main_column, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005644 }
Selim Cinek247fa012016-02-18 09:50:48 -08005645 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005646 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07005647 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005648 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08005649 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005650 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08005651 }
Selim Cinek593610c2016-02-16 18:42:57 -08005652 }
5653
Selim Cinek03eb3b72016-02-18 10:39:45 -08005654 /**
5655 * Notification style for media custom views that are decorated by the system
5656 *
5657 * <p>Instead of providing a media notification that is completely custom, a developer can set
5658 * this style and still obtain system decorations like the notification header with the expand
5659 * affordance and actions.
5660 *
5661 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5662 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5663 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5664 * corresponding custom views to display.
5665 *
5666 * To use this style with your Notification, feed it to
5667 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5668 * <pre class="prettyprint">
5669 * Notification noti = new Notification.Builder()
5670 * .setSmallIcon(R.drawable.ic_stat_player)
5671 * .setLargeIcon(albumArtBitmap))
5672 * .setCustomContentView(contentView);
5673 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
5674 * .setMediaSession(mySession))
5675 * .build();
5676 * </pre>
5677 *
5678 * @see android.app.Notification.DecoratedCustomViewStyle
5679 * @see android.app.Notification.MediaStyle
5680 */
5681 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
5682
5683 public DecoratedMediaCustomViewStyle() {
5684 }
5685
Selim Cinek03eb3b72016-02-18 10:39:45 -08005686 /**
5687 * @hide
5688 */
5689 public boolean displayCustomViewInline() {
5690 return true;
5691 }
5692
5693 /**
5694 * @hide
5695 */
5696 @Override
5697 public RemoteViews makeContentView() {
5698 RemoteViews remoteViews = super.makeContentView();
5699 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5700 mBuilder.mN.contentView);
5701 }
5702
5703 /**
5704 * @hide
5705 */
5706 @Override
5707 public RemoteViews makeBigContentView() {
5708 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
5709 ? mBuilder.mN.bigContentView
5710 : mBuilder.mN.contentView;
5711 return makeBigContentViewWithCustomContent(customRemoteView);
5712 }
5713
5714 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
5715 RemoteViews remoteViews = super.makeBigContentView();
5716 if (remoteViews != null) {
5717 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
5718 customRemoteView);
5719 } else if (customRemoteView != mBuilder.mN.contentView){
5720 remoteViews = super.makeContentView();
5721 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5722 customRemoteView);
5723 } else {
5724 return null;
5725 }
5726 }
5727
5728 /**
5729 * @hide
5730 */
5731 @Override
5732 public RemoteViews makeHeadsUpContentView() {
5733 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
5734 ? mBuilder.mN.headsUpContentView
5735 : mBuilder.mN.contentView;
5736 return makeBigContentViewWithCustomContent(customRemoteView);
5737 }
5738
5739 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
5740 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005741 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005742 // Need to clone customContent before adding, because otherwise it can no longer be
5743 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005744 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005745 remoteViews.removeAllViews(id);
5746 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005747 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08005748 return remoteViews;
5749 }
5750 }
5751
Christoph Studer4600f9b2014-07-22 22:44:43 +02005752 // When adding a new Style subclass here, don't forget to update
5753 // Builder.getNotificationStyleClass.
5754
Griff Hazen61a9e862014-05-22 16:05:19 -07005755 /**
5756 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
5757 * metadata or change options on a notification builder.
5758 */
5759 public interface Extender {
5760 /**
5761 * Apply this extender to a notification builder.
5762 * @param builder the builder to be modified.
5763 * @return the build object for chaining.
5764 */
5765 public Builder extend(Builder builder);
5766 }
5767
5768 /**
5769 * Helper class to add wearable extensions to notifications.
5770 * <p class="note"> See
5771 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
5772 * for Android Wear</a> for more information on how to use this class.
5773 * <p>
5774 * To create a notification with wearable extensions:
5775 * <ol>
5776 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
5777 * properties.
5778 * <li>Create a {@link android.app.Notification.WearableExtender}.
5779 * <li>Set wearable-specific properties using the
5780 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
5781 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
5782 * notification.
5783 * <li>Post the notification to the notification system with the
5784 * {@code NotificationManager.notify(...)} methods.
5785 * </ol>
5786 *
5787 * <pre class="prettyprint">
5788 * Notification notif = new Notification.Builder(mContext)
5789 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5790 * .setContentText(subject)
5791 * .setSmallIcon(R.drawable.new_mail)
5792 * .extend(new Notification.WearableExtender()
5793 * .setContentIcon(R.drawable.new_mail))
5794 * .build();
5795 * NotificationManager notificationManger =
5796 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
5797 * notificationManger.notify(0, notif);</pre>
5798 *
5799 * <p>Wearable extensions can be accessed on an existing notification by using the
5800 * {@code WearableExtender(Notification)} constructor,
5801 * and then using the {@code get} methods to access values.
5802 *
5803 * <pre class="prettyprint">
5804 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
5805 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07005806 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005807 */
5808 public static final class WearableExtender implements Extender {
5809 /**
5810 * Sentinel value for an action index that is unset.
5811 */
5812 public static final int UNSET_ACTION_INDEX = -1;
5813
5814 /**
5815 * Size value for use with {@link #setCustomSizePreset} to show this notification with
5816 * default sizing.
5817 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07005818 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07005819 * on their content.
5820 */
5821 public static final int SIZE_DEFAULT = 0;
5822
5823 /**
5824 * Size value for use with {@link #setCustomSizePreset} to show this notification
5825 * with an extra small size.
5826 * <p>This value is only applicable for custom display notifications created using
5827 * {@link #setDisplayIntent}.
5828 */
5829 public static final int SIZE_XSMALL = 1;
5830
5831 /**
5832 * Size value for use with {@link #setCustomSizePreset} to show this notification
5833 * with a small size.
5834 * <p>This value is only applicable for custom display notifications created using
5835 * {@link #setDisplayIntent}.
5836 */
5837 public static final int SIZE_SMALL = 2;
5838
5839 /**
5840 * Size value for use with {@link #setCustomSizePreset} to show this notification
5841 * with a medium size.
5842 * <p>This value is only applicable for custom display notifications created using
5843 * {@link #setDisplayIntent}.
5844 */
5845 public static final int SIZE_MEDIUM = 3;
5846
5847 /**
5848 * Size value for use with {@link #setCustomSizePreset} to show this notification
5849 * with a large size.
5850 * <p>This value is only applicable for custom display notifications created using
5851 * {@link #setDisplayIntent}.
5852 */
5853 public static final int SIZE_LARGE = 4;
5854
Griff Hazend5f11f92014-05-27 15:40:09 -07005855 /**
5856 * Size value for use with {@link #setCustomSizePreset} to show this notification
5857 * full screen.
5858 * <p>This value is only applicable for custom display notifications created using
5859 * {@link #setDisplayIntent}.
5860 */
5861 public static final int SIZE_FULL_SCREEN = 5;
5862
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005863 /**
5864 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
5865 * short amount of time when this notification is displayed on the screen. This
5866 * is the default value.
5867 */
5868 public static final int SCREEN_TIMEOUT_SHORT = 0;
5869
5870 /**
5871 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
5872 * for a longer amount of time when this notification is displayed on the screen.
5873 */
5874 public static final int SCREEN_TIMEOUT_LONG = -1;
5875
Griff Hazen61a9e862014-05-22 16:05:19 -07005876 /** Notification extra which contains wearable extensions */
5877 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
5878
Pete Gastaf6781d2014-10-07 15:17:05 -04005879 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07005880 private static final String KEY_ACTIONS = "actions";
5881 private static final String KEY_FLAGS = "flags";
5882 private static final String KEY_DISPLAY_INTENT = "displayIntent";
5883 private static final String KEY_PAGES = "pages";
5884 private static final String KEY_BACKGROUND = "background";
5885 private static final String KEY_CONTENT_ICON = "contentIcon";
5886 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
5887 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
5888 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
5889 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
5890 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005891 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04005892 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04005893 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07005894
5895 // Flags bitwise-ored to mFlags
5896 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
5897 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
5898 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
5899 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005900 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04005901 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04005902 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07005903
5904 // Default value for flags integer
5905 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
5906
5907 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
5908 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
5909
5910 private ArrayList<Action> mActions = new ArrayList<Action>();
5911 private int mFlags = DEFAULT_FLAGS;
5912 private PendingIntent mDisplayIntent;
5913 private ArrayList<Notification> mPages = new ArrayList<Notification>();
5914 private Bitmap mBackground;
5915 private int mContentIcon;
5916 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
5917 private int mContentActionIndex = UNSET_ACTION_INDEX;
5918 private int mCustomSizePreset = SIZE_DEFAULT;
5919 private int mCustomContentHeight;
5920 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005921 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04005922 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04005923 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07005924
5925 /**
5926 * Create a {@link android.app.Notification.WearableExtender} with default
5927 * options.
5928 */
5929 public WearableExtender() {
5930 }
5931
5932 public WearableExtender(Notification notif) {
5933 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
5934 if (wearableBundle != null) {
5935 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
5936 if (actions != null) {
5937 mActions.addAll(actions);
5938 }
5939
5940 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
5941 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
5942
5943 Notification[] pages = getNotificationArrayFromBundle(
5944 wearableBundle, KEY_PAGES);
5945 if (pages != null) {
5946 Collections.addAll(mPages, pages);
5947 }
5948
5949 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
5950 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
5951 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
5952 DEFAULT_CONTENT_ICON_GRAVITY);
5953 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
5954 UNSET_ACTION_INDEX);
5955 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
5956 SIZE_DEFAULT);
5957 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
5958 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005959 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04005960 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04005961 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07005962 }
5963 }
5964
5965 /**
5966 * Apply wearable extensions to a notification that is being built. This is typically
5967 * called by the {@link android.app.Notification.Builder#extend} method of
5968 * {@link android.app.Notification.Builder}.
5969 */
5970 @Override
5971 public Notification.Builder extend(Notification.Builder builder) {
5972 Bundle wearableBundle = new Bundle();
5973
5974 if (!mActions.isEmpty()) {
5975 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
5976 }
5977 if (mFlags != DEFAULT_FLAGS) {
5978 wearableBundle.putInt(KEY_FLAGS, mFlags);
5979 }
5980 if (mDisplayIntent != null) {
5981 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
5982 }
5983 if (!mPages.isEmpty()) {
5984 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
5985 new Notification[mPages.size()]));
5986 }
5987 if (mBackground != null) {
5988 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
5989 }
5990 if (mContentIcon != 0) {
5991 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
5992 }
5993 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
5994 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
5995 }
5996 if (mContentActionIndex != UNSET_ACTION_INDEX) {
5997 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
5998 mContentActionIndex);
5999 }
6000 if (mCustomSizePreset != SIZE_DEFAULT) {
6001 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
6002 }
6003 if (mCustomContentHeight != 0) {
6004 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
6005 }
6006 if (mGravity != DEFAULT_GRAVITY) {
6007 wearableBundle.putInt(KEY_GRAVITY, mGravity);
6008 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006009 if (mHintScreenTimeout != 0) {
6010 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
6011 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04006012 if (mDismissalId != null) {
6013 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
6014 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006015 if (mBridgeTag != null) {
6016 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
6017 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006018
6019 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
6020 return builder;
6021 }
6022
6023 @Override
6024 public WearableExtender clone() {
6025 WearableExtender that = new WearableExtender();
6026 that.mActions = new ArrayList<Action>(this.mActions);
6027 that.mFlags = this.mFlags;
6028 that.mDisplayIntent = this.mDisplayIntent;
6029 that.mPages = new ArrayList<Notification>(this.mPages);
6030 that.mBackground = this.mBackground;
6031 that.mContentIcon = this.mContentIcon;
6032 that.mContentIconGravity = this.mContentIconGravity;
6033 that.mContentActionIndex = this.mContentActionIndex;
6034 that.mCustomSizePreset = this.mCustomSizePreset;
6035 that.mCustomContentHeight = this.mCustomContentHeight;
6036 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006037 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04006038 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006039 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07006040 return that;
6041 }
6042
6043 /**
6044 * Add a wearable action to this notification.
6045 *
6046 * <p>When wearable actions are added using this method, the set of actions that
6047 * show on a wearable device splits from devices that only show actions added
6048 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
6049 * of which actions display on different devices.
6050 *
6051 * @param action the action to add to this notification
6052 * @return this object for method chaining
6053 * @see android.app.Notification.Action
6054 */
6055 public WearableExtender addAction(Action action) {
6056 mActions.add(action);
6057 return this;
6058 }
6059
6060 /**
6061 * Adds wearable actions to this notification.
6062 *
6063 * <p>When wearable actions are added using this method, the set of actions that
6064 * show on a wearable device splits from devices that only show actions added
6065 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
6066 * of which actions display on different devices.
6067 *
6068 * @param actions the actions to add to this notification
6069 * @return this object for method chaining
6070 * @see android.app.Notification.Action
6071 */
6072 public WearableExtender addActions(List<Action> actions) {
6073 mActions.addAll(actions);
6074 return this;
6075 }
6076
6077 /**
6078 * Clear all wearable actions present on this builder.
6079 * @return this object for method chaining.
6080 * @see #addAction
6081 */
6082 public WearableExtender clearActions() {
6083 mActions.clear();
6084 return this;
6085 }
6086
6087 /**
6088 * Get the wearable actions present on this notification.
6089 */
6090 public List<Action> getActions() {
6091 return mActions;
6092 }
6093
6094 /**
6095 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07006096 * this notification. The {@link PendingIntent} provided should be for an activity.
6097 *
6098 * <pre class="prettyprint">
6099 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
6100 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
6101 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
6102 * Notification notif = new Notification.Builder(context)
6103 * .extend(new Notification.WearableExtender()
6104 * .setDisplayIntent(displayPendingIntent)
6105 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
6106 * .build();</pre>
6107 *
6108 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07006109 * should have an empty task affinity. It is also recommended to use the device
6110 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07006111 *
6112 * <p>Example AndroidManifest.xml entry:
6113 * <pre class="prettyprint">
6114 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
6115 * android:exported=&quot;true&quot;
6116 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07006117 * android:taskAffinity=&quot;&quot;
6118 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07006119 *
6120 * @param intent the {@link PendingIntent} for an activity
6121 * @return this object for method chaining
6122 * @see android.app.Notification.WearableExtender#getDisplayIntent
6123 */
6124 public WearableExtender setDisplayIntent(PendingIntent intent) {
6125 mDisplayIntent = intent;
6126 return this;
6127 }
6128
6129 /**
6130 * Get the intent to launch inside of an activity view when displaying this
6131 * notification. This {@code PendingIntent} should be for an activity.
6132 */
6133 public PendingIntent getDisplayIntent() {
6134 return mDisplayIntent;
6135 }
6136
6137 /**
6138 * Add an additional page of content to display with this notification. The current
6139 * notification forms the first page, and pages added using this function form
6140 * subsequent pages. This field can be used to separate a notification into multiple
6141 * sections.
6142 *
6143 * @param page the notification to add as another page
6144 * @return this object for method chaining
6145 * @see android.app.Notification.WearableExtender#getPages
6146 */
6147 public WearableExtender addPage(Notification page) {
6148 mPages.add(page);
6149 return this;
6150 }
6151
6152 /**
6153 * Add additional pages of content to display with this notification. The current
6154 * notification forms the first page, and pages added using this function form
6155 * subsequent pages. This field can be used to separate a notification into multiple
6156 * sections.
6157 *
6158 * @param pages a list of notifications
6159 * @return this object for method chaining
6160 * @see android.app.Notification.WearableExtender#getPages
6161 */
6162 public WearableExtender addPages(List<Notification> pages) {
6163 mPages.addAll(pages);
6164 return this;
6165 }
6166
6167 /**
6168 * Clear all additional pages present on this builder.
6169 * @return this object for method chaining.
6170 * @see #addPage
6171 */
6172 public WearableExtender clearPages() {
6173 mPages.clear();
6174 return this;
6175 }
6176
6177 /**
6178 * Get the array of additional pages of content for displaying this notification. The
6179 * current notification forms the first page, and elements within this array form
6180 * subsequent pages. This field can be used to separate a notification into multiple
6181 * sections.
6182 * @return the pages for this notification
6183 */
6184 public List<Notification> getPages() {
6185 return mPages;
6186 }
6187
6188 /**
6189 * Set a background image to be displayed behind the notification content.
6190 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
6191 * will work with any notification style.
6192 *
6193 * @param background the background bitmap
6194 * @return this object for method chaining
6195 * @see android.app.Notification.WearableExtender#getBackground
6196 */
6197 public WearableExtender setBackground(Bitmap background) {
6198 mBackground = background;
6199 return this;
6200 }
6201
6202 /**
6203 * Get a background image to be displayed behind the notification content.
6204 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
6205 * will work with any notification style.
6206 *
6207 * @return the background image
6208 * @see android.app.Notification.WearableExtender#setBackground
6209 */
6210 public Bitmap getBackground() {
6211 return mBackground;
6212 }
6213
6214 /**
6215 * Set an icon that goes with the content of this notification.
6216 */
6217 public WearableExtender setContentIcon(int icon) {
6218 mContentIcon = icon;
6219 return this;
6220 }
6221
6222 /**
6223 * Get an icon that goes with the content of this notification.
6224 */
6225 public int getContentIcon() {
6226 return mContentIcon;
6227 }
6228
6229 /**
6230 * Set the gravity that the content icon should have within the notification display.
6231 * Supported values include {@link android.view.Gravity#START} and
6232 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
6233 * @see #setContentIcon
6234 */
6235 public WearableExtender setContentIconGravity(int contentIconGravity) {
6236 mContentIconGravity = contentIconGravity;
6237 return this;
6238 }
6239
6240 /**
6241 * Get the gravity that the content icon should have within the notification display.
6242 * Supported values include {@link android.view.Gravity#START} and
6243 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
6244 * @see #getContentIcon
6245 */
6246 public int getContentIconGravity() {
6247 return mContentIconGravity;
6248 }
6249
6250 /**
6251 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07006252 * this notification. This action will no longer display separately from the
6253 * notification's content.
6254 *
Griff Hazenca48d352014-05-28 22:37:13 -07006255 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07006256 * set, although the list of available actions comes from the main notification and not
6257 * from the child page's notification.
6258 *
6259 * @param actionIndex The index of the action to hoist onto the current notification page.
6260 * If wearable actions were added to the main notification, this index
6261 * will apply to that list, otherwise it will apply to the regular
6262 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07006263 */
6264 public WearableExtender setContentAction(int actionIndex) {
6265 mContentActionIndex = actionIndex;
6266 return this;
6267 }
6268
6269 /**
Griff Hazenca48d352014-05-28 22:37:13 -07006270 * Get the index of the notification action, if any, that was specified as being clickable
6271 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07006272 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07006273 *
Griff Hazenca48d352014-05-28 22:37:13 -07006274 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07006275 * set, although the list of available actions comes from the main notification and not
6276 * from the child page's notification.
6277 *
6278 * <p>If wearable specific actions were added to the main notification, this index will
6279 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07006280 *
6281 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07006282 */
6283 public int getContentAction() {
6284 return mContentActionIndex;
6285 }
6286
6287 /**
6288 * Set the gravity that this notification should have within the available viewport space.
6289 * Supported values include {@link android.view.Gravity#TOP},
6290 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
6291 * The default value is {@link android.view.Gravity#BOTTOM}.
6292 */
6293 public WearableExtender setGravity(int gravity) {
6294 mGravity = gravity;
6295 return this;
6296 }
6297
6298 /**
6299 * Get the gravity that this notification should have within the available viewport space.
6300 * Supported values include {@link android.view.Gravity#TOP},
6301 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
6302 * The default value is {@link android.view.Gravity#BOTTOM}.
6303 */
6304 public int getGravity() {
6305 return mGravity;
6306 }
6307
6308 /**
6309 * Set the custom size preset for the display of this notification out of the available
6310 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
6311 * {@link #SIZE_LARGE}.
6312 * <p>Some custom size presets are only applicable for custom display notifications created
6313 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
6314 * documentation for the preset in question. See also
6315 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
6316 */
6317 public WearableExtender setCustomSizePreset(int sizePreset) {
6318 mCustomSizePreset = sizePreset;
6319 return this;
6320 }
6321
6322 /**
6323 * Get the custom size preset for the display of this notification out of the available
6324 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
6325 * {@link #SIZE_LARGE}.
6326 * <p>Some custom size presets are only applicable for custom display notifications created
6327 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
6328 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
6329 */
6330 public int getCustomSizePreset() {
6331 return mCustomSizePreset;
6332 }
6333
6334 /**
6335 * Set the custom height in pixels for the display of this notification's content.
6336 * <p>This option is only available for custom display notifications created
6337 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
6338 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
6339 * {@link #getCustomContentHeight}.
6340 */
6341 public WearableExtender setCustomContentHeight(int height) {
6342 mCustomContentHeight = height;
6343 return this;
6344 }
6345
6346 /**
6347 * Get the custom height in pixels for the display of this notification's content.
6348 * <p>This option is only available for custom display notifications created
6349 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
6350 * {@link #setCustomContentHeight}.
6351 */
6352 public int getCustomContentHeight() {
6353 return mCustomContentHeight;
6354 }
6355
6356 /**
6357 * Set whether the scrolling position for the contents of this notification should start
6358 * at the bottom of the contents instead of the top when the contents are too long to
6359 * display within the screen. Default is false (start scroll at the top).
6360 */
6361 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
6362 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
6363 return this;
6364 }
6365
6366 /**
6367 * Get whether the scrolling position for the contents of this notification should start
6368 * at the bottom of the contents instead of the top when the contents are too long to
6369 * display within the screen. Default is false (start scroll at the top).
6370 */
6371 public boolean getStartScrollBottom() {
6372 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
6373 }
6374
6375 /**
6376 * Set whether the content intent is available when the wearable device is not connected
6377 * to a companion device. The user can still trigger this intent when the wearable device
6378 * is offline, but a visual hint will indicate that the content intent may not be available.
6379 * Defaults to true.
6380 */
6381 public WearableExtender setContentIntentAvailableOffline(
6382 boolean contentIntentAvailableOffline) {
6383 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
6384 return this;
6385 }
6386
6387 /**
6388 * Get whether the content intent is available when the wearable device is not connected
6389 * to a companion device. The user can still trigger this intent when the wearable device
6390 * is offline, but a visual hint will indicate that the content intent may not be available.
6391 * Defaults to true.
6392 */
6393 public boolean getContentIntentAvailableOffline() {
6394 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
6395 }
6396
6397 /**
6398 * Set a hint that this notification's icon should not be displayed.
6399 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
6400 * @return this object for method chaining
6401 */
6402 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
6403 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
6404 return this;
6405 }
6406
6407 /**
6408 * Get a hint that this notification's icon should not be displayed.
6409 * @return {@code true} if this icon should not be displayed, false otherwise.
6410 * The default value is {@code false} if this was never set.
6411 */
6412 public boolean getHintHideIcon() {
6413 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
6414 }
6415
6416 /**
6417 * Set a visual hint that only the background image of this notification should be
6418 * displayed, and other semantic content should be hidden. This hint is only applicable
6419 * to sub-pages added using {@link #addPage}.
6420 */
6421 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
6422 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
6423 return this;
6424 }
6425
6426 /**
6427 * Get a visual hint that only the background image of this notification should be
6428 * displayed, and other semantic content should be hidden. This hint is only applicable
6429 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
6430 */
6431 public boolean getHintShowBackgroundOnly() {
6432 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
6433 }
6434
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006435 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08006436 * Set a hint that this notification's background should not be clipped if possible,
6437 * and should instead be resized to fully display on the screen, retaining the aspect
6438 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006439 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
6440 * @return this object for method chaining
6441 */
6442 public WearableExtender setHintAvoidBackgroundClipping(
6443 boolean hintAvoidBackgroundClipping) {
6444 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
6445 return this;
6446 }
6447
6448 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08006449 * Get a hint that this notification's background should not be clipped if possible,
6450 * and should instead be resized to fully display on the screen, retaining the aspect
6451 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006452 * @return {@code true} if it's ok if the background is clipped on the screen, false
6453 * otherwise. The default value is {@code false} if this was never set.
6454 */
6455 public boolean getHintAvoidBackgroundClipping() {
6456 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
6457 }
6458
6459 /**
6460 * Set a hint that the screen should remain on for at least this duration when
6461 * this notification is displayed on the screen.
6462 * @param timeout The requested screen timeout in milliseconds. Can also be either
6463 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
6464 * @return this object for method chaining
6465 */
6466 public WearableExtender setHintScreenTimeout(int timeout) {
6467 mHintScreenTimeout = timeout;
6468 return this;
6469 }
6470
6471 /**
6472 * Get the duration, in milliseconds, that the screen should remain on for
6473 * when this notification is displayed.
6474 * @return the duration in milliseconds if > 0, or either one of the sentinel values
6475 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
6476 */
6477 public int getHintScreenTimeout() {
6478 return mHintScreenTimeout;
6479 }
6480
Alex Hills9ab3a232016-04-05 14:54:56 -04006481 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04006482 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
6483 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
6484 * qr codes, as well as other simple black-and-white tickets.
6485 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
6486 * @return this object for method chaining
6487 */
6488 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
6489 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
6490 return this;
6491 }
6492
6493 /**
6494 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
6495 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
6496 * qr codes, as well as other simple black-and-white tickets.
6497 * @return {@code true} if it should be displayed in ambient, false otherwise
6498 * otherwise. The default value is {@code false} if this was never set.
6499 */
6500 public boolean getHintAmbientBigPicture() {
6501 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
6502 }
6503
6504 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04006505 * Set a hint that this notification's content intent will launch an {@link Activity}
6506 * directly, telling the platform that it can generate the appropriate transitions.
6507 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
6508 * an activity and transitions should be generated, false otherwise.
6509 * @return this object for method chaining
6510 */
6511 public WearableExtender setHintContentIntentLaunchesActivity(
6512 boolean hintContentIntentLaunchesActivity) {
6513 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
6514 return this;
6515 }
6516
6517 /**
6518 * Get a hint that this notification's content intent will launch an {@link Activity}
6519 * directly, telling the platform that it can generate the appropriate transitions
6520 * @return {@code true} if the content intent will launch an activity and transitions should
6521 * be generated, false otherwise. The default value is {@code false} if this was never set.
6522 */
6523 public boolean getHintContentIntentLaunchesActivity() {
6524 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
6525 }
6526
Nadia Benbernou948627e2016-04-14 14:41:08 -04006527 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006528 * Sets the dismissal id for this notification. If a notification is posted with a
6529 * dismissal id, then when that notification is canceled, notifications on other wearables
6530 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04006531 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006532 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04006533 * @param dismissalId the dismissal id of the notification.
6534 * @return this object for method chaining
6535 */
6536 public WearableExtender setDismissalId(String dismissalId) {
6537 mDismissalId = dismissalId;
6538 return this;
6539 }
6540
6541 /**
6542 * Returns the dismissal id of the notification.
6543 * @return the dismissal id of the notification or null if it has not been set.
6544 */
6545 public String getDismissalId() {
6546 return mDismissalId;
6547 }
6548
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006549 /**
6550 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
6551 * posted from a phone to provide finer-grained control on what notifications are bridged
6552 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
6553 * Features to Notifications</a> for more information.
6554 * @param bridgeTag the bridge tag of the notification.
6555 * @return this object for method chaining
6556 */
6557 public WearableExtender setBridgeTag(String bridgeTag) {
6558 mBridgeTag = bridgeTag;
6559 return this;
6560 }
6561
6562 /**
6563 * Returns the bridge tag of the notification.
6564 * @return the bridge tag or null if not present.
6565 */
6566 public String getBridgeTag() {
6567 return mBridgeTag;
6568 }
6569
Griff Hazen61a9e862014-05-22 16:05:19 -07006570 private void setFlag(int mask, boolean value) {
6571 if (value) {
6572 mFlags |= mask;
6573 } else {
6574 mFlags &= ~mask;
6575 }
6576 }
6577 }
6578
6579 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006580 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
6581 * with car extensions:
6582 *
6583 * <ol>
6584 * <li>Create an {@link Notification.Builder}, setting any desired
6585 * properties.
6586 * <li>Create a {@link CarExtender}.
6587 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
6588 * {@link CarExtender}.
6589 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
6590 * to apply the extensions to a notification.
6591 * </ol>
6592 *
6593 * <pre class="prettyprint">
6594 * Notification notification = new Notification.Builder(context)
6595 * ...
6596 * .extend(new CarExtender()
6597 * .set*(...))
6598 * .build();
6599 * </pre>
6600 *
6601 * <p>Car extensions can be accessed on an existing notification by using the
6602 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
6603 * to access values.
6604 */
6605 public static final class CarExtender implements Extender {
6606 private static final String TAG = "CarExtender";
6607
6608 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
6609 private static final String EXTRA_LARGE_ICON = "large_icon";
6610 private static final String EXTRA_CONVERSATION = "car_conversation";
6611 private static final String EXTRA_COLOR = "app_color";
6612
6613 private Bitmap mLargeIcon;
6614 private UnreadConversation mUnreadConversation;
6615 private int mColor = Notification.COLOR_DEFAULT;
6616
6617 /**
6618 * Create a {@link CarExtender} with default options.
6619 */
6620 public CarExtender() {
6621 }
6622
6623 /**
6624 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
6625 *
6626 * @param notif The notification from which to copy options.
6627 */
6628 public CarExtender(Notification notif) {
6629 Bundle carBundle = notif.extras == null ?
6630 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
6631 if (carBundle != null) {
6632 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
6633 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
6634
6635 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
6636 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
6637 }
6638 }
6639
6640 /**
6641 * Apply car extensions to a notification that is being built. This is typically called by
6642 * the {@link Notification.Builder#extend(Notification.Extender)}
6643 * method of {@link Notification.Builder}.
6644 */
6645 @Override
6646 public Notification.Builder extend(Notification.Builder builder) {
6647 Bundle carExtensions = new Bundle();
6648
6649 if (mLargeIcon != null) {
6650 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
6651 }
6652 if (mColor != Notification.COLOR_DEFAULT) {
6653 carExtensions.putInt(EXTRA_COLOR, mColor);
6654 }
6655
6656 if (mUnreadConversation != null) {
6657 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
6658 carExtensions.putBundle(EXTRA_CONVERSATION, b);
6659 }
6660
6661 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
6662 return builder;
6663 }
6664
6665 /**
6666 * Sets the accent color to use when Android Auto presents the notification.
6667 *
6668 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
6669 * to accent the displayed notification. However, not all colors are acceptable in an
6670 * automotive setting. This method can be used to override the color provided in the
6671 * notification in such a situation.
6672 */
Tor Norbye80756e32015-03-02 09:39:27 -08006673 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006674 mColor = color;
6675 return this;
6676 }
6677
6678 /**
6679 * Gets the accent color.
6680 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006681 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006682 */
Tor Norbye80756e32015-03-02 09:39:27 -08006683 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006684 public int getColor() {
6685 return mColor;
6686 }
6687
6688 /**
6689 * Sets the large icon of the car notification.
6690 *
6691 * If no large icon is set in the extender, Android Auto will display the icon
6692 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
6693 *
6694 * @param largeIcon The large icon to use in the car notification.
6695 * @return This object for method chaining.
6696 */
6697 public CarExtender setLargeIcon(Bitmap largeIcon) {
6698 mLargeIcon = largeIcon;
6699 return this;
6700 }
6701
6702 /**
6703 * Gets the large icon used in this car notification, or null if no icon has been set.
6704 *
6705 * @return The large icon for the car notification.
6706 * @see CarExtender#setLargeIcon
6707 */
6708 public Bitmap getLargeIcon() {
6709 return mLargeIcon;
6710 }
6711
6712 /**
6713 * Sets the unread conversation in a message notification.
6714 *
6715 * @param unreadConversation The unread part of the conversation this notification conveys.
6716 * @return This object for method chaining.
6717 */
6718 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
6719 mUnreadConversation = unreadConversation;
6720 return this;
6721 }
6722
6723 /**
6724 * Returns the unread conversation conveyed by this notification.
6725 * @see #setUnreadConversation(UnreadConversation)
6726 */
6727 public UnreadConversation getUnreadConversation() {
6728 return mUnreadConversation;
6729 }
6730
6731 /**
6732 * A class which holds the unread messages from a conversation.
6733 */
6734 public static class UnreadConversation {
6735 private static final String KEY_AUTHOR = "author";
6736 private static final String KEY_TEXT = "text";
6737 private static final String KEY_MESSAGES = "messages";
6738 private static final String KEY_REMOTE_INPUT = "remote_input";
6739 private static final String KEY_ON_REPLY = "on_reply";
6740 private static final String KEY_ON_READ = "on_read";
6741 private static final String KEY_PARTICIPANTS = "participants";
6742 private static final String KEY_TIMESTAMP = "timestamp";
6743
6744 private final String[] mMessages;
6745 private final RemoteInput mRemoteInput;
6746 private final PendingIntent mReplyPendingIntent;
6747 private final PendingIntent mReadPendingIntent;
6748 private final String[] mParticipants;
6749 private final long mLatestTimestamp;
6750
6751 UnreadConversation(String[] messages, RemoteInput remoteInput,
6752 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
6753 String[] participants, long latestTimestamp) {
6754 mMessages = messages;
6755 mRemoteInput = remoteInput;
6756 mReadPendingIntent = readPendingIntent;
6757 mReplyPendingIntent = replyPendingIntent;
6758 mParticipants = participants;
6759 mLatestTimestamp = latestTimestamp;
6760 }
6761
6762 /**
6763 * Gets the list of messages conveyed by this notification.
6764 */
6765 public String[] getMessages() {
6766 return mMessages;
6767 }
6768
6769 /**
6770 * Gets the remote input that will be used to convey the response to a message list, or
6771 * null if no such remote input exists.
6772 */
6773 public RemoteInput getRemoteInput() {
6774 return mRemoteInput;
6775 }
6776
6777 /**
6778 * Gets the pending intent that will be triggered when the user replies to this
6779 * notification.
6780 */
6781 public PendingIntent getReplyPendingIntent() {
6782 return mReplyPendingIntent;
6783 }
6784
6785 /**
6786 * Gets the pending intent that Android Auto will send after it reads aloud all messages
6787 * in this object's message list.
6788 */
6789 public PendingIntent getReadPendingIntent() {
6790 return mReadPendingIntent;
6791 }
6792
6793 /**
6794 * Gets the participants in the conversation.
6795 */
6796 public String[] getParticipants() {
6797 return mParticipants;
6798 }
6799
6800 /**
6801 * Gets the firs participant in the conversation.
6802 */
6803 public String getParticipant() {
6804 return mParticipants.length > 0 ? mParticipants[0] : null;
6805 }
6806
6807 /**
6808 * Gets the timestamp of the conversation.
6809 */
6810 public long getLatestTimestamp() {
6811 return mLatestTimestamp;
6812 }
6813
6814 Bundle getBundleForUnreadConversation() {
6815 Bundle b = new Bundle();
6816 String author = null;
6817 if (mParticipants != null && mParticipants.length > 1) {
6818 author = mParticipants[0];
6819 }
6820 Parcelable[] messages = new Parcelable[mMessages.length];
6821 for (int i = 0; i < messages.length; i++) {
6822 Bundle m = new Bundle();
6823 m.putString(KEY_TEXT, mMessages[i]);
6824 m.putString(KEY_AUTHOR, author);
6825 messages[i] = m;
6826 }
6827 b.putParcelableArray(KEY_MESSAGES, messages);
6828 if (mRemoteInput != null) {
6829 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
6830 }
6831 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
6832 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
6833 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
6834 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
6835 return b;
6836 }
6837
6838 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
6839 if (b == null) {
6840 return null;
6841 }
6842 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
6843 String[] messages = null;
6844 if (parcelableMessages != null) {
6845 String[] tmp = new String[parcelableMessages.length];
6846 boolean success = true;
6847 for (int i = 0; i < tmp.length; i++) {
6848 if (!(parcelableMessages[i] instanceof Bundle)) {
6849 success = false;
6850 break;
6851 }
6852 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
6853 if (tmp[i] == null) {
6854 success = false;
6855 break;
6856 }
6857 }
6858 if (success) {
6859 messages = tmp;
6860 } else {
6861 return null;
6862 }
6863 }
6864
6865 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
6866 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
6867
6868 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
6869
6870 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
6871 if (participants == null || participants.length != 1) {
6872 return null;
6873 }
6874
6875 return new UnreadConversation(messages,
6876 remoteInput,
6877 onReply,
6878 onRead,
6879 participants, b.getLong(KEY_TIMESTAMP));
6880 }
6881 };
6882
6883 /**
6884 * Builder class for {@link CarExtender.UnreadConversation} objects.
6885 */
6886 public static class Builder {
6887 private final List<String> mMessages = new ArrayList<String>();
6888 private final String mParticipant;
6889 private RemoteInput mRemoteInput;
6890 private PendingIntent mReadPendingIntent;
6891 private PendingIntent mReplyPendingIntent;
6892 private long mLatestTimestamp;
6893
6894 /**
6895 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
6896 *
6897 * @param name The name of the other participant in the conversation.
6898 */
6899 public Builder(String name) {
6900 mParticipant = name;
6901 }
6902
6903 /**
6904 * Appends a new unread message to the list of messages for this conversation.
6905 *
6906 * The messages should be added from oldest to newest.
6907 *
6908 * @param message The text of the new unread message.
6909 * @return This object for method chaining.
6910 */
6911 public Builder addMessage(String message) {
6912 mMessages.add(message);
6913 return this;
6914 }
6915
6916 /**
6917 * Sets the pending intent and remote input which will convey the reply to this
6918 * notification.
6919 *
6920 * @param pendingIntent The pending intent which will be triggered on a reply.
6921 * @param remoteInput The remote input parcelable which will carry the reply.
6922 * @return This object for method chaining.
6923 *
6924 * @see CarExtender.UnreadConversation#getRemoteInput
6925 * @see CarExtender.UnreadConversation#getReplyPendingIntent
6926 */
6927 public Builder setReplyAction(
6928 PendingIntent pendingIntent, RemoteInput remoteInput) {
6929 mRemoteInput = remoteInput;
6930 mReplyPendingIntent = pendingIntent;
6931
6932 return this;
6933 }
6934
6935 /**
6936 * Sets the pending intent that will be sent once the messages in this notification
6937 * are read.
6938 *
6939 * @param pendingIntent The pending intent to use.
6940 * @return This object for method chaining.
6941 */
6942 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
6943 mReadPendingIntent = pendingIntent;
6944 return this;
6945 }
6946
6947 /**
6948 * Sets the timestamp of the most recent message in an unread conversation.
6949 *
6950 * If a messaging notification has been posted by your application and has not
6951 * yet been cancelled, posting a later notification with the same id and tag
6952 * but without a newer timestamp may result in Android Auto not displaying a
6953 * heads up notification for the later notification.
6954 *
6955 * @param timestamp The timestamp of the most recent message in the conversation.
6956 * @return This object for method chaining.
6957 */
6958 public Builder setLatestTimestamp(long timestamp) {
6959 mLatestTimestamp = timestamp;
6960 return this;
6961 }
6962
6963 /**
6964 * Builds a new unread conversation object.
6965 *
6966 * @return The new unread conversation object.
6967 */
6968 public UnreadConversation build() {
6969 String[] messages = mMessages.toArray(new String[mMessages.size()]);
6970 String[] participants = { mParticipant };
6971 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
6972 mReadPendingIntent, participants, mLatestTimestamp);
6973 }
6974 }
6975 }
6976
6977 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07006978 * Get an array of Notification objects from a parcelable array bundle field.
6979 * Update the bundle to have a typed array so fetches in the future don't need
6980 * to do an array copy.
6981 */
6982 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
6983 Parcelable[] array = bundle.getParcelableArray(key);
6984 if (array instanceof Notification[] || array == null) {
6985 return (Notification[]) array;
6986 }
6987 Notification[] typedArray = Arrays.copyOf(array, array.length,
6988 Notification[].class);
6989 bundle.putParcelableArray(key, typedArray);
6990 return typedArray;
6991 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006992
6993 private static class BuilderRemoteViews extends RemoteViews {
6994 public BuilderRemoteViews(Parcel parcel) {
6995 super(parcel);
6996 }
6997
Kenny Guy77320062014-08-27 21:37:15 +01006998 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
6999 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007000 }
7001
7002 @Override
7003 public BuilderRemoteViews clone() {
7004 Parcel p = Parcel.obtain();
7005 writeToParcel(p, 0);
7006 p.setDataPosition(0);
7007 BuilderRemoteViews brv = new BuilderRemoteViews(p);
7008 p.recycle();
7009 return brv;
7010 }
7011 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08007012
7013 private static class StandardTemplateParams {
7014 boolean hasProgress = true;
7015 boolean ambient = false;
7016 CharSequence title;
7017 CharSequence text;
7018
7019 final StandardTemplateParams reset() {
7020 hasProgress = true;
7021 ambient = false;
7022 title = null;
7023 text = null;
7024 return this;
7025 }
7026
7027 final StandardTemplateParams hasProgress(boolean hasProgress) {
7028 this.hasProgress = hasProgress;
7029 return this;
7030 }
7031
7032 final StandardTemplateParams title(CharSequence title) {
7033 this.title = title;
7034 return this;
7035 }
7036
7037 final StandardTemplateParams text(CharSequence text) {
7038 this.text = text;
7039 return this;
7040 }
7041
7042 final StandardTemplateParams ambient(boolean ambient) {
7043 this.ambient = ambient;
7044 return this;
7045 }
7046
7047 final StandardTemplateParams fillTextsFrom(Builder b) {
7048 Bundle extras = b.mN.extras;
7049 title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE));
7050 text = b.processLegacyText(extras.getCharSequence(EXTRA_TEXT));
7051 return this;
7052 }
7053 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007054}