blob: 1103d9e900c1f4a9ba9adfa0e0d1e6e126a18a94 [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;
Jeff Browndba34ba2014-06-24 20:46:03 -070040import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040042import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020043import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050044import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045import android.os.Parcel;
46import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040047import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070048import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070049import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080050import android.text.SpannableStringBuilder;
51import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080053import android.text.style.AbsoluteSizeSpan;
Selim Cinek981962e2016-07-20 20:41:58 -070054import android.text.style.BackgroundColorSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080055import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070056import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080057import android.text.style.RelativeSizeSpan;
58import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070059import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040060import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050061import android.util.SparseArray;
Griff Hazen61a9e862014-05-22 16:05:19 -070062import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080063import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080064import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070065import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070066import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067import android.widget.RemoteViews;
68
Griff Hazen959591e2014-05-15 22:26:18 -070069import com.android.internal.R;
Svet Ganovddb94882016-06-23 19:55:24 -070070import com.android.internal.util.ArrayUtils;
Griff Hazenc091ba82014-05-16 10:13:26 -070071import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070072
Tor Norbyed9273d62013-05-30 15:59:53 -070073import java.lang.annotation.Retention;
74import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020075import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050076import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070077import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070078import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070079import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050080import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080081
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082/**
83 * A class that represents how a persistent notification is to be presented to
84 * the user using the {@link android.app.NotificationManager}.
85 *
Joe Onoratocb109a02011-01-18 17:57:41 -080086 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
87 * easier to construct Notifications.</p>
88 *
Joe Fernandez558459f2011-10-13 16:47:36 -070089 * <div class="special reference">
90 * <h3>Developer Guides</h3>
91 * <p>For a guide to creating notifications, read the
92 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
93 * developer guide.</p>
94 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 */
96public class Notification implements Parcelable
97{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040098 private static final String TAG = "Notification";
99
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400101 * An activity that provides a user interface for adjusting notification preferences for its
102 * containing application. Optional but recommended for apps that post
103 * {@link android.app.Notification Notifications}.
104 */
105 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
106 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
107 = "android.intent.category.NOTIFICATION_PREFERENCES";
108
109 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 * Use all default values (where applicable).
111 */
112 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 /**
115 * Use the default notification sound. This will ignore any given
116 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500117 *
Chris Wren47c20a12014-06-18 17:27:29 -0400118 * <p>
119 * A notification that is noisy is more likely to be presented as a heads-up notification.
120 * </p>
121 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500123 */
124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 public static final int DEFAULT_SOUND = 1;
126
127 /**
128 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500129 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700130 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500131 *
Chris Wren47c20a12014-06-18 17:27:29 -0400132 * <p>
133 * A notification that vibrates is more likely to be presented as a heads-up notification.
134 * </p>
135 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500137 */
138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 /**
142 * Use the default notification lights. This will ignore the
143 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
144 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500145 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500147 */
148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500150
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200152 * Maximum length of CharSequences accepted by Builder and friends.
153 *
154 * <p>
155 * Avoids spamming the system with overly large strings such as full e-mails.
156 */
157 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
158
159 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800160 * Maximum entries of reply text that are accepted by Builder and friends.
161 */
162 private static final int MAX_REPLY_HISTORY = 5;
163
164 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500165 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800166 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500167 * Default value: {@link System#currentTimeMillis() Now}.
168 *
169 * Choose a timestamp that will be most relevant to the user. For most finite events, this
170 * corresponds to the time the event happened (or will happen, in the case of events that have
171 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800172 * timestamped according to when the activity began.
173 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500174 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800175 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500176 * <ul>
177 * <li>Notification of a new chat message should be stamped when the message was received.</li>
178 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
179 * <li>Notification of a completed file download should be stamped when the download finished.</li>
180 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
181 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
182 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800183 * </ul>
184 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700185 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
186 * anymore by default and must be opted into by using
187 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 */
189 public long when;
190
191 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700192 * The creation time of the notification
193 */
194 private long creationTime;
195
196 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400198 *
199 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 */
Dan Sandler86647982015-05-13 23:41:13 -0400201 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700202 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 public int icon;
204
205 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800206 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
207 * leave it at its default value of 0.
208 *
209 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700210 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800211 */
212 public int iconLevel;
213
214 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500215 * The number of events that this notification represents. For example, in a new mail
216 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800217 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500218 * The system may or may not use this field to modify the appearance of the notification. For
219 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
220 * superimposed over the icon in the status bar. Starting with
221 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
222 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800223 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500224 * If the number is 0 or negative, it is never shown.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -0700225 *
226 * @deprecated this number is not shown anymore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 */
228 public int number;
229
230 /**
231 * The intent to execute when the expanded status entry is clicked. If
232 * this is an activity, it must include the
233 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800234 * that you take care of task management as described in the
235 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800236 * Stack</a> document. In particular, make sure to read the notification section
237 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
238 * Notifications</a> for the correct ways to launch an application from a
239 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 */
241 public PendingIntent contentIntent;
242
243 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500244 * The intent to execute when the notification is explicitly dismissed by the user, either with
245 * the "Clear All" button or by swiping it away individually.
246 *
247 * This probably shouldn't be launching an activity since several of those will be sent
248 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800249 */
250 public PendingIntent deleteIntent;
251
252 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700253 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800254 *
Chris Wren47c20a12014-06-18 17:27:29 -0400255 * <p>
256 * The system UI may choose to display a heads-up notification, instead of
257 * launching this intent, while the user is using the device.
258 * </p>
259 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800260 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400261 */
262 public PendingIntent fullScreenIntent;
263
264 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400265 * Text that summarizes this notification for accessibility services.
266 *
267 * As of the L release, this text is no longer shown on screen, but it is still useful to
268 * accessibility services (where it serves as an audible announcement of the notification's
269 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400270 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800271 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 */
273 public CharSequence tickerText;
274
275 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400276 * Formerly, a view showing the {@link #tickerText}.
277 *
278 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400279 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400280 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800281 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400282
283 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400284 * The view that will represent this notification in the notification list (which is pulled
285 * down from the status bar).
286 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500287 * As of N, this field may be null. The notification view is determined by the inputs
288 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400289 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800290 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400291 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800292 public RemoteViews contentView;
293
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400294 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400295 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400296 * opportunity to show more detail. The system UI may choose to show this
297 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400298 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500299 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400300 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
301 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400302 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400303 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400304 public RemoteViews bigContentView;
305
Chris Wren8fd39ec2014-02-27 17:43:26 -0500306
307 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400308 * A medium-format version of {@link #contentView}, providing the Notification an
309 * opportunity to add action buttons to contentView. At its discretion, the system UI may
310 * choose to show this as a heads-up notification, which will pop up so the user can see
311 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400312 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500313 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400314 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
315 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500316 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400317 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500318 public RemoteViews headsUpContentView;
319
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400320 /**
Dan Sandler86647982015-05-13 23:41:13 -0400321 * A large bitmap to be shown in the notification content area.
322 *
323 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800324 */
Dan Sandler86647982015-05-13 23:41:13 -0400325 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800326 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327
328 /**
329 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500330 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800331 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400332 * A notification that is noisy is more likely to be presented as a heads-up notification.
333 * </p>
334 *
335 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500336 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 * </p>
338 */
339 public Uri sound;
340
341 /**
342 * Use this constant as the value for audioStreamType to request that
343 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700344 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400345 *
346 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700348 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800349 public static final int STREAM_DEFAULT = -1;
350
351 /**
352 * The audio stream type to use when playing the sound.
353 * Should be one of the STREAM_ constants from
354 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400355 *
356 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800357 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700358 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800359 public int audioStreamType = STREAM_DEFAULT;
360
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800361 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400362 * The default value of {@link #audioAttributes}.
363 */
364 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
365 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
366 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
367 .build();
368
369 /**
370 * The {@link AudioAttributes audio attributes} to use when playing the sound.
371 */
372 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
373
374 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500375 * The pattern with which to vibrate.
376 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800377 * <p>
378 * To vibrate the default pattern, see {@link #defaults}.
379 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500380 *
Chris Wren47c20a12014-06-18 17:27:29 -0400381 * <p>
382 * A notification that vibrates is more likely to be presented as a heads-up notification.
383 * </p>
384 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800385 * @see android.os.Vibrator#vibrate(long[],int)
386 */
387 public long[] vibrate;
388
389 /**
390 * The color of the led. The hardware will do its best approximation.
391 *
392 * @see #FLAG_SHOW_LIGHTS
393 * @see #flags
394 */
Tor Norbye80756e32015-03-02 09:39:27 -0800395 @ColorInt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396 public int ledARGB;
397
398 /**
399 * The number of milliseconds for the LED to be on while it's flashing.
400 * The hardware will do its best approximation.
401 *
402 * @see #FLAG_SHOW_LIGHTS
403 * @see #flags
404 */
405 public int ledOnMS;
406
407 /**
408 * The number of milliseconds for the LED to be off while it's flashing.
409 * The hardware will do its best approximation.
410 *
411 * @see #FLAG_SHOW_LIGHTS
412 * @see #flags
413 */
414 public int ledOffMS;
415
416 /**
417 * Specifies which values should be taken from the defaults.
418 * <p>
419 * To set, OR the desired from {@link #DEFAULT_SOUND},
420 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
421 * values, use {@link #DEFAULT_ALL}.
422 * </p>
423 */
424 public int defaults;
425
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800426 /**
427 * Bit to be bitwise-ored into the {@link #flags} field that should be
428 * set if you want the LED on for this notification.
429 * <ul>
430 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
431 * or 0 for both ledOnMS and ledOffMS.</li>
432 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
433 * <li>To flash the LED, pass the number of milliseconds that it should
434 * be on and off to ledOnMS and ledOffMS.</li>
435 * </ul>
436 * <p>
437 * Since hardware varies, you are not guaranteed that any of the values
438 * you pass are honored exactly. Use the system defaults (TODO) if possible
439 * because they will be set to values that work on any given hardware.
440 * <p>
441 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500442 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 */
444 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
445
446 /**
447 * Bit to be bitwise-ored into the {@link #flags} field that should be
448 * set if this notification is in reference to something that is ongoing,
449 * like a phone call. It should not be set if this notification is in
450 * reference to something that happened at a particular point in time,
451 * like a missed phone call.
452 */
453 public static final int FLAG_ONGOING_EVENT = 0x00000002;
454
455 /**
456 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700457 * the audio will be repeated until the notification is
458 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800459 */
460 public static final int FLAG_INSISTENT = 0x00000004;
461
462 /**
463 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700464 * set if you would only like the sound, vibrate and ticker to be played
465 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800466 */
467 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
468
469 /**
470 * Bit to be bitwise-ored into the {@link #flags} field that should be
471 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500472 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800473 */
474 public static final int FLAG_AUTO_CANCEL = 0x00000010;
475
476 /**
477 * Bit to be bitwise-ored into the {@link #flags} field that should be
478 * set if the notification should not be canceled when the user clicks
479 * the Clear all button.
480 */
481 public static final int FLAG_NO_CLEAR = 0x00000020;
482
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700483 /**
484 * Bit to be bitwise-ored into the {@link #flags} field that should be
485 * set if this notification represents a currently running service. This
486 * will normally be set for you by {@link Service#startForeground}.
487 */
488 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
489
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400490 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500491 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800492 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500493 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400494 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500495 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400496
Griff Hazendfcb0802014-02-11 12:00:00 -0800497 /**
498 * Bit to be bitswise-ored into the {@link #flags} field that should be
499 * set if this notification is relevant to the current device only
500 * and it is not recommended that it bridge to other devices.
501 */
502 public static final int FLAG_LOCAL_ONLY = 0x00000100;
503
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700504 /**
505 * Bit to be bitswise-ored into the {@link #flags} field that should be
506 * set if this notification is the group summary for a group of notifications.
507 * Grouped notifications may display in a cluster or stack on devices which
508 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
509 */
510 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
511
Julia Reynoldse46bb372016-03-17 11:05:58 -0400512 /**
513 * Bit to be bitswise-ored into the {@link #flags} field that should be
514 * set if this notification is the group summary for an auto-group of notifications.
515 *
516 * @hide
517 */
518 @SystemApi
519 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
520
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800521 public int flags;
522
Tor Norbyed9273d62013-05-30 15:59:53 -0700523 /** @hide */
524 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
525 @Retention(RetentionPolicy.SOURCE)
526 public @interface Priority {}
527
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800528 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500529 * Default notification {@link #priority}. If your application does not prioritize its own
530 * notifications, use this value for all notifications.
531 */
532 public static final int PRIORITY_DEFAULT = 0;
533
534 /**
535 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
536 * items smaller, or at a different position in the list, compared with your app's
537 * {@link #PRIORITY_DEFAULT} items.
538 */
539 public static final int PRIORITY_LOW = -1;
540
541 /**
542 * Lowest {@link #priority}; these items might not be shown to the user except under special
543 * circumstances, such as detailed notification logs.
544 */
545 public static final int PRIORITY_MIN = -2;
546
547 /**
548 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
549 * show these items larger, or at a different position in notification lists, compared with
550 * your app's {@link #PRIORITY_DEFAULT} items.
551 */
552 public static final int PRIORITY_HIGH = 1;
553
554 /**
555 * Highest {@link #priority}, for your application's most important items that require the
556 * user's prompt attention or input.
557 */
558 public static final int PRIORITY_MAX = 2;
559
560 /**
561 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800562 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500563 * Priority is an indication of how much of the user's valuable attention should be consumed by
564 * this notification. Low-priority notifications may be hidden from the user in certain
565 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500566 * system will make a determination about how to interpret this priority when presenting
567 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400568 *
569 * <p>
570 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
571 * as a heads-up notification.
572 * </p>
573 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500574 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700575 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500576 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800577
Dan Sandler26e81cf2014-05-06 10:01:27 -0400578 /**
579 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
580 * to be applied by the standard Style templates when presenting this notification.
581 *
582 * The current template design constructs a colorful header image by overlaying the
583 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
584 * ignored.
585 */
Tor Norbye80756e32015-03-02 09:39:27 -0800586 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400587 public int color = COLOR_DEFAULT;
588
589 /**
590 * Special value of {@link #color} telling the system not to decorate this notification with
591 * any special color but instead use default colors when presenting this notification.
592 */
Tor Norbye80756e32015-03-02 09:39:27 -0800593 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400594 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600595
596 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800597 * Special value of {@link #color} used as a place holder for an invalid color.
598 */
599 @ColorInt
600 private static final int COLOR_INVALID = 1;
601
602 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700603 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
604 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600605 * lockscreen).
606 *
607 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
608 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
609 * shown in all situations, but the contents are only available if the device is unlocked for
610 * the appropriate user.
611 *
612 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
613 * can be read even in an "insecure" context (that is, above a secure lockscreen).
614 * To modify the public version of this notification—for example, to redact some portions—see
615 * {@link Builder#setPublicVersion(Notification)}.
616 *
617 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
618 * and ticker until the user has bypassed the lockscreen.
619 */
620 public int visibility;
621
Griff Hazenfc3922d2014-08-20 11:56:44 -0700622 /**
623 * Notification visibility: Show this notification in its entirety on all lockscreens.
624 *
625 * {@see #visibility}
626 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600627 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700628
629 /**
630 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
631 * private information on secure lockscreens.
632 *
633 * {@see #visibility}
634 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600635 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700636
637 /**
638 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
639 *
640 * {@see #visibility}
641 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600642 public static final int VISIBILITY_SECRET = -1;
643
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500644 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400645 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500646 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400647 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500648
649 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400650 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500651 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400652 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500653
654 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400655 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500656 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400657 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500658
659 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400660 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500661 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400662 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500663
664 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400665 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500666 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400667 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500668
669 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400670 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500671 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400672 public static final String CATEGORY_ALARM = "alarm";
673
674 /**
675 * Notification category: progress of a long-running background operation.
676 */
677 public static final String CATEGORY_PROGRESS = "progress";
678
679 /**
680 * Notification category: social network or sharing update.
681 */
682 public static final String CATEGORY_SOCIAL = "social";
683
684 /**
685 * Notification category: error in background operation or authentication status.
686 */
687 public static final String CATEGORY_ERROR = "err";
688
689 /**
690 * Notification category: media transport control for playback.
691 */
692 public static final String CATEGORY_TRANSPORT = "transport";
693
694 /**
695 * Notification category: system or device status update. Reserved for system use.
696 */
697 public static final String CATEGORY_SYSTEM = "sys";
698
699 /**
700 * Notification category: indication of running background service.
701 */
702 public static final String CATEGORY_SERVICE = "service";
703
704 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400705 * Notification category: a specific, timely recommendation for a single thing.
706 * For example, a news app might want to recommend a news story it believes the user will
707 * want to read next.
708 */
709 public static final String CATEGORY_RECOMMENDATION = "recommendation";
710
711 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400712 * Notification category: ongoing information about device or contextual status.
713 */
714 public static final String CATEGORY_STATUS = "status";
715
716 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400717 * Notification category: user-scheduled reminder.
718 */
719 public static final String CATEGORY_REMINDER = "reminder";
720
721 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400722 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
723 * that best describes this Notification. May be used by the system for ranking and filtering.
724 */
725 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500726
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700727 private String mGroupKey;
728
729 /**
730 * Get the key used to group this notification into a cluster or stack
731 * with other notifications on devices which support such rendering.
732 */
733 public String getGroup() {
734 return mGroupKey;
735 }
736
737 private String mSortKey;
738
739 /**
740 * Get a sort key that orders this notification among other notifications from the
741 * same package. This can be useful if an external sort was already applied and an app
742 * would like to preserve this. Notifications will be sorted lexicographically using this
743 * value, although providing different priorities in addition to providing sort key may
744 * cause this value to be ignored.
745 *
746 * <p>This sort key can also be used to order members of a notification group. See
747 * {@link Builder#setGroup}.
748 *
749 * @see String#compareTo(String)
750 */
751 public String getSortKey() {
752 return mSortKey;
753 }
754
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500755 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400756 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400757 * <p>
758 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
759 * APIs, and are intended to be used by
760 * {@link android.service.notification.NotificationListenerService} implementations to extract
761 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500762 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400763 public Bundle extras = new Bundle();
764
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400765 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700766 * All pending intents in the notification as the system needs to be able to access them but
767 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700768 * custom parcelable objects.
769 *
770 * @hide
771 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700772 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700773
774 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400775 * {@link #extras} key: this is the title of the notification,
776 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
777 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500778 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400779
780 /**
781 * {@link #extras} key: this is the title of the notification when shown in expanded form,
782 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
783 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400784 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400785
786 /**
787 * {@link #extras} key: this is the main text payload, as supplied to
788 * {@link Builder#setContentText(CharSequence)}.
789 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500790 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400791
792 /**
793 * {@link #extras} key: this is a third line of text, as supplied to
794 * {@link Builder#setSubText(CharSequence)}.
795 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400796 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400797
798 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800799 * {@link #extras} key: this is the remote input history, as supplied to
800 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700801 *
802 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
803 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
804 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
805 * notifications once the other party has responded).
806 *
807 * The extra with this key is of type CharSequence[] and contains the most recent entry at
808 * the 0 index, the second most recent at the 1 index, etc.
809 *
810 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800811 */
812 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
813
814 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400815 * {@link #extras} key: this is a small piece of additional text as supplied to
816 * {@link Builder#setContentInfo(CharSequence)}.
817 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400818 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400819
820 /**
821 * {@link #extras} key: this is a line of summary information intended to be shown
822 * alongside expanded notifications, as supplied to (e.g.)
823 * {@link BigTextStyle#setSummaryText(CharSequence)}.
824 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400825 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400826
827 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200828 * {@link #extras} key: this is the longer text shown in the big form of a
829 * {@link BigTextStyle} notification, as supplied to
830 * {@link BigTextStyle#bigText(CharSequence)}.
831 */
832 public static final String EXTRA_BIG_TEXT = "android.bigText";
833
834 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400835 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
836 * supplied to {@link Builder#setSmallIcon(int)}.
837 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500838 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400839
840 /**
841 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
842 * notification payload, as
843 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
844 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400845 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400846
847 /**
848 * {@link #extras} key: this is a bitmap to be used instead of the one from
849 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
850 * shown in its expanded form, as supplied to
851 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
852 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400853 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400854
855 /**
856 * {@link #extras} key: this is the progress value supplied to
857 * {@link Builder#setProgress(int, int, boolean)}.
858 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400859 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400860
861 /**
862 * {@link #extras} key: this is the maximum value supplied to
863 * {@link Builder#setProgress(int, int, boolean)}.
864 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400865 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400866
867 /**
868 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
869 * {@link Builder#setProgress(int, int, boolean)}.
870 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400871 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400872
873 /**
874 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
875 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
876 * {@link Builder#setUsesChronometer(boolean)}.
877 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400878 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400879
880 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800881 * {@link #extras} key: whether the chronometer set on the notification should count down
882 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -0700883 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -0800884 */
Adrian Roos96b7e202016-05-17 13:50:38 -0700885 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -0800886
887 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400888 * {@link #extras} key: whether {@link #when} should be shown,
889 * as supplied to {@link Builder#setShowWhen(boolean)}.
890 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400891 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400892
893 /**
894 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
895 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
896 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400897 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400898
899 /**
900 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
901 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
902 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400903 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400904
905 /**
906 * {@link #extras} key: A string representing the name of the specific
907 * {@link android.app.Notification.Style} used to create this notification.
908 */
Chris Wren91ad5632013-06-05 15:05:57 -0400909 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400910
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400911 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400912 * {@link #extras} key: A String array containing the people that this notification relates to,
913 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400914 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400915 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500916
917 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400918 * Allow certain system-generated notifications to appear before the device is provisioned.
919 * Only available to notifications coming from the android package.
920 * @hide
921 */
922 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
923
924 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700925 * {@link #extras} key: A
926 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
927 * in the background when the notification is selected. The URI must point to an image stream
928 * suitable for passing into
929 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
930 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
931 * URI used for this purpose must require no permissions to read the image data.
932 */
933 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
934
935 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400936 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700937 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400938 * {@link android.app.Notification.MediaStyle} notification.
939 */
940 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
941
942 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100943 * {@link #extras} key: the indices of actions to be shown in the compact view,
944 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
945 */
946 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
947
Christoph Studer943aa672014-08-03 20:31:16 +0200948 /**
Alex Hillsfc737de2016-03-23 17:33:02 -0400949 * {@link #extras} key: the username to be displayed for all messages sent by the user including
950 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -0700951 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
952 * {@link CharSequence}
Alex Hillsfc737de2016-03-23 17:33:02 -0400953 */
954 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
955
956 /**
Adrian Roos96b7e202016-05-17 13:50:38 -0700957 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -0400958 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -0400959 */
Alex Hillsd9b04d92016-04-11 16:38:16 -0400960 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -0400961
962 /**
963 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
964 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -0700965 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
966 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -0400967 */
968 public static final String EXTRA_MESSAGES = "android.messages";
969
970 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100971 * {@link #extras} key: the user that built the notification.
972 *
973 * @hide
974 */
975 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
976
977 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400978 * @hide
979 */
980 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
981
Selim Cinek247fa012016-02-18 09:50:48 -0800982 /**
983 * @hide
984 */
985 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
986
Dan Sandler80eaa592016-04-14 23:34:54 -0400987 /** @hide */
988 @SystemApi
Dan Sandler732bd6c2016-04-12 14:20:32 -0400989 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
990
Dan Sandlerd63f9322015-05-06 15:18:49 -0400991 private Icon mSmallIcon;
992 private Icon mLargeIcon;
993
Chris Wren51c75102013-07-16 20:49:17 -0400994 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400995 * Structure to encapsulate a named action that can be shown as part of this notification.
996 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
997 * selected by the user.
998 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700999 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1000 * or {@link Notification.Builder#addAction(Notification.Action)}
1001 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001002 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001003 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -07001004 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001005 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001006 private final RemoteInput[] mRemoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001007 private boolean mAllowGeneratedReplies = false;
Griff Hazen959591e2014-05-15 22:26:18 -07001008
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001009 /**
1010 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001011 *
1012 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001013 */
Dan Sandler86647982015-05-13 23:41:13 -04001014 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001015 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001016
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001017 /**
1018 * Title of the action.
1019 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001020 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001021
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001022 /**
1023 * Intent to send when the user invokes this action. May be null, in which case the action
1024 * may be rendered in a disabled presentation by the system UI.
1025 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001026 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001027
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001028 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001029 if (in.readInt() != 0) {
1030 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001031 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1032 icon = mIcon.getResId();
1033 }
Dan Sandler86647982015-05-13 23:41:13 -04001034 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001035 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1036 if (in.readInt() == 1) {
1037 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1038 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001039 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001040 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001041 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001042 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001043
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001044 /**
Dan Sandler86647982015-05-13 23:41:13 -04001045 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001046 */
Dan Sandler86647982015-05-13 23:41:13 -04001047 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001048 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills42b0c4d2016-04-26 13:35:36 -04001049 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, false);
Griff Hazen959591e2014-05-15 22:26:18 -07001050 }
1051
Dan Sandler86647982015-05-13 23:41:13 -04001052 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001053 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001054 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001055 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1056 this.icon = icon.getResId();
1057 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001058 this.title = title;
1059 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001060 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001061 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001062 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001063 }
1064
1065 /**
Dan Sandler86647982015-05-13 23:41:13 -04001066 * Return an icon representing the action.
1067 */
1068 public Icon getIcon() {
1069 if (mIcon == null && icon != 0) {
1070 // you snuck an icon in here without using the builder; let's try to keep it
1071 mIcon = Icon.createWithResource("", icon);
1072 }
1073 return mIcon;
1074 }
1075
1076 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001077 * Get additional metadata carried around with this Action.
1078 */
1079 public Bundle getExtras() {
1080 return mExtras;
1081 }
1082
1083 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001084 * Return whether the platform should automatically generate possible replies for this
1085 * {@link Action}
1086 */
1087 public boolean getAllowGeneratedReplies() {
1088 return mAllowGeneratedReplies;
1089 }
1090
1091 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001092 * Get the list of inputs to be collected from the user when this action is sent.
1093 * May return null if no remote inputs were added.
1094 */
1095 public RemoteInput[] getRemoteInputs() {
1096 return mRemoteInputs;
1097 }
1098
1099 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001100 * Builder class for {@link Action} objects.
1101 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001102 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001103 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001104 private final CharSequence mTitle;
1105 private final PendingIntent mIntent;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001106 private boolean mAllowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001107 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001108 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001109
1110 /**
1111 * Construct a new builder for {@link Action} object.
1112 * @param icon icon to show for this action
1113 * @param title the title of the action
1114 * @param intent the {@link PendingIntent} to fire when users trigger this action
1115 */
Dan Sandler86647982015-05-13 23:41:13 -04001116 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001117 public Builder(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -04001118 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
1119 }
1120
1121 /**
1122 * Construct a new builder for {@link Action} object.
1123 * @param icon icon to show for this action
1124 * @param title the title of the action
1125 * @param intent the {@link PendingIntent} to fire when users trigger this action
1126 */
1127 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001128 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -07001129 }
1130
1131 /**
1132 * Construct a new builder for {@link Action} object using the fields from an
1133 * {@link Action}.
1134 * @param action the action to read fields from.
1135 */
1136 public Builder(Action action) {
Dan Sandler86647982015-05-13 23:41:13 -04001137 this(action.getIcon(), action.title, action.actionIntent, new Bundle(action.mExtras),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001138 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -07001139 }
1140
Dan Sandler86647982015-05-13 23:41:13 -04001141 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001142 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -07001143 mIcon = icon;
1144 mTitle = title;
1145 mIntent = intent;
1146 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001147 if (remoteInputs != null) {
1148 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1149 Collections.addAll(mRemoteInputs, remoteInputs);
1150 }
Griff Hazen959591e2014-05-15 22:26:18 -07001151 }
1152
1153 /**
1154 * Merge additional metadata into this builder.
1155 *
1156 * <p>Values within the Bundle will replace existing extras values in this Builder.
1157 *
1158 * @see Notification.Action#extras
1159 */
1160 public Builder addExtras(Bundle extras) {
1161 if (extras != null) {
1162 mExtras.putAll(extras);
1163 }
1164 return this;
1165 }
1166
1167 /**
1168 * Get the metadata Bundle used by this Builder.
1169 *
1170 * <p>The returned Bundle is shared with this Builder.
1171 */
1172 public Bundle getExtras() {
1173 return mExtras;
1174 }
1175
1176 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001177 * Add an input to be collected from the user when this action is sent.
1178 * Response values can be retrieved from the fired intent by using the
1179 * {@link RemoteInput#getResultsFromIntent} function.
1180 * @param remoteInput a {@link RemoteInput} to add to the action
1181 * @return this object for method chaining
1182 */
1183 public Builder addRemoteInput(RemoteInput remoteInput) {
1184 if (mRemoteInputs == null) {
1185 mRemoteInputs = new ArrayList<RemoteInput>();
1186 }
1187 mRemoteInputs.add(remoteInput);
1188 return this;
1189 }
1190
1191 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001192 * Set whether the platform should automatically generate possible replies to add to
1193 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1194 * {@link RemoteInput}, this has no effect.
1195 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1196 * otherwise
1197 * @return this object for method chaining
1198 * The default value is {@code false}
1199 */
1200 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1201 mAllowGeneratedReplies = allowGeneratedReplies;
1202 return this;
1203 }
1204
1205 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001206 * Apply an extender to this action builder. Extenders may be used to add
1207 * metadata or change options on this builder.
1208 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001209 public Builder extend(Extender extender) {
1210 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001211 return this;
1212 }
1213
1214 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001215 * Combine all of the options that have been set and return a new {@link Action}
1216 * object.
1217 * @return the built action
1218 */
1219 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001220 RemoteInput[] remoteInputs = mRemoteInputs != null
1221 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001222 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs,
1223 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001224 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001225 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001226
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001227 @Override
1228 public Action clone() {
1229 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001230 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001231 title,
1232 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001233 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001234 getRemoteInputs(),
1235 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001236 }
1237 @Override
1238 public int describeContents() {
1239 return 0;
1240 }
1241 @Override
1242 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001243 final Icon ic = getIcon();
1244 if (ic != null) {
1245 out.writeInt(1);
1246 ic.writeToParcel(out, 0);
1247 } else {
1248 out.writeInt(0);
1249 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001250 TextUtils.writeToParcel(title, out, flags);
1251 if (actionIntent != null) {
1252 out.writeInt(1);
1253 actionIntent.writeToParcel(out, flags);
1254 } else {
1255 out.writeInt(0);
1256 }
Griff Hazen959591e2014-05-15 22:26:18 -07001257 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001258 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001259 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001260 }
Griff Hazen959591e2014-05-15 22:26:18 -07001261 public static final Parcelable.Creator<Action> CREATOR =
1262 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001263 public Action createFromParcel(Parcel in) {
1264 return new Action(in);
1265 }
1266 public Action[] newArray(int size) {
1267 return new Action[size];
1268 }
1269 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001270
1271 /**
1272 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1273 * metadata or change options on an action builder.
1274 */
1275 public interface Extender {
1276 /**
1277 * Apply this extender to a notification action builder.
1278 * @param builder the builder to be modified.
1279 * @return the build object for chaining.
1280 */
1281 public Builder extend(Builder builder);
1282 }
1283
1284 /**
1285 * Wearable extender for notification actions. To add extensions to an action,
1286 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1287 * the {@code WearableExtender()} constructor and apply it to a
1288 * {@link android.app.Notification.Action.Builder} using
1289 * {@link android.app.Notification.Action.Builder#extend}.
1290 *
1291 * <pre class="prettyprint">
1292 * Notification.Action action = new Notification.Action.Builder(
1293 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001294 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001295 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001296 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001297 */
1298 public static final class WearableExtender implements Extender {
1299 /** Notification action extra which contains wearable extensions */
1300 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1301
Pete Gastaf6781d2014-10-07 15:17:05 -04001302 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001303 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001304 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1305 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1306 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001307
1308 // Flags bitwise-ored to mFlags
1309 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001310 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001311 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001312
1313 // Default value for flags integer
1314 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1315
1316 private int mFlags = DEFAULT_FLAGS;
1317
Pete Gastaf6781d2014-10-07 15:17:05 -04001318 private CharSequence mInProgressLabel;
1319 private CharSequence mConfirmLabel;
1320 private CharSequence mCancelLabel;
1321
Griff Hazen61a9e862014-05-22 16:05:19 -07001322 /**
1323 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1324 * options.
1325 */
1326 public WearableExtender() {
1327 }
1328
1329 /**
1330 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1331 * wearable options present in an existing notification action.
1332 * @param action the notification action to inspect.
1333 */
1334 public WearableExtender(Action action) {
1335 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1336 if (wearableBundle != null) {
1337 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001338 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1339 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1340 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001341 }
1342 }
1343
1344 /**
1345 * Apply wearable extensions to a notification action that is being built. This is
1346 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1347 * method of {@link android.app.Notification.Action.Builder}.
1348 */
1349 @Override
1350 public Action.Builder extend(Action.Builder builder) {
1351 Bundle wearableBundle = new Bundle();
1352
1353 if (mFlags != DEFAULT_FLAGS) {
1354 wearableBundle.putInt(KEY_FLAGS, mFlags);
1355 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001356 if (mInProgressLabel != null) {
1357 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1358 }
1359 if (mConfirmLabel != null) {
1360 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1361 }
1362 if (mCancelLabel != null) {
1363 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1364 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001365
1366 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1367 return builder;
1368 }
1369
1370 @Override
1371 public WearableExtender clone() {
1372 WearableExtender that = new WearableExtender();
1373 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001374 that.mInProgressLabel = this.mInProgressLabel;
1375 that.mConfirmLabel = this.mConfirmLabel;
1376 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001377 return that;
1378 }
1379
1380 /**
1381 * Set whether this action is available when the wearable device is not connected to
1382 * a companion device. The user can still trigger this action when the wearable device is
1383 * offline, but a visual hint will indicate that the action may not be available.
1384 * Defaults to true.
1385 */
1386 public WearableExtender setAvailableOffline(boolean availableOffline) {
1387 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1388 return this;
1389 }
1390
1391 /**
1392 * Get whether this action is available when the wearable device is not connected to
1393 * a companion device. The user can still trigger this action when the wearable device is
1394 * offline, but a visual hint will indicate that the action may not be available.
1395 * Defaults to true.
1396 */
1397 public boolean isAvailableOffline() {
1398 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1399 }
1400
1401 private void setFlag(int mask, boolean value) {
1402 if (value) {
1403 mFlags |= mask;
1404 } else {
1405 mFlags &= ~mask;
1406 }
1407 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001408
1409 /**
1410 * Set a label to display while the wearable is preparing to automatically execute the
1411 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1412 *
1413 * @param label the label to display while the action is being prepared to execute
1414 * @return this object for method chaining
1415 */
1416 public WearableExtender setInProgressLabel(CharSequence label) {
1417 mInProgressLabel = label;
1418 return this;
1419 }
1420
1421 /**
1422 * Get the label to display while the wearable is preparing to automatically execute
1423 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1424 *
1425 * @return the label to display while the action is being prepared to execute
1426 */
1427 public CharSequence getInProgressLabel() {
1428 return mInProgressLabel;
1429 }
1430
1431 /**
1432 * Set a label to display to confirm that the action should be executed.
1433 * This is usually an imperative verb like "Send".
1434 *
1435 * @param label the label to confirm the action should be executed
1436 * @return this object for method chaining
1437 */
1438 public WearableExtender setConfirmLabel(CharSequence label) {
1439 mConfirmLabel = label;
1440 return this;
1441 }
1442
1443 /**
1444 * Get the label to display to confirm that the action should be executed.
1445 * This is usually an imperative verb like "Send".
1446 *
1447 * @return the label to confirm the action should be executed
1448 */
1449 public CharSequence getConfirmLabel() {
1450 return mConfirmLabel;
1451 }
1452
1453 /**
1454 * Set a label to display to cancel the action.
1455 * This is usually an imperative verb, like "Cancel".
1456 *
1457 * @param label the label to display to cancel the action
1458 * @return this object for method chaining
1459 */
1460 public WearableExtender setCancelLabel(CharSequence label) {
1461 mCancelLabel = label;
1462 return this;
1463 }
1464
1465 /**
1466 * Get the label to display to cancel the action.
1467 * This is usually an imperative verb like "Cancel".
1468 *
1469 * @return the label to display to cancel the action
1470 */
1471 public CharSequence getCancelLabel() {
1472 return mCancelLabel;
1473 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001474
1475 /**
1476 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1477 * platform that it can generate the appropriate transitions.
1478 * @param hintLaunchesActivity {@code true} if the content intent will launch
1479 * an activity and transitions should be generated, false otherwise.
1480 * @return this object for method chaining
1481 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001482 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001483 boolean hintLaunchesActivity) {
1484 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1485 return this;
1486 }
1487
1488 /**
1489 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1490 * platform that it can generate the appropriate transitions
1491 * @return {@code true} if the content intent will launch an activity and transitions
1492 * should be generated, false otherwise. The default value is {@code false} if this was
1493 * never set.
1494 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001495 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001496 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1497 }
Alex Hills9f087612016-06-07 09:08:59 -04001498
1499 /**
1500 * Set a hint that this Action should be displayed inline.
1501 *
1502 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1503 * otherwise
1504 * @return this object for method chaining
1505 */
1506 public WearableExtender setHintDisplayActionInline(
1507 boolean hintDisplayInline) {
1508 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1509 return this;
1510 }
1511
1512 /**
1513 * Get a hint that this Action should be displayed inline.
1514 *
1515 * @return {@code true} if the Action should be displayed inline, {@code false}
1516 * otherwise. The default value is {@code false} if this was never set.
1517 */
1518 public boolean getHintDisplayActionInline() {
1519 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1520 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001521 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001522 }
1523
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001524 /**
1525 * Array of all {@link Action} structures attached to this notification by
1526 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1527 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1528 * interface for invoking actions.
1529 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001530 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001531
1532 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001533 * Replacement version of this notification whose content will be shown
1534 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1535 * and {@link #VISIBILITY_PUBLIC}.
1536 */
1537 public Notification publicVersion;
1538
1539 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001540 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001541 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 */
1543 public Notification()
1544 {
1545 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001546 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001547 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 }
1549
1550 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 * @hide
1552 */
1553 public Notification(Context context, int icon, CharSequence tickerText, long when,
1554 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1555 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001556 new Builder(context)
1557 .setWhen(when)
1558 .setSmallIcon(icon)
1559 .setTicker(tickerText)
1560 .setContentTitle(contentTitle)
1561 .setContentText(contentText)
1562 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1563 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 }
1565
1566 /**
1567 * Constructs a Notification object with the information needed to
1568 * have a status bar icon without the standard expanded view.
1569 *
1570 * @param icon The resource id of the icon to put in the status bar.
1571 * @param tickerText The text that flows by in the status bar when the notification first
1572 * activates.
1573 * @param when The time to show in the time field. In the System.currentTimeMillis
1574 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001575 *
1576 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001578 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001579 public Notification(int icon, CharSequence tickerText, long when)
1580 {
1581 this.icon = icon;
1582 this.tickerText = tickerText;
1583 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001584 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001585 }
1586
1587 /**
1588 * Unflatten the notification from a parcel.
1589 */
Svet Ganovddb94882016-06-23 19:55:24 -07001590 @SuppressWarnings("unchecked")
1591 public Notification(Parcel parcel) {
1592 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
1593 // intents in extras are always written as the last entry.
1594 readFromParcelImpl(parcel);
1595 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001596 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07001597 }
1598
1599 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 {
1601 int version = parcel.readInt();
1602
1603 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001604 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001605 if (parcel.readInt() != 0) {
1606 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001607 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1608 icon = mSmallIcon.getResId();
1609 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001610 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001611 number = parcel.readInt();
1612 if (parcel.readInt() != 0) {
1613 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1614 }
1615 if (parcel.readInt() != 0) {
1616 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1617 }
1618 if (parcel.readInt() != 0) {
1619 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1620 }
1621 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001622 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001623 }
1624 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001625 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1626 }
Joe Onorato561d3852010-11-20 18:09:34 -08001627 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001628 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001629 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001630 defaults = parcel.readInt();
1631 flags = parcel.readInt();
1632 if (parcel.readInt() != 0) {
1633 sound = Uri.CREATOR.createFromParcel(parcel);
1634 }
1635
1636 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001637 if (parcel.readInt() != 0) {
1638 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1639 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 vibrate = parcel.createLongArray();
1641 ledARGB = parcel.readInt();
1642 ledOnMS = parcel.readInt();
1643 ledOffMS = parcel.readInt();
1644 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001645
1646 if (parcel.readInt() != 0) {
1647 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1648 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001649
1650 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001651
John Spurlockfd7f1e02014-03-18 16:41:57 -04001652 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001653
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001654 mGroupKey = parcel.readString();
1655
1656 mSortKey = parcel.readString();
1657
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001658 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001659
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001660 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1661
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001662 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001663 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1664 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001665
Chris Wren8fd39ec2014-02-27 17:43:26 -05001666 if (parcel.readInt() != 0) {
1667 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1668 }
1669
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001670 visibility = parcel.readInt();
1671
1672 if (parcel.readInt() != 0) {
1673 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1674 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001675
1676 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001677 }
1678
Andy Stadler110988c2010-12-03 14:29:16 -08001679 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001680 public Notification clone() {
1681 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001682 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001683 return that;
1684 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001685
Daniel Sandler1a497d32013-04-18 14:52:45 -04001686 /**
1687 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1688 * of this into that.
1689 * @hide
1690 */
1691 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001692 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001693 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001694 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001695 that.number = this.number;
1696
1697 // PendingIntents are global, so there's no reason (or way) to clone them.
1698 that.contentIntent = this.contentIntent;
1699 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001700 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001701
1702 if (this.tickerText != null) {
1703 that.tickerText = this.tickerText.toString();
1704 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001705 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001706 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001707 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001708 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001709 that.contentView = this.contentView.clone();
1710 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001711 if (heavy && this.mLargeIcon != null) {
1712 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001713 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001714 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001715 that.sound = this.sound; // android.net.Uri is immutable
1716 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001717 if (this.audioAttributes != null) {
1718 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1719 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001720
1721 final long[] vibrate = this.vibrate;
1722 if (vibrate != null) {
1723 final int N = vibrate.length;
1724 final long[] vib = that.vibrate = new long[N];
1725 System.arraycopy(vibrate, 0, vib, 0, N);
1726 }
1727
1728 that.ledARGB = this.ledARGB;
1729 that.ledOnMS = this.ledOnMS;
1730 that.ledOffMS = this.ledOffMS;
1731 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001732
Joe Onorato18e69df2010-05-17 22:26:12 -07001733 that.flags = this.flags;
1734
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001735 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001736
John Spurlockfd7f1e02014-03-18 16:41:57 -04001737 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001738
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001739 that.mGroupKey = this.mGroupKey;
1740
1741 that.mSortKey = this.mSortKey;
1742
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001743 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001744 try {
1745 that.extras = new Bundle(this.extras);
1746 // will unparcel
1747 that.extras.size();
1748 } catch (BadParcelableException e) {
1749 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1750 that.extras = null;
1751 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001752 }
1753
Felipe Lemedd85da62016-06-28 11:29:54 -07001754 if (!ArrayUtils.isEmpty(allPendingIntents)) {
1755 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07001756 }
1757
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001758 if (this.actions != null) {
1759 that.actions = new Action[this.actions.length];
1760 for(int i=0; i<this.actions.length; i++) {
1761 that.actions[i] = this.actions[i].clone();
1762 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001763 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001764
Daniel Sandler1a497d32013-04-18 14:52:45 -04001765 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001766 that.bigContentView = this.bigContentView.clone();
1767 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001768
Chris Wren8fd39ec2014-02-27 17:43:26 -05001769 if (heavy && this.headsUpContentView != null) {
1770 that.headsUpContentView = this.headsUpContentView.clone();
1771 }
1772
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001773 that.visibility = this.visibility;
1774
1775 if (this.publicVersion != null) {
1776 that.publicVersion = new Notification();
1777 this.publicVersion.cloneInto(that.publicVersion, heavy);
1778 }
1779
Dan Sandler26e81cf2014-05-06 10:01:27 -04001780 that.color = this.color;
1781
Daniel Sandler1a497d32013-04-18 14:52:45 -04001782 if (!heavy) {
1783 that.lightenPayload(); // will clean out extras
1784 }
1785 }
1786
1787 /**
1788 * Removes heavyweight parts of the Notification object for archival or for sending to
1789 * listeners when the full contents are not necessary.
1790 * @hide
1791 */
1792 public final void lightenPayload() {
1793 tickerView = null;
1794 contentView = null;
1795 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001796 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001797 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05001798 if (extras != null && !extras.isEmpty()) {
1799 final Set<String> keyset = extras.keySet();
1800 final int N = keyset.size();
1801 final String[] keys = keyset.toArray(new String[N]);
1802 for (int i=0; i<N; i++) {
1803 final String key = keys[i];
1804 final Object obj = extras.get(key);
1805 if (obj != null &&
1806 ( obj instanceof Parcelable
1807 || obj instanceof Parcelable[]
1808 || obj instanceof SparseArray
1809 || obj instanceof ArrayList)) {
1810 extras.remove(key);
1811 }
1812 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001813 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001814 }
1815
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001816 /**
1817 * Make sure this CharSequence is safe to put into a bundle, which basically
1818 * means it had better not be some custom Parcelable implementation.
1819 * @hide
1820 */
1821 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001822 if (cs == null) return cs;
1823 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1824 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1825 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001826 if (cs instanceof Parcelable) {
1827 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1828 + " instance is a custom Parcelable and not allowed in Notification");
1829 return cs.toString();
1830 }
Selim Cinek60a54252016-02-26 17:03:25 -08001831 return removeTextSizeSpans(cs);
1832 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001833
Selim Cinek60a54252016-02-26 17:03:25 -08001834 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
1835 if (charSequence instanceof Spanned) {
1836 Spanned ss = (Spanned) charSequence;
1837 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
1838 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
1839 for (Object span : spans) {
1840 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08001841 if (resultSpan instanceof CharacterStyle) {
1842 resultSpan = ((CharacterStyle) span).getUnderlying();
1843 }
1844 if (resultSpan instanceof TextAppearanceSpan) {
1845 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08001846 resultSpan = new TextAppearanceSpan(
1847 originalSpan.getFamily(),
1848 originalSpan.getTextStyle(),
1849 -1,
1850 originalSpan.getTextColor(),
1851 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08001852 } else if (resultSpan instanceof RelativeSizeSpan
1853 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08001854 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08001855 } else {
1856 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08001857 }
1858 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
1859 ss.getSpanFlags(span));
1860 }
1861 return builder;
1862 }
1863 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001864 }
1865
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 public int describeContents() {
1867 return 0;
1868 }
1869
1870 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001871 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 */
Svet Ganovddb94882016-06-23 19:55:24 -07001873 public void writeToParcel(Parcel parcel, int flags) {
1874 // We need to mark all pending intents getting into the notification
1875 // system as being put there to later allow the notification ranker
1876 // to launch them and by doing so add the app to the battery saver white
1877 // list for a short period of time. The problem is that the system
1878 // cannot look into the extras as there may be parcelables there that
1879 // the platform does not know how to handle. To go around that we have
1880 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07001881 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07001882 if (collectPendingIntents) {
1883 PendingIntent.setOnMarshaledListener(
1884 (PendingIntent intent, Parcel out, int outFlags) -> {
1885 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07001886 if (allPendingIntents == null) {
1887 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07001888 }
Felipe Lemedd85da62016-06-28 11:29:54 -07001889 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07001890 }
1891 });
1892 }
1893 try {
1894 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
1895 // want to intercept all pending events written to the pacel.
1896 writeToParcelImpl(parcel, flags);
1897 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001898 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07001899 } finally {
1900 if (collectPendingIntents) {
1901 PendingIntent.setOnMarshaledListener(null);
1902 }
1903 }
1904 }
1905
1906 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 parcel.writeInt(1);
1908
1909 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001910 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04001911 if (mSmallIcon == null && icon != 0) {
1912 // you snuck an icon in here without using the builder; let's try to keep it
1913 mSmallIcon = Icon.createWithResource("", icon);
1914 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001915 if (mSmallIcon != null) {
1916 parcel.writeInt(1);
1917 mSmallIcon.writeToParcel(parcel, 0);
1918 } else {
1919 parcel.writeInt(0);
1920 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001921 parcel.writeInt(number);
1922 if (contentIntent != null) {
1923 parcel.writeInt(1);
1924 contentIntent.writeToParcel(parcel, 0);
1925 } else {
1926 parcel.writeInt(0);
1927 }
1928 if (deleteIntent != null) {
1929 parcel.writeInt(1);
1930 deleteIntent.writeToParcel(parcel, 0);
1931 } else {
1932 parcel.writeInt(0);
1933 }
1934 if (tickerText != null) {
1935 parcel.writeInt(1);
1936 TextUtils.writeToParcel(tickerText, parcel, flags);
1937 } else {
1938 parcel.writeInt(0);
1939 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001940 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001941 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001942 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001943 } else {
1944 parcel.writeInt(0);
1945 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001946 if (contentView != null) {
1947 parcel.writeInt(1);
1948 contentView.writeToParcel(parcel, 0);
1949 } else {
1950 parcel.writeInt(0);
1951 }
Selim Cinek279fa862016-06-14 10:57:25 -07001952 if (mLargeIcon == null && largeIcon != null) {
1953 // you snuck an icon in here without using the builder; let's try to keep it
1954 mLargeIcon = Icon.createWithBitmap(largeIcon);
1955 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001956 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001957 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001958 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001959 } else {
1960 parcel.writeInt(0);
1961 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001962
1963 parcel.writeInt(defaults);
1964 parcel.writeInt(this.flags);
1965
1966 if (sound != null) {
1967 parcel.writeInt(1);
1968 sound.writeToParcel(parcel, 0);
1969 } else {
1970 parcel.writeInt(0);
1971 }
1972 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001973
1974 if (audioAttributes != null) {
1975 parcel.writeInt(1);
1976 audioAttributes.writeToParcel(parcel, 0);
1977 } else {
1978 parcel.writeInt(0);
1979 }
1980
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 parcel.writeLongArray(vibrate);
1982 parcel.writeInt(ledARGB);
1983 parcel.writeInt(ledOnMS);
1984 parcel.writeInt(ledOffMS);
1985 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001986
1987 if (fullScreenIntent != null) {
1988 parcel.writeInt(1);
1989 fullScreenIntent.writeToParcel(parcel, 0);
1990 } else {
1991 parcel.writeInt(0);
1992 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001993
1994 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001995
John Spurlockfd7f1e02014-03-18 16:41:57 -04001996 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001997
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001998 parcel.writeString(mGroupKey);
1999
2000 parcel.writeString(mSortKey);
2001
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002002 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002003
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002004 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002005
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002006 if (bigContentView != null) {
2007 parcel.writeInt(1);
2008 bigContentView.writeToParcel(parcel, 0);
2009 } else {
2010 parcel.writeInt(0);
2011 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002012
Chris Wren8fd39ec2014-02-27 17:43:26 -05002013 if (headsUpContentView != null) {
2014 parcel.writeInt(1);
2015 headsUpContentView.writeToParcel(parcel, 0);
2016 } else {
2017 parcel.writeInt(0);
2018 }
2019
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002020 parcel.writeInt(visibility);
2021
2022 if (publicVersion != null) {
2023 parcel.writeInt(1);
2024 publicVersion.writeToParcel(parcel, 0);
2025 } else {
2026 parcel.writeInt(0);
2027 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002028
2029 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002030 }
2031
2032 /**
2033 * Parcelable.Creator that instantiates Notification objects
2034 */
2035 public static final Parcelable.Creator<Notification> CREATOR
2036 = new Parcelable.Creator<Notification>()
2037 {
2038 public Notification createFromParcel(Parcel parcel)
2039 {
2040 return new Notification(parcel);
2041 }
2042
2043 public Notification[] newArray(int size)
2044 {
2045 return new Notification[size];
2046 }
2047 };
2048
2049 /**
2050 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2051 * layout.
2052 *
2053 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2054 * in the view.</p>
2055 * @param context The context for your application / activity.
2056 * @param contentTitle The title that goes in the expanded entry.
2057 * @param contentText The text that goes in the expanded entry.
2058 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2059 * If this is an activity, it must include the
2060 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002061 * that you take care of task management as described in the
2062 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2063 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002064 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002065 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002066 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002067 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002068 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002069 public void setLatestEventInfo(Context context,
2070 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002071 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2072 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2073 new Throwable());
2074 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002075
Selim Cinek4ac6f602016-06-13 15:47:03 -07002076 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2077 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2078 }
2079
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002080 // ensure that any information already set directly is preserved
2081 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002082
2083 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002085 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 }
2087 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002088 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002089 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002090 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002091
2092 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002093 }
2094
Julia Reynoldsda303542015-11-23 14:00:20 -05002095 /**
2096 * @hide
2097 */
2098 public static void addFieldsFromContext(Context context, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002099 addFieldsFromContext(context.getApplicationInfo(), context.getUserId(), notification);
2100 }
2101
2102 /**
2103 * @hide
2104 */
2105 public static void addFieldsFromContext(ApplicationInfo ai, int userId,
2106 Notification notification) {
2107 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
2108 notification.extras.putInt(EXTRA_ORIGINATING_USERID, userId);
Julia Reynoldsda303542015-11-23 14:00:20 -05002109 }
2110
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002111 @Override
2112 public String toString() {
2113 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002114 sb.append("Notification(pri=");
2115 sb.append(priority);
2116 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002117 if (contentView != null) {
2118 sb.append(contentView.getPackage());
2119 sb.append("/0x");
2120 sb.append(Integer.toHexString(contentView.getLayoutId()));
2121 } else {
2122 sb.append("null");
2123 }
2124 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002125 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2126 sb.append("default");
2127 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128 int N = this.vibrate.length-1;
2129 sb.append("[");
2130 for (int i=0; i<N; i++) {
2131 sb.append(this.vibrate[i]);
2132 sb.append(',');
2133 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002134 if (N != -1) {
2135 sb.append(this.vibrate[N]);
2136 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002137 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002138 } else {
2139 sb.append("null");
2140 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002141 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002142 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002143 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002144 } else if (this.sound != null) {
2145 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002146 } else {
2147 sb.append("null");
2148 }
Chris Wren365b6d32015-07-16 10:39:26 -04002149 if (this.tickerText != null) {
2150 sb.append(" tick");
2151 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002152 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002153 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002154 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002155 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002156 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002157 if (this.category != null) {
2158 sb.append(" category=");
2159 sb.append(this.category);
2160 }
2161 if (this.mGroupKey != null) {
2162 sb.append(" groupKey=");
2163 sb.append(this.mGroupKey);
2164 }
2165 if (this.mSortKey != null) {
2166 sb.append(" sortKey=");
2167 sb.append(this.mSortKey);
2168 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002169 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002170 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002171 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002172 }
2173 sb.append(" vis=");
2174 sb.append(visibilityToString(this.visibility));
2175 if (this.publicVersion != null) {
2176 sb.append(" publicVersion=");
2177 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002178 }
2179 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002180 return sb.toString();
2181 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002182
Dan Sandler1b718782014-07-18 12:43:45 -04002183 /**
2184 * {@hide}
2185 */
2186 public static String visibilityToString(int vis) {
2187 switch (vis) {
2188 case VISIBILITY_PRIVATE:
2189 return "PRIVATE";
2190 case VISIBILITY_PUBLIC:
2191 return "PUBLIC";
2192 case VISIBILITY_SECRET:
2193 return "SECRET";
2194 default:
2195 return "UNKNOWN(" + String.valueOf(vis) + ")";
2196 }
2197 }
2198
Joe Onoratocb109a02011-01-18 17:57:41 -08002199 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002200 * {@hide}
2201 */
2202 public static String priorityToString(@Priority int pri) {
2203 switch (pri) {
2204 case PRIORITY_MIN:
2205 return "MIN";
2206 case PRIORITY_LOW:
2207 return "LOW";
2208 case PRIORITY_DEFAULT:
2209 return "DEFAULT";
2210 case PRIORITY_HIGH:
2211 return "HIGH";
2212 case PRIORITY_MAX:
2213 return "MAX";
2214 default:
2215 return "UNKNOWN(" + String.valueOf(pri) + ")";
2216 }
2217 }
2218
2219 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002220 * The small icon representing this notification in the status bar and content view.
2221 *
2222 * @return the small icon representing this notification.
2223 *
2224 * @see Builder#getSmallIcon()
2225 * @see Builder#setSmallIcon(Icon)
2226 */
2227 public Icon getSmallIcon() {
2228 return mSmallIcon;
2229 }
2230
2231 /**
2232 * Used when notifying to clean up legacy small icons.
2233 * @hide
2234 */
2235 public void setSmallIcon(Icon icon) {
2236 mSmallIcon = icon;
2237 }
2238
2239 /**
2240 * The large icon shown in this notification's content view.
2241 * @see Builder#getLargeIcon()
2242 * @see Builder#setLargeIcon(Icon)
2243 */
2244 public Icon getLargeIcon() {
2245 return mLargeIcon;
2246 }
2247
2248 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002249 * @hide
2250 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002251 public boolean isGroupSummary() {
2252 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2253 }
2254
2255 /**
2256 * @hide
2257 */
2258 public boolean isGroupChild() {
2259 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2260 }
2261
2262 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002263 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002264 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002265 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002266 * content views using the platform's notification layout template. If your app supports
2267 * versions of Android as old as API level 4, you can instead use
2268 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2269 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2270 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002271 *
Scott Main183bf112012-08-13 19:12:13 -07002272 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002273 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002274 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002275 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002276 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2277 * .setContentText(subject)
2278 * .setSmallIcon(R.drawable.new_mail)
2279 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002280 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002281 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002282 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002283 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002284 /**
2285 * @hide
2286 */
2287 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2288 "android.rebuild.contentViewActionCount";
2289 /**
2290 * @hide
2291 */
2292 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2293 = "android.rebuild.bigViewActionCount";
2294 /**
2295 * @hide
2296 */
2297 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2298 = "android.rebuild.hudViewActionCount";
2299
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002300 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002301
Joe Onorato46439ce2010-11-19 13:56:21 -08002302 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002303 private Notification mN;
2304 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002305 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002306 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2307 private ArrayList<String> mPersonList = new ArrayList<String>();
2308 private NotificationColorUtil mColorUtil;
2309 private boolean mColorUtilInited = false;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002310
2311 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002312 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2313 */
2314 private int mCachedContrastColor = COLOR_INVALID;
2315 private int mCachedContrastColorIsFor = COLOR_INVALID;
2316
2317 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002318 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002319 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002320
2321 * <table>
2322 * <tr><th align=right>priority</th>
2323 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2324 * <tr><th align=right>when</th>
2325 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2326 * <tr><th align=right>audio stream</th>
2327 * <td>{@link #STREAM_DEFAULT}</td></tr>
2328 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002329 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002330
2331 * @param context
2332 * A {@link Context} that will be used by the Builder to construct the
2333 * RemoteViews. The Context will not be held past the lifetime of this Builder
2334 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002335 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002336 public Builder(Context context) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002337 this(context, null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002338 }
2339
Joe Onoratocb109a02011-01-18 17:57:41 -08002340 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002341 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002342 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002343 public Builder(Context context, Notification toAdopt) {
2344 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002345
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002346 if (toAdopt == null) {
2347 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002348 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2349 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2350 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002351 mN.priority = PRIORITY_DEFAULT;
2352 mN.visibility = VISIBILITY_PRIVATE;
2353 } else {
2354 mN = toAdopt;
2355 if (mN.actions != null) {
2356 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002357 }
2358
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002359 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2360 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2361 }
2362
Selim Cinek4ac6f602016-06-13 15:47:03 -07002363 if (mN.getSmallIcon() == null && mN.icon != 0) {
2364 setSmallIcon(mN.icon);
2365 }
2366
2367 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
2368 setLargeIcon(mN.largeIcon);
2369 }
2370
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002371 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2372 if (!TextUtils.isEmpty(templateClass)) {
2373 final Class<? extends Style> styleClass
2374 = getNotificationStyleClass(templateClass);
2375 if (styleClass == null) {
2376 Log.d(TAG, "Unknown style class: " + templateClass);
2377 } else {
2378 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002379 final Constructor<? extends Style> ctor =
2380 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002381 ctor.setAccessible(true);
2382 final Style style = ctor.newInstance();
2383 style.restoreFromExtras(mN.extras);
2384
2385 if (style != null) {
2386 setStyle(style);
2387 }
2388 } catch (Throwable t) {
2389 Log.e(TAG, "Could not create Style", t);
2390 }
2391 }
2392 }
2393
2394 }
2395 }
2396
2397 private NotificationColorUtil getColorUtil() {
2398 if (!mColorUtilInited) {
2399 mColorUtilInited = true;
2400 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
2401 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002402 }
2403 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002404 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002405 }
2406
2407 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002408 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002409 *
2410 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2411 * shown anymore by default and must be opted into by using
2412 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002413 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002414 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002415 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002416 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002417 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002418 return this;
2419 }
2420
Joe Onoratocb109a02011-01-18 17:57:41 -08002421 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002422 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002423 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002424 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2425 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002426 */
2427 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002428 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002429 return this;
2430 }
2431
2432 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002433 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002434 *
2435 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002436 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002437 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002438 * Useful when showing an elapsed time (like an ongoing phone call).
2439 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002440 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07002441 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08002442 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002443 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002444 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07002445 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002446 */
2447 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002448 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002449 return this;
2450 }
2451
2452 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002453 * Sets the Chronometer to count down instead of counting up.
2454 *
2455 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2456 * If it isn't set the chronometer will count up.
2457 *
2458 * @see #setUsesChronometer(boolean)
2459 */
Adrian Roos96b7e202016-05-17 13:50:38 -07002460 public Builder setChronometerCountDown(boolean countDown) {
2461 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08002462 return this;
2463 }
2464
2465 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002466 * Set the small icon resource, which will be used to represent the notification in the
2467 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002468 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002469
2470 * The platform template for the expanded view will draw this icon in the left, unless a
2471 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2472 * icon will be moved to the right-hand side.
2473 *
2474
2475 * @param icon
2476 * A resource ID in the application's package of the drawable to use.
2477 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002478 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002479 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002480 return setSmallIcon(icon != 0
2481 ? Icon.createWithResource(mContext, icon)
2482 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002483 }
2484
Joe Onoratocb109a02011-01-18 17:57:41 -08002485 /**
2486 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2487 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2488 * LevelListDrawable}.
2489 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002490 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002491 * @param level The level to use for the icon.
2492 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002493 * @see Notification#icon
2494 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002495 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002496 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002497 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002498 return setSmallIcon(icon);
2499 }
2500
2501 /**
2502 * Set the small icon, which will be used to represent the notification in the
2503 * status bar and content view (unless overriden there by a
2504 * {@link #setLargeIcon(Bitmap) large icon}).
2505 *
2506 * @param icon An Icon object to use.
2507 * @see Notification#icon
2508 */
2509 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002510 mN.setSmallIcon(icon);
2511 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2512 mN.icon = icon.getResId();
2513 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002514 return this;
2515 }
2516
Joe Onoratocb109a02011-01-18 17:57:41 -08002517 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002518 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002519 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002520 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002521 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002522 return this;
2523 }
2524
Joe Onoratocb109a02011-01-18 17:57:41 -08002525 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002526 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002527 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002528 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002529 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002530 return this;
2531 }
2532
Joe Onoratocb109a02011-01-18 17:57:41 -08002533 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002534 * This provides some additional information that is displayed in the notification. No
2535 * guarantees are given where exactly it is displayed.
2536 *
2537 * <p>This information should only be provided if it provides an essential
2538 * benefit to the understanding of the notification. The more text you provide the
2539 * less readable it becomes. For example, an email client should only provide the account
2540 * name here if more than one email account has been added.</p>
2541 *
2542 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
2543 * notification header area.
2544 *
2545 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
2546 * this will be shown in the third line of text in the platform notification template.
2547 * You should not be using {@link #setProgress(int, int, boolean)} at the
2548 * same time on those versions; they occupy the same place.
2549 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002550 */
2551 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002552 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002553 return this;
2554 }
2555
2556 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002557 * Set the remote input history.
2558 *
2559 * This should be set to the most recent inputs that have been sent
2560 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2561 * longer relevant (e.g. for chat notifications once the other party has responded).
2562 *
2563 * The most recent input must be stored at the 0 index, the second most recent at the
2564 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2565 * and how much of each individual input is shown.
2566 *
2567 * <p>Note: The reply text will only be shown on notifications that have least one action
2568 * with a {@code RemoteInput}.</p>
2569 */
2570 public Builder setRemoteInputHistory(CharSequence[] text) {
2571 if (text == null) {
2572 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2573 } else {
2574 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2575 CharSequence[] safe = new CharSequence[N];
2576 for (int i = 0; i < N; i++) {
2577 safe[i] = safeCharSequence(text[i]);
2578 }
2579 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2580 }
2581 return this;
2582 }
2583
2584 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002585 * Set the large number at the right-hand side of the notification. This is
2586 * equivalent to setContentInfo, although it might show the number in a different
2587 * font size for readability.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002588 *
2589 * @deprecated this number is not shown anywhere anymore
Joe Onoratocb109a02011-01-18 17:57:41 -08002590 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002591 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002592 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002593 return this;
2594 }
2595
Joe Onoratocb109a02011-01-18 17:57:41 -08002596 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002597 * A small piece of additional information pertaining to this notification.
2598 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002599 * The platform template will draw this on the last line of the notification, at the far
2600 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002601 *
2602 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
2603 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
2604 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08002605 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002606 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002607 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002608 return this;
2609 }
2610
Joe Onoratocb109a02011-01-18 17:57:41 -08002611 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002612 * Set the progress this notification represents.
2613 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002614 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002615 */
2616 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002617 mN.extras.putInt(EXTRA_PROGRESS, progress);
2618 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
2619 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002620 return this;
2621 }
2622
2623 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002624 * Supply a custom RemoteViews to use instead of the platform template.
2625 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002626 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08002627 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002628 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002629 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002630 return setCustomContentView(views);
2631 }
2632
2633 /**
2634 * Supply custom RemoteViews to use instead of the platform template.
2635 *
2636 * This will override the layout that would otherwise be constructed by this Builder
2637 * object.
2638 */
2639 public Builder setCustomContentView(RemoteViews contentView) {
2640 mN.contentView = contentView;
2641 return this;
2642 }
2643
2644 /**
2645 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
2646 *
2647 * This will override the expanded layout that would otherwise be constructed by this
2648 * Builder object.
2649 */
2650 public Builder setCustomBigContentView(RemoteViews contentView) {
2651 mN.bigContentView = contentView;
2652 return this;
2653 }
2654
2655 /**
2656 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
2657 *
2658 * This will override the heads-up layout that would otherwise be constructed by this
2659 * Builder object.
2660 */
2661 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
2662 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002663 return this;
2664 }
2665
Joe Onoratocb109a02011-01-18 17:57:41 -08002666 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002667 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2668 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002669 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2670 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2671 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002672 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002673 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002674 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002675 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002676 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002677 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002678 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002679 return this;
2680 }
2681
Joe Onoratocb109a02011-01-18 17:57:41 -08002682 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002683 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2684 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002685 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002686 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002687 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002688 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002689 return this;
2690 }
2691
Joe Onoratocb109a02011-01-18 17:57:41 -08002692 /**
2693 * An intent to launch instead of posting the notification to the status bar.
2694 * Only for use with extremely high-priority notifications demanding the user's
2695 * <strong>immediate</strong> attention, such as an incoming phone call or
2696 * alarm clock that the user has explicitly set to a particular time.
2697 * If this facility is used for something else, please give the user an option
2698 * to turn it off and use a normal notification, as this can be extremely
2699 * disruptive.
2700 *
Chris Wren47c20a12014-06-18 17:27:29 -04002701 * <p>
2702 * The system UI may choose to display a heads-up notification, instead of
2703 * launching this intent, while the user is using the device.
2704 * </p>
2705 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002706 * @param intent The pending intent to launch.
2707 * @param highPriority Passing true will cause this notification to be sent
2708 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002709 *
2710 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002711 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002712 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002713 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002714 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2715 return this;
2716 }
2717
Joe Onoratocb109a02011-01-18 17:57:41 -08002718 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002719 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002720 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002721 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002722 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002723 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002724 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002725 return this;
2726 }
2727
Joe Onoratocb109a02011-01-18 17:57:41 -08002728 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002729 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002730 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002731 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002732 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002733 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002734 setTicker(tickerText);
2735 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08002736 return this;
2737 }
2738
Joe Onoratocb109a02011-01-18 17:57:41 -08002739 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002740 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002741 *
2742 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002743 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2744 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002745 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002746 public Builder setLargeIcon(Bitmap b) {
2747 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2748 }
2749
2750 /**
2751 * Add a large icon to the notification content view.
2752 *
2753 * In the platform template, this image will be shown on the left of the notification view
2754 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2755 * badge atop the large icon).
2756 */
2757 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002758 mN.mLargeIcon = icon;
2759 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08002760 return this;
2761 }
2762
Joe Onoratocb109a02011-01-18 17:57:41 -08002763 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002764 * Set the sound to play.
2765 *
John Spurlockc0650f022014-07-19 13:22:39 -04002766 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2767 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002768 *
Chris Wren47c20a12014-06-18 17:27:29 -04002769 * <p>
2770 * A notification that is noisy is more likely to be presented as a heads-up notification.
2771 * </p>
2772 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002773 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002774 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002775 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002776 mN.sound = sound;
2777 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002778 return this;
2779 }
2780
Joe Onoratocb109a02011-01-18 17:57:41 -08002781 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002782 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002783 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002784 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2785 *
Chris Wren47c20a12014-06-18 17:27:29 -04002786 * <p>
2787 * A notification that is noisy is more likely to be presented as a heads-up notification.
2788 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002789 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002790 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002791 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002792 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002793 public Builder setSound(Uri sound, int streamType) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002794 mN.sound = sound;
2795 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08002796 return this;
2797 }
2798
Joe Onoratocb109a02011-01-18 17:57:41 -08002799 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002800 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2801 * use during playback.
2802 *
2803 * <p>
2804 * A notification that is noisy is more likely to be presented as a heads-up notification.
2805 * </p>
2806 *
2807 * @see Notification#sound
2808 */
2809 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002810 mN.sound = sound;
2811 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04002812 return this;
2813 }
2814
2815 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002816 * Set the vibration pattern to use.
2817 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002818 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2819 * <code>pattern</code> parameter.
2820 *
Chris Wren47c20a12014-06-18 17:27:29 -04002821 * <p>
2822 * A notification that vibrates is more likely to be presented as a heads-up notification.
2823 * </p>
2824 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002825 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002826 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002827 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002828 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08002829 return this;
2830 }
2831
Joe Onoratocb109a02011-01-18 17:57:41 -08002832 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002833 * Set the desired color for the indicator LED on the device, as well as the
2834 * blink duty cycle (specified in milliseconds).
2835 *
2836
2837 * Not all devices will honor all (or even any) of these values.
2838 *
2839
2840 * @see Notification#ledARGB
2841 * @see Notification#ledOnMS
2842 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002843 */
Tor Norbye80756e32015-03-02 09:39:27 -08002844 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002845 mN.ledARGB = argb;
2846 mN.ledOnMS = onMs;
2847 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002848 if (onMs != 0 || offMs != 0) {
2849 mN.flags |= FLAG_SHOW_LIGHTS;
2850 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002851 return this;
2852 }
2853
Joe Onoratocb109a02011-01-18 17:57:41 -08002854 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002855 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002856 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002857
2858 * Ongoing notifications cannot be dismissed by the user, so your application or service
2859 * must take care of canceling them.
2860 *
2861
2862 * They are typically used to indicate a background task that the user is actively engaged
2863 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2864 * (e.g., a file download, sync operation, active network connection).
2865 *
2866
2867 * @see Notification#FLAG_ONGOING_EVENT
2868 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002869 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002870 public Builder setOngoing(boolean ongoing) {
2871 setFlag(FLAG_ONGOING_EVENT, ongoing);
2872 return this;
2873 }
2874
Joe Onoratocb109a02011-01-18 17:57:41 -08002875 /**
2876 * Set this flag if you would only like the sound, vibrate
2877 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002878 *
2879 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002880 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002881 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2882 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2883 return this;
2884 }
2885
Joe Onoratocb109a02011-01-18 17:57:41 -08002886 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002887 * Make this notification automatically dismissed when the user touches it. The
2888 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2889 *
2890 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002891 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002892 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002893 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002894 return this;
2895 }
2896
Joe Onoratocb109a02011-01-18 17:57:41 -08002897 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002898 * Set whether or not this notification should not bridge to other devices.
2899 *
2900 * <p>Some notifications can be bridged to other devices for remote display.
2901 * This hint can be set to recommend this notification not be bridged.
2902 */
2903 public Builder setLocalOnly(boolean localOnly) {
2904 setFlag(FLAG_LOCAL_ONLY, localOnly);
2905 return this;
2906 }
2907
2908 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002909 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002910 * <p>
2911 * The value should be one or more of the following fields combined with
2912 * bitwise-or:
2913 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2914 * <p>
2915 * For all default values, use {@link #DEFAULT_ALL}.
2916 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002917 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002918 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08002919 return this;
2920 }
2921
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002922 /**
2923 * Set the priority of this notification.
2924 *
2925 * @see Notification#priority
2926 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002927 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002928 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002929 return this;
2930 }
Joe Malin8d40d042012-11-05 11:36:40 -08002931
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002932 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002933 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002934 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002935 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002936 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002937 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002938 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002939 return this;
2940 }
2941
2942 /**
Chris Wrendde75302014-03-26 17:24:15 -04002943 * Add a person that is relevant to this notification.
2944 *
Chris Wrene6c48932014-09-29 17:19:27 -04002945 * <P>
2946 * Depending on user preferences, this annotation may allow the notification to pass
2947 * through interruption filters, and to appear more prominently in the user interface.
2948 * </P>
2949 *
2950 * <P>
2951 * The person should be specified by the {@code String} representation of a
2952 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2953 * </P>
2954 *
2955 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2956 * URIs. The path part of these URIs must exist in the contacts database, in the
2957 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2958 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2959 * </P>
2960 *
2961 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002962 * @see Notification#EXTRA_PEOPLE
2963 */
Chris Wrene6c48932014-09-29 17:19:27 -04002964 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002965 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002966 return this;
2967 }
2968
2969 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002970 * Set this notification to be part of a group of notifications sharing the same key.
2971 * Grouped notifications may display in a cluster or stack on devices which
2972 * support such rendering.
2973 *
2974 * <p>To make this notification the summary for its group, also call
2975 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2976 * {@link #setSortKey}.
2977 * @param groupKey The group key of the group.
2978 * @return this object for method chaining
2979 */
2980 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002981 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002982 return this;
2983 }
2984
2985 /**
2986 * Set this notification to be the group summary for a group of notifications.
2987 * Grouped notifications may display in a cluster or stack on devices which
2988 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2989 * @param isGroupSummary Whether this notification should be a group summary.
2990 * @return this object for method chaining
2991 */
2992 public Builder setGroupSummary(boolean isGroupSummary) {
2993 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2994 return this;
2995 }
2996
2997 /**
2998 * Set a sort key that orders this notification among other notifications from the
2999 * same package. This can be useful if an external sort was already applied and an app
3000 * would like to preserve this. Notifications will be sorted lexicographically using this
3001 * value, although providing different priorities in addition to providing sort key may
3002 * cause this value to be ignored.
3003 *
3004 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003005 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003006 *
3007 * @see String#compareTo(String)
3008 */
3009 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003010 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003011 return this;
3012 }
3013
3014 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003015 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003016 *
Griff Hazen720042b2014-02-24 15:46:56 -08003017 * <p>Values within the Bundle will replace existing extras values in this Builder.
3018 *
3019 * @see Notification#extras
3020 */
Griff Hazen959591e2014-05-15 22:26:18 -07003021 public Builder addExtras(Bundle extras) {
3022 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003023 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003024 }
3025 return this;
3026 }
3027
3028 /**
3029 * Set metadata for this notification.
3030 *
3031 * <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 -04003032 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003033 * called.
3034 *
Griff Hazen720042b2014-02-24 15:46:56 -08003035 * <p>Replaces any existing extras values with those from the provided Bundle.
3036 * Use {@link #addExtras} to merge in metadata instead.
3037 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003038 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003039 */
Griff Hazen959591e2014-05-15 22:26:18 -07003040 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003041 if (extras != null) {
3042 mUserExtras = extras;
3043 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003044 return this;
3045 }
3046
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003047 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003048 * Get the current metadata Bundle used by this notification Builder.
3049 *
3050 * <p>The returned Bundle is shared with this Builder.
3051 *
3052 * <p>The current contents of this Bundle are copied into the Notification each time
3053 * {@link #build()} is called.
3054 *
3055 * @see Notification#extras
3056 */
3057 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003058 return mUserExtras;
3059 }
3060
3061 private Bundle getAllExtras() {
3062 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3063 saveExtras.putAll(mN.extras);
3064 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003065 }
3066
3067 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003068 * Add an action to this notification. Actions are typically displayed by
3069 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003070 * <p>
3071 * Every action must have an icon (32dp square and matching the
3072 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3073 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3074 * <p>
3075 * A notification in its expanded form can display up to 3 actions, from left to right in
3076 * the order they were added. Actions will not be displayed when the notification is
3077 * collapsed, however, so be sure that any essential functions may be accessed by the user
3078 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003079 *
3080 * @param icon Resource ID of a drawable that represents the action.
3081 * @param title Text describing the action.
3082 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003083 *
3084 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003085 */
Dan Sandler86647982015-05-13 23:41:13 -04003086 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003087 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003088 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003089 return this;
3090 }
3091
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003092 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003093 * Add an action to this notification. Actions are typically displayed by
3094 * the system as a button adjacent to the notification content.
3095 * <p>
3096 * Every action must have an icon (32dp square and matching the
3097 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3098 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3099 * <p>
3100 * A notification in its expanded form can display up to 3 actions, from left to right in
3101 * the order they were added. Actions will not be displayed when the notification is
3102 * collapsed, however, so be sure that any essential functions may be accessed by the user
3103 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3104 *
3105 * @param action The action to add.
3106 */
3107 public Builder addAction(Action action) {
3108 mActions.add(action);
3109 return this;
3110 }
3111
3112 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003113 * Alter the complete list of actions attached to this notification.
3114 * @see #addAction(Action).
3115 *
3116 * @param actions
3117 * @return
3118 */
3119 public Builder setActions(Action... actions) {
3120 mActions.clear();
3121 for (int i = 0; i < actions.length; i++) {
3122 mActions.add(actions[i]);
3123 }
3124 return this;
3125 }
3126
3127 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003128 * Add a rich notification style to be applied at build time.
3129 *
3130 * @param style Object responsible for modifying the notification style.
3131 */
3132 public Builder setStyle(Style style) {
3133 if (mStyle != style) {
3134 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003135 if (mStyle != null) {
3136 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003137 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3138 } else {
3139 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003140 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003141 }
3142 return this;
3143 }
3144
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003145 /**
3146 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003147 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003148 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
3149 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
3150 *
3151 * @return The same Builder.
3152 */
3153 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003154 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003155 return this;
3156 }
3157
3158 /**
3159 * Supply a replacement Notification whose contents should be shown in insecure contexts
3160 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3161 * @param n A replacement notification, presumably with some or all info redacted.
3162 * @return The same Builder.
3163 */
3164 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003165 if (n != null) {
3166 mN.publicVersion = new Notification();
3167 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3168 } else {
3169 mN.publicVersion = null;
3170 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003171 return this;
3172 }
3173
Griff Hazenb720abe2014-05-20 13:15:30 -07003174 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003175 * Apply an extender to this notification builder. Extenders may be used to add
3176 * metadata or change options on this builder.
3177 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003178 public Builder extend(Extender extender) {
3179 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003180 return this;
3181 }
3182
Dan Sandler4e787062015-06-17 15:09:48 -04003183 /**
3184 * @hide
3185 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003186 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003187 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003188 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003189 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003190 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003191 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003192 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003193 }
3194
Dan Sandler26e81cf2014-05-06 10:01:27 -04003195 /**
3196 * Sets {@link Notification#color}.
3197 *
3198 * @param argb The accent color to use
3199 *
3200 * @return The same Builder.
3201 */
Tor Norbye80756e32015-03-02 09:39:27 -08003202 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003203 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003204 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003205 return this;
3206 }
3207
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003208 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04003209 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
3210 // This user can never be a badged profile,
3211 // and also includes USER_ALL system notifications.
3212 return null;
3213 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02003214 // Note: This assumes that the current user can read the profile badge of the
3215 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003216 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003217 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003218 }
3219
3220 private Bitmap getProfileBadge() {
3221 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003222 if (badge == null) {
3223 return null;
3224 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003225 final int size = mContext.getResources().getDimensionPixelSize(
3226 R.dimen.notification_badge_size);
3227 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003228 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003229 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003230 badge.draw(canvas);
3231 return bitmap;
3232 }
3233
Selim Cinekc848c3a2016-01-13 15:27:30 -08003234 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003235 Bitmap profileBadge = getProfileBadge();
3236
Kenny Guy98193ea2014-07-24 19:54:37 +01003237 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003238 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3239 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Kenny Guy98193ea2014-07-24 19:54:37 +01003240 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003241 }
3242
Christoph Studerfe718432014-09-01 18:21:18 +02003243 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003244 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003245 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003246 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003247 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003248 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003249 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003250 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003251 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003252 contentView.setTextViewText(R.id.text_line_1, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003253 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003254 }
3255
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003256 /**
3257 * Resets the notification header to its original state
3258 */
3259 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07003260 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
3261 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08003262 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003263 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003264 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003265 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07003266 contentView.setTextViewText(R.id.header_text, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003267 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003268 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003269 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003270 contentView.setImageViewIcon(R.id.profile_badge, null);
3271 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003272 }
3273
3274 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003275 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
3276 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003277 }
3278
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003279 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003280 return applyStandardTemplate(resId, true /* hasProgress */);
3281 }
3282
3283 /**
3284 * @param hasProgress whether the progress bar should be shown and set
3285 */
3286 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003287 final Bundle ex = mN.extras;
3288
3289 CharSequence title = processLegacyText(ex.getCharSequence(EXTRA_TITLE));
3290 CharSequence text = processLegacyText(ex.getCharSequence(EXTRA_TEXT));
3291 return applyStandardTemplate(resId, hasProgress, title, text);
3292 }
3293
3294 /**
3295 * @param hasProgress whether the progress bar should be shown and set
3296 */
3297 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress,
3298 CharSequence title, CharSequence text) {
Kenny Guy77320062014-08-27 21:37:15 +01003299 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003300
Christoph Studerfe718432014-09-01 18:21:18 +02003301 resetStandardTemplate(contentView);
3302
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003303 final Bundle ex = mN.extras;
Dan Sandler190d58d2014-05-15 09:33:39 -04003304
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003305 bindNotificationHeader(contentView);
3306 bindLargeIcon(contentView);
Selim Cinek954cc232016-05-20 13:29:23 -07003307 boolean showProgress = handleProgressBar(hasProgress, contentView, ex);
Adrian Roosc1a80b02016-04-05 14:54:55 -07003308 if (title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003309 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Adrian Roosc1a80b02016-04-05 14:54:55 -07003310 contentView.setTextViewText(R.id.title, title);
Selim Cinek954cc232016-05-20 13:29:23 -07003311 contentView.setViewLayoutWidth(R.id.title, showProgress
3312 ? ViewGroup.LayoutParams.WRAP_CONTENT
3313 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08003314 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07003315 if (text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003316 int textId = showProgress ? com.android.internal.R.id.text_line_1
3317 : com.android.internal.R.id.text;
Adrian Roosc1a80b02016-04-05 14:54:55 -07003318 contentView.setTextViewText(textId, text);
Selim Cinek41598732016-01-11 16:58:37 -08003319 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003320 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003321
Selim Cinek279fa862016-06-14 10:57:25 -07003322 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003323
Selim Cinek29603462015-11-17 19:04:39 -08003324 return contentView;
3325 }
3326
Selim Cinek860b6da2015-12-16 19:02:19 -08003327 /**
3328 * @param remoteView the remote view to update the minheight in
3329 * @param hasMinHeight does it have a mimHeight
3330 * @hide
3331 */
3332 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3333 int minHeight = 0;
3334 if (hasMinHeight) {
3335 // we need to set the minHeight of the notification
3336 minHeight = mContext.getResources().getDimensionPixelSize(
3337 com.android.internal.R.dimen.notification_min_content_height);
3338 }
3339 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3340 }
3341
Selim Cinek29603462015-11-17 19:04:39 -08003342 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003343 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3344 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3345 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3346 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003347 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003348 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003349 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003350 contentView.setProgressBackgroundTintList(
3351 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3352 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003353 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003354 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003355 contentView.setProgressTintList(R.id.progress, colorStateList);
3356 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003357 }
Selim Cinek29603462015-11-17 19:04:39 -08003358 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003359 } else {
3360 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003361 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003362 }
Joe Onorato561d3852010-11-20 18:09:34 -08003363 }
3364
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003365 private void bindLargeIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07003366 if (mN.mLargeIcon == null && mN.largeIcon != null) {
3367 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
3368 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003369 if (mN.mLargeIcon != null) {
3370 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3371 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3372 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003373 int endMargin = R.dimen.notification_content_picture_margin;
3374 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
3375 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
3376 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003377 }
3378 }
3379
3380 private void bindNotificationHeader(RemoteViews contentView) {
3381 bindSmallIcon(contentView);
3382 bindHeaderAppName(contentView);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003383 bindHeaderText(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003384 bindHeaderChronometerAndTime(contentView);
3385 bindExpandButton(contentView);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003386 bindProfileBadge(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003387 }
3388
3389 private void bindExpandButton(RemoteViews contentView) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003390 contentView.setDrawableParameters(R.id.expand_button, false, -1, resolveContrastColor(),
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003391 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003392 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003393 resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003394 }
3395
3396 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3397 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003398 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003399 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3400 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3401 contentView.setLong(R.id.chronometer, "setBase",
3402 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3403 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07003404 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07003405 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003406 } else {
3407 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3408 contentView.setLong(R.id.time, "setTime", mN.when);
3409 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003410 } else {
3411 // We still want a time to be set but gone, such that we can show and hide it
3412 // on demand in case it's a child notification without anything in the header
3413 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003414 }
3415 }
3416
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003417 private void bindHeaderText(RemoteViews contentView) {
3418 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3419 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05003420 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003421 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05003422 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003423 if (headerText == null
3424 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3425 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3426 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
3427 }
3428 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003429 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003430 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
3431 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
3432 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003433 }
3434 }
3435
Adrian Rooseba05822016-04-22 17:09:27 -07003436 /**
3437 * @hide
3438 */
3439 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04003440 CharSequence name = null;
3441 final PackageManager pm = mContext.getPackageManager();
3442 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
3443 // only system packages which lump together a bunch of unrelated stuff
3444 // may substitute a different name to make the purpose of the
3445 // notification more clear. the correct package label should always
3446 // be accessible via SystemUI.
3447 final String pkg = mContext.getPackageName();
3448 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
3449 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
3450 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
3451 name = subName;
3452 } else {
3453 Log.w(TAG, "warning: pkg "
3454 + pkg + " attempting to substitute app name '" + subName
3455 + "' without holding perm "
3456 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
3457 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003458 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04003459 if (TextUtils.isEmpty(name)) {
3460 name = pm.getApplicationLabel(mContext.getApplicationInfo());
3461 }
3462 if (TextUtils.isEmpty(name)) {
3463 // still nothing?
3464 return null;
3465 }
3466
3467 return String.valueOf(name);
3468 }
3469 private void bindHeaderAppName(RemoteViews contentView) {
3470 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos4ff3b122016-02-01 12:26:13 -08003471 contentView.setTextColor(R.id.app_name_text, resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003472 }
3473
3474 private void bindSmallIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07003475 if (mN.mSmallIcon == null && mN.icon != 0) {
3476 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
3477 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003478 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Adrian Roos9b45a152016-06-28 13:32:29 -07003479 contentView.setDrawableParameters(R.id.icon, false /* targetBackground */,
3480 -1 /* alpha */, -1 /* colorFilter */, null /* mode */, mN.iconLevel);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003481 processSmallIconColor(mN.mSmallIcon, contentView);
3482 }
3483
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003484 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003485 * @return true if the built notification will show the time or the chronometer; false
3486 * otherwise
3487 */
3488 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07003489 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003490 }
3491
Christoph Studerfe718432014-09-01 18:21:18 +02003492 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003493 // actions_container is only reset when there are no actions to avoid focus issues with
3494 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02003495 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003496 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003497
3498 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3499 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3500
3501 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3502 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3503 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3504 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07003505
3506 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003507 }
3508
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003509 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos48d746a2016-04-12 14:57:28 -07003510 final Bundle ex = mN.extras;
3511
3512 CharSequence title = processLegacyText(ex.getCharSequence(EXTRA_TITLE));
3513 CharSequence text = processLegacyText(ex.getCharSequence(EXTRA_TEXT));
3514 return applyStandardTemplateWithActions(layoutId, true /* hasProgress */, title, text);
3515 }
3516
3517 private RemoteViews applyStandardTemplateWithActions(int layoutId, boolean hasProgress,
3518 CharSequence title, CharSequence text) {
3519 RemoteViews big = applyStandardTemplate(layoutId, hasProgress, title, text);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003520
Christoph Studerfe718432014-09-01 18:21:18 +02003521 resetStandardTemplateWithActions(big);
3522
Adrian Roose458aa82015-12-08 16:17:19 -08003523 boolean validRemoteInput = false;
3524
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003525 int N = mActions.size();
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003526 boolean emphazisedMode = mN.fullScreenIntent != null;
3527 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003528 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08003529 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003530 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roosf852a422016-06-03 13:33:43 -07003531 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
3532 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003533 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003534 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003535 Action action = mActions.get(i);
3536 validRemoteInput |= hasValidRemoteInput(action);
3537
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003538 final RemoteViews button = generateActionButton(action, emphazisedMode,
3539 i % 2 != 0);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003540 big.addView(R.id.actions, button);
3541 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003542 } else {
3543 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003544 }
Adrian Roose458aa82015-12-08 16:17:19 -08003545
3546 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
3547 if (validRemoteInput && replyText != null
3548 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3549 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3550 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
3551
3552 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3553 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3554 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
3555
3556 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3557 big.setViewVisibility(
3558 R.id.notification_material_reply_text_3, View.VISIBLE);
3559 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
3560 }
3561 }
3562 }
3563
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003564 return big;
3565 }
3566
Adrian Roose458aa82015-12-08 16:17:19 -08003567 private boolean hasValidRemoteInput(Action action) {
3568 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
3569 // Weird actions
3570 return false;
3571 }
3572
3573 RemoteInput[] remoteInputs = action.getRemoteInputs();
3574 if (remoteInputs == null) {
3575 return false;
3576 }
3577
3578 for (RemoteInput r : remoteInputs) {
3579 CharSequence[] choices = r.getChoices();
3580 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
3581 return true;
3582 }
3583 }
3584 return false;
3585 }
3586
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003587 /**
3588 * Construct a RemoteViews for the final 1U notification layout. In order:
3589 * 1. Custom contentView from the caller
3590 * 2. Style's proposed content view
3591 * 3. Standard template view
3592 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003593 public RemoteViews createContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003594 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003595 return mN.contentView;
3596 } else if (mStyle != null) {
3597 final RemoteViews styleView = mStyle.makeContentView();
3598 if (styleView != null) {
3599 return styleView;
3600 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003601 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003602 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003603 }
3604
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003605 /**
3606 * Construct a RemoteViews for the final big notification layout.
3607 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003608 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05003609 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08003610 if (mN.bigContentView != null
3611 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003612 return mN.bigContentView;
3613 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05003614 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08003615 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003616 } else if (mActions.size() != 0) {
3617 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05003618 }
3619 adaptNotificationHeaderForBigContentView(result);
3620 return result;
3621 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003622
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003623 /**
3624 * Construct a RemoteViews for the final notification header only
3625 *
3626 * @hide
3627 */
3628 public RemoteViews makeNotificationHeader() {
3629 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
3630 R.layout.notification_template_header);
3631 resetNotificationHeader(header);
3632 bindNotificationHeader(header);
3633 return header;
3634 }
3635
Selim Cinek29603462015-11-17 19:04:39 -08003636 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003637 if (result != null) {
3638 result.setViewVisibility(R.id.text_line_1, View.GONE);
3639 }
Selim Cinek29603462015-11-17 19:04:39 -08003640 }
3641
Selim Cinek850a8542015-11-11 11:48:36 -05003642 private void adaptNotificationHeaderForBigContentView(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003643 if (result != null) {
3644 result.setBoolean(R.id.notification_header, "setExpanded", true);
3645 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003646 }
3647
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003648 /**
3649 * Construct a RemoteViews for the final heads-up notification layout.
3650 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003651 public RemoteViews createHeadsUpContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003652 if (mN.headsUpContentView != null
3653 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003654 return mN.headsUpContentView;
3655 } else if (mStyle != null) {
3656 final RemoteViews styleView = mStyle.makeHeadsUpContentView();
3657 if (styleView != null) {
3658 return styleView;
3659 }
3660 } else if (mActions.size() == 0) {
3661 return null;
3662 }
3663
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003664 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003665 }
3666
Selim Cinek624c02db2015-12-14 21:00:02 -08003667 /**
3668 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
3669 *
3670 * @hide
3671 */
3672 public RemoteViews makePublicContentView() {
3673 if (mN.publicVersion != null) {
3674 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Julia Reynolds3b848122016-02-26 10:45:32 -05003675 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08003676 }
3677 Bundle savedBundle = mN.extras;
3678 Style style = mStyle;
3679 mStyle = null;
3680 Icon largeIcon = mN.mLargeIcon;
3681 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07003682 Bitmap largeIconLegacy = mN.largeIcon;
3683 mN.largeIcon = null;
Selim Cinek624c02db2015-12-14 21:00:02 -08003684 Bundle publicExtras = new Bundle();
3685 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
3686 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
3687 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
3688 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07003689 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
3690 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08003691 publicExtras.putCharSequence(EXTRA_TITLE,
3692 mContext.getString(R.string.notification_hidden_text));
3693 mN.extras = publicExtras;
3694 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
3695 mN.extras = savedBundle;
3696 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07003697 mN.largeIcon = largeIconLegacy;
Selim Cinek624c02db2015-12-14 21:00:02 -08003698 mStyle = style;
3699 return publicView;
3700 }
3701
3702
Chris Wren8fd39ec2014-02-27 17:43:26 -05003703
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003704 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
3705 boolean oddAction) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003706 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07003707 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003708 emphazisedMode ? getEmphasizedActionLayoutResource()
3709 : tombstone ? getActionTombstoneLayoutResource()
3710 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04003711 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003712 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003713 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003714 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003715 if (action.mRemoteInputs != null) {
3716 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
3717 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003718 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07003719 // change the background bgColor
3720 int bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
3721 : R.color.notification_action_list_dark);
3722 button.setDrawableParameters(R.id.button_holder, true, -1, bgColor,
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003723 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinek981962e2016-07-20 20:41:58 -07003724 CharSequence title = action.title;
3725 ColorStateList[] outResultColor = null;
3726 if (isLegacy()) {
3727 title = clearColorSpans(title);
3728 } else {
3729 outResultColor = new ColorStateList[1];
3730 title = ensureColorSpanContrast(title, bgColor, outResultColor);
3731 }
3732 button.setTextViewText(R.id.action0, title);
3733 if (outResultColor != null && outResultColor[0] != null) {
3734 // We need to set the text color as well since changing a text to uppercase
3735 // clears its spans.
3736 button.setTextColor(R.id.action0, outResultColor[0]);
3737 } else if (mN.color != COLOR_DEFAULT) {
3738 button.setTextColor(R.id.action0,resolveContrastColor());
3739 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003740 } else {
Selim Cinek981962e2016-07-20 20:41:58 -07003741 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Selim Cinek06e9e1f2016-07-08 17:14:16 -07003742 if (mN.color != COLOR_DEFAULT) {
3743 button.setTextColor(R.id.action0, resolveContrastColor());
3744 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003745 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003746 return button;
3747 }
3748
Joe Onoratocb109a02011-01-18 17:57:41 -08003749 /**
Selim Cinek981962e2016-07-20 20:41:58 -07003750 * Clears all color spans of a text
3751 * @param charSequence the input text
3752 * @return the same text but without color spans
3753 */
3754 private CharSequence clearColorSpans(CharSequence charSequence) {
3755 if (charSequence instanceof Spanned) {
3756 Spanned ss = (Spanned) charSequence;
3757 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
3758 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
3759 for (Object span : spans) {
3760 Object resultSpan = span;
3761 if (resultSpan instanceof CharacterStyle) {
3762 resultSpan = ((CharacterStyle) span).getUnderlying();
3763 }
3764 if (resultSpan instanceof TextAppearanceSpan) {
3765 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
3766 if (originalSpan.getTextColor() != null) {
3767 resultSpan = new TextAppearanceSpan(
3768 originalSpan.getFamily(),
3769 originalSpan.getTextStyle(),
3770 originalSpan.getTextSize(),
3771 null,
3772 originalSpan.getLinkTextColor());
3773 }
3774 } else if (resultSpan instanceof ForegroundColorSpan
3775 || (resultSpan instanceof BackgroundColorSpan)) {
3776 continue;
3777 } else {
3778 resultSpan = span;
3779 }
3780 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
3781 ss.getSpanFlags(span));
3782 }
3783 return builder;
3784 }
3785 return charSequence;
3786 }
3787
3788 /**
3789 * Ensures contrast on color spans against a background color. also returns the color of the
3790 * text if a span was found that spans over the whole text.
3791 *
3792 * @param charSequence the charSequence on which the spans are
3793 * @param background the background color to ensure the contrast against
3794 * @param outResultColor an array in which a color will be returned as the first element if
3795 * there exists a full length color span.
3796 * @return the contrasted charSequence
3797 */
3798 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
3799 ColorStateList[] outResultColor) {
3800 if (charSequence instanceof Spanned) {
3801 Spanned ss = (Spanned) charSequence;
3802 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
3803 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
3804 for (Object span : spans) {
3805 Object resultSpan = span;
3806 int spanStart = ss.getSpanStart(span);
3807 int spanEnd = ss.getSpanEnd(span);
3808 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
3809 if (resultSpan instanceof CharacterStyle) {
3810 resultSpan = ((CharacterStyle) span).getUnderlying();
3811 }
3812 if (resultSpan instanceof TextAppearanceSpan) {
3813 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
3814 ColorStateList textColor = originalSpan.getTextColor();
3815 if (textColor != null) {
3816 int[] colors = textColor.getColors();
3817 int[] newColors = new int[colors.length];
3818 for (int i = 0; i < newColors.length; i++) {
3819 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
3820 colors[i], background);
3821 }
3822 textColor = new ColorStateList(textColor.getStates().clone(),
3823 newColors);
3824 resultSpan = new TextAppearanceSpan(
3825 originalSpan.getFamily(),
3826 originalSpan.getTextStyle(),
3827 originalSpan.getTextSize(),
3828 textColor,
3829 originalSpan.getLinkTextColor());
3830 if (fullLength) {
3831 outResultColor[0] = new ColorStateList(
3832 textColor.getStates().clone(), newColors);
3833 }
3834 }
3835 } else if (resultSpan instanceof ForegroundColorSpan) {
3836 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
3837 int foregroundColor = originalSpan.getForegroundColor();
3838 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
3839 foregroundColor, background);
3840 resultSpan = new ForegroundColorSpan(foregroundColor);
3841 if (fullLength) {
3842 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
3843 }
3844 } else {
3845 resultSpan = span;
3846 }
3847
3848 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
3849 }
3850 return builder;
3851 }
3852 return charSequence;
3853 }
3854
3855 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003856 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003857 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003858 */
3859 private boolean isLegacy() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003860 return getColorUtil() != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003861 }
3862
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003863 private CharSequence processLegacyText(CharSequence charSequence) {
3864 if (isLegacy()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003865 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003866 } else {
3867 return charSequence;
3868 }
3869 }
3870
Dan Sandler26e81cf2014-05-06 10:01:27 -04003871 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003872 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04003873 */
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003874 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView) {
Selim Cinekea4bef72015-12-02 15:51:10 -08003875 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
3876 if (colorable) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003877 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003878 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003879
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003880 }
Selim Cinekea4bef72015-12-02 15:51:10 -08003881 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003882 colorable ? resolveContrastColor() : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003883 }
3884
Dan Sandler26e81cf2014-05-06 10:01:27 -04003885 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003886 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04003887 * if it's grayscale).
3888 */
3889 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003890 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3891 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003892 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003893 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08003894 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003895 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003896 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003897 }
3898
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003899 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003900 if (mN.color != COLOR_DEFAULT) {
3901 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02003902 }
Jorim Jaggi74419312014-06-10 20:57:21 +02003903 }
3904
Adrian Roos4ff3b122016-02-01 12:26:13 -08003905 int resolveContrastColor() {
3906 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
3907 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003908 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08003909 final int contrasted = NotificationColorUtil.resolveContrastColor(mContext, mN.color);
3910
3911 mCachedContrastColorIsFor = mN.color;
3912 return mCachedContrastColor = contrasted;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003913 }
3914
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003915 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003916 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003917 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003918 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003919 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003920 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003921 mN.actions = new Action[mActions.size()];
3922 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003923 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003924 if (!mPersonList.isEmpty()) {
3925 mN.extras.putStringArray(EXTRA_PEOPLE,
3926 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003927 }
Selim Cinek247fa012016-02-18 09:50:48 -08003928 if (mN.bigContentView != null || mN.contentView != null
3929 || mN.headsUpContentView != null) {
3930 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
3931 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003932 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08003933 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003934
Julia Reynolds3b848122016-02-26 10:45:32 -05003935 /**
3936 * Creates a Builder from an existing notification so further changes can be made.
3937 * @param context The context for your application / activity.
3938 * @param n The notification to create a Builder from.
3939 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003940 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003941 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003942 ApplicationInfo applicationInfo = n.extras.getParcelable(
3943 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003944 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05003945 if (applicationInfo != null) {
3946 try {
3947 builderContext = context.createApplicationContext(applicationInfo,
3948 Context.CONTEXT_RESTRICTED);
3949 } catch (NameNotFoundException e) {
3950 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
3951 builderContext = context; // try with our context
3952 }
3953 } else {
3954 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02003955 }
3956
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003957 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003958 }
3959
3960 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
Selim Cinek593610c2016-02-16 18:42:57 -08003961 Class<? extends Style>[] classes = new Class[] {
3962 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
Alex Hillsfc737de2016-03-23 17:33:02 -04003963 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
3964 MessagingStyle.class };
Christoph Studer4600f9b2014-07-22 22:44:43 +02003965 for (Class<? extends Style> innerClass : classes) {
3966 if (templateClass.equals(innerClass.getName())) {
3967 return innerClass;
3968 }
3969 }
3970 return null;
3971 }
3972
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003973 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003974 * @deprecated Use {@link #build()} instead.
3975 */
3976 @Deprecated
3977 public Notification getNotification() {
3978 return build();
3979 }
3980
3981 /**
3982 * Combine all of the options that have been set and return a new {@link Notification}
3983 * object.
3984 */
3985 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003986 // first, add any extras from the calling code
3987 if (mUserExtras != null) {
3988 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003989 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003990
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003991 mN.creationTime = System.currentTimeMillis();
3992
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003993 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05003994 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02003995
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003996 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003997
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003998 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003999 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004000 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004001
Adrian Roos5081c0d2016-02-26 16:04:19 -08004002 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4003 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004004 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004005 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004006 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
4007 mN.contentView.getSequenceNumber());
4008 }
4009 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004010 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004011 if (mN.bigContentView != null) {
4012 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
4013 mN.bigContentView.getSequenceNumber());
4014 }
4015 }
4016 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004017 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004018 if (mN.headsUpContentView != null) {
4019 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
4020 mN.headsUpContentView.getSequenceNumber());
4021 }
4022 }
4023 }
4024
Julia Reynolds4c0c2022016-02-02 15:11:59 -05004025 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
4026 mN.flags |= FLAG_SHOW_LIGHTS;
4027 }
4028
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004029 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004030 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004031
4032 /**
4033 * Apply this Builder to an existing {@link Notification} object.
4034 *
4035 * @hide
4036 */
4037 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04004038 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004039 return n;
4040 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004041
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004042 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08004043 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
4044 * change.
4045 *
4046 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
4047 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004048 * @hide
4049 */
Adrian Roos184bfe022016-03-03 13:41:44 -08004050 public static Notification maybeCloneStrippedForDelivery(Notification n) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004051 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08004052
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004053 // Only strip views for known Styles because we won't know how to
4054 // re-create them otherwise.
Adrian Roos184bfe022016-03-03 13:41:44 -08004055 if (!TextUtils.isEmpty(templateClass)
4056 && getNotificationStyleClass(templateClass) == null) {
4057 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004058 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004059
4060 // Only strip unmodified BuilderRemoteViews.
4061 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004062 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004063 n.contentView.getSequenceNumber();
4064 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004065 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004066 n.bigContentView.getSequenceNumber();
4067 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004068 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004069 n.headsUpContentView.getSequenceNumber();
4070
4071 // Nothing to do here, no need to clone.
4072 if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
4073 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004074 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004075
4076 Notification clone = n.clone();
4077 if (stripContentView) {
4078 clone.contentView = null;
4079 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
4080 }
4081 if (stripBigContentView) {
4082 clone.bigContentView = null;
4083 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
4084 }
4085 if (stripHeadsUpContentView) {
4086 clone.headsUpContentView = null;
4087 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
4088 }
4089 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004090 }
4091
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004092 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004093 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004094 }
4095
4096 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004097 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004098 }
4099
4100 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004101 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004102 }
4103
4104 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004105 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004106 }
4107
4108 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004109 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004110 }
4111
Adrian Roosc1a80b02016-04-05 14:54:55 -07004112 private int getMessagingLayoutResource() {
4113 return R.layout.notification_template_material_messaging;
4114 }
4115
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004116 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004117 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004118 }
4119
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004120 private int getEmphasizedActionLayoutResource() {
4121 return R.layout.notification_material_action_emphasized;
4122 }
4123
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004124 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004125 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004126 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004127 }
4128
Selim Cinek279fa862016-06-14 10:57:25 -07004129 private boolean hasLargeIcon() {
4130 return mLargeIcon != null || largeIcon != null;
4131 }
4132
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004133 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004134 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004135 * @hide
4136 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07004137 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004138 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
4139 }
4140
4141 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004142 * @return true if the notification will show a chronometer; false otherwise
4143 * @hide
4144 */
4145 public boolean showsChronometer() {
4146 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
4147 }
4148
4149 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004150 * An object that can apply a rich notification style to a {@link Notification.Builder}
4151 * object.
4152 */
Griff Hazendfcb0802014-02-11 12:00:00 -08004153 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04004154 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004155
4156 /**
4157 * @hide
4158 */
4159 protected CharSequence mSummaryText = null;
4160
4161 /**
4162 * @hide
4163 */
4164 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04004165
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004166 protected Builder mBuilder;
4167
Chris Wrend6297db2012-05-03 16:20:13 -04004168 /**
4169 * Overrides ContentTitle in the big form of the template.
4170 * This defaults to the value passed to setContentTitle().
4171 */
4172 protected void internalSetBigContentTitle(CharSequence title) {
4173 mBigContentTitle = title;
4174 }
4175
4176 /**
4177 * Set the first line of text after the detail section in the big form of the template.
4178 */
4179 protected void internalSetSummaryText(CharSequence cs) {
4180 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04004181 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04004182 }
4183
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004184 public void setBuilder(Builder builder) {
4185 if (mBuilder != builder) {
4186 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004187 if (mBuilder != null) {
4188 mBuilder.setStyle(this);
4189 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004190 }
4191 }
4192
Chris Wrend6297db2012-05-03 16:20:13 -04004193 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004194 if (mBuilder == null) {
4195 throw new IllegalArgumentException("Style requires a valid Builder object");
4196 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004197 }
Chris Wrend6297db2012-05-03 16:20:13 -04004198
4199 protected RemoteViews getStandardView(int layoutId) {
4200 checkBuilder();
4201
Christoph Studer4600f9b2014-07-22 22:44:43 +02004202 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004203 CharSequence oldBuilderContentTitle =
4204 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004205 if (mBigContentTitle != null) {
4206 mBuilder.setContentTitle(mBigContentTitle);
4207 }
4208
Chris Wrend6297db2012-05-03 16:20:13 -04004209 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
4210
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004211 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004212
Chris Wrend6297db2012-05-03 16:20:13 -04004213 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
4214 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04004215 } else {
4216 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004217 }
4218
Chris Wrend6297db2012-05-03 16:20:13 -04004219 return contentView;
4220 }
4221
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004222 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004223 * Construct a Style-specific RemoteViews for the final 1U notification layout.
4224 * The default implementation has nothing additional to add.
4225 * @hide
4226 */
4227 public RemoteViews makeContentView() {
4228 return null;
4229 }
4230
4231 /**
4232 * Construct a Style-specific RemoteViews for the final big notification layout.
4233 * @hide
4234 */
4235 public RemoteViews makeBigContentView() {
4236 return null;
4237 }
4238
4239 /**
4240 * Construct a Style-specific RemoteViews for the final HUN layout.
4241 * @hide
4242 */
4243 public RemoteViews makeHeadsUpContentView() {
4244 return null;
4245 }
4246
4247 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004248 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004249 * @hide
4250 */
4251 public void addExtras(Bundle extras) {
4252 if (mSummaryTextSet) {
4253 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
4254 }
4255 if (mBigContentTitle != null) {
4256 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
4257 }
Chris Wren91ad5632013-06-05 15:05:57 -04004258 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004259 }
4260
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004261 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004262 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004263 * @hide
4264 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02004265 protected void restoreFromExtras(Bundle extras) {
4266 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
4267 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
4268 mSummaryTextSet = true;
4269 }
4270 if (extras.containsKey(EXTRA_TITLE_BIG)) {
4271 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
4272 }
4273 }
4274
4275
4276 /**
4277 * @hide
4278 */
4279 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004280 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004281 return wip;
4282 }
4283
Daniel Sandler0ec46202015-06-24 01:27:05 -04004284 /**
4285 * @hide
4286 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004287 public void purgeResources() {}
4288
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004289 /**
4290 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
4291 * attached to.
4292 *
4293 * @return the fully constructed Notification.
4294 */
4295 public Notification build() {
4296 checkBuilder();
4297 return mBuilder.build();
4298 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004299
4300 /**
4301 * @hide
4302 * @return true if the style positions the progress bar on the second line; false if the
4303 * style hides the progress bar
4304 */
4305 protected boolean hasProgress() {
4306 return true;
4307 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004308
4309 /**
4310 * @hide
4311 * @return Whether we should put the summary be put into the notification header
4312 */
4313 public boolean hasSummaryInHeader() {
4314 return true;
4315 }
Selim Cinek593610c2016-02-16 18:42:57 -08004316
4317 /**
4318 * @hide
4319 * @return Whether custom content views are displayed inline in the style
4320 */
4321 public boolean displayCustomViewInline() {
4322 return false;
4323 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004324 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004325
4326 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004327 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08004328 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004329 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004330 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004331 * Notification notif = new Notification.Builder(mContext)
4332 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
4333 * .setContentText(subject)
4334 * .setSmallIcon(R.drawable.new_post)
4335 * .setLargeIcon(aBitmap)
4336 * .setStyle(new Notification.BigPictureStyle()
4337 * .bigPicture(aBigBitmap))
4338 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004339 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004340 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004341 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004342 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004343 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004344 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004345 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004346 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004347
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004348 public BigPictureStyle() {
4349 }
4350
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004351 /**
4352 * @deprecated use {@code BigPictureStyle()}.
4353 */
4354 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004355 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004356 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004357 }
4358
Chris Wrend6297db2012-05-03 16:20:13 -04004359 /**
4360 * Overrides ContentTitle in the big form of the template.
4361 * This defaults to the value passed to setContentTitle().
4362 */
4363 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004364 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004365 return this;
4366 }
4367
4368 /**
4369 * Set the first line of text after the detail section in the big form of the template.
4370 */
4371 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004372 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004373 return this;
4374 }
4375
Chris Wren0bd664d2012-08-01 13:56:56 -04004376 /**
4377 * Provide the bitmap to be used as the payload for the BigPicture notification.
4378 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004379 public BigPictureStyle bigPicture(Bitmap b) {
4380 mPicture = b;
4381 return this;
4382 }
4383
Chris Wren3745a3d2012-05-22 15:11:52 -04004384 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04004385 * Override the large icon when the big notification is shown.
4386 */
4387 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04004388 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
4389 }
4390
4391 /**
4392 * Override the large icon when the big notification is shown.
4393 */
4394 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04004395 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004396 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004397 return this;
4398 }
4399
Riley Andrews0394a0c2015-11-03 23:36:52 -08004400 /** @hide */
4401 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
4402
Daniel Sandler0ec46202015-06-24 01:27:05 -04004403 /**
4404 * @hide
4405 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004406 @Override
4407 public void purgeResources() {
4408 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08004409 if (mPicture != null &&
4410 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08004411 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004412 mPicture = mPicture.createAshmemBitmap();
4413 }
4414 if (mBigLargeIcon != null) {
4415 mBigLargeIcon.convertToAshmem();
4416 }
4417 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01004418
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004419 /**
4420 * @hide
4421 */
4422 public RemoteViews makeBigContentView() {
4423 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01004424 // This covers the following cases:
4425 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004426 // mN.mLargeIcon
4427 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04004428 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07004429 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004430 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004431 oldLargeIcon = mBuilder.mN.mLargeIcon;
4432 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07004433 // The legacy largeIcon might not allow us to clear the image, as it's taken in
4434 // replacement if the other one is null. Because we're restoring these legacy icons
4435 // for old listeners, this is in general non-null.
4436 largeIconLegacy = mBuilder.mN.largeIcon;
4437 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004438 }
4439
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004440 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05004441 if (mSummaryTextSet) {
4442 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinekc848c3a2016-01-13 15:27:30 -08004443 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05004444 }
Selim Cinek279fa862016-06-14 10:57:25 -07004445 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08004446
Christoph Studer5c510ee2014-12-15 16:32:27 +01004447 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004448 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07004449 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004450 }
4451
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004452 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004453 return contentView;
4454 }
4455
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004456 /**
4457 * @hide
4458 */
4459 public void addExtras(Bundle extras) {
4460 super.addExtras(extras);
4461
4462 if (mBigLargeIconSet) {
4463 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
4464 }
4465 extras.putParcelable(EXTRA_PICTURE, mPicture);
4466 }
4467
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004468 /**
4469 * @hide
4470 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004471 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004472 protected void restoreFromExtras(Bundle extras) {
4473 super.restoreFromExtras(extras);
4474
4475 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01004476 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02004477 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04004478 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02004479 mPicture = extras.getParcelable(EXTRA_PICTURE);
4480 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004481
4482 /**
4483 * @hide
4484 */
4485 @Override
4486 public boolean hasSummaryInHeader() {
4487 return false;
4488 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004489 }
4490
4491 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004492 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08004493 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004494 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004495 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004496 * Notification notif = new Notification.Builder(mContext)
4497 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4498 * .setContentText(subject)
4499 * .setSmallIcon(R.drawable.new_mail)
4500 * .setLargeIcon(aBitmap)
4501 * .setStyle(new Notification.BigTextStyle()
4502 * .bigText(aVeryLongString))
4503 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004504 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004505 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004506 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004507 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004508 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004509
4510 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004511 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004512
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004513 private CharSequence mBigText;
4514
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004515 public BigTextStyle() {
4516 }
4517
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004518 /**
4519 * @deprecated use {@code BigTextStyle()}.
4520 */
4521 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004522 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004523 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004524 }
4525
Chris Wrend6297db2012-05-03 16:20:13 -04004526 /**
4527 * Overrides ContentTitle in the big form of the template.
4528 * This defaults to the value passed to setContentTitle().
4529 */
4530 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004531 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004532 return this;
4533 }
4534
4535 /**
4536 * Set the first line of text after the detail section in the big form of the template.
4537 */
4538 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004539 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004540 return this;
4541 }
4542
Chris Wren0bd664d2012-08-01 13:56:56 -04004543 /**
4544 * Provide the longer text to be displayed in the big form of the
4545 * template in place of the content text.
4546 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004547 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004548 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004549 return this;
4550 }
4551
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004552 /**
4553 * @hide
4554 */
4555 public void addExtras(Bundle extras) {
4556 super.addExtras(extras);
4557
Christoph Studer4600f9b2014-07-22 22:44:43 +02004558 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4559 }
4560
4561 /**
4562 * @hide
4563 */
4564 @Override
4565 protected void restoreFromExtras(Bundle extras) {
4566 super.restoreFromExtras(extras);
4567
4568 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004569 }
4570
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004571 /**
4572 * @hide
4573 */
4574 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004575
4576 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07004577 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004578 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004579
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004580 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004581
Selim Cinek75998782016-04-26 10:39:17 -07004582 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004583
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004584 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07004585 if (TextUtils.isEmpty(bigTextText)) {
4586 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
4587 // experience
4588 bigTextText = mBuilder.processLegacyText(text);
4589 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07004590 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08004591
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004592 return contentView;
4593 }
4594
Adrian Roosb1f427c2016-05-26 12:27:15 -07004595 static void applyBigTextContentView(Builder builder,
4596 RemoteViews contentView, CharSequence bigTextText) {
4597 contentView.setTextViewText(R.id.big_text, bigTextText);
4598 contentView.setViewVisibility(R.id.big_text,
4599 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
4600 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines(builder));
Selim Cinek279fa862016-06-14 10:57:25 -07004601 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07004602 }
4603
4604 private static int calculateMaxLines(Builder builder) {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004605 int lineCount = MAX_LINES;
Adrian Roosb1f427c2016-05-26 12:27:15 -07004606 boolean hasActions = builder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004607 if (hasActions) {
4608 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4609 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004610 return lineCount;
4611 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004612 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004613
4614 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004615 * Helper class for generating large-format notifications that include multiple back-and-forth
4616 * messages of varying types between any number of people.
4617 *
4618 * <br>
4619 * If the platform does not provide large-format notifications, this method has no effect. The
4620 * user will always see the normal notification view.
4621 * <br>
4622 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
4623 * so:
4624 * <pre class="prettyprint">
4625 *
4626 * Notification noti = new Notification.Builder()
4627 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
4628 * .setContentText(subject)
4629 * .setSmallIcon(R.drawable.new_message)
4630 * .setLargeIcon(aBitmap)
4631 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
4632 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
4633 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
4634 * .build();
4635 * </pre>
4636 */
4637 public static class MessagingStyle extends Style {
4638
4639 /**
4640 * The maximum number of messages that will be retained in the Notification itself (the
4641 * number displayed is up to the platform).
4642 */
4643 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
4644
4645 CharSequence mUserDisplayName;
4646 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04004647 List<Message> mMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04004648
4649 MessagingStyle() {
4650 }
4651
4652 /**
Alex Hillsfd590442016-10-07 09:52:44 -04004653 * @param userDisplayName Required - the name to be displayed for any replies sent by the
4654 * user before the posting app reposts the notification with those messages after they've
4655 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04004656 * {@link #addMessage(Notification.MessagingStyle.Message)}
4657 */
Alex Hillsfd590442016-10-07 09:52:44 -04004658 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Alex Hillsfc737de2016-03-23 17:33:02 -04004659 mUserDisplayName = userDisplayName;
4660 }
4661
4662 /**
4663 * Returns the name to be displayed for any replies sent by the user
4664 */
4665 public CharSequence getUserDisplayName() {
4666 return mUserDisplayName;
4667 }
4668
4669 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004670 * Sets the title to be displayed on this conversation. This should only be used for
4671 * group messaging and left unset for one-on-one conversations.
4672 * @param conversationTitle
4673 * @return this object for method chaining.
4674 */
4675 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
4676 mConversationTitle = conversationTitle;
4677 return this;
4678 }
4679
4680 /**
4681 * Return the title to be displayed on this conversation. Can be <code>null</code> and
4682 * should be for one-on-one conversations
4683 */
4684 public CharSequence getConversationTitle() {
4685 return mConversationTitle;
4686 }
4687
4688 /**
4689 * Adds a message for display by this notification. Convenience call for a simple
4690 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
4691 * @param text A {@link CharSequence} to be displayed as the message content
4692 * @param timestamp Time at which the message arrived
4693 * @param sender A {@link CharSequence} to be used for displaying the name of the
4694 * sender. Should be <code>null</code> for messages by the current user, in which case
4695 * the platform will insert {@link #getUserDisplayName()}.
4696 * Should be unique amongst all individuals in the conversation, and should be
4697 * consistent during re-posts of the notification.
4698 *
4699 * @see Message#Message(CharSequence, long, CharSequence)
4700 *
4701 * @return this object for method chaining
4702 */
4703 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
4704 mMessages.add(new Message(text, timestamp, sender));
4705 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4706 mMessages.remove(0);
4707 }
4708 return this;
4709 }
4710
4711 /**
4712 * Adds a {@link Message} for display in this notification.
4713 * @param message The {@link Message} to be displayed
4714 * @return this object for method chaining
4715 */
4716 public MessagingStyle addMessage(Message message) {
4717 mMessages.add(message);
4718 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4719 mMessages.remove(0);
4720 }
4721 return this;
4722 }
4723
4724 /**
4725 * Gets the list of {@code Message} objects that represent the notification
4726 */
4727 public List<Message> getMessages() {
4728 return mMessages;
4729 }
4730
4731 /**
4732 * @hide
4733 */
4734 @Override
4735 public void addExtras(Bundle extras) {
4736 super.addExtras(extras);
4737 if (mUserDisplayName != null) {
4738 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
4739 }
4740 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004741 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04004742 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004743 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
4744 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04004745 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07004746
4747 fixTitleAndTextExtras(extras);
4748 }
4749
4750 private void fixTitleAndTextExtras(Bundle extras) {
4751 Message m = findLatestIncomingMessage();
4752 CharSequence text = (m == null) ? null : m.mText;
4753 CharSequence sender = m == null ? null
4754 : TextUtils.isEmpty(m.mSender) ? mUserDisplayName : m.mSender;
4755 CharSequence title;
4756 if (!TextUtils.isEmpty(mConversationTitle)) {
4757 if (!TextUtils.isEmpty(sender)) {
4758 BidiFormatter bidi = BidiFormatter.getInstance();
4759 title = mBuilder.mContext.getString(
4760 com.android.internal.R.string.notification_messaging_title_template,
4761 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(m.mSender));
4762 } else {
4763 title = mConversationTitle;
4764 }
4765 } else {
4766 title = sender;
4767 }
4768
4769 if (title != null) {
4770 extras.putCharSequence(EXTRA_TITLE, title);
4771 }
4772 if (text != null) {
4773 extras.putCharSequence(EXTRA_TEXT, text);
4774 }
Alex Hillsfc737de2016-03-23 17:33:02 -04004775 }
4776
4777 /**
4778 * @hide
4779 */
4780 @Override
4781 protected void restoreFromExtras(Bundle extras) {
4782 super.restoreFromExtras(extras);
4783
4784 mMessages.clear();
Adrian Roos96b7e202016-05-17 13:50:38 -07004785 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
4786 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Alex Hillsd9b04d92016-04-11 16:38:16 -04004787 Parcelable[] parcelables = extras.getParcelableArray(EXTRA_MESSAGES);
Adrian Roosdedd1df2016-04-26 16:38:47 -07004788 if (parcelables != null && parcelables instanceof Parcelable[]) {
4789 mMessages = Message.getMessagesFromBundleArray(parcelables);
Alex Hillsfc737de2016-03-23 17:33:02 -04004790 }
4791 }
4792
4793 /**
4794 * @hide
4795 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07004796 @Override
4797 public RemoteViews makeContentView() {
4798 Message m = findLatestIncomingMessage();
4799 CharSequence title = mConversationTitle != null
4800 ? mConversationTitle
4801 : (m == null) ? null : m.mSender;
4802 CharSequence text = (m == null)
4803 ? null
4804 : mConversationTitle != null ? makeMessageLine(m) : m.mText;
Alex Hillsfc737de2016-03-23 17:33:02 -04004805
Adrian Roosc1a80b02016-04-05 14:54:55 -07004806 return mBuilder.applyStandardTemplate(mBuilder.getBaseLayoutResource(),
4807 false /* hasProgress */,
4808 title,
4809 text);
4810 }
4811
4812 private Message findLatestIncomingMessage() {
4813 for (int i = mMessages.size() - 1; i >= 0; i--) {
4814 Message m = mMessages.get(i);
4815 // Incoming messages have a non-empty sender.
4816 if (!TextUtils.isEmpty(m.mSender)) {
4817 return m;
4818 }
4819 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07004820 if (!mMessages.isEmpty()) {
4821 // No incoming messages, fall back to outgoing message
4822 return mMessages.get(mMessages.size() - 1);
4823 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07004824 return null;
4825 }
4826
4827 /**
4828 * @hide
4829 */
4830 @Override
4831 public RemoteViews makeBigContentView() {
4832 CharSequence title = !TextUtils.isEmpty(super.mBigContentTitle)
4833 ? super.mBigContentTitle
4834 : mConversationTitle;
4835 boolean hasTitle = !TextUtils.isEmpty(title);
4836
Adrian Roosfeafa052016-06-01 17:09:45 -07004837 if (mMessages.size() == 1) {
4838 // Special case for a single message: Use the big text style
4839 // so the collapsed and expanded versions match nicely.
4840 CharSequence bigTitle;
4841 CharSequence text;
4842 if (hasTitle) {
4843 bigTitle = title;
4844 text = makeMessageLine(mMessages.get(0));
4845 } else {
4846 bigTitle = mMessages.get(0).mSender;
4847 text = mMessages.get(0).mText;
4848 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07004849 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
4850 mBuilder.getBigTextLayoutResource(),
Adrian Roosfeafa052016-06-01 17:09:45 -07004851 false /* progress */, bigTitle, null /* text */);
Adrian Roosb1f427c2016-05-26 12:27:15 -07004852 BigTextStyle.applyBigTextContentView(mBuilder, contentView, text);
4853 return contentView;
4854 }
4855
Adrian Roos48d746a2016-04-12 14:57:28 -07004856 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07004857 mBuilder.getMessagingLayoutResource(),
4858 false /* hasProgress */,
4859 title,
4860 null /* text */);
4861
4862 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
4863 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
4864
4865 // Make sure all rows are gone in case we reuse a view.
4866 for (int rowId : rowIds) {
4867 contentView.setViewVisibility(rowId, View.GONE);
4868 }
4869
4870 int i=0;
Adrian Roos2d5dbba2016-06-08 17:11:53 -07004871 contentView.setViewLayoutMarginBottomDimen(R.id.line1,
4872 hasTitle ? R.dimen.notification_messaging_spacing : 0);
Adrian Roosc1a80b02016-04-05 14:54:55 -07004873 contentView.setInt(R.id.notification_messaging, "setNumIndentLines",
Selim Cinek279fa862016-06-14 10:57:25 -07004874 !mBuilder.mN.hasLargeIcon() ? 0 : (hasTitle ? 1 : 2));
Adrian Roosc1a80b02016-04-05 14:54:55 -07004875
Adrian Roosfeafa052016-06-01 17:09:45 -07004876 int contractedChildId = View.NO_ID;
4877 Message contractedMessage = findLatestIncomingMessage();
Adrian Roosc1a80b02016-04-05 14:54:55 -07004878 int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
4879 while (firstMessage + i < mMessages.size() && i < rowIds.length) {
4880 Message m = mMessages.get(firstMessage + i);
4881 int rowId = rowIds[i];
4882
4883 contentView.setViewVisibility(rowId, View.VISIBLE);
4884 contentView.setTextViewText(rowId, makeMessageLine(m));
4885
Adrian Roosfeafa052016-06-01 17:09:45 -07004886 if (contractedMessage == m) {
4887 contractedChildId = rowId;
4888 }
4889
Adrian Roosc1a80b02016-04-05 14:54:55 -07004890 i++;
4891 }
Adrian Roosfeafa052016-06-01 17:09:45 -07004892 // Record this here to allow transformation between the contracted and expanded views.
4893 contentView.setInt(R.id.notification_messaging, "setContractedChildId",
4894 contractedChildId);
Alex Hillsfc737de2016-03-23 17:33:02 -04004895 return contentView;
4896 }
4897
Adrian Roosc1a80b02016-04-05 14:54:55 -07004898 private CharSequence makeMessageLine(Message m) {
4899 BidiFormatter bidi = BidiFormatter.getInstance();
4900 SpannableStringBuilder sb = new SpannableStringBuilder();
4901 if (TextUtils.isEmpty(m.mSender)) {
4902 CharSequence replyName = mUserDisplayName == null ? "" : mUserDisplayName;
4903 sb.append(bidi.unicodeWrap(replyName),
4904 makeFontColorSpan(mBuilder.resolveContrastColor()),
4905 0 /* flags */);
4906 } else {
4907 sb.append(bidi.unicodeWrap(m.mSender),
4908 makeFontColorSpan(Color.BLACK),
4909 0 /* flags */);
4910 }
4911 CharSequence text = m.mText == null ? "" : m.mText;
4912 sb.append(" ").append(bidi.unicodeWrap(text));
4913 return sb;
4914 }
4915
Adrian Roosdedd1df2016-04-26 16:38:47 -07004916 /**
4917 * @hide
4918 */
4919 @Override
4920 public RemoteViews makeHeadsUpContentView() {
4921 Message m = findLatestIncomingMessage();
4922 CharSequence title = mConversationTitle != null
4923 ? mConversationTitle
4924 : (m == null) ? null : m.mSender;
4925 CharSequence text = (m == null)
4926 ? null
4927 : mConversationTitle != null ? makeMessageLine(m) : m.mText;
4928
4929 return mBuilder.applyStandardTemplateWithActions(mBuilder.getBigBaseLayoutResource(),
4930 false /* hasProgress */,
4931 title,
4932 text);
4933 }
4934
Adrian Roosc1a80b02016-04-05 14:54:55 -07004935 private static TextAppearanceSpan makeFontColorSpan(int color) {
4936 return new TextAppearanceSpan(null, 0, 0,
4937 ColorStateList.valueOf(color), null);
4938 }
4939
Alex Hillsd9b04d92016-04-11 16:38:16 -04004940 public static final class Message {
4941
4942 static final String KEY_TEXT = "text";
4943 static final String KEY_TIMESTAMP = "time";
4944 static final String KEY_SENDER = "sender";
4945 static final String KEY_DATA_MIME_TYPE = "type";
4946 static final String KEY_DATA_URI= "uri";
Alex Hillsfc737de2016-03-23 17:33:02 -04004947
4948 private final CharSequence mText;
4949 private final long mTimestamp;
4950 private final CharSequence mSender;
4951
4952 private String mDataMimeType;
4953 private Uri mDataUri;
4954
4955 /**
4956 * Constructor
4957 * @param text A {@link CharSequence} to be displayed as the message content
4958 * @param timestamp Time at which the message arrived
4959 * @param sender A {@link CharSequence} to be used for displaying the name of the
4960 * sender. Should be <code>null</code> for messages by the current user, in which case
4961 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
4962 * Should be unique amongst all individuals in the conversation, and should be
4963 * consistent during re-posts of the notification.
4964 */
4965 public Message(CharSequence text, long timestamp, CharSequence sender){
4966 mText = text;
4967 mTimestamp = timestamp;
4968 mSender = sender;
4969 }
4970
4971 /**
4972 * Sets a binary blob of data and an associated MIME type for a message. In the case
4973 * where the platform doesn't support the MIME type, the original text provided in the
4974 * constructor will be used.
4975 * @param dataMimeType The MIME type of the content. See
4976 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
4977 * types on Android and Android Wear.
4978 * @param dataUri The uri containing the content whose type is given by the MIME type.
4979 * <p class="note">
4980 * <ol>
4981 * <li>Notification Listeners including the System UI need permission to access the
4982 * data the Uri points to. The recommended ways to do this are:</li>
4983 * <li>Store the data in your own ContentProvider, making sure that other apps have
4984 * the correct permission to access your provider. The preferred mechanism for
4985 * providing access is to use per-URI permissions which are temporary and only
4986 * grant access to the receiving application. An easy way to create a
4987 * ContentProvider like this is to use the FileProvider helper class.</li>
4988 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
4989 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
4990 * also store non-media types (see MediaStore.Files for more info). Files can be
4991 * inserted into the MediaStore using scanFile() after which a content:// style
4992 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
4993 * Note that once added to the system MediaStore the content is accessible to any
4994 * app on the device.</li>
4995 * </ol>
4996 * @return this object for method chaining
4997 */
4998 public Message setData(String dataMimeType, Uri dataUri) {
4999 mDataMimeType = dataMimeType;
5000 mDataUri = dataUri;
5001 return this;
5002 }
5003
Alex Hillsfc737de2016-03-23 17:33:02 -04005004 /**
5005 * Get the text to be used for this message, or the fallback text if a type and content
5006 * Uri have been set
5007 */
5008 public CharSequence getText() {
5009 return mText;
5010 }
5011
5012 /**
5013 * Get the time at which this message arrived
5014 */
5015 public long getTimestamp() {
5016 return mTimestamp;
5017 }
5018
5019 /**
5020 * Get the text used to display the contact's name in the messaging experience
5021 */
5022 public CharSequence getSender() {
5023 return mSender;
5024 }
5025
5026 /**
5027 * Get the MIME type of the data pointed to by the Uri
5028 */
5029 public String getDataMimeType() {
5030 return mDataMimeType;
5031 }
5032
5033 /**
5034 * Get the the Uri pointing to the content of the message. Can be null, in which case
5035 * {@see #getText()} is used.
5036 */
5037 public Uri getDataUri() {
5038 return mDataUri;
5039 }
5040
Alex Hillsd9b04d92016-04-11 16:38:16 -04005041 private Bundle toBundle() {
5042 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04005043 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005044 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04005045 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005046 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04005047 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005048 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04005049 }
5050 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005051 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04005052 }
5053 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005054 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04005055 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005056 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04005057 }
5058
Alex Hillsd9b04d92016-04-11 16:38:16 -04005059 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
5060 Bundle[] bundles = new Bundle[messages.size()];
5061 final int N = messages.size();
5062 for (int i = 0; i < N; i++) {
5063 bundles[i] = messages.get(i).toBundle();
5064 }
5065 return bundles;
5066 }
5067
Adrian Roosdedd1df2016-04-26 16:38:47 -07005068 static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005069 List<Message> messages = new ArrayList<>(bundles.length);
5070 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07005071 if (bundles[i] instanceof Bundle) {
5072 Message message = getMessageFromBundle((Bundle)bundles[i]);
5073 if (message != null) {
5074 messages.add(message);
5075 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005076 }
5077 }
5078 return messages;
5079 }
5080
5081 static Message getMessageFromBundle(Bundle bundle) {
5082 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07005083 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005084 return null;
5085 } else {
5086 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
5087 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
5088 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
5089 bundle.containsKey(KEY_DATA_URI)) {
5090
5091 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
5092 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04005093 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005094 return message;
5095 }
5096 } catch (ClassCastException e) {
5097 return null;
5098 }
5099 }
Alex Hillsfc737de2016-03-23 17:33:02 -04005100 }
5101 }
5102
5103 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04005104 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08005105 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005106 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04005107 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005108 * Notification notif = new Notification.Builder(mContext)
5109 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
5110 * .setContentText(subject)
5111 * .setSmallIcon(R.drawable.new_mail)
5112 * .setLargeIcon(aBitmap)
5113 * .setStyle(new Notification.InboxStyle()
5114 * .addLine(str1)
5115 * .addLine(str2)
5116 * .setContentTitle(&quot;&quot;)
5117 * .setSummaryText(&quot;+3 more&quot;))
5118 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04005119 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005120 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04005121 * @see Notification#bigContentView
5122 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005123 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005124 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
5125
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005126 public InboxStyle() {
5127 }
5128
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005129 /**
5130 * @deprecated use {@code InboxStyle()}.
5131 */
5132 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04005133 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005134 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04005135 }
5136
Chris Wrend6297db2012-05-03 16:20:13 -04005137 /**
5138 * Overrides ContentTitle in the big form of the template.
5139 * This defaults to the value passed to setContentTitle().
5140 */
5141 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005142 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005143 return this;
5144 }
5145
5146 /**
5147 * Set the first line of text after the detail section in the big form of the template.
5148 */
5149 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005150 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005151 return this;
5152 }
5153
Chris Wren0bd664d2012-08-01 13:56:56 -04005154 /**
5155 * Append a line to the digest section of the Inbox notification.
5156 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04005157 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005158 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04005159 return this;
5160 }
5161
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005162 /**
5163 * @hide
5164 */
5165 public void addExtras(Bundle extras) {
5166 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005167
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005168 CharSequence[] a = new CharSequence[mTexts.size()];
5169 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
5170 }
5171
Christoph Studer4600f9b2014-07-22 22:44:43 +02005172 /**
5173 * @hide
5174 */
5175 @Override
5176 protected void restoreFromExtras(Bundle extras) {
5177 super.restoreFromExtras(extras);
5178
5179 mTexts.clear();
5180 if (extras.containsKey(EXTRA_TEXT_LINES)) {
5181 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
5182 }
5183 }
5184
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005185 /**
5186 * @hide
5187 */
5188 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08005189 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02005190 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005191 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
5192 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005193
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005194 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04005195
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005196 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005197
Chris Wrend6297db2012-05-03 16:20:13 -04005198 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 -04005199 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04005200
Chris Wren4ed80d52012-05-17 09:30:03 -04005201 // Make sure all rows are gone in case we reuse a view.
5202 for (int rowId : rowIds) {
5203 contentView.setViewVisibility(rowId, View.GONE);
5204 }
5205
Daniel Sandler879c5e02012-04-17 16:46:51 -04005206 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07005207 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
5208 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08005209 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07005210 int onlyViewId = 0;
5211 int maxRows = rowIds.length;
5212 if (mBuilder.mActions.size() > 0) {
5213 maxRows--;
5214 }
5215 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005216 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07005217 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005218 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01005219 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Selim Cinek07c80172016-04-21 16:40:47 -07005220 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08005221 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07005222 if (first) {
5223 onlyViewId = rowIds[i];
5224 } else {
5225 onlyViewId = 0;
5226 }
Selim Cinek247fa012016-02-18 09:50:48 -08005227 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04005228 }
5229 i++;
5230 }
Selim Cinek07c80172016-04-21 16:40:47 -07005231 if (onlyViewId != 0) {
5232 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
5233 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
5234 R.dimen.notification_text_margin_top);
5235 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
5236 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005237
Daniel Sandler879c5e02012-04-17 16:46:51 -04005238 return contentView;
5239 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005240
Selim Cinek247fa012016-02-18 09:50:48 -08005241 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08005242 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08005243 if (first) {
5244 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
5245 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
5246 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07005247 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005248 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08005249 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08005250 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005251 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08005252 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005253 }
Dan Sandler842dd772014-05-15 09:36:47 -04005254
5255 /**
5256 * Notification style for media playback notifications.
5257 *
5258 * In the expanded form, {@link Notification#bigContentView}, up to 5
5259 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04005260 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04005261 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
5262 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
5263 * treated as album artwork.
5264 *
5265 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
5266 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01005267 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04005268 * in the standard view alongside the usual content.
5269 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005270 * Notifications created with MediaStyle will have their category set to
5271 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
5272 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
5273 *
Jeff Browndba34ba2014-06-24 20:46:03 -07005274 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
5275 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04005276 * the System UI can identify this as a notification representing an active media session
5277 * and respond accordingly (by showing album artwork in the lockscreen, for example).
5278 *
5279 * To use this style with your Notification, feed it to
5280 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5281 * <pre class="prettyprint">
5282 * Notification noti = new Notification.Builder()
5283 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01005284 * .setContentTitle(&quot;Track title&quot;)
5285 * .setContentText(&quot;Artist - Album&quot;)
5286 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005287 * .setStyle(<b>new Notification.MediaStyle()</b>
5288 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04005289 * .build();
5290 * </pre>
5291 *
5292 * @see Notification#bigContentView
5293 */
5294 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005295 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04005296 static final int MAX_MEDIA_BUTTONS = 5;
5297
5298 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07005299 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04005300
5301 public MediaStyle() {
5302 }
5303
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005304 /**
5305 * @deprecated use {@code MediaStyle()}.
5306 */
5307 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04005308 public MediaStyle(Builder builder) {
5309 setBuilder(builder);
5310 }
5311
5312 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005313 * 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 -04005314 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005315 *
5316 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04005317 */
5318 public MediaStyle setShowActionsInCompactView(int...actions) {
5319 mActionsToShowInCompact = actions;
5320 return this;
5321 }
5322
5323 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07005324 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
5325 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04005326 */
Jeff Browndba34ba2014-06-24 20:46:03 -07005327 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04005328 mToken = token;
5329 return this;
5330 }
5331
Christoph Studer4600f9b2014-07-22 22:44:43 +02005332 /**
5333 * @hide
5334 */
Dan Sandler842dd772014-05-15 09:36:47 -04005335 @Override
5336 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005337 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005338 if (wip.category == null) {
5339 wip.category = Notification.CATEGORY_TRANSPORT;
5340 }
Dan Sandler842dd772014-05-15 09:36:47 -04005341 return wip;
5342 }
5343
Christoph Studer4600f9b2014-07-22 22:44:43 +02005344 /**
5345 * @hide
5346 */
5347 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005348 public RemoteViews makeContentView() {
5349 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02005350 }
5351
5352 /**
5353 * @hide
5354 */
5355 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005356 public RemoteViews makeBigContentView() {
5357 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02005358 }
5359
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005360 /**
5361 * @hide
5362 */
5363 @Override
5364 public RemoteViews makeHeadsUpContentView() {
5365 RemoteViews expanded = makeMediaBigContentView();
5366 return expanded != null ? expanded : makeMediaContentView();
5367 }
5368
Dan Sandler842dd772014-05-15 09:36:47 -04005369 /** @hide */
5370 @Override
5371 public void addExtras(Bundle extras) {
5372 super.addExtras(extras);
5373
5374 if (mToken != null) {
5375 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
5376 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01005377 if (mActionsToShowInCompact != null) {
5378 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
5379 }
Dan Sandler842dd772014-05-15 09:36:47 -04005380 }
5381
Christoph Studer4600f9b2014-07-22 22:44:43 +02005382 /**
5383 * @hide
5384 */
5385 @Override
5386 protected void restoreFromExtras(Bundle extras) {
5387 super.restoreFromExtras(extras);
5388
5389 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
5390 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
5391 }
5392 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
5393 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
5394 }
5395 }
5396
Selim Cinek5bf069a2015-11-10 19:14:27 -05005397 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04005398 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005399 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07005400 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04005401 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05005402 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
5403 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04005404 if (!tombstone) {
5405 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
5406 }
5407 button.setContentDescription(R.id.action0, action.title);
5408 return button;
5409 }
5410
5411 private RemoteViews makeMediaContentView() {
5412 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005413 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04005414
5415 final int numActions = mBuilder.mActions.size();
5416 final int N = mActionsToShowInCompact == null
5417 ? 0
5418 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
5419 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005420 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04005421 for (int i = 0; i < N; i++) {
5422 if (i >= numActions) {
5423 throw new IllegalArgumentException(String.format(
5424 "setShowActionsInCompactView: action %d out of bounds (max %d)",
5425 i, numActions - 1));
5426 }
5427
5428 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05005429 final RemoteViews button = generateMediaActionButton(action,
Adrian Roos4ff3b122016-02-01 12:26:13 -08005430 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005431 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04005432 }
5433 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08005434 handleImage(view);
5435 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005436 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07005437 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005438 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08005439 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005440 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04005441 return view;
5442 }
5443
5444 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005445 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005446 // Dont add an expanded view if there is no more content to be revealed
5447 int actionsInCompact = mActionsToShowInCompact == null
5448 ? 0
5449 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07005450 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005451 return null;
5452 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05005453 RemoteViews big = mBuilder.applyStandardTemplate(
5454 R.layout.notification_template_material_big_media,
5455 false);
Dan Sandler842dd772014-05-15 09:36:47 -04005456
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005457 if (actionCount > 0) {
5458 big.removeAllViews(com.android.internal.R.id.media_actions);
5459 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05005460 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Adrian Roos4ff3b122016-02-01 12:26:13 -08005461 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005462 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04005463 }
5464 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05005465 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04005466 return big;
5467 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005468
Selim Cinek5bf069a2015-11-10 19:14:27 -05005469 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07005470 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005471 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
5472 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005473 }
5474 }
5475
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005476 /**
5477 * @hide
5478 */
5479 @Override
5480 protected boolean hasProgress() {
5481 return false;
5482 }
Dan Sandler842dd772014-05-15 09:36:47 -04005483 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005484
Selim Cinek593610c2016-02-16 18:42:57 -08005485 /**
5486 * Notification style for custom views that are decorated by the system
5487 *
5488 * <p>Instead of providing a notification that is completely custom, a developer can set this
5489 * style and still obtain system decorations like the notification header with the expand
5490 * affordance and actions.
5491 *
5492 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5493 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5494 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5495 * corresponding custom views to display.
5496 *
5497 * To use this style with your Notification, feed it to
5498 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5499 * <pre class="prettyprint">
5500 * Notification noti = new Notification.Builder()
5501 * .setSmallIcon(R.drawable.ic_stat_player)
5502 * .setLargeIcon(albumArtBitmap))
5503 * .setCustomContentView(contentView);
5504 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
5505 * .build();
5506 * </pre>
5507 */
5508 public static class DecoratedCustomViewStyle extends Style {
5509
5510 public DecoratedCustomViewStyle() {
5511 }
5512
Selim Cinek593610c2016-02-16 18:42:57 -08005513 /**
5514 * @hide
5515 */
5516 public boolean displayCustomViewInline() {
5517 return true;
5518 }
5519
5520 /**
5521 * @hide
5522 */
5523 @Override
5524 public RemoteViews makeContentView() {
5525 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
5526 }
5527
5528 /**
5529 * @hide
5530 */
5531 @Override
5532 public RemoteViews makeBigContentView() {
5533 return makeDecoratedBigContentView();
5534 }
5535
5536 /**
5537 * @hide
5538 */
5539 @Override
5540 public RemoteViews makeHeadsUpContentView() {
5541 return makeDecoratedHeadsUpContentView();
5542 }
5543
Selim Cinek593610c2016-02-16 18:42:57 -08005544 private RemoteViews makeDecoratedHeadsUpContentView() {
5545 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
5546 ? mBuilder.mN.contentView
5547 : mBuilder.mN.headsUpContentView;
5548 if (mBuilder.mActions.size() == 0) {
5549 return makeStandardTemplateWithCustomContent(headsUpContentView);
5550 }
5551 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5552 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005553 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005554 return remoteViews;
5555 }
5556
Selim Cinek593610c2016-02-16 18:42:57 -08005557 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
5558 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
5559 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005560 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08005561 return remoteViews;
5562 }
5563
Selim Cinek593610c2016-02-16 18:42:57 -08005564 private RemoteViews makeDecoratedBigContentView() {
5565 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
5566 ? mBuilder.mN.contentView
5567 : mBuilder.mN.bigContentView;
5568 if (mBuilder.mActions.size() == 0) {
5569 return makeStandardTemplateWithCustomContent(bigContentView);
5570 }
5571 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5572 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005573 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005574 return remoteViews;
5575 }
Selim Cinek247fa012016-02-18 09:50:48 -08005576
5577 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
5578 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005579 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005580 // Need to clone customContent before adding, because otherwise it can no longer be
5581 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005582 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005583 remoteViews.removeAllViews(R.id.notification_main_column);
5584 remoteViews.addView(R.id.notification_main_column, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005585 }
Selim Cinek247fa012016-02-18 09:50:48 -08005586 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005587 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07005588 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005589 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08005590 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005591 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08005592 }
Selim Cinek593610c2016-02-16 18:42:57 -08005593 }
5594
Selim Cinek03eb3b72016-02-18 10:39:45 -08005595 /**
5596 * Notification style for media custom views that are decorated by the system
5597 *
5598 * <p>Instead of providing a media notification that is completely custom, a developer can set
5599 * this style and still obtain system decorations like the notification header with the expand
5600 * affordance and actions.
5601 *
5602 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5603 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5604 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5605 * corresponding custom views to display.
5606 *
5607 * To use this style with your Notification, feed it to
5608 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5609 * <pre class="prettyprint">
5610 * Notification noti = new Notification.Builder()
5611 * .setSmallIcon(R.drawable.ic_stat_player)
5612 * .setLargeIcon(albumArtBitmap))
5613 * .setCustomContentView(contentView);
5614 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
5615 * .setMediaSession(mySession))
5616 * .build();
5617 * </pre>
5618 *
5619 * @see android.app.Notification.DecoratedCustomViewStyle
5620 * @see android.app.Notification.MediaStyle
5621 */
5622 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
5623
5624 public DecoratedMediaCustomViewStyle() {
5625 }
5626
Selim Cinek03eb3b72016-02-18 10:39:45 -08005627 /**
5628 * @hide
5629 */
5630 public boolean displayCustomViewInline() {
5631 return true;
5632 }
5633
5634 /**
5635 * @hide
5636 */
5637 @Override
5638 public RemoteViews makeContentView() {
5639 RemoteViews remoteViews = super.makeContentView();
5640 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5641 mBuilder.mN.contentView);
5642 }
5643
5644 /**
5645 * @hide
5646 */
5647 @Override
5648 public RemoteViews makeBigContentView() {
5649 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
5650 ? mBuilder.mN.bigContentView
5651 : mBuilder.mN.contentView;
5652 return makeBigContentViewWithCustomContent(customRemoteView);
5653 }
5654
5655 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
5656 RemoteViews remoteViews = super.makeBigContentView();
5657 if (remoteViews != null) {
5658 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
5659 customRemoteView);
5660 } else if (customRemoteView != mBuilder.mN.contentView){
5661 remoteViews = super.makeContentView();
5662 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5663 customRemoteView);
5664 } else {
5665 return null;
5666 }
5667 }
5668
5669 /**
5670 * @hide
5671 */
5672 @Override
5673 public RemoteViews makeHeadsUpContentView() {
5674 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
5675 ? mBuilder.mN.headsUpContentView
5676 : mBuilder.mN.contentView;
5677 return makeBigContentViewWithCustomContent(customRemoteView);
5678 }
5679
5680 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
5681 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005682 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005683 // Need to clone customContent before adding, because otherwise it can no longer be
5684 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005685 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005686 remoteViews.removeAllViews(id);
5687 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005688 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08005689 return remoteViews;
5690 }
5691 }
5692
Christoph Studer4600f9b2014-07-22 22:44:43 +02005693 // When adding a new Style subclass here, don't forget to update
5694 // Builder.getNotificationStyleClass.
5695
Griff Hazen61a9e862014-05-22 16:05:19 -07005696 /**
5697 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
5698 * metadata or change options on a notification builder.
5699 */
5700 public interface Extender {
5701 /**
5702 * Apply this extender to a notification builder.
5703 * @param builder the builder to be modified.
5704 * @return the build object for chaining.
5705 */
5706 public Builder extend(Builder builder);
5707 }
5708
5709 /**
5710 * Helper class to add wearable extensions to notifications.
5711 * <p class="note"> See
5712 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
5713 * for Android Wear</a> for more information on how to use this class.
5714 * <p>
5715 * To create a notification with wearable extensions:
5716 * <ol>
5717 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
5718 * properties.
5719 * <li>Create a {@link android.app.Notification.WearableExtender}.
5720 * <li>Set wearable-specific properties using the
5721 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
5722 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
5723 * notification.
5724 * <li>Post the notification to the notification system with the
5725 * {@code NotificationManager.notify(...)} methods.
5726 * </ol>
5727 *
5728 * <pre class="prettyprint">
5729 * Notification notif = new Notification.Builder(mContext)
5730 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5731 * .setContentText(subject)
5732 * .setSmallIcon(R.drawable.new_mail)
5733 * .extend(new Notification.WearableExtender()
5734 * .setContentIcon(R.drawable.new_mail))
5735 * .build();
5736 * NotificationManager notificationManger =
5737 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
5738 * notificationManger.notify(0, notif);</pre>
5739 *
5740 * <p>Wearable extensions can be accessed on an existing notification by using the
5741 * {@code WearableExtender(Notification)} constructor,
5742 * and then using the {@code get} methods to access values.
5743 *
5744 * <pre class="prettyprint">
5745 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
5746 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07005747 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005748 */
5749 public static final class WearableExtender implements Extender {
5750 /**
5751 * Sentinel value for an action index that is unset.
5752 */
5753 public static final int UNSET_ACTION_INDEX = -1;
5754
5755 /**
5756 * Size value for use with {@link #setCustomSizePreset} to show this notification with
5757 * default sizing.
5758 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07005759 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07005760 * on their content.
5761 */
5762 public static final int SIZE_DEFAULT = 0;
5763
5764 /**
5765 * Size value for use with {@link #setCustomSizePreset} to show this notification
5766 * with an extra small size.
5767 * <p>This value is only applicable for custom display notifications created using
5768 * {@link #setDisplayIntent}.
5769 */
5770 public static final int SIZE_XSMALL = 1;
5771
5772 /**
5773 * Size value for use with {@link #setCustomSizePreset} to show this notification
5774 * with a small size.
5775 * <p>This value is only applicable for custom display notifications created using
5776 * {@link #setDisplayIntent}.
5777 */
5778 public static final int SIZE_SMALL = 2;
5779
5780 /**
5781 * Size value for use with {@link #setCustomSizePreset} to show this notification
5782 * with a medium size.
5783 * <p>This value is only applicable for custom display notifications created using
5784 * {@link #setDisplayIntent}.
5785 */
5786 public static final int SIZE_MEDIUM = 3;
5787
5788 /**
5789 * Size value for use with {@link #setCustomSizePreset} to show this notification
5790 * with a large size.
5791 * <p>This value is only applicable for custom display notifications created using
5792 * {@link #setDisplayIntent}.
5793 */
5794 public static final int SIZE_LARGE = 4;
5795
Griff Hazend5f11f92014-05-27 15:40:09 -07005796 /**
5797 * Size value for use with {@link #setCustomSizePreset} to show this notification
5798 * full screen.
5799 * <p>This value is only applicable for custom display notifications created using
5800 * {@link #setDisplayIntent}.
5801 */
5802 public static final int SIZE_FULL_SCREEN = 5;
5803
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005804 /**
5805 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
5806 * short amount of time when this notification is displayed on the screen. This
5807 * is the default value.
5808 */
5809 public static final int SCREEN_TIMEOUT_SHORT = 0;
5810
5811 /**
5812 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
5813 * for a longer amount of time when this notification is displayed on the screen.
5814 */
5815 public static final int SCREEN_TIMEOUT_LONG = -1;
5816
Griff Hazen61a9e862014-05-22 16:05:19 -07005817 /** Notification extra which contains wearable extensions */
5818 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
5819
Pete Gastaf6781d2014-10-07 15:17:05 -04005820 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07005821 private static final String KEY_ACTIONS = "actions";
5822 private static final String KEY_FLAGS = "flags";
5823 private static final String KEY_DISPLAY_INTENT = "displayIntent";
5824 private static final String KEY_PAGES = "pages";
5825 private static final String KEY_BACKGROUND = "background";
5826 private static final String KEY_CONTENT_ICON = "contentIcon";
5827 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
5828 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
5829 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
5830 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
5831 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005832 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04005833 private static final String KEY_DISMISSAL_ID = "dismissalId";
Griff Hazen61a9e862014-05-22 16:05:19 -07005834
5835 // Flags bitwise-ored to mFlags
5836 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
5837 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
5838 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
5839 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005840 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04005841 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04005842 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07005843
5844 // Default value for flags integer
5845 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
5846
5847 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
5848 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
5849
5850 private ArrayList<Action> mActions = new ArrayList<Action>();
5851 private int mFlags = DEFAULT_FLAGS;
5852 private PendingIntent mDisplayIntent;
5853 private ArrayList<Notification> mPages = new ArrayList<Notification>();
5854 private Bitmap mBackground;
5855 private int mContentIcon;
5856 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
5857 private int mContentActionIndex = UNSET_ACTION_INDEX;
5858 private int mCustomSizePreset = SIZE_DEFAULT;
5859 private int mCustomContentHeight;
5860 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005861 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04005862 private String mDismissalId;
Griff Hazen61a9e862014-05-22 16:05:19 -07005863
5864 /**
5865 * Create a {@link android.app.Notification.WearableExtender} with default
5866 * options.
5867 */
5868 public WearableExtender() {
5869 }
5870
5871 public WearableExtender(Notification notif) {
5872 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
5873 if (wearableBundle != null) {
5874 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
5875 if (actions != null) {
5876 mActions.addAll(actions);
5877 }
5878
5879 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
5880 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
5881
5882 Notification[] pages = getNotificationArrayFromBundle(
5883 wearableBundle, KEY_PAGES);
5884 if (pages != null) {
5885 Collections.addAll(mPages, pages);
5886 }
5887
5888 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
5889 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
5890 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
5891 DEFAULT_CONTENT_ICON_GRAVITY);
5892 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
5893 UNSET_ACTION_INDEX);
5894 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
5895 SIZE_DEFAULT);
5896 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
5897 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005898 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04005899 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Griff Hazen61a9e862014-05-22 16:05:19 -07005900 }
5901 }
5902
5903 /**
5904 * Apply wearable extensions to a notification that is being built. This is typically
5905 * called by the {@link android.app.Notification.Builder#extend} method of
5906 * {@link android.app.Notification.Builder}.
5907 */
5908 @Override
5909 public Notification.Builder extend(Notification.Builder builder) {
5910 Bundle wearableBundle = new Bundle();
5911
5912 if (!mActions.isEmpty()) {
5913 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
5914 }
5915 if (mFlags != DEFAULT_FLAGS) {
5916 wearableBundle.putInt(KEY_FLAGS, mFlags);
5917 }
5918 if (mDisplayIntent != null) {
5919 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
5920 }
5921 if (!mPages.isEmpty()) {
5922 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
5923 new Notification[mPages.size()]));
5924 }
5925 if (mBackground != null) {
5926 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
5927 }
5928 if (mContentIcon != 0) {
5929 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
5930 }
5931 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
5932 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
5933 }
5934 if (mContentActionIndex != UNSET_ACTION_INDEX) {
5935 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
5936 mContentActionIndex);
5937 }
5938 if (mCustomSizePreset != SIZE_DEFAULT) {
5939 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
5940 }
5941 if (mCustomContentHeight != 0) {
5942 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
5943 }
5944 if (mGravity != DEFAULT_GRAVITY) {
5945 wearableBundle.putInt(KEY_GRAVITY, mGravity);
5946 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005947 if (mHintScreenTimeout != 0) {
5948 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
5949 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04005950 if (mDismissalId != null) {
5951 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
5952 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005953
5954 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
5955 return builder;
5956 }
5957
5958 @Override
5959 public WearableExtender clone() {
5960 WearableExtender that = new WearableExtender();
5961 that.mActions = new ArrayList<Action>(this.mActions);
5962 that.mFlags = this.mFlags;
5963 that.mDisplayIntent = this.mDisplayIntent;
5964 that.mPages = new ArrayList<Notification>(this.mPages);
5965 that.mBackground = this.mBackground;
5966 that.mContentIcon = this.mContentIcon;
5967 that.mContentIconGravity = this.mContentIconGravity;
5968 that.mContentActionIndex = this.mContentActionIndex;
5969 that.mCustomSizePreset = this.mCustomSizePreset;
5970 that.mCustomContentHeight = this.mCustomContentHeight;
5971 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005972 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04005973 that.mDismissalId = this.mDismissalId;
Griff Hazen61a9e862014-05-22 16:05:19 -07005974 return that;
5975 }
5976
5977 /**
5978 * Add a wearable action to this notification.
5979 *
5980 * <p>When wearable actions are added using this method, the set of actions that
5981 * show on a wearable device splits from devices that only show actions added
5982 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
5983 * of which actions display on different devices.
5984 *
5985 * @param action the action to add to this notification
5986 * @return this object for method chaining
5987 * @see android.app.Notification.Action
5988 */
5989 public WearableExtender addAction(Action action) {
5990 mActions.add(action);
5991 return this;
5992 }
5993
5994 /**
5995 * Adds wearable actions to this notification.
5996 *
5997 * <p>When wearable actions are added using this method, the set of actions that
5998 * show on a wearable device splits from devices that only show actions added
5999 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
6000 * of which actions display on different devices.
6001 *
6002 * @param actions the actions to add to this notification
6003 * @return this object for method chaining
6004 * @see android.app.Notification.Action
6005 */
6006 public WearableExtender addActions(List<Action> actions) {
6007 mActions.addAll(actions);
6008 return this;
6009 }
6010
6011 /**
6012 * Clear all wearable actions present on this builder.
6013 * @return this object for method chaining.
6014 * @see #addAction
6015 */
6016 public WearableExtender clearActions() {
6017 mActions.clear();
6018 return this;
6019 }
6020
6021 /**
6022 * Get the wearable actions present on this notification.
6023 */
6024 public List<Action> getActions() {
6025 return mActions;
6026 }
6027
6028 /**
6029 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07006030 * this notification. The {@link PendingIntent} provided should be for an activity.
6031 *
6032 * <pre class="prettyprint">
6033 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
6034 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
6035 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
6036 * Notification notif = new Notification.Builder(context)
6037 * .extend(new Notification.WearableExtender()
6038 * .setDisplayIntent(displayPendingIntent)
6039 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
6040 * .build();</pre>
6041 *
6042 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07006043 * should have an empty task affinity. It is also recommended to use the device
6044 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07006045 *
6046 * <p>Example AndroidManifest.xml entry:
6047 * <pre class="prettyprint">
6048 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
6049 * android:exported=&quot;true&quot;
6050 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07006051 * android:taskAffinity=&quot;&quot;
6052 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07006053 *
6054 * @param intent the {@link PendingIntent} for an activity
6055 * @return this object for method chaining
6056 * @see android.app.Notification.WearableExtender#getDisplayIntent
6057 */
6058 public WearableExtender setDisplayIntent(PendingIntent intent) {
6059 mDisplayIntent = intent;
6060 return this;
6061 }
6062
6063 /**
6064 * Get the intent to launch inside of an activity view when displaying this
6065 * notification. This {@code PendingIntent} should be for an activity.
6066 */
6067 public PendingIntent getDisplayIntent() {
6068 return mDisplayIntent;
6069 }
6070
6071 /**
6072 * Add an additional page of content to display with this notification. The current
6073 * notification forms the first page, and pages added using this function form
6074 * subsequent pages. This field can be used to separate a notification into multiple
6075 * sections.
6076 *
6077 * @param page the notification to add as another page
6078 * @return this object for method chaining
6079 * @see android.app.Notification.WearableExtender#getPages
6080 */
6081 public WearableExtender addPage(Notification page) {
6082 mPages.add(page);
6083 return this;
6084 }
6085
6086 /**
6087 * Add additional pages of content to display with this notification. The current
6088 * notification forms the first page, and pages added using this function form
6089 * subsequent pages. This field can be used to separate a notification into multiple
6090 * sections.
6091 *
6092 * @param pages a list of notifications
6093 * @return this object for method chaining
6094 * @see android.app.Notification.WearableExtender#getPages
6095 */
6096 public WearableExtender addPages(List<Notification> pages) {
6097 mPages.addAll(pages);
6098 return this;
6099 }
6100
6101 /**
6102 * Clear all additional pages present on this builder.
6103 * @return this object for method chaining.
6104 * @see #addPage
6105 */
6106 public WearableExtender clearPages() {
6107 mPages.clear();
6108 return this;
6109 }
6110
6111 /**
6112 * Get the array of additional pages of content for displaying this notification. The
6113 * current notification forms the first page, and elements within this array form
6114 * subsequent pages. This field can be used to separate a notification into multiple
6115 * sections.
6116 * @return the pages for this notification
6117 */
6118 public List<Notification> getPages() {
6119 return mPages;
6120 }
6121
6122 /**
6123 * Set a background image to be displayed behind the notification content.
6124 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
6125 * will work with any notification style.
6126 *
6127 * @param background the background bitmap
6128 * @return this object for method chaining
6129 * @see android.app.Notification.WearableExtender#getBackground
6130 */
6131 public WearableExtender setBackground(Bitmap background) {
6132 mBackground = background;
6133 return this;
6134 }
6135
6136 /**
6137 * Get a background image to be displayed behind the notification content.
6138 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
6139 * will work with any notification style.
6140 *
6141 * @return the background image
6142 * @see android.app.Notification.WearableExtender#setBackground
6143 */
6144 public Bitmap getBackground() {
6145 return mBackground;
6146 }
6147
6148 /**
6149 * Set an icon that goes with the content of this notification.
6150 */
6151 public WearableExtender setContentIcon(int icon) {
6152 mContentIcon = icon;
6153 return this;
6154 }
6155
6156 /**
6157 * Get an icon that goes with the content of this notification.
6158 */
6159 public int getContentIcon() {
6160 return mContentIcon;
6161 }
6162
6163 /**
6164 * Set the gravity that the content icon should have within the notification display.
6165 * Supported values include {@link android.view.Gravity#START} and
6166 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
6167 * @see #setContentIcon
6168 */
6169 public WearableExtender setContentIconGravity(int contentIconGravity) {
6170 mContentIconGravity = contentIconGravity;
6171 return this;
6172 }
6173
6174 /**
6175 * Get the gravity that the content icon should have within the notification display.
6176 * Supported values include {@link android.view.Gravity#START} and
6177 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
6178 * @see #getContentIcon
6179 */
6180 public int getContentIconGravity() {
6181 return mContentIconGravity;
6182 }
6183
6184 /**
6185 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07006186 * this notification. This action will no longer display separately from the
6187 * notification's content.
6188 *
Griff Hazenca48d352014-05-28 22:37:13 -07006189 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07006190 * set, although the list of available actions comes from the main notification and not
6191 * from the child page's notification.
6192 *
6193 * @param actionIndex The index of the action to hoist onto the current notification page.
6194 * If wearable actions were added to the main notification, this index
6195 * will apply to that list, otherwise it will apply to the regular
6196 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07006197 */
6198 public WearableExtender setContentAction(int actionIndex) {
6199 mContentActionIndex = actionIndex;
6200 return this;
6201 }
6202
6203 /**
Griff Hazenca48d352014-05-28 22:37:13 -07006204 * Get the index of the notification action, if any, that was specified as being clickable
6205 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07006206 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07006207 *
Griff Hazenca48d352014-05-28 22:37:13 -07006208 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07006209 * set, although the list of available actions comes from the main notification and not
6210 * from the child page's notification.
6211 *
6212 * <p>If wearable specific actions were added to the main notification, this index will
6213 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07006214 *
6215 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07006216 */
6217 public int getContentAction() {
6218 return mContentActionIndex;
6219 }
6220
6221 /**
6222 * Set the gravity that this notification should have within the available viewport space.
6223 * Supported values include {@link android.view.Gravity#TOP},
6224 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
6225 * The default value is {@link android.view.Gravity#BOTTOM}.
6226 */
6227 public WearableExtender setGravity(int gravity) {
6228 mGravity = gravity;
6229 return this;
6230 }
6231
6232 /**
6233 * Get the gravity that this notification should have within the available viewport space.
6234 * Supported values include {@link android.view.Gravity#TOP},
6235 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
6236 * The default value is {@link android.view.Gravity#BOTTOM}.
6237 */
6238 public int getGravity() {
6239 return mGravity;
6240 }
6241
6242 /**
6243 * Set the custom size preset for the display of this notification out of the available
6244 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
6245 * {@link #SIZE_LARGE}.
6246 * <p>Some custom size presets are only applicable for custom display notifications created
6247 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
6248 * documentation for the preset in question. See also
6249 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
6250 */
6251 public WearableExtender setCustomSizePreset(int sizePreset) {
6252 mCustomSizePreset = sizePreset;
6253 return this;
6254 }
6255
6256 /**
6257 * Get the custom size preset for the display of this notification out of the available
6258 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
6259 * {@link #SIZE_LARGE}.
6260 * <p>Some custom size presets are only applicable for custom display notifications created
6261 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
6262 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
6263 */
6264 public int getCustomSizePreset() {
6265 return mCustomSizePreset;
6266 }
6267
6268 /**
6269 * Set the custom height in pixels for the display of this notification's content.
6270 * <p>This option is only available for custom display notifications created
6271 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
6272 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
6273 * {@link #getCustomContentHeight}.
6274 */
6275 public WearableExtender setCustomContentHeight(int height) {
6276 mCustomContentHeight = height;
6277 return this;
6278 }
6279
6280 /**
6281 * Get the custom height in pixels for the display of this notification's content.
6282 * <p>This option is only available for custom display notifications created
6283 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
6284 * {@link #setCustomContentHeight}.
6285 */
6286 public int getCustomContentHeight() {
6287 return mCustomContentHeight;
6288 }
6289
6290 /**
6291 * Set whether the scrolling position for the contents of this notification should start
6292 * at the bottom of the contents instead of the top when the contents are too long to
6293 * display within the screen. Default is false (start scroll at the top).
6294 */
6295 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
6296 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
6297 return this;
6298 }
6299
6300 /**
6301 * Get whether the scrolling position for the contents of this notification should start
6302 * at the bottom of the contents instead of the top when the contents are too long to
6303 * display within the screen. Default is false (start scroll at the top).
6304 */
6305 public boolean getStartScrollBottom() {
6306 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
6307 }
6308
6309 /**
6310 * Set whether the content intent is available when the wearable device is not connected
6311 * to a companion device. The user can still trigger this intent when the wearable device
6312 * is offline, but a visual hint will indicate that the content intent may not be available.
6313 * Defaults to true.
6314 */
6315 public WearableExtender setContentIntentAvailableOffline(
6316 boolean contentIntentAvailableOffline) {
6317 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
6318 return this;
6319 }
6320
6321 /**
6322 * Get whether the content intent is available when the wearable device is not connected
6323 * to a companion device. The user can still trigger this intent when the wearable device
6324 * is offline, but a visual hint will indicate that the content intent may not be available.
6325 * Defaults to true.
6326 */
6327 public boolean getContentIntentAvailableOffline() {
6328 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
6329 }
6330
6331 /**
6332 * Set a hint that this notification's icon should not be displayed.
6333 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
6334 * @return this object for method chaining
6335 */
6336 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
6337 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
6338 return this;
6339 }
6340
6341 /**
6342 * Get a hint that this notification's icon should not be displayed.
6343 * @return {@code true} if this icon should not be displayed, false otherwise.
6344 * The default value is {@code false} if this was never set.
6345 */
6346 public boolean getHintHideIcon() {
6347 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
6348 }
6349
6350 /**
6351 * Set a visual hint that only the background image of this notification should be
6352 * displayed, and other semantic content should be hidden. This hint is only applicable
6353 * to sub-pages added using {@link #addPage}.
6354 */
6355 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
6356 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
6357 return this;
6358 }
6359
6360 /**
6361 * Get a visual hint that only the background image of this notification should be
6362 * displayed, and other semantic content should be hidden. This hint is only applicable
6363 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
6364 */
6365 public boolean getHintShowBackgroundOnly() {
6366 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
6367 }
6368
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006369 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08006370 * Set a hint that this notification's background should not be clipped if possible,
6371 * and should instead be resized to fully display on the screen, retaining the aspect
6372 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006373 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
6374 * @return this object for method chaining
6375 */
6376 public WearableExtender setHintAvoidBackgroundClipping(
6377 boolean hintAvoidBackgroundClipping) {
6378 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
6379 return this;
6380 }
6381
6382 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08006383 * Get a hint that this notification's background should not be clipped if possible,
6384 * and should instead be resized to fully display on the screen, retaining the aspect
6385 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006386 * @return {@code true} if it's ok if the background is clipped on the screen, false
6387 * otherwise. The default value is {@code false} if this was never set.
6388 */
6389 public boolean getHintAvoidBackgroundClipping() {
6390 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
6391 }
6392
6393 /**
6394 * Set a hint that the screen should remain on for at least this duration when
6395 * this notification is displayed on the screen.
6396 * @param timeout The requested screen timeout in milliseconds. Can also be either
6397 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
6398 * @return this object for method chaining
6399 */
6400 public WearableExtender setHintScreenTimeout(int timeout) {
6401 mHintScreenTimeout = timeout;
6402 return this;
6403 }
6404
6405 /**
6406 * Get the duration, in milliseconds, that the screen should remain on for
6407 * when this notification is displayed.
6408 * @return the duration in milliseconds if > 0, or either one of the sentinel values
6409 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
6410 */
6411 public int getHintScreenTimeout() {
6412 return mHintScreenTimeout;
6413 }
6414
Alex Hills9ab3a232016-04-05 14:54:56 -04006415 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04006416 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
6417 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
6418 * qr codes, as well as other simple black-and-white tickets.
6419 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
6420 * @return this object for method chaining
6421 */
6422 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
6423 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
6424 return this;
6425 }
6426
6427 /**
6428 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
6429 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
6430 * qr codes, as well as other simple black-and-white tickets.
6431 * @return {@code true} if it should be displayed in ambient, false otherwise
6432 * otherwise. The default value is {@code false} if this was never set.
6433 */
6434 public boolean getHintAmbientBigPicture() {
6435 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
6436 }
6437
6438 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04006439 * Set a hint that this notification's content intent will launch an {@link Activity}
6440 * directly, telling the platform that it can generate the appropriate transitions.
6441 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
6442 * an activity and transitions should be generated, false otherwise.
6443 * @return this object for method chaining
6444 */
6445 public WearableExtender setHintContentIntentLaunchesActivity(
6446 boolean hintContentIntentLaunchesActivity) {
6447 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
6448 return this;
6449 }
6450
6451 /**
6452 * Get a hint that this notification's content intent will launch an {@link Activity}
6453 * directly, telling the platform that it can generate the appropriate transitions
6454 * @return {@code true} if the content intent will launch an activity and transitions should
6455 * be generated, false otherwise. The default value is {@code false} if this was never set.
6456 */
6457 public boolean getHintContentIntentLaunchesActivity() {
6458 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
6459 }
6460
Nadia Benbernou948627e2016-04-14 14:41:08 -04006461 /**
6462 * When you post a notification, if you set the dismissal id field, then when that
6463 * notification is canceled, notifications on other wearables and the paired Android phone
6464 * having that same dismissal id will also be canceled. Note that this only works if you
6465 * have notification bridge mode set to NO_BRIDGING in your Wear app manifest. See
6466 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
6467 * Notifications</a> for more information on how to use the bridge mode feature.
6468 * @param dismissalId the dismissal id of the notification.
6469 * @return this object for method chaining
6470 */
6471 public WearableExtender setDismissalId(String dismissalId) {
6472 mDismissalId = dismissalId;
6473 return this;
6474 }
6475
6476 /**
6477 * Returns the dismissal id of the notification.
6478 * @return the dismissal id of the notification or null if it has not been set.
6479 */
6480 public String getDismissalId() {
6481 return mDismissalId;
6482 }
6483
Griff Hazen61a9e862014-05-22 16:05:19 -07006484 private void setFlag(int mask, boolean value) {
6485 if (value) {
6486 mFlags |= mask;
6487 } else {
6488 mFlags &= ~mask;
6489 }
6490 }
6491 }
6492
6493 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006494 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
6495 * with car extensions:
6496 *
6497 * <ol>
6498 * <li>Create an {@link Notification.Builder}, setting any desired
6499 * properties.
6500 * <li>Create a {@link CarExtender}.
6501 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
6502 * {@link CarExtender}.
6503 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
6504 * to apply the extensions to a notification.
6505 * </ol>
6506 *
6507 * <pre class="prettyprint">
6508 * Notification notification = new Notification.Builder(context)
6509 * ...
6510 * .extend(new CarExtender()
6511 * .set*(...))
6512 * .build();
6513 * </pre>
6514 *
6515 * <p>Car extensions can be accessed on an existing notification by using the
6516 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
6517 * to access values.
6518 */
6519 public static final class CarExtender implements Extender {
6520 private static final String TAG = "CarExtender";
6521
6522 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
6523 private static final String EXTRA_LARGE_ICON = "large_icon";
6524 private static final String EXTRA_CONVERSATION = "car_conversation";
6525 private static final String EXTRA_COLOR = "app_color";
6526
6527 private Bitmap mLargeIcon;
6528 private UnreadConversation mUnreadConversation;
6529 private int mColor = Notification.COLOR_DEFAULT;
6530
6531 /**
6532 * Create a {@link CarExtender} with default options.
6533 */
6534 public CarExtender() {
6535 }
6536
6537 /**
6538 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
6539 *
6540 * @param notif The notification from which to copy options.
6541 */
6542 public CarExtender(Notification notif) {
6543 Bundle carBundle = notif.extras == null ?
6544 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
6545 if (carBundle != null) {
6546 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
6547 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
6548
6549 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
6550 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
6551 }
6552 }
6553
6554 /**
6555 * Apply car extensions to a notification that is being built. This is typically called by
6556 * the {@link Notification.Builder#extend(Notification.Extender)}
6557 * method of {@link Notification.Builder}.
6558 */
6559 @Override
6560 public Notification.Builder extend(Notification.Builder builder) {
6561 Bundle carExtensions = new Bundle();
6562
6563 if (mLargeIcon != null) {
6564 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
6565 }
6566 if (mColor != Notification.COLOR_DEFAULT) {
6567 carExtensions.putInt(EXTRA_COLOR, mColor);
6568 }
6569
6570 if (mUnreadConversation != null) {
6571 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
6572 carExtensions.putBundle(EXTRA_CONVERSATION, b);
6573 }
6574
6575 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
6576 return builder;
6577 }
6578
6579 /**
6580 * Sets the accent color to use when Android Auto presents the notification.
6581 *
6582 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
6583 * to accent the displayed notification. However, not all colors are acceptable in an
6584 * automotive setting. This method can be used to override the color provided in the
6585 * notification in such a situation.
6586 */
Tor Norbye80756e32015-03-02 09:39:27 -08006587 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006588 mColor = color;
6589 return this;
6590 }
6591
6592 /**
6593 * Gets the accent color.
6594 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006595 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006596 */
Tor Norbye80756e32015-03-02 09:39:27 -08006597 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006598 public int getColor() {
6599 return mColor;
6600 }
6601
6602 /**
6603 * Sets the large icon of the car notification.
6604 *
6605 * If no large icon is set in the extender, Android Auto will display the icon
6606 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
6607 *
6608 * @param largeIcon The large icon to use in the car notification.
6609 * @return This object for method chaining.
6610 */
6611 public CarExtender setLargeIcon(Bitmap largeIcon) {
6612 mLargeIcon = largeIcon;
6613 return this;
6614 }
6615
6616 /**
6617 * Gets the large icon used in this car notification, or null if no icon has been set.
6618 *
6619 * @return The large icon for the car notification.
6620 * @see CarExtender#setLargeIcon
6621 */
6622 public Bitmap getLargeIcon() {
6623 return mLargeIcon;
6624 }
6625
6626 /**
6627 * Sets the unread conversation in a message notification.
6628 *
6629 * @param unreadConversation The unread part of the conversation this notification conveys.
6630 * @return This object for method chaining.
6631 */
6632 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
6633 mUnreadConversation = unreadConversation;
6634 return this;
6635 }
6636
6637 /**
6638 * Returns the unread conversation conveyed by this notification.
6639 * @see #setUnreadConversation(UnreadConversation)
6640 */
6641 public UnreadConversation getUnreadConversation() {
6642 return mUnreadConversation;
6643 }
6644
6645 /**
6646 * A class which holds the unread messages from a conversation.
6647 */
6648 public static class UnreadConversation {
6649 private static final String KEY_AUTHOR = "author";
6650 private static final String KEY_TEXT = "text";
6651 private static final String KEY_MESSAGES = "messages";
6652 private static final String KEY_REMOTE_INPUT = "remote_input";
6653 private static final String KEY_ON_REPLY = "on_reply";
6654 private static final String KEY_ON_READ = "on_read";
6655 private static final String KEY_PARTICIPANTS = "participants";
6656 private static final String KEY_TIMESTAMP = "timestamp";
6657
6658 private final String[] mMessages;
6659 private final RemoteInput mRemoteInput;
6660 private final PendingIntent mReplyPendingIntent;
6661 private final PendingIntent mReadPendingIntent;
6662 private final String[] mParticipants;
6663 private final long mLatestTimestamp;
6664
6665 UnreadConversation(String[] messages, RemoteInput remoteInput,
6666 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
6667 String[] participants, long latestTimestamp) {
6668 mMessages = messages;
6669 mRemoteInput = remoteInput;
6670 mReadPendingIntent = readPendingIntent;
6671 mReplyPendingIntent = replyPendingIntent;
6672 mParticipants = participants;
6673 mLatestTimestamp = latestTimestamp;
6674 }
6675
6676 /**
6677 * Gets the list of messages conveyed by this notification.
6678 */
6679 public String[] getMessages() {
6680 return mMessages;
6681 }
6682
6683 /**
6684 * Gets the remote input that will be used to convey the response to a message list, or
6685 * null if no such remote input exists.
6686 */
6687 public RemoteInput getRemoteInput() {
6688 return mRemoteInput;
6689 }
6690
6691 /**
6692 * Gets the pending intent that will be triggered when the user replies to this
6693 * notification.
6694 */
6695 public PendingIntent getReplyPendingIntent() {
6696 return mReplyPendingIntent;
6697 }
6698
6699 /**
6700 * Gets the pending intent that Android Auto will send after it reads aloud all messages
6701 * in this object's message list.
6702 */
6703 public PendingIntent getReadPendingIntent() {
6704 return mReadPendingIntent;
6705 }
6706
6707 /**
6708 * Gets the participants in the conversation.
6709 */
6710 public String[] getParticipants() {
6711 return mParticipants;
6712 }
6713
6714 /**
6715 * Gets the firs participant in the conversation.
6716 */
6717 public String getParticipant() {
6718 return mParticipants.length > 0 ? mParticipants[0] : null;
6719 }
6720
6721 /**
6722 * Gets the timestamp of the conversation.
6723 */
6724 public long getLatestTimestamp() {
6725 return mLatestTimestamp;
6726 }
6727
6728 Bundle getBundleForUnreadConversation() {
6729 Bundle b = new Bundle();
6730 String author = null;
6731 if (mParticipants != null && mParticipants.length > 1) {
6732 author = mParticipants[0];
6733 }
6734 Parcelable[] messages = new Parcelable[mMessages.length];
6735 for (int i = 0; i < messages.length; i++) {
6736 Bundle m = new Bundle();
6737 m.putString(KEY_TEXT, mMessages[i]);
6738 m.putString(KEY_AUTHOR, author);
6739 messages[i] = m;
6740 }
6741 b.putParcelableArray(KEY_MESSAGES, messages);
6742 if (mRemoteInput != null) {
6743 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
6744 }
6745 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
6746 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
6747 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
6748 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
6749 return b;
6750 }
6751
6752 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
6753 if (b == null) {
6754 return null;
6755 }
6756 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
6757 String[] messages = null;
6758 if (parcelableMessages != null) {
6759 String[] tmp = new String[parcelableMessages.length];
6760 boolean success = true;
6761 for (int i = 0; i < tmp.length; i++) {
6762 if (!(parcelableMessages[i] instanceof Bundle)) {
6763 success = false;
6764 break;
6765 }
6766 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
6767 if (tmp[i] == null) {
6768 success = false;
6769 break;
6770 }
6771 }
6772 if (success) {
6773 messages = tmp;
6774 } else {
6775 return null;
6776 }
6777 }
6778
6779 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
6780 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
6781
6782 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
6783
6784 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
6785 if (participants == null || participants.length != 1) {
6786 return null;
6787 }
6788
6789 return new UnreadConversation(messages,
6790 remoteInput,
6791 onReply,
6792 onRead,
6793 participants, b.getLong(KEY_TIMESTAMP));
6794 }
6795 };
6796
6797 /**
6798 * Builder class for {@link CarExtender.UnreadConversation} objects.
6799 */
6800 public static class Builder {
6801 private final List<String> mMessages = new ArrayList<String>();
6802 private final String mParticipant;
6803 private RemoteInput mRemoteInput;
6804 private PendingIntent mReadPendingIntent;
6805 private PendingIntent mReplyPendingIntent;
6806 private long mLatestTimestamp;
6807
6808 /**
6809 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
6810 *
6811 * @param name The name of the other participant in the conversation.
6812 */
6813 public Builder(String name) {
6814 mParticipant = name;
6815 }
6816
6817 /**
6818 * Appends a new unread message to the list of messages for this conversation.
6819 *
6820 * The messages should be added from oldest to newest.
6821 *
6822 * @param message The text of the new unread message.
6823 * @return This object for method chaining.
6824 */
6825 public Builder addMessage(String message) {
6826 mMessages.add(message);
6827 return this;
6828 }
6829
6830 /**
6831 * Sets the pending intent and remote input which will convey the reply to this
6832 * notification.
6833 *
6834 * @param pendingIntent The pending intent which will be triggered on a reply.
6835 * @param remoteInput The remote input parcelable which will carry the reply.
6836 * @return This object for method chaining.
6837 *
6838 * @see CarExtender.UnreadConversation#getRemoteInput
6839 * @see CarExtender.UnreadConversation#getReplyPendingIntent
6840 */
6841 public Builder setReplyAction(
6842 PendingIntent pendingIntent, RemoteInput remoteInput) {
6843 mRemoteInput = remoteInput;
6844 mReplyPendingIntent = pendingIntent;
6845
6846 return this;
6847 }
6848
6849 /**
6850 * Sets the pending intent that will be sent once the messages in this notification
6851 * are read.
6852 *
6853 * @param pendingIntent The pending intent to use.
6854 * @return This object for method chaining.
6855 */
6856 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
6857 mReadPendingIntent = pendingIntent;
6858 return this;
6859 }
6860
6861 /**
6862 * Sets the timestamp of the most recent message in an unread conversation.
6863 *
6864 * If a messaging notification has been posted by your application and has not
6865 * yet been cancelled, posting a later notification with the same id and tag
6866 * but without a newer timestamp may result in Android Auto not displaying a
6867 * heads up notification for the later notification.
6868 *
6869 * @param timestamp The timestamp of the most recent message in the conversation.
6870 * @return This object for method chaining.
6871 */
6872 public Builder setLatestTimestamp(long timestamp) {
6873 mLatestTimestamp = timestamp;
6874 return this;
6875 }
6876
6877 /**
6878 * Builds a new unread conversation object.
6879 *
6880 * @return The new unread conversation object.
6881 */
6882 public UnreadConversation build() {
6883 String[] messages = mMessages.toArray(new String[mMessages.size()]);
6884 String[] participants = { mParticipant };
6885 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
6886 mReadPendingIntent, participants, mLatestTimestamp);
6887 }
6888 }
6889 }
6890
6891 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07006892 * Get an array of Notification objects from a parcelable array bundle field.
6893 * Update the bundle to have a typed array so fetches in the future don't need
6894 * to do an array copy.
6895 */
6896 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
6897 Parcelable[] array = bundle.getParcelableArray(key);
6898 if (array instanceof Notification[] || array == null) {
6899 return (Notification[]) array;
6900 }
6901 Notification[] typedArray = Arrays.copyOf(array, array.length,
6902 Notification[].class);
6903 bundle.putParcelableArray(key, typedArray);
6904 return typedArray;
6905 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006906
6907 private static class BuilderRemoteViews extends RemoteViews {
6908 public BuilderRemoteViews(Parcel parcel) {
6909 super(parcel);
6910 }
6911
Kenny Guy77320062014-08-27 21:37:15 +01006912 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
6913 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006914 }
6915
6916 @Override
6917 public BuilderRemoteViews clone() {
6918 Parcel p = Parcel.obtain();
6919 writeToParcel(p, 0);
6920 p.setDataPosition(0);
6921 BuilderRemoteViews brv = new BuilderRemoteViews(p);
6922 p.recycle();
6923 return brv;
6924 }
6925 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006926}