blob: ccbba1eb69c752ca4effc41d26d693754356701f [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;
Daniel Sandler01df1c62014-06-09 10:54:01 -040022import android.annotation.SdkConstant;
23import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040024import android.annotation.SystemApi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.Context;
26import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010027import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040028import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020029import android.content.pm.PackageManager.NameNotFoundException;
Jorim Jaggief72a192014-08-26 21:57:46 +020030import android.content.res.ColorStateList;
Joe Onoratoef1e7762010-09-17 18:38:38 -040031import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010032import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070033import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010034import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010035import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040036import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040037import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070038import android.media.AudioManager;
Jeff Browndba34ba2014-06-24 20:46:03 -070039import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040041import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020042import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050043import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044import android.os.Parcel;
45import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040046import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070047import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070048import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080049import android.text.SpannableStringBuilder;
50import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080052import android.text.style.AbsoluteSizeSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080053import android.text.style.CharacterStyle;
Selim Cinek60a54252016-02-26 17:03:25 -080054import android.text.style.RelativeSizeSpan;
55import android.text.style.TextAppearanceSpan;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040056import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050057import android.util.SparseArray;
Griff Hazen61a9e862014-05-22 16:05:19 -070058import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080059import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080060import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070061import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070062import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063import android.widget.RemoteViews;
64
Griff Hazen959591e2014-05-15 22:26:18 -070065import com.android.internal.R;
Griff Hazenc091ba82014-05-16 10:13:26 -070066import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070067
Tor Norbyed9273d62013-05-30 15:59:53 -070068import java.lang.annotation.Retention;
69import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020070import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050071import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070072import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070073import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070074import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050075import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077/**
78 * A class that represents how a persistent notification is to be presented to
79 * the user using the {@link android.app.NotificationManager}.
80 *
Joe Onoratocb109a02011-01-18 17:57:41 -080081 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
82 * easier to construct Notifications.</p>
83 *
Joe Fernandez558459f2011-10-13 16:47:36 -070084 * <div class="special reference">
85 * <h3>Developer Guides</h3>
86 * <p>For a guide to creating notifications, read the
87 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
88 * developer guide.</p>
89 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 */
91public class Notification implements Parcelable
92{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040093 private static final String TAG = "Notification";
94
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -040096 * An activity that provides a user interface for adjusting notification preferences for its
97 * containing application. Optional but recommended for apps that post
98 * {@link android.app.Notification Notifications}.
99 */
100 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
101 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
102 = "android.intent.category.NOTIFICATION_PREFERENCES";
103
104 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 * Use all default values (where applicable).
106 */
107 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500108
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 /**
110 * Use the default notification sound. This will ignore any given
111 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500112 *
Chris Wren47c20a12014-06-18 17:27:29 -0400113 * <p>
114 * A notification that is noisy is more likely to be presented as a heads-up notification.
115 * </p>
116 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500118 */
119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 public static final int DEFAULT_SOUND = 1;
121
122 /**
123 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500124 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700125 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500126 *
Chris Wren47c20a12014-06-18 17:27:29 -0400127 * <p>
128 * A notification that vibrates is more likely to be presented as a heads-up notification.
129 * </p>
130 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800131 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500132 */
133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500135
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800136 /**
137 * Use the default notification lights. This will ignore the
138 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
139 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500140 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500142 */
143
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500145
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200147 * Maximum length of CharSequences accepted by Builder and friends.
148 *
149 * <p>
150 * Avoids spamming the system with overly large strings such as full e-mails.
151 */
152 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
153
154 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800155 * Maximum entries of reply text that are accepted by Builder and friends.
156 */
157 private static final int MAX_REPLY_HISTORY = 5;
158
159 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500160 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800161 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500162 * Default value: {@link System#currentTimeMillis() Now}.
163 *
164 * Choose a timestamp that will be most relevant to the user. For most finite events, this
165 * corresponds to the time the event happened (or will happen, in the case of events that have
166 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800167 * timestamped according to when the activity began.
168 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500169 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800170 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500171 * <ul>
172 * <li>Notification of a new chat message should be stamped when the message was received.</li>
173 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
174 * <li>Notification of a completed file download should be stamped when the download finished.</li>
175 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
176 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
177 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800178 * </ul>
179 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700180 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
181 * anymore by default and must be opted into by using
182 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 */
184 public long when;
185
186 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700187 * The creation time of the notification
188 */
189 private long creationTime;
190
191 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400193 *
194 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 */
Dan Sandler86647982015-05-13 23:41:13 -0400196 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700197 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800198 public int icon;
199
200 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800201 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
202 * leave it at its default value of 0.
203 *
204 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700205 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800206 */
207 public int iconLevel;
208
209 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500210 * The number of events that this notification represents. For example, in a new mail
211 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800212 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500213 * The system may or may not use this field to modify the appearance of the notification. For
214 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
215 * superimposed over the icon in the status bar. Starting with
216 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
217 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800218 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500219 * If the number is 0 or negative, it is never shown.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -0700220 *
221 * @deprecated this number is not shown anymore
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 */
223 public int number;
224
225 /**
226 * The intent to execute when the expanded status entry is clicked. If
227 * this is an activity, it must include the
228 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800229 * that you take care of task management as described in the
230 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800231 * Stack</a> document. In particular, make sure to read the notification section
232 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
233 * Notifications</a> for the correct ways to launch an application from a
234 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 */
236 public PendingIntent contentIntent;
237
238 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500239 * The intent to execute when the notification is explicitly dismissed by the user, either with
240 * the "Clear All" button or by swiping it away individually.
241 *
242 * This probably shouldn't be launching an activity since several of those will be sent
243 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800244 */
245 public PendingIntent deleteIntent;
246
247 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700248 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800249 *
Chris Wren47c20a12014-06-18 17:27:29 -0400250 * <p>
251 * The system UI may choose to display a heads-up notification, instead of
252 * launching this intent, while the user is using the device.
253 * </p>
254 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800255 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400256 */
257 public PendingIntent fullScreenIntent;
258
259 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400260 * Text that summarizes this notification for accessibility services.
261 *
262 * As of the L release, this text is no longer shown on screen, but it is still useful to
263 * accessibility services (where it serves as an audible announcement of the notification's
264 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400265 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800266 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800267 */
268 public CharSequence tickerText;
269
270 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400271 * Formerly, a view showing the {@link #tickerText}.
272 *
273 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400274 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400275 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800276 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400277
278 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400279 * The view that will represent this notification in the notification list (which is pulled
280 * down from the status bar).
281 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500282 * As of N, this field may be null. The notification view is determined by the inputs
283 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400284 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400286 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 public RemoteViews contentView;
288
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400289 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400290 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400291 * opportunity to show more detail. The system UI may choose to show this
292 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400293 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500294 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400295 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
296 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400297 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400298 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400299 public RemoteViews bigContentView;
300
Chris Wren8fd39ec2014-02-27 17:43:26 -0500301
302 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400303 * A medium-format version of {@link #contentView}, providing the Notification an
304 * opportunity to add action buttons to contentView. At its discretion, the system UI may
305 * choose to show this as a heads-up notification, which will pop up so the user can see
306 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400307 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500308 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400309 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
310 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500311 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400312 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500313 public RemoteViews headsUpContentView;
314
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400315 /**
Dan Sandler86647982015-05-13 23:41:13 -0400316 * A large bitmap to be shown in the notification content area.
317 *
318 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 */
Dan Sandler86647982015-05-13 23:41:13 -0400320 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800321 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322
323 /**
324 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500325 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400327 * A notification that is noisy is more likely to be presented as a heads-up notification.
328 * </p>
329 *
330 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500331 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 * </p>
333 */
334 public Uri sound;
335
336 /**
337 * Use this constant as the value for audioStreamType to request that
338 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700339 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400340 *
341 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700343 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 public static final int STREAM_DEFAULT = -1;
345
346 /**
347 * The audio stream type to use when playing the sound.
348 * Should be one of the STREAM_ constants from
349 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400350 *
351 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800352 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700353 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 public int audioStreamType = STREAM_DEFAULT;
355
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400357 * The default value of {@link #audioAttributes}.
358 */
359 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
360 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
361 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
362 .build();
363
364 /**
365 * The {@link AudioAttributes audio attributes} to use when playing the sound.
366 */
367 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
368
369 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500370 * The pattern with which to vibrate.
371 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 * <p>
373 * To vibrate the default pattern, see {@link #defaults}.
374 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500375 *
Chris Wren47c20a12014-06-18 17:27:29 -0400376 * <p>
377 * A notification that vibrates is more likely to be presented as a heads-up notification.
378 * </p>
379 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 * @see android.os.Vibrator#vibrate(long[],int)
381 */
382 public long[] vibrate;
383
384 /**
385 * The color of the led. The hardware will do its best approximation.
386 *
387 * @see #FLAG_SHOW_LIGHTS
388 * @see #flags
389 */
Tor Norbye80756e32015-03-02 09:39:27 -0800390 @ColorInt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800391 public int ledARGB;
392
393 /**
394 * The number of milliseconds for the LED to be on while it's flashing.
395 * The hardware will do its best approximation.
396 *
397 * @see #FLAG_SHOW_LIGHTS
398 * @see #flags
399 */
400 public int ledOnMS;
401
402 /**
403 * The number of milliseconds for the LED to be off while it's flashing.
404 * The hardware will do its best approximation.
405 *
406 * @see #FLAG_SHOW_LIGHTS
407 * @see #flags
408 */
409 public int ledOffMS;
410
411 /**
412 * Specifies which values should be taken from the defaults.
413 * <p>
414 * To set, OR the desired from {@link #DEFAULT_SOUND},
415 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
416 * values, use {@link #DEFAULT_ALL}.
417 * </p>
418 */
419 public int defaults;
420
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 /**
422 * Bit to be bitwise-ored into the {@link #flags} field that should be
423 * set if you want the LED on for this notification.
424 * <ul>
425 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
426 * or 0 for both ledOnMS and ledOffMS.</li>
427 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
428 * <li>To flash the LED, pass the number of milliseconds that it should
429 * be on and off to ledOnMS and ledOffMS.</li>
430 * </ul>
431 * <p>
432 * Since hardware varies, you are not guaranteed that any of the values
433 * you pass are honored exactly. Use the system defaults (TODO) if possible
434 * because they will be set to values that work on any given hardware.
435 * <p>
436 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500437 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 */
439 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
440
441 /**
442 * Bit to be bitwise-ored into the {@link #flags} field that should be
443 * set if this notification is in reference to something that is ongoing,
444 * like a phone call. It should not be set if this notification is in
445 * reference to something that happened at a particular point in time,
446 * like a missed phone call.
447 */
448 public static final int FLAG_ONGOING_EVENT = 0x00000002;
449
450 /**
451 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700452 * the audio will be repeated until the notification is
453 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800454 */
455 public static final int FLAG_INSISTENT = 0x00000004;
456
457 /**
458 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700459 * set if you would only like the sound, vibrate and ticker to be played
460 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 */
462 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
463
464 /**
465 * Bit to be bitwise-ored into the {@link #flags} field that should be
466 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500467 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800468 */
469 public static final int FLAG_AUTO_CANCEL = 0x00000010;
470
471 /**
472 * Bit to be bitwise-ored into the {@link #flags} field that should be
473 * set if the notification should not be canceled when the user clicks
474 * the Clear all button.
475 */
476 public static final int FLAG_NO_CLEAR = 0x00000020;
477
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700478 /**
479 * Bit to be bitwise-ored into the {@link #flags} field that should be
480 * set if this notification represents a currently running service. This
481 * will normally be set for you by {@link Service#startForeground}.
482 */
483 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
484
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400485 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500486 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800487 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500488 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400489 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500490 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400491
Griff Hazendfcb0802014-02-11 12:00:00 -0800492 /**
493 * Bit to be bitswise-ored into the {@link #flags} field that should be
494 * set if this notification is relevant to the current device only
495 * and it is not recommended that it bridge to other devices.
496 */
497 public static final int FLAG_LOCAL_ONLY = 0x00000100;
498
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700499 /**
500 * Bit to be bitswise-ored into the {@link #flags} field that should be
501 * set if this notification is the group summary for a group of notifications.
502 * Grouped notifications may display in a cluster or stack on devices which
503 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
504 */
505 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
506
Julia Reynoldse46bb372016-03-17 11:05:58 -0400507 /**
508 * Bit to be bitswise-ored into the {@link #flags} field that should be
509 * set if this notification is the group summary for an auto-group of notifications.
510 *
511 * @hide
512 */
513 @SystemApi
514 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
515
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 public int flags;
517
Tor Norbyed9273d62013-05-30 15:59:53 -0700518 /** @hide */
519 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
520 @Retention(RetentionPolicy.SOURCE)
521 public @interface Priority {}
522
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800523 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500524 * Default notification {@link #priority}. If your application does not prioritize its own
525 * notifications, use this value for all notifications.
526 */
527 public static final int PRIORITY_DEFAULT = 0;
528
529 /**
530 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
531 * items smaller, or at a different position in the list, compared with your app's
532 * {@link #PRIORITY_DEFAULT} items.
533 */
534 public static final int PRIORITY_LOW = -1;
535
536 /**
537 * Lowest {@link #priority}; these items might not be shown to the user except under special
538 * circumstances, such as detailed notification logs.
539 */
540 public static final int PRIORITY_MIN = -2;
541
542 /**
543 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
544 * show these items larger, or at a different position in notification lists, compared with
545 * your app's {@link #PRIORITY_DEFAULT} items.
546 */
547 public static final int PRIORITY_HIGH = 1;
548
549 /**
550 * Highest {@link #priority}, for your application's most important items that require the
551 * user's prompt attention or input.
552 */
553 public static final int PRIORITY_MAX = 2;
554
555 /**
556 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800557 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500558 * Priority is an indication of how much of the user's valuable attention should be consumed by
559 * this notification. Low-priority notifications may be hidden from the user in certain
560 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500561 * system will make a determination about how to interpret this priority when presenting
562 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400563 *
564 * <p>
565 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
566 * as a heads-up notification.
567 * </p>
568 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500569 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700570 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500571 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800572
Dan Sandler26e81cf2014-05-06 10:01:27 -0400573 /**
574 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
575 * to be applied by the standard Style templates when presenting this notification.
576 *
577 * The current template design constructs a colorful header image by overlaying the
578 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
579 * ignored.
580 */
Tor Norbye80756e32015-03-02 09:39:27 -0800581 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400582 public int color = COLOR_DEFAULT;
583
584 /**
585 * Special value of {@link #color} telling the system not to decorate this notification with
586 * any special color but instead use default colors when presenting this notification.
587 */
Tor Norbye80756e32015-03-02 09:39:27 -0800588 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400589 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600590
591 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800592 * Special value of {@link #color} used as a place holder for an invalid color.
593 */
594 @ColorInt
595 private static final int COLOR_INVALID = 1;
596
597 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700598 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
599 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600600 * lockscreen).
601 *
602 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
603 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
604 * shown in all situations, but the contents are only available if the device is unlocked for
605 * the appropriate user.
606 *
607 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
608 * can be read even in an "insecure" context (that is, above a secure lockscreen).
609 * To modify the public version of this notification—for example, to redact some portions—see
610 * {@link Builder#setPublicVersion(Notification)}.
611 *
612 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
613 * and ticker until the user has bypassed the lockscreen.
614 */
615 public int visibility;
616
Griff Hazenfc3922d2014-08-20 11:56:44 -0700617 /**
618 * Notification visibility: Show this notification in its entirety on all lockscreens.
619 *
620 * {@see #visibility}
621 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600622 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700623
624 /**
625 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
626 * private information on secure lockscreens.
627 *
628 * {@see #visibility}
629 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600630 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700631
632 /**
633 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
634 *
635 * {@see #visibility}
636 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600637 public static final int VISIBILITY_SECRET = -1;
638
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500639 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400640 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500641 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400642 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500643
644 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400645 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500646 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400647 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500648
649 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400650 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500651 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400652 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500653
654 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400655 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500656 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400657 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500658
659 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400660 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500661 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400662 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500663
664 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400665 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500666 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400667 public static final String CATEGORY_ALARM = "alarm";
668
669 /**
670 * Notification category: progress of a long-running background operation.
671 */
672 public static final String CATEGORY_PROGRESS = "progress";
673
674 /**
675 * Notification category: social network or sharing update.
676 */
677 public static final String CATEGORY_SOCIAL = "social";
678
679 /**
680 * Notification category: error in background operation or authentication status.
681 */
682 public static final String CATEGORY_ERROR = "err";
683
684 /**
685 * Notification category: media transport control for playback.
686 */
687 public static final String CATEGORY_TRANSPORT = "transport";
688
689 /**
690 * Notification category: system or device status update. Reserved for system use.
691 */
692 public static final String CATEGORY_SYSTEM = "sys";
693
694 /**
695 * Notification category: indication of running background service.
696 */
697 public static final String CATEGORY_SERVICE = "service";
698
699 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400700 * Notification category: a specific, timely recommendation for a single thing.
701 * For example, a news app might want to recommend a news story it believes the user will
702 * want to read next.
703 */
704 public static final String CATEGORY_RECOMMENDATION = "recommendation";
705
706 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400707 * Notification category: ongoing information about device or contextual status.
708 */
709 public static final String CATEGORY_STATUS = "status";
710
711 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400712 * Notification category: user-scheduled reminder.
713 */
714 public static final String CATEGORY_REMINDER = "reminder";
715
716 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400717 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
718 * that best describes this Notification. May be used by the system for ranking and filtering.
719 */
720 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500721
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700722 private String mGroupKey;
723
724 /**
725 * Get the key used to group this notification into a cluster or stack
726 * with other notifications on devices which support such rendering.
727 */
728 public String getGroup() {
729 return mGroupKey;
730 }
731
732 private String mSortKey;
733
734 /**
735 * Get a sort key that orders this notification among other notifications from the
736 * same package. This can be useful if an external sort was already applied and an app
737 * would like to preserve this. Notifications will be sorted lexicographically using this
738 * value, although providing different priorities in addition to providing sort key may
739 * cause this value to be ignored.
740 *
741 * <p>This sort key can also be used to order members of a notification group. See
742 * {@link Builder#setGroup}.
743 *
744 * @see String#compareTo(String)
745 */
746 public String getSortKey() {
747 return mSortKey;
748 }
749
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500750 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400751 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400752 * <p>
753 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
754 * APIs, and are intended to be used by
755 * {@link android.service.notification.NotificationListenerService} implementations to extract
756 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500757 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400758 public Bundle extras = new Bundle();
759
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400760 /**
761 * {@link #extras} key: this is the title of the notification,
762 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
763 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500764 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400765
766 /**
767 * {@link #extras} key: this is the title of the notification when shown in expanded form,
768 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
769 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400770 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400771
772 /**
773 * {@link #extras} key: this is the main text payload, as supplied to
774 * {@link Builder#setContentText(CharSequence)}.
775 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500776 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400777
778 /**
779 * {@link #extras} key: this is a third line of text, as supplied to
780 * {@link Builder#setSubText(CharSequence)}.
781 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400782 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400783
784 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800785 * {@link #extras} key: this is the remote input history, as supplied to
786 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700787 *
788 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
789 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
790 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
791 * notifications once the other party has responded).
792 *
793 * The extra with this key is of type CharSequence[] and contains the most recent entry at
794 * the 0 index, the second most recent at the 1 index, etc.
795 *
796 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800797 */
798 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
799
800 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400801 * {@link #extras} key: this is a small piece of additional text as supplied to
802 * {@link Builder#setContentInfo(CharSequence)}.
803 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400804 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400805
806 /**
807 * {@link #extras} key: this is a line of summary information intended to be shown
808 * alongside expanded notifications, as supplied to (e.g.)
809 * {@link BigTextStyle#setSummaryText(CharSequence)}.
810 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400811 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400812
813 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200814 * {@link #extras} key: this is the longer text shown in the big form of a
815 * {@link BigTextStyle} notification, as supplied to
816 * {@link BigTextStyle#bigText(CharSequence)}.
817 */
818 public static final String EXTRA_BIG_TEXT = "android.bigText";
819
820 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400821 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
822 * supplied to {@link Builder#setSmallIcon(int)}.
823 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500824 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400825
826 /**
827 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
828 * notification payload, as
829 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
830 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400831 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400832
833 /**
834 * {@link #extras} key: this is a bitmap to be used instead of the one from
835 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
836 * shown in its expanded form, as supplied to
837 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
838 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400839 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400840
841 /**
842 * {@link #extras} key: this is the progress value supplied to
843 * {@link Builder#setProgress(int, int, boolean)}.
844 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400845 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400846
847 /**
848 * {@link #extras} key: this is the maximum value supplied to
849 * {@link Builder#setProgress(int, int, boolean)}.
850 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400851 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400852
853 /**
854 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
855 * {@link Builder#setProgress(int, int, boolean)}.
856 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400857 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400858
859 /**
860 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
861 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
862 * {@link Builder#setUsesChronometer(boolean)}.
863 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400864 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400865
866 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800867 * {@link #extras} key: whether the chronometer set on the notification should count down
868 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -0700869 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -0800870 */
Adrian Roos96b7e202016-05-17 13:50:38 -0700871 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -0800872
873 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400874 * {@link #extras} key: whether {@link #when} should be shown,
875 * as supplied to {@link Builder#setShowWhen(boolean)}.
876 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400877 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400878
879 /**
880 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
881 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
882 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400883 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400884
885 /**
886 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
887 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
888 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400889 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400890
891 /**
892 * {@link #extras} key: A string representing the name of the specific
893 * {@link android.app.Notification.Style} used to create this notification.
894 */
Chris Wren91ad5632013-06-05 15:05:57 -0400895 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400896
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400897 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400898 * {@link #extras} key: A String array containing the people that this notification relates to,
899 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400900 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400901 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500902
903 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400904 * Allow certain system-generated notifications to appear before the device is provisioned.
905 * Only available to notifications coming from the android package.
906 * @hide
907 */
908 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
909
910 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700911 * {@link #extras} key: A
912 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
913 * in the background when the notification is selected. The URI must point to an image stream
914 * suitable for passing into
915 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
916 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
917 * URI used for this purpose must require no permissions to read the image data.
918 */
919 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
920
921 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400922 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700923 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400924 * {@link android.app.Notification.MediaStyle} notification.
925 */
926 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
927
928 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100929 * {@link #extras} key: the indices of actions to be shown in the compact view,
930 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
931 */
932 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
933
Christoph Studer943aa672014-08-03 20:31:16 +0200934 /**
Alex Hillsfc737de2016-03-23 17:33:02 -0400935 * {@link #extras} key: the username to be displayed for all messages sent by the user including
936 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -0700937 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
938 * {@link CharSequence}
Alex Hillsfc737de2016-03-23 17:33:02 -0400939 */
940 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
941
942 /**
Adrian Roos96b7e202016-05-17 13:50:38 -0700943 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -0400944 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -0400945 */
Alex Hillsd9b04d92016-04-11 16:38:16 -0400946 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -0400947
948 /**
949 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
950 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -0700951 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
952 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -0400953 */
954 public static final String EXTRA_MESSAGES = "android.messages";
955
956 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100957 * {@link #extras} key: the user that built the notification.
958 *
959 * @hide
960 */
961 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
962
963 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400964 * @hide
965 */
966 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
967
Selim Cinek247fa012016-02-18 09:50:48 -0800968 /**
969 * @hide
970 */
971 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
972
Dan Sandler80eaa592016-04-14 23:34:54 -0400973 /** @hide */
974 @SystemApi
Dan Sandler732bd6c2016-04-12 14:20:32 -0400975 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
976
Dan Sandlerd63f9322015-05-06 15:18:49 -0400977 private Icon mSmallIcon;
978 private Icon mLargeIcon;
979
Chris Wren51c75102013-07-16 20:49:17 -0400980 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400981 * Structure to encapsulate a named action that can be shown as part of this notification.
982 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
983 * selected by the user.
984 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700985 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
986 * or {@link Notification.Builder#addAction(Notification.Action)}
987 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400988 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500989 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700990 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -0400991 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -0700992 private final RemoteInput[] mRemoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -0400993 private boolean mAllowGeneratedReplies = false;
Griff Hazen959591e2014-05-15 22:26:18 -0700994
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400995 /**
996 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -0400997 *
998 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400999 */
Dan Sandler86647982015-05-13 23:41:13 -04001000 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001001 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001002
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001003 /**
1004 * Title of the action.
1005 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001006 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001007
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001008 /**
1009 * Intent to send when the user invokes this action. May be null, in which case the action
1010 * may be rendered in a disabled presentation by the system UI.
1011 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001012 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001013
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001014 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001015 if (in.readInt() != 0) {
1016 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001017 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1018 icon = mIcon.getResId();
1019 }
Dan Sandler86647982015-05-13 23:41:13 -04001020 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001021 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1022 if (in.readInt() == 1) {
1023 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1024 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001025 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001026 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001027 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001028 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001029
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001030 /**
Dan Sandler86647982015-05-13 23:41:13 -04001031 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001032 */
Dan Sandler86647982015-05-13 23:41:13 -04001033 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001034 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills42b0c4d2016-04-26 13:35:36 -04001035 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, false);
Griff Hazen959591e2014-05-15 22:26:18 -07001036 }
1037
Dan Sandler86647982015-05-13 23:41:13 -04001038 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001039 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001040 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001041 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1042 this.icon = icon.getResId();
1043 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001044 this.title = title;
1045 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001046 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001047 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001048 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001049 }
1050
1051 /**
Dan Sandler86647982015-05-13 23:41:13 -04001052 * Return an icon representing the action.
1053 */
1054 public Icon getIcon() {
1055 if (mIcon == null && icon != 0) {
1056 // you snuck an icon in here without using the builder; let's try to keep it
1057 mIcon = Icon.createWithResource("", icon);
1058 }
1059 return mIcon;
1060 }
1061
1062 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001063 * Get additional metadata carried around with this Action.
1064 */
1065 public Bundle getExtras() {
1066 return mExtras;
1067 }
1068
1069 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001070 * Return whether the platform should automatically generate possible replies for this
1071 * {@link Action}
1072 */
1073 public boolean getAllowGeneratedReplies() {
1074 return mAllowGeneratedReplies;
1075 }
1076
1077 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001078 * Get the list of inputs to be collected from the user when this action is sent.
1079 * May return null if no remote inputs were added.
1080 */
1081 public RemoteInput[] getRemoteInputs() {
1082 return mRemoteInputs;
1083 }
1084
1085 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001086 * Builder class for {@link Action} objects.
1087 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001088 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001089 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001090 private final CharSequence mTitle;
1091 private final PendingIntent mIntent;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001092 private boolean mAllowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001093 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001094 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001095
1096 /**
1097 * Construct a new builder for {@link Action} object.
1098 * @param icon icon to show for this action
1099 * @param title the title of the action
1100 * @param intent the {@link PendingIntent} to fire when users trigger this action
1101 */
Dan Sandler86647982015-05-13 23:41:13 -04001102 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001103 public Builder(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -04001104 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
1105 }
1106
1107 /**
1108 * Construct a new builder for {@link Action} object.
1109 * @param icon icon to show for this action
1110 * @param title the title of the action
1111 * @param intent the {@link PendingIntent} to fire when users trigger this action
1112 */
1113 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001114 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -07001115 }
1116
1117 /**
1118 * Construct a new builder for {@link Action} object using the fields from an
1119 * {@link Action}.
1120 * @param action the action to read fields from.
1121 */
1122 public Builder(Action action) {
Dan Sandler86647982015-05-13 23:41:13 -04001123 this(action.getIcon(), action.title, action.actionIntent, new Bundle(action.mExtras),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001124 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -07001125 }
1126
Dan Sandler86647982015-05-13 23:41:13 -04001127 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001128 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -07001129 mIcon = icon;
1130 mTitle = title;
1131 mIntent = intent;
1132 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001133 if (remoteInputs != null) {
1134 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1135 Collections.addAll(mRemoteInputs, remoteInputs);
1136 }
Griff Hazen959591e2014-05-15 22:26:18 -07001137 }
1138
1139 /**
1140 * Merge additional metadata into this builder.
1141 *
1142 * <p>Values within the Bundle will replace existing extras values in this Builder.
1143 *
1144 * @see Notification.Action#extras
1145 */
1146 public Builder addExtras(Bundle extras) {
1147 if (extras != null) {
1148 mExtras.putAll(extras);
1149 }
1150 return this;
1151 }
1152
1153 /**
1154 * Get the metadata Bundle used by this Builder.
1155 *
1156 * <p>The returned Bundle is shared with this Builder.
1157 */
1158 public Bundle getExtras() {
1159 return mExtras;
1160 }
1161
1162 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001163 * Add an input to be collected from the user when this action is sent.
1164 * Response values can be retrieved from the fired intent by using the
1165 * {@link RemoteInput#getResultsFromIntent} function.
1166 * @param remoteInput a {@link RemoteInput} to add to the action
1167 * @return this object for method chaining
1168 */
1169 public Builder addRemoteInput(RemoteInput remoteInput) {
1170 if (mRemoteInputs == null) {
1171 mRemoteInputs = new ArrayList<RemoteInput>();
1172 }
1173 mRemoteInputs.add(remoteInput);
1174 return this;
1175 }
1176
1177 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001178 * Set whether the platform should automatically generate possible replies to add to
1179 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1180 * {@link RemoteInput}, this has no effect.
1181 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1182 * otherwise
1183 * @return this object for method chaining
1184 * The default value is {@code false}
1185 */
1186 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1187 mAllowGeneratedReplies = allowGeneratedReplies;
1188 return this;
1189 }
1190
1191 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001192 * Apply an extender to this action builder. Extenders may be used to add
1193 * metadata or change options on this builder.
1194 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001195 public Builder extend(Extender extender) {
1196 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001197 return this;
1198 }
1199
1200 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001201 * Combine all of the options that have been set and return a new {@link Action}
1202 * object.
1203 * @return the built action
1204 */
1205 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001206 RemoteInput[] remoteInputs = mRemoteInputs != null
1207 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001208 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs,
1209 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001210 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001211 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001212
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001213 @Override
1214 public Action clone() {
1215 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001216 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001217 title,
1218 actionIntent, // safe to alias
1219 new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001220 getRemoteInputs(),
1221 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001222 }
1223 @Override
1224 public int describeContents() {
1225 return 0;
1226 }
1227 @Override
1228 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001229 final Icon ic = getIcon();
1230 if (ic != null) {
1231 out.writeInt(1);
1232 ic.writeToParcel(out, 0);
1233 } else {
1234 out.writeInt(0);
1235 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001236 TextUtils.writeToParcel(title, out, flags);
1237 if (actionIntent != null) {
1238 out.writeInt(1);
1239 actionIntent.writeToParcel(out, flags);
1240 } else {
1241 out.writeInt(0);
1242 }
Griff Hazen959591e2014-05-15 22:26:18 -07001243 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001244 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001245 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001246 }
Griff Hazen959591e2014-05-15 22:26:18 -07001247 public static final Parcelable.Creator<Action> CREATOR =
1248 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001249 public Action createFromParcel(Parcel in) {
1250 return new Action(in);
1251 }
1252 public Action[] newArray(int size) {
1253 return new Action[size];
1254 }
1255 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001256
1257 /**
1258 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1259 * metadata or change options on an action builder.
1260 */
1261 public interface Extender {
1262 /**
1263 * Apply this extender to a notification action builder.
1264 * @param builder the builder to be modified.
1265 * @return the build object for chaining.
1266 */
1267 public Builder extend(Builder builder);
1268 }
1269
1270 /**
1271 * Wearable extender for notification actions. To add extensions to an action,
1272 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1273 * the {@code WearableExtender()} constructor and apply it to a
1274 * {@link android.app.Notification.Action.Builder} using
1275 * {@link android.app.Notification.Action.Builder#extend}.
1276 *
1277 * <pre class="prettyprint">
1278 * Notification.Action action = new Notification.Action.Builder(
1279 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001280 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001281 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001282 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001283 */
1284 public static final class WearableExtender implements Extender {
1285 /** Notification action extra which contains wearable extensions */
1286 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1287
Pete Gastaf6781d2014-10-07 15:17:05 -04001288 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001289 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001290 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1291 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1292 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001293
1294 // Flags bitwise-ored to mFlags
1295 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001296 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001297 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001298
1299 // Default value for flags integer
1300 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1301
1302 private int mFlags = DEFAULT_FLAGS;
1303
Pete Gastaf6781d2014-10-07 15:17:05 -04001304 private CharSequence mInProgressLabel;
1305 private CharSequence mConfirmLabel;
1306 private CharSequence mCancelLabel;
1307
Griff Hazen61a9e862014-05-22 16:05:19 -07001308 /**
1309 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1310 * options.
1311 */
1312 public WearableExtender() {
1313 }
1314
1315 /**
1316 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1317 * wearable options present in an existing notification action.
1318 * @param action the notification action to inspect.
1319 */
1320 public WearableExtender(Action action) {
1321 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1322 if (wearableBundle != null) {
1323 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001324 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1325 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1326 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001327 }
1328 }
1329
1330 /**
1331 * Apply wearable extensions to a notification action that is being built. This is
1332 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1333 * method of {@link android.app.Notification.Action.Builder}.
1334 */
1335 @Override
1336 public Action.Builder extend(Action.Builder builder) {
1337 Bundle wearableBundle = new Bundle();
1338
1339 if (mFlags != DEFAULT_FLAGS) {
1340 wearableBundle.putInt(KEY_FLAGS, mFlags);
1341 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001342 if (mInProgressLabel != null) {
1343 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1344 }
1345 if (mConfirmLabel != null) {
1346 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1347 }
1348 if (mCancelLabel != null) {
1349 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1350 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001351
1352 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1353 return builder;
1354 }
1355
1356 @Override
1357 public WearableExtender clone() {
1358 WearableExtender that = new WearableExtender();
1359 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001360 that.mInProgressLabel = this.mInProgressLabel;
1361 that.mConfirmLabel = this.mConfirmLabel;
1362 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001363 return that;
1364 }
1365
1366 /**
1367 * Set whether this action is available when the wearable device is not connected to
1368 * a companion device. The user can still trigger this action when the wearable device is
1369 * offline, but a visual hint will indicate that the action may not be available.
1370 * Defaults to true.
1371 */
1372 public WearableExtender setAvailableOffline(boolean availableOffline) {
1373 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1374 return this;
1375 }
1376
1377 /**
1378 * Get whether this action is available when the wearable device is not connected to
1379 * a companion device. The user can still trigger this action when the wearable device is
1380 * offline, but a visual hint will indicate that the action may not be available.
1381 * Defaults to true.
1382 */
1383 public boolean isAvailableOffline() {
1384 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1385 }
1386
1387 private void setFlag(int mask, boolean value) {
1388 if (value) {
1389 mFlags |= mask;
1390 } else {
1391 mFlags &= ~mask;
1392 }
1393 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001394
1395 /**
1396 * Set a label to display while the wearable is preparing to automatically execute the
1397 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1398 *
1399 * @param label the label to display while the action is being prepared to execute
1400 * @return this object for method chaining
1401 */
1402 public WearableExtender setInProgressLabel(CharSequence label) {
1403 mInProgressLabel = label;
1404 return this;
1405 }
1406
1407 /**
1408 * Get the label to display while the wearable is preparing to automatically execute
1409 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1410 *
1411 * @return the label to display while the action is being prepared to execute
1412 */
1413 public CharSequence getInProgressLabel() {
1414 return mInProgressLabel;
1415 }
1416
1417 /**
1418 * Set a label to display to confirm that the action should be executed.
1419 * This is usually an imperative verb like "Send".
1420 *
1421 * @param label the label to confirm the action should be executed
1422 * @return this object for method chaining
1423 */
1424 public WearableExtender setConfirmLabel(CharSequence label) {
1425 mConfirmLabel = label;
1426 return this;
1427 }
1428
1429 /**
1430 * Get the label to display to confirm that the action should be executed.
1431 * This is usually an imperative verb like "Send".
1432 *
1433 * @return the label to confirm the action should be executed
1434 */
1435 public CharSequence getConfirmLabel() {
1436 return mConfirmLabel;
1437 }
1438
1439 /**
1440 * Set a label to display to cancel the action.
1441 * This is usually an imperative verb, like "Cancel".
1442 *
1443 * @param label the label to display to cancel the action
1444 * @return this object for method chaining
1445 */
1446 public WearableExtender setCancelLabel(CharSequence label) {
1447 mCancelLabel = label;
1448 return this;
1449 }
1450
1451 /**
1452 * Get the label to display to cancel the action.
1453 * This is usually an imperative verb like "Cancel".
1454 *
1455 * @return the label to display to cancel the action
1456 */
1457 public CharSequence getCancelLabel() {
1458 return mCancelLabel;
1459 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001460
1461 /**
1462 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1463 * platform that it can generate the appropriate transitions.
1464 * @param hintLaunchesActivity {@code true} if the content intent will launch
1465 * an activity and transitions should be generated, false otherwise.
1466 * @return this object for method chaining
1467 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001468 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001469 boolean hintLaunchesActivity) {
1470 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1471 return this;
1472 }
1473
1474 /**
1475 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1476 * platform that it can generate the appropriate transitions
1477 * @return {@code true} if the content intent will launch an activity and transitions
1478 * should be generated, false otherwise. The default value is {@code false} if this was
1479 * never set.
1480 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001481 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001482 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1483 }
Alex Hills9f087612016-06-07 09:08:59 -04001484
1485 /**
1486 * Set a hint that this Action should be displayed inline.
1487 *
1488 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1489 * otherwise
1490 * @return this object for method chaining
1491 */
1492 public WearableExtender setHintDisplayActionInline(
1493 boolean hintDisplayInline) {
1494 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1495 return this;
1496 }
1497
1498 /**
1499 * Get a hint that this Action should be displayed inline.
1500 *
1501 * @return {@code true} if the Action should be displayed inline, {@code false}
1502 * otherwise. The default value is {@code false} if this was never set.
1503 */
1504 public boolean getHintDisplayActionInline() {
1505 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1506 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001507 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001508 }
1509
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001510 /**
1511 * Array of all {@link Action} structures attached to this notification by
1512 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1513 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1514 * interface for invoking actions.
1515 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001516 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001517
1518 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001519 * Replacement version of this notification whose content will be shown
1520 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1521 * and {@link #VISIBILITY_PUBLIC}.
1522 */
1523 public Notification publicVersion;
1524
1525 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001526 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001527 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 */
1529 public Notification()
1530 {
1531 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001532 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001533 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 }
1535
1536 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001537 * @hide
1538 */
1539 public Notification(Context context, int icon, CharSequence tickerText, long when,
1540 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1541 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001542 new Builder(context)
1543 .setWhen(when)
1544 .setSmallIcon(icon)
1545 .setTicker(tickerText)
1546 .setContentTitle(contentTitle)
1547 .setContentText(contentText)
1548 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1549 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001550 }
1551
1552 /**
1553 * Constructs a Notification object with the information needed to
1554 * have a status bar icon without the standard expanded view.
1555 *
1556 * @param icon The resource id of the icon to put in the status bar.
1557 * @param tickerText The text that flows by in the status bar when the notification first
1558 * activates.
1559 * @param when The time to show in the time field. In the System.currentTimeMillis
1560 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001561 *
1562 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001563 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001564 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001565 public Notification(int icon, CharSequence tickerText, long when)
1566 {
1567 this.icon = icon;
1568 this.tickerText = tickerText;
1569 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001570 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001571 }
1572
1573 /**
1574 * Unflatten the notification from a parcel.
1575 */
1576 public Notification(Parcel parcel)
1577 {
1578 int version = parcel.readInt();
1579
1580 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001581 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001582 if (parcel.readInt() != 0) {
1583 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001584 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1585 icon = mSmallIcon.getResId();
1586 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001587 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001588 number = parcel.readInt();
1589 if (parcel.readInt() != 0) {
1590 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1591 }
1592 if (parcel.readInt() != 0) {
1593 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1594 }
1595 if (parcel.readInt() != 0) {
1596 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1597 }
1598 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001599 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001600 }
1601 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001602 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1603 }
Joe Onorato561d3852010-11-20 18:09:34 -08001604 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001605 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001606 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001607 defaults = parcel.readInt();
1608 flags = parcel.readInt();
1609 if (parcel.readInt() != 0) {
1610 sound = Uri.CREATOR.createFromParcel(parcel);
1611 }
1612
1613 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001614 if (parcel.readInt() != 0) {
1615 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1616 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001617 vibrate = parcel.createLongArray();
1618 ledARGB = parcel.readInt();
1619 ledOnMS = parcel.readInt();
1620 ledOffMS = parcel.readInt();
1621 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001622
1623 if (parcel.readInt() != 0) {
1624 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1625 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001626
1627 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001628
John Spurlockfd7f1e02014-03-18 16:41:57 -04001629 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001630
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001631 mGroupKey = parcel.readString();
1632
1633 mSortKey = parcel.readString();
1634
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001635 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001636
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001637 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1638
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001639 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001640 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1641 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001642
Chris Wren8fd39ec2014-02-27 17:43:26 -05001643 if (parcel.readInt() != 0) {
1644 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1645 }
1646
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001647 visibility = parcel.readInt();
1648
1649 if (parcel.readInt() != 0) {
1650 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1651 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001652
1653 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001654 }
1655
Andy Stadler110988c2010-12-03 14:29:16 -08001656 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001657 public Notification clone() {
1658 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001659 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001660 return that;
1661 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001662
Daniel Sandler1a497d32013-04-18 14:52:45 -04001663 /**
1664 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1665 * of this into that.
1666 * @hide
1667 */
1668 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001669 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001670 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001671 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001672 that.number = this.number;
1673
1674 // PendingIntents are global, so there's no reason (or way) to clone them.
1675 that.contentIntent = this.contentIntent;
1676 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001677 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001678
1679 if (this.tickerText != null) {
1680 that.tickerText = this.tickerText.toString();
1681 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001682 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001683 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001684 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001685 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001686 that.contentView = this.contentView.clone();
1687 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001688 if (heavy && this.mLargeIcon != null) {
1689 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001690 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001691 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001692 that.sound = this.sound; // android.net.Uri is immutable
1693 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001694 if (this.audioAttributes != null) {
1695 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1696 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001697
1698 final long[] vibrate = this.vibrate;
1699 if (vibrate != null) {
1700 final int N = vibrate.length;
1701 final long[] vib = that.vibrate = new long[N];
1702 System.arraycopy(vibrate, 0, vib, 0, N);
1703 }
1704
1705 that.ledARGB = this.ledARGB;
1706 that.ledOnMS = this.ledOnMS;
1707 that.ledOffMS = this.ledOffMS;
1708 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001709
Joe Onorato18e69df2010-05-17 22:26:12 -07001710 that.flags = this.flags;
1711
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001712 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001713
John Spurlockfd7f1e02014-03-18 16:41:57 -04001714 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001715
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001716 that.mGroupKey = this.mGroupKey;
1717
1718 that.mSortKey = this.mSortKey;
1719
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001720 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001721 try {
1722 that.extras = new Bundle(this.extras);
1723 // will unparcel
1724 that.extras.size();
1725 } catch (BadParcelableException e) {
1726 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1727 that.extras = null;
1728 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001729 }
1730
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001731 if (this.actions != null) {
1732 that.actions = new Action[this.actions.length];
1733 for(int i=0; i<this.actions.length; i++) {
1734 that.actions[i] = this.actions[i].clone();
1735 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001736 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001737
Daniel Sandler1a497d32013-04-18 14:52:45 -04001738 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001739 that.bigContentView = this.bigContentView.clone();
1740 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001741
Chris Wren8fd39ec2014-02-27 17:43:26 -05001742 if (heavy && this.headsUpContentView != null) {
1743 that.headsUpContentView = this.headsUpContentView.clone();
1744 }
1745
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001746 that.visibility = this.visibility;
1747
1748 if (this.publicVersion != null) {
1749 that.publicVersion = new Notification();
1750 this.publicVersion.cloneInto(that.publicVersion, heavy);
1751 }
1752
Dan Sandler26e81cf2014-05-06 10:01:27 -04001753 that.color = this.color;
1754
Daniel Sandler1a497d32013-04-18 14:52:45 -04001755 if (!heavy) {
1756 that.lightenPayload(); // will clean out extras
1757 }
1758 }
1759
1760 /**
1761 * Removes heavyweight parts of the Notification object for archival or for sending to
1762 * listeners when the full contents are not necessary.
1763 * @hide
1764 */
1765 public final void lightenPayload() {
1766 tickerView = null;
1767 contentView = null;
1768 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001769 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001770 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05001771 if (extras != null && !extras.isEmpty()) {
1772 final Set<String> keyset = extras.keySet();
1773 final int N = keyset.size();
1774 final String[] keys = keyset.toArray(new String[N]);
1775 for (int i=0; i<N; i++) {
1776 final String key = keys[i];
1777 final Object obj = extras.get(key);
1778 if (obj != null &&
1779 ( obj instanceof Parcelable
1780 || obj instanceof Parcelable[]
1781 || obj instanceof SparseArray
1782 || obj instanceof ArrayList)) {
1783 extras.remove(key);
1784 }
1785 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001786 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001787 }
1788
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001789 /**
1790 * Make sure this CharSequence is safe to put into a bundle, which basically
1791 * means it had better not be some custom Parcelable implementation.
1792 * @hide
1793 */
1794 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001795 if (cs == null) return cs;
1796 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1797 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1798 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001799 if (cs instanceof Parcelable) {
1800 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1801 + " instance is a custom Parcelable and not allowed in Notification");
1802 return cs.toString();
1803 }
Selim Cinek60a54252016-02-26 17:03:25 -08001804 return removeTextSizeSpans(cs);
1805 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001806
Selim Cinek60a54252016-02-26 17:03:25 -08001807 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
1808 if (charSequence instanceof Spanned) {
1809 Spanned ss = (Spanned) charSequence;
1810 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
1811 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
1812 for (Object span : spans) {
1813 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08001814 if (resultSpan instanceof CharacterStyle) {
1815 resultSpan = ((CharacterStyle) span).getUnderlying();
1816 }
1817 if (resultSpan instanceof TextAppearanceSpan) {
1818 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08001819 resultSpan = new TextAppearanceSpan(
1820 originalSpan.getFamily(),
1821 originalSpan.getTextStyle(),
1822 -1,
1823 originalSpan.getTextColor(),
1824 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08001825 } else if (resultSpan instanceof RelativeSizeSpan
1826 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08001827 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08001828 } else {
1829 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08001830 }
1831 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
1832 ss.getSpanFlags(span));
1833 }
1834 return builder;
1835 }
1836 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001837 }
1838
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 public int describeContents() {
1840 return 0;
1841 }
1842
1843 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001844 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 */
1846 public void writeToParcel(Parcel parcel, int flags)
1847 {
1848 parcel.writeInt(1);
1849
1850 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001851 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04001852 if (mSmallIcon == null && icon != 0) {
1853 // you snuck an icon in here without using the builder; let's try to keep it
1854 mSmallIcon = Icon.createWithResource("", icon);
1855 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001856 if (mSmallIcon != null) {
1857 parcel.writeInt(1);
1858 mSmallIcon.writeToParcel(parcel, 0);
1859 } else {
1860 parcel.writeInt(0);
1861 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001862 parcel.writeInt(number);
1863 if (contentIntent != null) {
1864 parcel.writeInt(1);
1865 contentIntent.writeToParcel(parcel, 0);
1866 } else {
1867 parcel.writeInt(0);
1868 }
1869 if (deleteIntent != null) {
1870 parcel.writeInt(1);
1871 deleteIntent.writeToParcel(parcel, 0);
1872 } else {
1873 parcel.writeInt(0);
1874 }
1875 if (tickerText != null) {
1876 parcel.writeInt(1);
1877 TextUtils.writeToParcel(tickerText, parcel, flags);
1878 } else {
1879 parcel.writeInt(0);
1880 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001881 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001882 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001883 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001884 } else {
1885 parcel.writeInt(0);
1886 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 if (contentView != null) {
1888 parcel.writeInt(1);
1889 contentView.writeToParcel(parcel, 0);
1890 } else {
1891 parcel.writeInt(0);
1892 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001893 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001894 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001895 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001896 } else {
1897 parcel.writeInt(0);
1898 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001899
1900 parcel.writeInt(defaults);
1901 parcel.writeInt(this.flags);
1902
1903 if (sound != null) {
1904 parcel.writeInt(1);
1905 sound.writeToParcel(parcel, 0);
1906 } else {
1907 parcel.writeInt(0);
1908 }
1909 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001910
1911 if (audioAttributes != null) {
1912 parcel.writeInt(1);
1913 audioAttributes.writeToParcel(parcel, 0);
1914 } else {
1915 parcel.writeInt(0);
1916 }
1917
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 parcel.writeLongArray(vibrate);
1919 parcel.writeInt(ledARGB);
1920 parcel.writeInt(ledOnMS);
1921 parcel.writeInt(ledOffMS);
1922 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001923
1924 if (fullScreenIntent != null) {
1925 parcel.writeInt(1);
1926 fullScreenIntent.writeToParcel(parcel, 0);
1927 } else {
1928 parcel.writeInt(0);
1929 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001930
1931 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001932
John Spurlockfd7f1e02014-03-18 16:41:57 -04001933 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001934
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001935 parcel.writeString(mGroupKey);
1936
1937 parcel.writeString(mSortKey);
1938
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001939 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001940
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001941 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001942
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001943 if (bigContentView != null) {
1944 parcel.writeInt(1);
1945 bigContentView.writeToParcel(parcel, 0);
1946 } else {
1947 parcel.writeInt(0);
1948 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001949
Chris Wren8fd39ec2014-02-27 17:43:26 -05001950 if (headsUpContentView != null) {
1951 parcel.writeInt(1);
1952 headsUpContentView.writeToParcel(parcel, 0);
1953 } else {
1954 parcel.writeInt(0);
1955 }
1956
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001957 parcel.writeInt(visibility);
1958
1959 if (publicVersion != null) {
1960 parcel.writeInt(1);
1961 publicVersion.writeToParcel(parcel, 0);
1962 } else {
1963 parcel.writeInt(0);
1964 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001965
1966 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001967 }
1968
1969 /**
1970 * Parcelable.Creator that instantiates Notification objects
1971 */
1972 public static final Parcelable.Creator<Notification> CREATOR
1973 = new Parcelable.Creator<Notification>()
1974 {
1975 public Notification createFromParcel(Parcel parcel)
1976 {
1977 return new Notification(parcel);
1978 }
1979
1980 public Notification[] newArray(int size)
1981 {
1982 return new Notification[size];
1983 }
1984 };
1985
1986 /**
1987 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1988 * layout.
1989 *
1990 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1991 * in the view.</p>
1992 * @param context The context for your application / activity.
1993 * @param contentTitle The title that goes in the expanded entry.
1994 * @param contentText The text that goes in the expanded entry.
1995 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1996 * If this is an activity, it must include the
1997 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001998 * that you take care of task management as described in the
1999 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2000 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002001 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002002 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002003 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002004 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002005 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002006 public void setLatestEventInfo(Context context,
2007 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002008 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2009 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2010 new Throwable());
2011 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002012
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002013 // ensure that any information already set directly is preserved
2014 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002015
2016 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002018 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002019 }
2020 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002021 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002023 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002024
2025 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002026 }
2027
Julia Reynoldsda303542015-11-23 14:00:20 -05002028 /**
2029 * @hide
2030 */
2031 public static void addFieldsFromContext(Context context, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002032 addFieldsFromContext(context.getApplicationInfo(), context.getUserId(), notification);
2033 }
2034
2035 /**
2036 * @hide
2037 */
2038 public static void addFieldsFromContext(ApplicationInfo ai, int userId,
2039 Notification notification) {
2040 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
2041 notification.extras.putInt(EXTRA_ORIGINATING_USERID, userId);
Julia Reynoldsda303542015-11-23 14:00:20 -05002042 }
2043
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002044 @Override
2045 public String toString() {
2046 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002047 sb.append("Notification(pri=");
2048 sb.append(priority);
2049 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002050 if (contentView != null) {
2051 sb.append(contentView.getPackage());
2052 sb.append("/0x");
2053 sb.append(Integer.toHexString(contentView.getLayoutId()));
2054 } else {
2055 sb.append("null");
2056 }
2057 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002058 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2059 sb.append("default");
2060 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002061 int N = this.vibrate.length-1;
2062 sb.append("[");
2063 for (int i=0; i<N; i++) {
2064 sb.append(this.vibrate[i]);
2065 sb.append(',');
2066 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002067 if (N != -1) {
2068 sb.append(this.vibrate[N]);
2069 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002070 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002071 } else {
2072 sb.append("null");
2073 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002074 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002075 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002076 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002077 } else if (this.sound != null) {
2078 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002079 } else {
2080 sb.append("null");
2081 }
Chris Wren365b6d32015-07-16 10:39:26 -04002082 if (this.tickerText != null) {
2083 sb.append(" tick");
2084 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002085 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002086 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002087 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002088 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002089 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002090 if (this.category != null) {
2091 sb.append(" category=");
2092 sb.append(this.category);
2093 }
2094 if (this.mGroupKey != null) {
2095 sb.append(" groupKey=");
2096 sb.append(this.mGroupKey);
2097 }
2098 if (this.mSortKey != null) {
2099 sb.append(" sortKey=");
2100 sb.append(this.mSortKey);
2101 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002102 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002103 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002104 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002105 }
2106 sb.append(" vis=");
2107 sb.append(visibilityToString(this.visibility));
2108 if (this.publicVersion != null) {
2109 sb.append(" publicVersion=");
2110 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002111 }
2112 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 return sb.toString();
2114 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002115
Dan Sandler1b718782014-07-18 12:43:45 -04002116 /**
2117 * {@hide}
2118 */
2119 public static String visibilityToString(int vis) {
2120 switch (vis) {
2121 case VISIBILITY_PRIVATE:
2122 return "PRIVATE";
2123 case VISIBILITY_PUBLIC:
2124 return "PUBLIC";
2125 case VISIBILITY_SECRET:
2126 return "SECRET";
2127 default:
2128 return "UNKNOWN(" + String.valueOf(vis) + ")";
2129 }
2130 }
2131
Joe Onoratocb109a02011-01-18 17:57:41 -08002132 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002133 * {@hide}
2134 */
2135 public static String priorityToString(@Priority int pri) {
2136 switch (pri) {
2137 case PRIORITY_MIN:
2138 return "MIN";
2139 case PRIORITY_LOW:
2140 return "LOW";
2141 case PRIORITY_DEFAULT:
2142 return "DEFAULT";
2143 case PRIORITY_HIGH:
2144 return "HIGH";
2145 case PRIORITY_MAX:
2146 return "MAX";
2147 default:
2148 return "UNKNOWN(" + String.valueOf(pri) + ")";
2149 }
2150 }
2151
2152 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002153 * The small icon representing this notification in the status bar and content view.
2154 *
2155 * @return the small icon representing this notification.
2156 *
2157 * @see Builder#getSmallIcon()
2158 * @see Builder#setSmallIcon(Icon)
2159 */
2160 public Icon getSmallIcon() {
2161 return mSmallIcon;
2162 }
2163
2164 /**
2165 * Used when notifying to clean up legacy small icons.
2166 * @hide
2167 */
2168 public void setSmallIcon(Icon icon) {
2169 mSmallIcon = icon;
2170 }
2171
2172 /**
2173 * The large icon shown in this notification's content view.
2174 * @see Builder#getLargeIcon()
2175 * @see Builder#setLargeIcon(Icon)
2176 */
2177 public Icon getLargeIcon() {
2178 return mLargeIcon;
2179 }
2180
2181 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002182 * @hide
2183 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002184 public boolean isGroupSummary() {
2185 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2186 }
2187
2188 /**
2189 * @hide
2190 */
2191 public boolean isGroupChild() {
2192 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2193 }
2194
2195 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002196 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002197 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002198 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002199 * content views using the platform's notification layout template. If your app supports
2200 * versions of Android as old as API level 4, you can instead use
2201 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2202 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2203 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002204 *
Scott Main183bf112012-08-13 19:12:13 -07002205 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002206 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002207 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002208 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002209 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2210 * .setContentText(subject)
2211 * .setSmallIcon(R.drawable.new_mail)
2212 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002213 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002214 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002215 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002216 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002217 /**
2218 * @hide
2219 */
2220 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2221 "android.rebuild.contentViewActionCount";
2222 /**
2223 * @hide
2224 */
2225 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2226 = "android.rebuild.bigViewActionCount";
2227 /**
2228 * @hide
2229 */
2230 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2231 = "android.rebuild.hudViewActionCount";
2232
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002233 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002234
Joe Onorato46439ce2010-11-19 13:56:21 -08002235 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002236 private Notification mN;
2237 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002238 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002239 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2240 private ArrayList<String> mPersonList = new ArrayList<String>();
2241 private NotificationColorUtil mColorUtil;
2242 private boolean mColorUtilInited = false;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002243
2244 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002245 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2246 */
2247 private int mCachedContrastColor = COLOR_INVALID;
2248 private int mCachedContrastColorIsFor = COLOR_INVALID;
2249
2250 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002251 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002252 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002253
2254 * <table>
2255 * <tr><th align=right>priority</th>
2256 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2257 * <tr><th align=right>when</th>
2258 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2259 * <tr><th align=right>audio stream</th>
2260 * <td>{@link #STREAM_DEFAULT}</td></tr>
2261 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002262 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002263
2264 * @param context
2265 * A {@link Context} that will be used by the Builder to construct the
2266 * RemoteViews. The Context will not be held past the lifetime of this Builder
2267 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002268 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002269 public Builder(Context context) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002270 this(context, null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002271 }
2272
Joe Onoratocb109a02011-01-18 17:57:41 -08002273 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002274 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002275 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002276 public Builder(Context context, Notification toAdopt) {
2277 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002278
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002279 if (toAdopt == null) {
2280 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002281 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2282 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2283 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002284 mN.priority = PRIORITY_DEFAULT;
2285 mN.visibility = VISIBILITY_PRIVATE;
2286 } else {
2287 mN = toAdopt;
2288 if (mN.actions != null) {
2289 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002290 }
2291
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002292 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2293 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2294 }
2295
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002296 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2297 if (!TextUtils.isEmpty(templateClass)) {
2298 final Class<? extends Style> styleClass
2299 = getNotificationStyleClass(templateClass);
2300 if (styleClass == null) {
2301 Log.d(TAG, "Unknown style class: " + templateClass);
2302 } else {
2303 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002304 final Constructor<? extends Style> ctor =
2305 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002306 ctor.setAccessible(true);
2307 final Style style = ctor.newInstance();
2308 style.restoreFromExtras(mN.extras);
2309
2310 if (style != null) {
2311 setStyle(style);
2312 }
2313 } catch (Throwable t) {
2314 Log.e(TAG, "Could not create Style", t);
2315 }
2316 }
2317 }
2318
2319 }
2320 }
2321
2322 private NotificationColorUtil getColorUtil() {
2323 if (!mColorUtilInited) {
2324 mColorUtilInited = true;
2325 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
2326 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002327 }
2328 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002329 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002330 }
2331
2332 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002333 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002334 *
2335 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2336 * shown anymore by default and must be opted into by using
2337 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002338 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002339 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002340 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002341 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002342 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002343 return this;
2344 }
2345
Joe Onoratocb109a02011-01-18 17:57:41 -08002346 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002347 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002348 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002349 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2350 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002351 */
2352 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002353 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002354 return this;
2355 }
2356
2357 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002358 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002359 *
2360 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002361 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002362 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002363 * Useful when showing an elapsed time (like an ongoing phone call).
2364 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002365 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07002366 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08002367 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002368 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002369 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07002370 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002371 */
2372 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002373 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002374 return this;
2375 }
2376
2377 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002378 * Sets the Chronometer to count down instead of counting up.
2379 *
2380 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2381 * If it isn't set the chronometer will count up.
2382 *
2383 * @see #setUsesChronometer(boolean)
2384 */
Adrian Roos96b7e202016-05-17 13:50:38 -07002385 public Builder setChronometerCountDown(boolean countDown) {
2386 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08002387 return this;
2388 }
2389
2390 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002391 * Set the small icon resource, which will be used to represent the notification in the
2392 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002393 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002394
2395 * The platform template for the expanded view will draw this icon in the left, unless a
2396 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2397 * icon will be moved to the right-hand side.
2398 *
2399
2400 * @param icon
2401 * A resource ID in the application's package of the drawable to use.
2402 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002403 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002404 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002405 return setSmallIcon(icon != 0
2406 ? Icon.createWithResource(mContext, icon)
2407 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002408 }
2409
Joe Onoratocb109a02011-01-18 17:57:41 -08002410 /**
2411 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2412 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2413 * LevelListDrawable}.
2414 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002415 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002416 * @param level The level to use for the icon.
2417 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002418 * @see Notification#icon
2419 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002420 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002421 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002422 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002423 return setSmallIcon(icon);
2424 }
2425
2426 /**
2427 * Set the small icon, which will be used to represent the notification in the
2428 * status bar and content view (unless overriden there by a
2429 * {@link #setLargeIcon(Bitmap) large icon}).
2430 *
2431 * @param icon An Icon object to use.
2432 * @see Notification#icon
2433 */
2434 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002435 mN.setSmallIcon(icon);
2436 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2437 mN.icon = icon.getResId();
2438 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002439 return this;
2440 }
2441
Joe Onoratocb109a02011-01-18 17:57:41 -08002442 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002443 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002444 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002445 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002446 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002447 return this;
2448 }
2449
Joe Onoratocb109a02011-01-18 17:57:41 -08002450 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002451 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002452 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002453 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002454 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002455 return this;
2456 }
2457
Joe Onoratocb109a02011-01-18 17:57:41 -08002458 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002459 * This provides some additional information that is displayed in the notification. No
2460 * guarantees are given where exactly it is displayed.
2461 *
2462 * <p>This information should only be provided if it provides an essential
2463 * benefit to the understanding of the notification. The more text you provide the
2464 * less readable it becomes. For example, an email client should only provide the account
2465 * name here if more than one email account has been added.</p>
2466 *
2467 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
2468 * notification header area.
2469 *
2470 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
2471 * this will be shown in the third line of text in the platform notification template.
2472 * You should not be using {@link #setProgress(int, int, boolean)} at the
2473 * same time on those versions; they occupy the same place.
2474 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002475 */
2476 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002477 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002478 return this;
2479 }
2480
2481 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002482 * Set the remote input history.
2483 *
2484 * This should be set to the most recent inputs that have been sent
2485 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2486 * longer relevant (e.g. for chat notifications once the other party has responded).
2487 *
2488 * The most recent input must be stored at the 0 index, the second most recent at the
2489 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2490 * and how much of each individual input is shown.
2491 *
2492 * <p>Note: The reply text will only be shown on notifications that have least one action
2493 * with a {@code RemoteInput}.</p>
2494 */
2495 public Builder setRemoteInputHistory(CharSequence[] text) {
2496 if (text == null) {
2497 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2498 } else {
2499 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2500 CharSequence[] safe = new CharSequence[N];
2501 for (int i = 0; i < N; i++) {
2502 safe[i] = safeCharSequence(text[i]);
2503 }
2504 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2505 }
2506 return this;
2507 }
2508
2509 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002510 * Set the large number at the right-hand side of the notification. This is
2511 * equivalent to setContentInfo, although it might show the number in a different
2512 * font size for readability.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002513 *
2514 * @deprecated this number is not shown anywhere anymore
Joe Onoratocb109a02011-01-18 17:57:41 -08002515 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002516 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002517 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002518 return this;
2519 }
2520
Joe Onoratocb109a02011-01-18 17:57:41 -08002521 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002522 * A small piece of additional information pertaining to this notification.
2523 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002524 * The platform template will draw this on the last line of the notification, at the far
2525 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002526 *
2527 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
2528 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
2529 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08002530 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002531 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002532 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002533 return this;
2534 }
2535
Joe Onoratocb109a02011-01-18 17:57:41 -08002536 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002537 * Set the progress this notification represents.
2538 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002539 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002540 */
2541 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002542 mN.extras.putInt(EXTRA_PROGRESS, progress);
2543 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
2544 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002545 return this;
2546 }
2547
2548 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002549 * Supply a custom RemoteViews to use instead of the platform template.
2550 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002551 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08002552 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002553 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002554 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002555 return setCustomContentView(views);
2556 }
2557
2558 /**
2559 * Supply custom RemoteViews to use instead of the platform template.
2560 *
2561 * This will override the layout that would otherwise be constructed by this Builder
2562 * object.
2563 */
2564 public Builder setCustomContentView(RemoteViews contentView) {
2565 mN.contentView = contentView;
2566 return this;
2567 }
2568
2569 /**
2570 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
2571 *
2572 * This will override the expanded layout that would otherwise be constructed by this
2573 * Builder object.
2574 */
2575 public Builder setCustomBigContentView(RemoteViews contentView) {
2576 mN.bigContentView = contentView;
2577 return this;
2578 }
2579
2580 /**
2581 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
2582 *
2583 * This will override the heads-up layout that would otherwise be constructed by this
2584 * Builder object.
2585 */
2586 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
2587 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002588 return this;
2589 }
2590
Joe Onoratocb109a02011-01-18 17:57:41 -08002591 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002592 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2593 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002594 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2595 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2596 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002597 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002598 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002599 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002600 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002601 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002602 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002603 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002604 return this;
2605 }
2606
Joe Onoratocb109a02011-01-18 17:57:41 -08002607 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002608 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2609 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002610 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002611 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002612 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002613 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002614 return this;
2615 }
2616
Joe Onoratocb109a02011-01-18 17:57:41 -08002617 /**
2618 * An intent to launch instead of posting the notification to the status bar.
2619 * Only for use with extremely high-priority notifications demanding the user's
2620 * <strong>immediate</strong> attention, such as an incoming phone call or
2621 * alarm clock that the user has explicitly set to a particular time.
2622 * If this facility is used for something else, please give the user an option
2623 * to turn it off and use a normal notification, as this can be extremely
2624 * disruptive.
2625 *
Chris Wren47c20a12014-06-18 17:27:29 -04002626 * <p>
2627 * The system UI may choose to display a heads-up notification, instead of
2628 * launching this intent, while the user is using the device.
2629 * </p>
2630 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002631 * @param intent The pending intent to launch.
2632 * @param highPriority Passing true will cause this notification to be sent
2633 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002634 *
2635 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002636 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002637 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002638 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002639 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2640 return this;
2641 }
2642
Joe Onoratocb109a02011-01-18 17:57:41 -08002643 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002644 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002645 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002646 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002647 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002648 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002649 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002650 return this;
2651 }
2652
Joe Onoratocb109a02011-01-18 17:57:41 -08002653 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002654 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002655 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002656 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002657 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002658 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002659 setTicker(tickerText);
2660 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08002661 return this;
2662 }
2663
Joe Onoratocb109a02011-01-18 17:57:41 -08002664 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002665 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002666 *
2667 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002668 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2669 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002670 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002671 public Builder setLargeIcon(Bitmap b) {
2672 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2673 }
2674
2675 /**
2676 * Add a large icon to the notification content view.
2677 *
2678 * In the platform template, this image will be shown on the left of the notification view
2679 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2680 * badge atop the large icon).
2681 */
2682 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002683 mN.mLargeIcon = icon;
2684 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08002685 return this;
2686 }
2687
Joe Onoratocb109a02011-01-18 17:57:41 -08002688 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002689 * Set the sound to play.
2690 *
John Spurlockc0650f022014-07-19 13:22:39 -04002691 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2692 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002693 *
Chris Wren47c20a12014-06-18 17:27:29 -04002694 * <p>
2695 * A notification that is noisy is more likely to be presented as a heads-up notification.
2696 * </p>
2697 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002698 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002699 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002700 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002701 mN.sound = sound;
2702 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002703 return this;
2704 }
2705
Joe Onoratocb109a02011-01-18 17:57:41 -08002706 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002707 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002708 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002709 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2710 *
Chris Wren47c20a12014-06-18 17:27:29 -04002711 * <p>
2712 * A notification that is noisy is more likely to be presented as a heads-up notification.
2713 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002714 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002715 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002716 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002717 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002718 public Builder setSound(Uri sound, int streamType) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002719 mN.sound = sound;
2720 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08002721 return this;
2722 }
2723
Joe Onoratocb109a02011-01-18 17:57:41 -08002724 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002725 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2726 * use during playback.
2727 *
2728 * <p>
2729 * A notification that is noisy is more likely to be presented as a heads-up notification.
2730 * </p>
2731 *
2732 * @see Notification#sound
2733 */
2734 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002735 mN.sound = sound;
2736 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04002737 return this;
2738 }
2739
2740 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002741 * Set the vibration pattern to use.
2742 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002743 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2744 * <code>pattern</code> parameter.
2745 *
Chris Wren47c20a12014-06-18 17:27:29 -04002746 * <p>
2747 * A notification that vibrates is more likely to be presented as a heads-up notification.
2748 * </p>
2749 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002750 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002751 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002752 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002753 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08002754 return this;
2755 }
2756
Joe Onoratocb109a02011-01-18 17:57:41 -08002757 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002758 * Set the desired color for the indicator LED on the device, as well as the
2759 * blink duty cycle (specified in milliseconds).
2760 *
2761
2762 * Not all devices will honor all (or even any) of these values.
2763 *
2764
2765 * @see Notification#ledARGB
2766 * @see Notification#ledOnMS
2767 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002768 */
Tor Norbye80756e32015-03-02 09:39:27 -08002769 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002770 mN.ledARGB = argb;
2771 mN.ledOnMS = onMs;
2772 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002773 if (onMs != 0 || offMs != 0) {
2774 mN.flags |= FLAG_SHOW_LIGHTS;
2775 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002776 return this;
2777 }
2778
Joe Onoratocb109a02011-01-18 17:57:41 -08002779 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002780 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002781 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002782
2783 * Ongoing notifications cannot be dismissed by the user, so your application or service
2784 * must take care of canceling them.
2785 *
2786
2787 * They are typically used to indicate a background task that the user is actively engaged
2788 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2789 * (e.g., a file download, sync operation, active network connection).
2790 *
2791
2792 * @see Notification#FLAG_ONGOING_EVENT
2793 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002794 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002795 public Builder setOngoing(boolean ongoing) {
2796 setFlag(FLAG_ONGOING_EVENT, ongoing);
2797 return this;
2798 }
2799
Joe Onoratocb109a02011-01-18 17:57:41 -08002800 /**
2801 * Set this flag if you would only like the sound, vibrate
2802 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002803 *
2804 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002805 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002806 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2807 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2808 return this;
2809 }
2810
Joe Onoratocb109a02011-01-18 17:57:41 -08002811 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002812 * Make this notification automatically dismissed when the user touches it. The
2813 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2814 *
2815 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002816 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002817 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002818 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002819 return this;
2820 }
2821
Joe Onoratocb109a02011-01-18 17:57:41 -08002822 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002823 * Set whether or not this notification should not bridge to other devices.
2824 *
2825 * <p>Some notifications can be bridged to other devices for remote display.
2826 * This hint can be set to recommend this notification not be bridged.
2827 */
2828 public Builder setLocalOnly(boolean localOnly) {
2829 setFlag(FLAG_LOCAL_ONLY, localOnly);
2830 return this;
2831 }
2832
2833 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002834 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002835 * <p>
2836 * The value should be one or more of the following fields combined with
2837 * bitwise-or:
2838 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2839 * <p>
2840 * For all default values, use {@link #DEFAULT_ALL}.
2841 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002842 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002843 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08002844 return this;
2845 }
2846
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002847 /**
2848 * Set the priority of this notification.
2849 *
2850 * @see Notification#priority
2851 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002852 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002853 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002854 return this;
2855 }
Joe Malin8d40d042012-11-05 11:36:40 -08002856
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002857 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002858 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002859 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002860 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002861 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002862 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002863 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002864 return this;
2865 }
2866
2867 /**
Chris Wrendde75302014-03-26 17:24:15 -04002868 * Add a person that is relevant to this notification.
2869 *
Chris Wrene6c48932014-09-29 17:19:27 -04002870 * <P>
2871 * Depending on user preferences, this annotation may allow the notification to pass
2872 * through interruption filters, and to appear more prominently in the user interface.
2873 * </P>
2874 *
2875 * <P>
2876 * The person should be specified by the {@code String} representation of a
2877 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2878 * </P>
2879 *
2880 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2881 * URIs. The path part of these URIs must exist in the contacts database, in the
2882 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2883 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2884 * </P>
2885 *
2886 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002887 * @see Notification#EXTRA_PEOPLE
2888 */
Chris Wrene6c48932014-09-29 17:19:27 -04002889 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002890 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002891 return this;
2892 }
2893
2894 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002895 * Set this notification to be part of a group of notifications sharing the same key.
2896 * Grouped notifications may display in a cluster or stack on devices which
2897 * support such rendering.
2898 *
2899 * <p>To make this notification the summary for its group, also call
2900 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2901 * {@link #setSortKey}.
2902 * @param groupKey The group key of the group.
2903 * @return this object for method chaining
2904 */
2905 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002906 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002907 return this;
2908 }
2909
2910 /**
2911 * Set this notification to be the group summary for a group of notifications.
2912 * Grouped notifications may display in a cluster or stack on devices which
2913 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2914 * @param isGroupSummary Whether this notification should be a group summary.
2915 * @return this object for method chaining
2916 */
2917 public Builder setGroupSummary(boolean isGroupSummary) {
2918 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2919 return this;
2920 }
2921
2922 /**
2923 * Set a sort key that orders this notification among other notifications from the
2924 * same package. This can be useful if an external sort was already applied and an app
2925 * would like to preserve this. Notifications will be sorted lexicographically using this
2926 * value, although providing different priorities in addition to providing sort key may
2927 * cause this value to be ignored.
2928 *
2929 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002930 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002931 *
2932 * @see String#compareTo(String)
2933 */
2934 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002935 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002936 return this;
2937 }
2938
2939 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002940 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002941 *
Griff Hazen720042b2014-02-24 15:46:56 -08002942 * <p>Values within the Bundle will replace existing extras values in this Builder.
2943 *
2944 * @see Notification#extras
2945 */
Griff Hazen959591e2014-05-15 22:26:18 -07002946 public Builder addExtras(Bundle extras) {
2947 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002948 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08002949 }
2950 return this;
2951 }
2952
2953 /**
2954 * Set metadata for this notification.
2955 *
2956 * <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 -04002957 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002958 * called.
2959 *
Griff Hazen720042b2014-02-24 15:46:56 -08002960 * <p>Replaces any existing extras values with those from the provided Bundle.
2961 * Use {@link #addExtras} to merge in metadata instead.
2962 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002963 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002964 */
Griff Hazen959591e2014-05-15 22:26:18 -07002965 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002966 if (extras != null) {
2967 mUserExtras = extras;
2968 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002969 return this;
2970 }
2971
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002972 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002973 * Get the current metadata Bundle used by this notification Builder.
2974 *
2975 * <p>The returned Bundle is shared with this Builder.
2976 *
2977 * <p>The current contents of this Bundle are copied into the Notification each time
2978 * {@link #build()} is called.
2979 *
2980 * @see Notification#extras
2981 */
2982 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002983 return mUserExtras;
2984 }
2985
2986 private Bundle getAllExtras() {
2987 final Bundle saveExtras = (Bundle) mUserExtras.clone();
2988 saveExtras.putAll(mN.extras);
2989 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08002990 }
2991
2992 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002993 * Add an action to this notification. Actions are typically displayed by
2994 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002995 * <p>
2996 * Every action must have an icon (32dp square and matching the
2997 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2998 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2999 * <p>
3000 * A notification in its expanded form can display up to 3 actions, from left to right in
3001 * the order they were added. Actions will not be displayed when the notification is
3002 * collapsed, however, so be sure that any essential functions may be accessed by the user
3003 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003004 *
3005 * @param icon Resource ID of a drawable that represents the action.
3006 * @param title Text describing the action.
3007 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003008 *
3009 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003010 */
Dan Sandler86647982015-05-13 23:41:13 -04003011 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003012 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003013 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003014 return this;
3015 }
3016
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003017 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003018 * Add an action to this notification. Actions are typically displayed by
3019 * the system as a button adjacent to the notification content.
3020 * <p>
3021 * Every action must have an icon (32dp square and matching the
3022 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3023 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3024 * <p>
3025 * A notification in its expanded form can display up to 3 actions, from left to right in
3026 * the order they were added. Actions will not be displayed when the notification is
3027 * collapsed, however, so be sure that any essential functions may be accessed by the user
3028 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3029 *
3030 * @param action The action to add.
3031 */
3032 public Builder addAction(Action action) {
3033 mActions.add(action);
3034 return this;
3035 }
3036
3037 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003038 * Alter the complete list of actions attached to this notification.
3039 * @see #addAction(Action).
3040 *
3041 * @param actions
3042 * @return
3043 */
3044 public Builder setActions(Action... actions) {
3045 mActions.clear();
3046 for (int i = 0; i < actions.length; i++) {
3047 mActions.add(actions[i]);
3048 }
3049 return this;
3050 }
3051
3052 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003053 * Add a rich notification style to be applied at build time.
3054 *
3055 * @param style Object responsible for modifying the notification style.
3056 */
3057 public Builder setStyle(Style style) {
3058 if (mStyle != style) {
3059 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003060 if (mStyle != null) {
3061 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003062 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3063 } else {
3064 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003065 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003066 }
3067 return this;
3068 }
3069
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003070 /**
3071 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003072 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003073 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
3074 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
3075 *
3076 * @return The same Builder.
3077 */
3078 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003079 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003080 return this;
3081 }
3082
3083 /**
3084 * Supply a replacement Notification whose contents should be shown in insecure contexts
3085 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3086 * @param n A replacement notification, presumably with some or all info redacted.
3087 * @return The same Builder.
3088 */
3089 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003090 if (n != null) {
3091 mN.publicVersion = new Notification();
3092 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3093 } else {
3094 mN.publicVersion = null;
3095 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003096 return this;
3097 }
3098
Griff Hazenb720abe2014-05-20 13:15:30 -07003099 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003100 * Apply an extender to this notification builder. Extenders may be used to add
3101 * metadata or change options on this builder.
3102 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003103 public Builder extend(Extender extender) {
3104 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003105 return this;
3106 }
3107
Dan Sandler4e787062015-06-17 15:09:48 -04003108 /**
3109 * @hide
3110 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003111 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003112 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003113 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003114 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003115 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003116 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003117 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003118 }
3119
Dan Sandler26e81cf2014-05-06 10:01:27 -04003120 /**
3121 * Sets {@link Notification#color}.
3122 *
3123 * @param argb The accent color to use
3124 *
3125 * @return The same Builder.
3126 */
Tor Norbye80756e32015-03-02 09:39:27 -08003127 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003128 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003129 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003130 return this;
3131 }
3132
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003133 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04003134 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
3135 // This user can never be a badged profile,
3136 // and also includes USER_ALL system notifications.
3137 return null;
3138 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02003139 // Note: This assumes that the current user can read the profile badge of the
3140 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003141 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003142 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003143 }
3144
3145 private Bitmap getProfileBadge() {
3146 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003147 if (badge == null) {
3148 return null;
3149 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003150 final int size = mContext.getResources().getDimensionPixelSize(
3151 R.dimen.notification_badge_size);
3152 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003153 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003154 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003155 badge.draw(canvas);
3156 return bitmap;
3157 }
3158
Selim Cinekc848c3a2016-01-13 15:27:30 -08003159 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003160 Bitmap profileBadge = getProfileBadge();
3161
Kenny Guy98193ea2014-07-24 19:54:37 +01003162 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003163 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3164 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Kenny Guy98193ea2014-07-24 19:54:37 +01003165 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003166 }
3167
Christoph Studerfe718432014-09-01 18:21:18 +02003168 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003169 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003170 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003171 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003172 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003173 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003174 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003175 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003176 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003177 contentView.setTextViewText(R.id.text_line_1, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003178 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003179 }
3180
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003181 /**
3182 * Resets the notification header to its original state
3183 */
3184 private void resetNotificationHeader(RemoteViews contentView) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003185 contentView.setImageViewResource(R.id.icon, 0);
Selim Cinek7b836392015-12-04 20:02:59 -08003186 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003187 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003188 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003189 contentView.setViewVisibility(R.id.header_text, View.GONE);
3190 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003191 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003192 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003193 contentView.setImageViewIcon(R.id.profile_badge, null);
3194 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003195 }
3196
3197 private void resetContentMargins(RemoteViews contentView) {
3198 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003199 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003200 }
3201
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003202 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003203 return applyStandardTemplate(resId, true /* hasProgress */);
3204 }
3205
3206 /**
3207 * @param hasProgress whether the progress bar should be shown and set
3208 */
3209 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07003210 final Bundle ex = mN.extras;
3211
3212 CharSequence title = processLegacyText(ex.getCharSequence(EXTRA_TITLE));
3213 CharSequence text = processLegacyText(ex.getCharSequence(EXTRA_TEXT));
3214 return applyStandardTemplate(resId, hasProgress, title, text);
3215 }
3216
3217 /**
3218 * @param hasProgress whether the progress bar should be shown and set
3219 */
3220 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress,
3221 CharSequence title, CharSequence text) {
Kenny Guy77320062014-08-27 21:37:15 +01003222 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003223
Christoph Studerfe718432014-09-01 18:21:18 +02003224 resetStandardTemplate(contentView);
3225
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003226 final Bundle ex = mN.extras;
Dan Sandler190d58d2014-05-15 09:33:39 -04003227
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003228 bindNotificationHeader(contentView);
3229 bindLargeIcon(contentView);
Selim Cinek954cc232016-05-20 13:29:23 -07003230 boolean showProgress = handleProgressBar(hasProgress, contentView, ex);
Adrian Roosc1a80b02016-04-05 14:54:55 -07003231 if (title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003232 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Adrian Roosc1a80b02016-04-05 14:54:55 -07003233 contentView.setTextViewText(R.id.title, title);
Selim Cinek954cc232016-05-20 13:29:23 -07003234 contentView.setViewLayoutWidth(R.id.title, showProgress
3235 ? ViewGroup.LayoutParams.WRAP_CONTENT
3236 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08003237 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07003238 if (text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003239 int textId = showProgress ? com.android.internal.R.id.text_line_1
3240 : com.android.internal.R.id.text;
Adrian Roosc1a80b02016-04-05 14:54:55 -07003241 contentView.setTextViewText(textId, text);
Selim Cinek41598732016-01-11 16:58:37 -08003242 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003243 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003244
Selim Cinek860b6da2015-12-16 19:02:19 -08003245 setContentMinHeight(contentView, showProgress || mN.mLargeIcon != null);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003246
Selim Cinek29603462015-11-17 19:04:39 -08003247 return contentView;
3248 }
3249
Selim Cinek860b6da2015-12-16 19:02:19 -08003250 /**
3251 * @param remoteView the remote view to update the minheight in
3252 * @param hasMinHeight does it have a mimHeight
3253 * @hide
3254 */
3255 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3256 int minHeight = 0;
3257 if (hasMinHeight) {
3258 // we need to set the minHeight of the notification
3259 minHeight = mContext.getResources().getDimensionPixelSize(
3260 com.android.internal.R.dimen.notification_min_content_height);
3261 }
3262 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3263 }
3264
Selim Cinek29603462015-11-17 19:04:39 -08003265 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003266 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3267 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3268 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3269 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003270 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003271 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003272 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003273 contentView.setProgressBackgroundTintList(
3274 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3275 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003276 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003277 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003278 contentView.setProgressTintList(R.id.progress, colorStateList);
3279 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003280 }
Selim Cinek29603462015-11-17 19:04:39 -08003281 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003282 } else {
3283 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003284 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003285 }
Joe Onorato561d3852010-11-20 18:09:34 -08003286 }
3287
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003288 private void bindLargeIcon(RemoteViews contentView) {
3289 if (mN.mLargeIcon != null) {
3290 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3291 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3292 processLargeLegacyIcon(mN.mLargeIcon, contentView);
3293 int endMargin = mContext.getResources().getDimensionPixelSize(
3294 R.dimen.notification_content_picture_margin);
3295 contentView.setViewLayoutMarginEnd(R.id.line1, endMargin);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003296 contentView.setViewLayoutMarginEnd(R.id.text, endMargin);
Selim Cinek29603462015-11-17 19:04:39 -08003297 contentView.setViewLayoutMarginEnd(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003298 }
3299 }
3300
3301 private void bindNotificationHeader(RemoteViews contentView) {
3302 bindSmallIcon(contentView);
3303 bindHeaderAppName(contentView);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003304 bindHeaderText(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003305 bindHeaderChronometerAndTime(contentView);
3306 bindExpandButton(contentView);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003307 bindProfileBadge(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003308 }
3309
3310 private void bindExpandButton(RemoteViews contentView) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003311 contentView.setDrawableParameters(R.id.expand_button, false, -1, resolveContrastColor(),
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003312 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003313 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003314 resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003315 }
3316
3317 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3318 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003319 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003320 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3321 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3322 contentView.setLong(R.id.chronometer, "setBase",
3323 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3324 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07003325 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07003326 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003327 } else {
3328 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3329 contentView.setLong(R.id.time, "setTime", mN.when);
3330 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003331 } else {
3332 // We still want a time to be set but gone, such that we can show and hide it
3333 // on demand in case it's a child notification without anything in the header
3334 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003335 }
3336 }
3337
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003338 private void bindHeaderText(RemoteViews contentView) {
3339 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3340 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05003341 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003342 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05003343 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003344 if (headerText == null
3345 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3346 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3347 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
3348 }
3349 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003350 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003351 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
3352 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
3353 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003354 }
3355 }
3356
Adrian Rooseba05822016-04-22 17:09:27 -07003357 /**
3358 * @hide
3359 */
3360 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04003361 CharSequence name = null;
3362 final PackageManager pm = mContext.getPackageManager();
3363 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
3364 // only system packages which lump together a bunch of unrelated stuff
3365 // may substitute a different name to make the purpose of the
3366 // notification more clear. the correct package label should always
3367 // be accessible via SystemUI.
3368 final String pkg = mContext.getPackageName();
3369 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
3370 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
3371 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
3372 name = subName;
3373 } else {
3374 Log.w(TAG, "warning: pkg "
3375 + pkg + " attempting to substitute app name '" + subName
3376 + "' without holding perm "
3377 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
3378 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003379 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04003380 if (TextUtils.isEmpty(name)) {
3381 name = pm.getApplicationLabel(mContext.getApplicationInfo());
3382 }
3383 if (TextUtils.isEmpty(name)) {
3384 // still nothing?
3385 return null;
3386 }
3387
3388 return String.valueOf(name);
3389 }
3390 private void bindHeaderAppName(RemoteViews contentView) {
3391 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos4ff3b122016-02-01 12:26:13 -08003392 contentView.setTextColor(R.id.app_name_text, resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003393 }
3394
3395 private void bindSmallIcon(RemoteViews contentView) {
3396 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
3397 processSmallIconColor(mN.mSmallIcon, contentView);
3398 }
3399
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003400 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003401 * @return true if the built notification will show the time or the chronometer; false
3402 * otherwise
3403 */
3404 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07003405 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003406 }
3407
Christoph Studerfe718432014-09-01 18:21:18 +02003408 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003409 // actions_container is only reset when there are no actions to avoid focus issues with
3410 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02003411 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003412 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003413
3414 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3415 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3416
3417 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3418 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3419 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3420 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003421 }
3422
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003423 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos48d746a2016-04-12 14:57:28 -07003424 final Bundle ex = mN.extras;
3425
3426 CharSequence title = processLegacyText(ex.getCharSequence(EXTRA_TITLE));
3427 CharSequence text = processLegacyText(ex.getCharSequence(EXTRA_TEXT));
3428 return applyStandardTemplateWithActions(layoutId, true /* hasProgress */, title, text);
3429 }
3430
3431 private RemoteViews applyStandardTemplateWithActions(int layoutId, boolean hasProgress,
3432 CharSequence title, CharSequence text) {
3433 RemoteViews big = applyStandardTemplate(layoutId, hasProgress, title, text);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003434
Christoph Studerfe718432014-09-01 18:21:18 +02003435 resetStandardTemplateWithActions(big);
3436
Adrian Roose458aa82015-12-08 16:17:19 -08003437 boolean validRemoteInput = false;
3438
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003439 int N = mActions.size();
3440 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08003441 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003442 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003443 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003444 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003445 Action action = mActions.get(i);
3446 validRemoteInput |= hasValidRemoteInput(action);
3447
3448 final RemoteViews button = generateActionButton(action);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003449 big.addView(R.id.actions, button);
3450 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003451 } else {
3452 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003453 }
Adrian Roose458aa82015-12-08 16:17:19 -08003454
3455 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
3456 if (validRemoteInput && replyText != null
3457 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3458 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3459 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
3460
3461 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3462 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3463 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
3464
3465 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3466 big.setViewVisibility(
3467 R.id.notification_material_reply_text_3, View.VISIBLE);
3468 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
3469 }
3470 }
3471 }
3472
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003473 return big;
3474 }
3475
Adrian Roose458aa82015-12-08 16:17:19 -08003476 private boolean hasValidRemoteInput(Action action) {
3477 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
3478 // Weird actions
3479 return false;
3480 }
3481
3482 RemoteInput[] remoteInputs = action.getRemoteInputs();
3483 if (remoteInputs == null) {
3484 return false;
3485 }
3486
3487 for (RemoteInput r : remoteInputs) {
3488 CharSequence[] choices = r.getChoices();
3489 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
3490 return true;
3491 }
3492 }
3493 return false;
3494 }
3495
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003496 /**
3497 * Construct a RemoteViews for the final 1U notification layout. In order:
3498 * 1. Custom contentView from the caller
3499 * 2. Style's proposed content view
3500 * 3. Standard template view
3501 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003502 public RemoteViews createContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003503 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003504 return mN.contentView;
3505 } else if (mStyle != null) {
3506 final RemoteViews styleView = mStyle.makeContentView();
3507 if (styleView != null) {
3508 return styleView;
3509 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003510 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003511 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003512 }
3513
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003514 /**
3515 * Construct a RemoteViews for the final big notification layout.
3516 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003517 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05003518 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08003519 if (mN.bigContentView != null
3520 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003521 return mN.bigContentView;
3522 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05003523 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08003524 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003525 } else if (mActions.size() != 0) {
3526 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05003527 }
3528 adaptNotificationHeaderForBigContentView(result);
3529 return result;
3530 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003531
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003532 /**
3533 * Construct a RemoteViews for the final notification header only
3534 *
3535 * @hide
3536 */
3537 public RemoteViews makeNotificationHeader() {
3538 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
3539 R.layout.notification_template_header);
3540 resetNotificationHeader(header);
3541 bindNotificationHeader(header);
3542 return header;
3543 }
3544
Selim Cinek29603462015-11-17 19:04:39 -08003545 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003546 if (result != null) {
3547 result.setViewVisibility(R.id.text_line_1, View.GONE);
3548 }
Selim Cinek29603462015-11-17 19:04:39 -08003549 }
3550
Selim Cinek850a8542015-11-11 11:48:36 -05003551 private void adaptNotificationHeaderForBigContentView(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003552 if (result != null) {
3553 result.setBoolean(R.id.notification_header, "setExpanded", true);
3554 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003555 }
3556
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003557 /**
3558 * Construct a RemoteViews for the final heads-up notification layout.
3559 */
Julia Reynolds3b848122016-02-26 10:45:32 -05003560 public RemoteViews createHeadsUpContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003561 if (mN.headsUpContentView != null
3562 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003563 return mN.headsUpContentView;
3564 } else if (mStyle != null) {
3565 final RemoteViews styleView = mStyle.makeHeadsUpContentView();
3566 if (styleView != null) {
3567 return styleView;
3568 }
3569 } else if (mActions.size() == 0) {
3570 return null;
3571 }
3572
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003573 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003574 }
3575
Selim Cinek624c02db2015-12-14 21:00:02 -08003576 /**
3577 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
3578 *
3579 * @hide
3580 */
3581 public RemoteViews makePublicContentView() {
3582 if (mN.publicVersion != null) {
3583 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Julia Reynolds3b848122016-02-26 10:45:32 -05003584 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08003585 }
3586 Bundle savedBundle = mN.extras;
3587 Style style = mStyle;
3588 mStyle = null;
3589 Icon largeIcon = mN.mLargeIcon;
3590 mN.mLargeIcon = null;
3591 Bundle publicExtras = new Bundle();
3592 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
3593 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
3594 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
3595 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07003596 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
3597 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08003598 publicExtras.putCharSequence(EXTRA_TITLE,
3599 mContext.getString(R.string.notification_hidden_text));
3600 mN.extras = publicExtras;
3601 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
3602 mN.extras = savedBundle;
3603 mN.mLargeIcon = largeIcon;
3604 mStyle = style;
3605 return publicView;
3606 }
3607
3608
Chris Wren8fd39ec2014-02-27 17:43:26 -05003609
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003610 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003611 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07003612 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003613 tombstone ? getActionTombstoneLayoutResource()
3614 : getActionLayoutResource());
Dan Sandler68079d52015-07-22 10:45:30 -04003615 final Icon ai = action.getIcon();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003616 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04003617 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003618 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003619 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003620 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003621 if (action.mRemoteInputs != null) {
3622 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
3623 }
3624 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003625 button.setTextColor(R.id.action0, resolveContrastColor());
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003626 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003627 return button;
3628 }
3629
Joe Onoratocb109a02011-01-18 17:57:41 -08003630 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003631 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003632 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003633 */
3634 private boolean isLegacy() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003635 return getColorUtil() != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003636 }
3637
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003638 private CharSequence processLegacyText(CharSequence charSequence) {
3639 if (isLegacy()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003640 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003641 } else {
3642 return charSequence;
3643 }
3644 }
3645
Dan Sandler26e81cf2014-05-06 10:01:27 -04003646 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003647 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04003648 */
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003649 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView) {
Selim Cinekea4bef72015-12-02 15:51:10 -08003650 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
3651 if (colorable) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003652 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003653 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003654
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003655 }
Selim Cinekea4bef72015-12-02 15:51:10 -08003656 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos4ff3b122016-02-01 12:26:13 -08003657 colorable ? resolveContrastColor() : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003658 }
3659
Dan Sandler26e81cf2014-05-06 10:01:27 -04003660 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003661 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04003662 * if it's grayscale).
3663 */
3664 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003665 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3666 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003667 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003668 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08003669 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003670 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003671 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003672 }
3673
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003674 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003675 if (mN.color != COLOR_DEFAULT) {
3676 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02003677 }
Jorim Jaggi74419312014-06-10 20:57:21 +02003678 }
3679
Adrian Roos4ff3b122016-02-01 12:26:13 -08003680 int resolveContrastColor() {
3681 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
3682 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003683 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08003684 final int contrasted = NotificationColorUtil.resolveContrastColor(mContext, mN.color);
3685
3686 mCachedContrastColorIsFor = mN.color;
3687 return mCachedContrastColor = contrasted;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003688 }
3689
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003690 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003691 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003692 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003693 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003694 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003695 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003696 mN.actions = new Action[mActions.size()];
3697 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003698 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003699 if (!mPersonList.isEmpty()) {
3700 mN.extras.putStringArray(EXTRA_PEOPLE,
3701 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003702 }
Selim Cinek247fa012016-02-18 09:50:48 -08003703 if (mN.bigContentView != null || mN.contentView != null
3704 || mN.headsUpContentView != null) {
3705 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
3706 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003707 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08003708 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003709
Julia Reynolds3b848122016-02-26 10:45:32 -05003710 /**
3711 * Creates a Builder from an existing notification so further changes can be made.
3712 * @param context The context for your application / activity.
3713 * @param n The notification to create a Builder from.
3714 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003715 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003716 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003717 ApplicationInfo applicationInfo = n.extras.getParcelable(
3718 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003719 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05003720 if (applicationInfo != null) {
3721 try {
3722 builderContext = context.createApplicationContext(applicationInfo,
3723 Context.CONTEXT_RESTRICTED);
3724 } catch (NameNotFoundException e) {
3725 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
3726 builderContext = context; // try with our context
3727 }
3728 } else {
3729 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02003730 }
3731
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003732 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003733 }
3734
3735 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
Selim Cinek593610c2016-02-16 18:42:57 -08003736 Class<? extends Style>[] classes = new Class[] {
3737 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
Alex Hillsfc737de2016-03-23 17:33:02 -04003738 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
3739 MessagingStyle.class };
Christoph Studer4600f9b2014-07-22 22:44:43 +02003740 for (Class<? extends Style> innerClass : classes) {
3741 if (templateClass.equals(innerClass.getName())) {
3742 return innerClass;
3743 }
3744 }
3745 return null;
3746 }
3747
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003748 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003749 * @deprecated Use {@link #build()} instead.
3750 */
3751 @Deprecated
3752 public Notification getNotification() {
3753 return build();
3754 }
3755
3756 /**
3757 * Combine all of the options that have been set and return a new {@link Notification}
3758 * object.
3759 */
3760 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003761 // first, add any extras from the calling code
3762 if (mUserExtras != null) {
3763 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003764 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003765
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003766 mN.creationTime = System.currentTimeMillis();
3767
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003768 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05003769 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02003770
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003771 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003772
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003773 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003774 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003775 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003776
Adrian Roos5081c0d2016-02-26 16:04:19 -08003777 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3778 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003779 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003780 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003781 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
3782 mN.contentView.getSequenceNumber());
3783 }
3784 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003785 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003786 if (mN.bigContentView != null) {
3787 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
3788 mN.bigContentView.getSequenceNumber());
3789 }
3790 }
3791 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05003792 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003793 if (mN.headsUpContentView != null) {
3794 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
3795 mN.headsUpContentView.getSequenceNumber());
3796 }
3797 }
3798 }
3799
Julia Reynolds4c0c2022016-02-02 15:11:59 -05003800 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
3801 mN.flags |= FLAG_SHOW_LIGHTS;
3802 }
3803
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003804 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003805 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003806
3807 /**
3808 * Apply this Builder to an existing {@link Notification} object.
3809 *
3810 * @hide
3811 */
3812 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003813 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003814 return n;
3815 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003816
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003817 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08003818 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
3819 * change.
3820 *
3821 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
3822 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003823 * @hide
3824 */
Adrian Roos184bfe022016-03-03 13:41:44 -08003825 public static Notification maybeCloneStrippedForDelivery(Notification n) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003826 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08003827
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003828 // Only strip views for known Styles because we won't know how to
3829 // re-create them otherwise.
Adrian Roos184bfe022016-03-03 13:41:44 -08003830 if (!TextUtils.isEmpty(templateClass)
3831 && getNotificationStyleClass(templateClass) == null) {
3832 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003833 }
Adrian Roos184bfe022016-03-03 13:41:44 -08003834
3835 // Only strip unmodified BuilderRemoteViews.
3836 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003837 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003838 n.contentView.getSequenceNumber();
3839 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003840 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003841 n.bigContentView.getSequenceNumber();
3842 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003843 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08003844 n.headsUpContentView.getSequenceNumber();
3845
3846 // Nothing to do here, no need to clone.
3847 if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
3848 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003849 }
Adrian Roos184bfe022016-03-03 13:41:44 -08003850
3851 Notification clone = n.clone();
3852 if (stripContentView) {
3853 clone.contentView = null;
3854 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
3855 }
3856 if (stripBigContentView) {
3857 clone.bigContentView = null;
3858 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
3859 }
3860 if (stripHeadsUpContentView) {
3861 clone.headsUpContentView = null;
3862 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
3863 }
3864 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003865 }
3866
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003867 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003868 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003869 }
3870
3871 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003872 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003873 }
3874
3875 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003876 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003877 }
3878
3879 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003880 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003881 }
3882
3883 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003884 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003885 }
3886
Adrian Roosc1a80b02016-04-05 14:54:55 -07003887 private int getMessagingLayoutResource() {
3888 return R.layout.notification_template_material_messaging;
3889 }
3890
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003891 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003892 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003893 }
3894
3895 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003896 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003897 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003898 }
3899
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003900 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07003901 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003902 * @hide
3903 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07003904 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003905 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
3906 }
3907
3908 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07003909 * @return true if the notification will show a chronometer; false otherwise
3910 * @hide
3911 */
3912 public boolean showsChronometer() {
3913 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
3914 }
3915
3916 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003917 * An object that can apply a rich notification style to a {@link Notification.Builder}
3918 * object.
3919 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003920 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003921 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003922
3923 /**
3924 * @hide
3925 */
3926 protected CharSequence mSummaryText = null;
3927
3928 /**
3929 * @hide
3930 */
3931 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003932
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003933 protected Builder mBuilder;
3934
Chris Wrend6297db2012-05-03 16:20:13 -04003935 /**
3936 * Overrides ContentTitle in the big form of the template.
3937 * This defaults to the value passed to setContentTitle().
3938 */
3939 protected void internalSetBigContentTitle(CharSequence title) {
3940 mBigContentTitle = title;
3941 }
3942
3943 /**
3944 * Set the first line of text after the detail section in the big form of the template.
3945 */
3946 protected void internalSetSummaryText(CharSequence cs) {
3947 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003948 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003949 }
3950
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003951 public void setBuilder(Builder builder) {
3952 if (mBuilder != builder) {
3953 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003954 if (mBuilder != null) {
3955 mBuilder.setStyle(this);
3956 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003957 }
3958 }
3959
Chris Wrend6297db2012-05-03 16:20:13 -04003960 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003961 if (mBuilder == null) {
3962 throw new IllegalArgumentException("Style requires a valid Builder object");
3963 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003964 }
Chris Wrend6297db2012-05-03 16:20:13 -04003965
3966 protected RemoteViews getStandardView(int layoutId) {
3967 checkBuilder();
3968
Christoph Studer4600f9b2014-07-22 22:44:43 +02003969 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003970 CharSequence oldBuilderContentTitle =
3971 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003972 if (mBigContentTitle != null) {
3973 mBuilder.setContentTitle(mBigContentTitle);
3974 }
3975
Chris Wrend6297db2012-05-03 16:20:13 -04003976 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3977
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003978 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003979
Chris Wrend6297db2012-05-03 16:20:13 -04003980 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3981 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003982 } else {
3983 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003984 }
3985
Chris Wrend6297db2012-05-03 16:20:13 -04003986 return contentView;
3987 }
3988
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003989 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003990 * Construct a Style-specific RemoteViews for the final 1U notification layout.
3991 * The default implementation has nothing additional to add.
3992 * @hide
3993 */
3994 public RemoteViews makeContentView() {
3995 return null;
3996 }
3997
3998 /**
3999 * Construct a Style-specific RemoteViews for the final big notification layout.
4000 * @hide
4001 */
4002 public RemoteViews makeBigContentView() {
4003 return null;
4004 }
4005
4006 /**
4007 * Construct a Style-specific RemoteViews for the final HUN layout.
4008 * @hide
4009 */
4010 public RemoteViews makeHeadsUpContentView() {
4011 return null;
4012 }
4013
4014 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004015 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004016 * @hide
4017 */
4018 public void addExtras(Bundle extras) {
4019 if (mSummaryTextSet) {
4020 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
4021 }
4022 if (mBigContentTitle != null) {
4023 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
4024 }
Chris Wren91ad5632013-06-05 15:05:57 -04004025 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004026 }
4027
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004028 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004029 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004030 * @hide
4031 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02004032 protected void restoreFromExtras(Bundle extras) {
4033 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
4034 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
4035 mSummaryTextSet = true;
4036 }
4037 if (extras.containsKey(EXTRA_TITLE_BIG)) {
4038 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
4039 }
4040 }
4041
4042
4043 /**
4044 * @hide
4045 */
4046 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004047 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004048 return wip;
4049 }
4050
Daniel Sandler0ec46202015-06-24 01:27:05 -04004051 /**
4052 * @hide
4053 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004054 public void purgeResources() {}
4055
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004056 /**
4057 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
4058 * attached to.
4059 *
4060 * @return the fully constructed Notification.
4061 */
4062 public Notification build() {
4063 checkBuilder();
4064 return mBuilder.build();
4065 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004066
4067 /**
4068 * @hide
4069 * @return true if the style positions the progress bar on the second line; false if the
4070 * style hides the progress bar
4071 */
4072 protected boolean hasProgress() {
4073 return true;
4074 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004075
4076 /**
4077 * @hide
4078 * @return Whether we should put the summary be put into the notification header
4079 */
4080 public boolean hasSummaryInHeader() {
4081 return true;
4082 }
Selim Cinek593610c2016-02-16 18:42:57 -08004083
4084 /**
4085 * @hide
4086 * @return Whether custom content views are displayed inline in the style
4087 */
4088 public boolean displayCustomViewInline() {
4089 return false;
4090 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004091 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004092
4093 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004094 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08004095 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004096 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004097 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004098 * Notification notif = new Notification.Builder(mContext)
4099 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
4100 * .setContentText(subject)
4101 * .setSmallIcon(R.drawable.new_post)
4102 * .setLargeIcon(aBitmap)
4103 * .setStyle(new Notification.BigPictureStyle()
4104 * .bigPicture(aBigBitmap))
4105 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004106 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004107 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004108 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004109 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004110 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004111 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004112 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004113 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004114
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004115 public BigPictureStyle() {
4116 }
4117
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004118 /**
4119 * @deprecated use {@code BigPictureStyle()}.
4120 */
4121 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004122 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004123 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004124 }
4125
Chris Wrend6297db2012-05-03 16:20:13 -04004126 /**
4127 * Overrides ContentTitle in the big form of the template.
4128 * This defaults to the value passed to setContentTitle().
4129 */
4130 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004131 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004132 return this;
4133 }
4134
4135 /**
4136 * Set the first line of text after the detail section in the big form of the template.
4137 */
4138 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004139 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004140 return this;
4141 }
4142
Chris Wren0bd664d2012-08-01 13:56:56 -04004143 /**
4144 * Provide the bitmap to be used as the payload for the BigPicture notification.
4145 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004146 public BigPictureStyle bigPicture(Bitmap b) {
4147 mPicture = b;
4148 return this;
4149 }
4150
Chris Wren3745a3d2012-05-22 15:11:52 -04004151 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04004152 * Override the large icon when the big notification is shown.
4153 */
4154 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04004155 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
4156 }
4157
4158 /**
4159 * Override the large icon when the big notification is shown.
4160 */
4161 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04004162 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04004163 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04004164 return this;
4165 }
4166
Riley Andrews0394a0c2015-11-03 23:36:52 -08004167 /** @hide */
4168 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
4169
Daniel Sandler0ec46202015-06-24 01:27:05 -04004170 /**
4171 * @hide
4172 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004173 @Override
4174 public void purgeResources() {
4175 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08004176 if (mPicture != null &&
4177 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08004178 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004179 mPicture = mPicture.createAshmemBitmap();
4180 }
4181 if (mBigLargeIcon != null) {
4182 mBigLargeIcon.convertToAshmem();
4183 }
4184 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01004185
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004186 /**
4187 * @hide
4188 */
4189 public RemoteViews makeBigContentView() {
4190 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01004191 // This covers the following cases:
4192 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004193 // mN.mLargeIcon
4194 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04004195 Icon oldLargeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004196 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004197 oldLargeIcon = mBuilder.mN.mLargeIcon;
4198 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004199 }
4200
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004201 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05004202 if (mSummaryTextSet) {
4203 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinekc848c3a2016-01-13 15:27:30 -08004204 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05004205 }
Selim Cinek860b6da2015-12-16 19:02:19 -08004206 mBuilder.setContentMinHeight(contentView, mBuilder.mN.mLargeIcon != null);
Selim Cinek53e64a42015-11-16 10:40:56 -08004207
Christoph Studer5c510ee2014-12-15 16:32:27 +01004208 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004209 mBuilder.mN.mLargeIcon = oldLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01004210 }
4211
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004212 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004213 return contentView;
4214 }
4215
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004216 /**
4217 * @hide
4218 */
4219 public void addExtras(Bundle extras) {
4220 super.addExtras(extras);
4221
4222 if (mBigLargeIconSet) {
4223 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
4224 }
4225 extras.putParcelable(EXTRA_PICTURE, mPicture);
4226 }
4227
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004228 /**
4229 * @hide
4230 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004231 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004232 protected void restoreFromExtras(Bundle extras) {
4233 super.restoreFromExtras(extras);
4234
4235 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01004236 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02004237 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04004238 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02004239 mPicture = extras.getParcelable(EXTRA_PICTURE);
4240 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004241
4242 /**
4243 * @hide
4244 */
4245 @Override
4246 public boolean hasSummaryInHeader() {
4247 return false;
4248 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004249 }
4250
4251 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004252 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08004253 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004254 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004255 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004256 * Notification notif = new Notification.Builder(mContext)
4257 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4258 * .setContentText(subject)
4259 * .setSmallIcon(R.drawable.new_mail)
4260 * .setLargeIcon(aBitmap)
4261 * .setStyle(new Notification.BigTextStyle()
4262 * .bigText(aVeryLongString))
4263 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004264 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004265 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004266 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004267 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004268 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004269
4270 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004271 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004272
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004273 private CharSequence mBigText;
4274
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004275 public BigTextStyle() {
4276 }
4277
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004278 /**
4279 * @deprecated use {@code BigTextStyle()}.
4280 */
4281 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004282 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004283 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004284 }
4285
Chris Wrend6297db2012-05-03 16:20:13 -04004286 /**
4287 * Overrides ContentTitle in the big form of the template.
4288 * This defaults to the value passed to setContentTitle().
4289 */
4290 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004291 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004292 return this;
4293 }
4294
4295 /**
4296 * Set the first line of text after the detail section in the big form of the template.
4297 */
4298 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004299 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004300 return this;
4301 }
4302
Chris Wren0bd664d2012-08-01 13:56:56 -04004303 /**
4304 * Provide the longer text to be displayed in the big form of the
4305 * template in place of the content text.
4306 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004307 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004308 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004309 return this;
4310 }
4311
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004312 /**
4313 * @hide
4314 */
4315 public void addExtras(Bundle extras) {
4316 super.addExtras(extras);
4317
Christoph Studer4600f9b2014-07-22 22:44:43 +02004318 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4319 }
4320
4321 /**
4322 * @hide
4323 */
4324 @Override
4325 protected void restoreFromExtras(Bundle extras) {
4326 super.restoreFromExtras(extras);
4327
4328 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004329 }
4330
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004331 /**
4332 * @hide
4333 */
4334 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004335
4336 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07004337 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004338 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004339
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004340 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004341
Selim Cinek75998782016-04-26 10:39:17 -07004342 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004343
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004344 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07004345 if (TextUtils.isEmpty(bigTextText)) {
4346 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
4347 // experience
4348 bigTextText = mBuilder.processLegacyText(text);
4349 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07004350 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08004351
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004352 return contentView;
4353 }
4354
Adrian Roosb1f427c2016-05-26 12:27:15 -07004355 static void applyBigTextContentView(Builder builder,
4356 RemoteViews contentView, CharSequence bigTextText) {
4357 contentView.setTextViewText(R.id.big_text, bigTextText);
4358 contentView.setViewVisibility(R.id.big_text,
4359 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
4360 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines(builder));
4361 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.mLargeIcon != null);
4362 }
4363
4364 private static int calculateMaxLines(Builder builder) {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004365 int lineCount = MAX_LINES;
Adrian Roosb1f427c2016-05-26 12:27:15 -07004366 boolean hasActions = builder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004367 if (hasActions) {
4368 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4369 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004370 return lineCount;
4371 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004372 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004373
4374 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004375 * Helper class for generating large-format notifications that include multiple back-and-forth
4376 * messages of varying types between any number of people.
4377 *
4378 * <br>
4379 * If the platform does not provide large-format notifications, this method has no effect. The
4380 * user will always see the normal notification view.
4381 * <br>
4382 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
4383 * so:
4384 * <pre class="prettyprint">
4385 *
4386 * Notification noti = new Notification.Builder()
4387 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
4388 * .setContentText(subject)
4389 * .setSmallIcon(R.drawable.new_message)
4390 * .setLargeIcon(aBitmap)
4391 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
4392 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
4393 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
4394 * .build();
4395 * </pre>
4396 */
4397 public static class MessagingStyle extends Style {
4398
4399 /**
4400 * The maximum number of messages that will be retained in the Notification itself (the
4401 * number displayed is up to the platform).
4402 */
4403 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
4404
4405 CharSequence mUserDisplayName;
4406 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04004407 List<Message> mMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04004408
4409 MessagingStyle() {
4410 }
4411
4412 /**
4413 * @param userDisplayName the name to be displayed for any replies sent by the user before the
4414 * posting app reposts the notification with those messages after they've been actually
4415 * sent and in previous messages sent by the user added in
4416 * {@link #addMessage(Notification.MessagingStyle.Message)}
4417 */
4418 public MessagingStyle(CharSequence userDisplayName) {
4419 mUserDisplayName = userDisplayName;
4420 }
4421
4422 /**
4423 * Returns the name to be displayed for any replies sent by the user
4424 */
4425 public CharSequence getUserDisplayName() {
4426 return mUserDisplayName;
4427 }
4428
4429 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04004430 * Sets the title to be displayed on this conversation. This should only be used for
4431 * group messaging and left unset for one-on-one conversations.
4432 * @param conversationTitle
4433 * @return this object for method chaining.
4434 */
4435 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
4436 mConversationTitle = conversationTitle;
4437 return this;
4438 }
4439
4440 /**
4441 * Return the title to be displayed on this conversation. Can be <code>null</code> and
4442 * should be for one-on-one conversations
4443 */
4444 public CharSequence getConversationTitle() {
4445 return mConversationTitle;
4446 }
4447
4448 /**
4449 * Adds a message for display by this notification. Convenience call for a simple
4450 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
4451 * @param text A {@link CharSequence} to be displayed as the message content
4452 * @param timestamp Time at which the message arrived
4453 * @param sender A {@link CharSequence} to be used for displaying the name of the
4454 * sender. Should be <code>null</code> for messages by the current user, in which case
4455 * the platform will insert {@link #getUserDisplayName()}.
4456 * Should be unique amongst all individuals in the conversation, and should be
4457 * consistent during re-posts of the notification.
4458 *
4459 * @see Message#Message(CharSequence, long, CharSequence)
4460 *
4461 * @return this object for method chaining
4462 */
4463 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
4464 mMessages.add(new Message(text, timestamp, sender));
4465 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4466 mMessages.remove(0);
4467 }
4468 return this;
4469 }
4470
4471 /**
4472 * Adds a {@link Message} for display in this notification.
4473 * @param message The {@link Message} to be displayed
4474 * @return this object for method chaining
4475 */
4476 public MessagingStyle addMessage(Message message) {
4477 mMessages.add(message);
4478 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
4479 mMessages.remove(0);
4480 }
4481 return this;
4482 }
4483
4484 /**
4485 * Gets the list of {@code Message} objects that represent the notification
4486 */
4487 public List<Message> getMessages() {
4488 return mMessages;
4489 }
4490
4491 /**
4492 * @hide
4493 */
4494 @Override
4495 public void addExtras(Bundle extras) {
4496 super.addExtras(extras);
4497 if (mUserDisplayName != null) {
4498 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
4499 }
4500 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004501 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04004502 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004503 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
4504 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04004505 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07004506
4507 fixTitleAndTextExtras(extras);
4508 }
4509
4510 private void fixTitleAndTextExtras(Bundle extras) {
4511 Message m = findLatestIncomingMessage();
4512 CharSequence text = (m == null) ? null : m.mText;
4513 CharSequence sender = m == null ? null
4514 : TextUtils.isEmpty(m.mSender) ? mUserDisplayName : m.mSender;
4515 CharSequence title;
4516 if (!TextUtils.isEmpty(mConversationTitle)) {
4517 if (!TextUtils.isEmpty(sender)) {
4518 BidiFormatter bidi = BidiFormatter.getInstance();
4519 title = mBuilder.mContext.getString(
4520 com.android.internal.R.string.notification_messaging_title_template,
4521 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(m.mSender));
4522 } else {
4523 title = mConversationTitle;
4524 }
4525 } else {
4526 title = sender;
4527 }
4528
4529 if (title != null) {
4530 extras.putCharSequence(EXTRA_TITLE, title);
4531 }
4532 if (text != null) {
4533 extras.putCharSequence(EXTRA_TEXT, text);
4534 }
Alex Hillsfc737de2016-03-23 17:33:02 -04004535 }
4536
4537 /**
4538 * @hide
4539 */
4540 @Override
4541 protected void restoreFromExtras(Bundle extras) {
4542 super.restoreFromExtras(extras);
4543
4544 mMessages.clear();
Adrian Roos96b7e202016-05-17 13:50:38 -07004545 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
4546 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Alex Hillsd9b04d92016-04-11 16:38:16 -04004547 Parcelable[] parcelables = extras.getParcelableArray(EXTRA_MESSAGES);
Adrian Roosdedd1df2016-04-26 16:38:47 -07004548 if (parcelables != null && parcelables instanceof Parcelable[]) {
4549 mMessages = Message.getMessagesFromBundleArray(parcelables);
Alex Hillsfc737de2016-03-23 17:33:02 -04004550 }
4551 }
4552
4553 /**
4554 * @hide
4555 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07004556 @Override
4557 public RemoteViews makeContentView() {
4558 Message m = findLatestIncomingMessage();
4559 CharSequence title = mConversationTitle != null
4560 ? mConversationTitle
4561 : (m == null) ? null : m.mSender;
4562 CharSequence text = (m == null)
4563 ? null
4564 : mConversationTitle != null ? makeMessageLine(m) : m.mText;
Alex Hillsfc737de2016-03-23 17:33:02 -04004565
Adrian Roosc1a80b02016-04-05 14:54:55 -07004566 return mBuilder.applyStandardTemplate(mBuilder.getBaseLayoutResource(),
4567 false /* hasProgress */,
4568 title,
4569 text);
4570 }
4571
4572 private Message findLatestIncomingMessage() {
4573 for (int i = mMessages.size() - 1; i >= 0; i--) {
4574 Message m = mMessages.get(i);
4575 // Incoming messages have a non-empty sender.
4576 if (!TextUtils.isEmpty(m.mSender)) {
4577 return m;
4578 }
4579 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07004580 if (!mMessages.isEmpty()) {
4581 // No incoming messages, fall back to outgoing message
4582 return mMessages.get(mMessages.size() - 1);
4583 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07004584 return null;
4585 }
4586
4587 /**
4588 * @hide
4589 */
4590 @Override
4591 public RemoteViews makeBigContentView() {
4592 CharSequence title = !TextUtils.isEmpty(super.mBigContentTitle)
4593 ? super.mBigContentTitle
4594 : mConversationTitle;
4595 boolean hasTitle = !TextUtils.isEmpty(title);
4596
Adrian Roosb1f427c2016-05-26 12:27:15 -07004597 if (!hasTitle && mMessages.size() == 1) {
4598 CharSequence sender = mMessages.get(0).mSender;
4599 CharSequence text = mMessages.get(0).mText;
4600 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
4601 mBuilder.getBigTextLayoutResource(),
4602 false /* progress */, sender, null /* text */);
4603 BigTextStyle.applyBigTextContentView(mBuilder, contentView, text);
4604 return contentView;
4605 }
4606
Adrian Roos48d746a2016-04-12 14:57:28 -07004607 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07004608 mBuilder.getMessagingLayoutResource(),
4609 false /* hasProgress */,
4610 title,
4611 null /* text */);
4612
4613 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
4614 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
4615
4616 // Make sure all rows are gone in case we reuse a view.
4617 for (int rowId : rowIds) {
4618 contentView.setViewVisibility(rowId, View.GONE);
4619 }
4620
4621 int i=0;
4622 int titlePadding = mBuilder.mContext.getResources().getDimensionPixelSize(
4623 R.dimen.notification_messaging_spacing);
4624 contentView.setViewLayoutMarginBottom(R.id.line1, hasTitle ? titlePadding : 0);
4625 contentView.setInt(R.id.notification_messaging, "setNumIndentLines",
4626 mBuilder.mN.mLargeIcon == null ? 0 : (hasTitle ? 1 : 2));
4627
4628 int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
4629 while (firstMessage + i < mMessages.size() && i < rowIds.length) {
4630 Message m = mMessages.get(firstMessage + i);
4631 int rowId = rowIds[i];
4632
4633 contentView.setViewVisibility(rowId, View.VISIBLE);
4634 contentView.setTextViewText(rowId, makeMessageLine(m));
4635
4636 i++;
4637 }
Alex Hillsfc737de2016-03-23 17:33:02 -04004638 return contentView;
4639 }
4640
Adrian Roosc1a80b02016-04-05 14:54:55 -07004641 private CharSequence makeMessageLine(Message m) {
4642 BidiFormatter bidi = BidiFormatter.getInstance();
4643 SpannableStringBuilder sb = new SpannableStringBuilder();
4644 if (TextUtils.isEmpty(m.mSender)) {
4645 CharSequence replyName = mUserDisplayName == null ? "" : mUserDisplayName;
4646 sb.append(bidi.unicodeWrap(replyName),
4647 makeFontColorSpan(mBuilder.resolveContrastColor()),
4648 0 /* flags */);
4649 } else {
4650 sb.append(bidi.unicodeWrap(m.mSender),
4651 makeFontColorSpan(Color.BLACK),
4652 0 /* flags */);
4653 }
4654 CharSequence text = m.mText == null ? "" : m.mText;
4655 sb.append(" ").append(bidi.unicodeWrap(text));
4656 return sb;
4657 }
4658
Adrian Roosdedd1df2016-04-26 16:38:47 -07004659 /**
4660 * @hide
4661 */
4662 @Override
4663 public RemoteViews makeHeadsUpContentView() {
4664 Message m = findLatestIncomingMessage();
4665 CharSequence title = mConversationTitle != null
4666 ? mConversationTitle
4667 : (m == null) ? null : m.mSender;
4668 CharSequence text = (m == null)
4669 ? null
4670 : mConversationTitle != null ? makeMessageLine(m) : m.mText;
4671
4672 return mBuilder.applyStandardTemplateWithActions(mBuilder.getBigBaseLayoutResource(),
4673 false /* hasProgress */,
4674 title,
4675 text);
4676 }
4677
Adrian Roosc1a80b02016-04-05 14:54:55 -07004678 private static TextAppearanceSpan makeFontColorSpan(int color) {
4679 return new TextAppearanceSpan(null, 0, 0,
4680 ColorStateList.valueOf(color), null);
4681 }
4682
Alex Hillsd9b04d92016-04-11 16:38:16 -04004683 public static final class Message {
4684
4685 static final String KEY_TEXT = "text";
4686 static final String KEY_TIMESTAMP = "time";
4687 static final String KEY_SENDER = "sender";
4688 static final String KEY_DATA_MIME_TYPE = "type";
4689 static final String KEY_DATA_URI= "uri";
Alex Hillsfc737de2016-03-23 17:33:02 -04004690
4691 private final CharSequence mText;
4692 private final long mTimestamp;
4693 private final CharSequence mSender;
4694
4695 private String mDataMimeType;
4696 private Uri mDataUri;
4697
4698 /**
4699 * Constructor
4700 * @param text A {@link CharSequence} to be displayed as the message content
4701 * @param timestamp Time at which the message arrived
4702 * @param sender A {@link CharSequence} to be used for displaying the name of the
4703 * sender. Should be <code>null</code> for messages by the current user, in which case
4704 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
4705 * Should be unique amongst all individuals in the conversation, and should be
4706 * consistent during re-posts of the notification.
4707 */
4708 public Message(CharSequence text, long timestamp, CharSequence sender){
4709 mText = text;
4710 mTimestamp = timestamp;
4711 mSender = sender;
4712 }
4713
4714 /**
4715 * Sets a binary blob of data and an associated MIME type for a message. In the case
4716 * where the platform doesn't support the MIME type, the original text provided in the
4717 * constructor will be used.
4718 * @param dataMimeType The MIME type of the content. See
4719 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
4720 * types on Android and Android Wear.
4721 * @param dataUri The uri containing the content whose type is given by the MIME type.
4722 * <p class="note">
4723 * <ol>
4724 * <li>Notification Listeners including the System UI need permission to access the
4725 * data the Uri points to. The recommended ways to do this are:</li>
4726 * <li>Store the data in your own ContentProvider, making sure that other apps have
4727 * the correct permission to access your provider. The preferred mechanism for
4728 * providing access is to use per-URI permissions which are temporary and only
4729 * grant access to the receiving application. An easy way to create a
4730 * ContentProvider like this is to use the FileProvider helper class.</li>
4731 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
4732 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
4733 * also store non-media types (see MediaStore.Files for more info). Files can be
4734 * inserted into the MediaStore using scanFile() after which a content:// style
4735 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
4736 * Note that once added to the system MediaStore the content is accessible to any
4737 * app on the device.</li>
4738 * </ol>
4739 * @return this object for method chaining
4740 */
4741 public Message setData(String dataMimeType, Uri dataUri) {
4742 mDataMimeType = dataMimeType;
4743 mDataUri = dataUri;
4744 return this;
4745 }
4746
Alex Hillsfc737de2016-03-23 17:33:02 -04004747 /**
4748 * Get the text to be used for this message, or the fallback text if a type and content
4749 * Uri have been set
4750 */
4751 public CharSequence getText() {
4752 return mText;
4753 }
4754
4755 /**
4756 * Get the time at which this message arrived
4757 */
4758 public long getTimestamp() {
4759 return mTimestamp;
4760 }
4761
4762 /**
4763 * Get the text used to display the contact's name in the messaging experience
4764 */
4765 public CharSequence getSender() {
4766 return mSender;
4767 }
4768
4769 /**
4770 * Get the MIME type of the data pointed to by the Uri
4771 */
4772 public String getDataMimeType() {
4773 return mDataMimeType;
4774 }
4775
4776 /**
4777 * Get the the Uri pointing to the content of the message. Can be null, in which case
4778 * {@see #getText()} is used.
4779 */
4780 public Uri getDataUri() {
4781 return mDataUri;
4782 }
4783
Alex Hillsd9b04d92016-04-11 16:38:16 -04004784 private Bundle toBundle() {
4785 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04004786 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004787 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04004788 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004789 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04004790 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004791 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04004792 }
4793 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004794 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04004795 }
4796 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004797 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04004798 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004799 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04004800 }
4801
Alex Hillsd9b04d92016-04-11 16:38:16 -04004802 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
4803 Bundle[] bundles = new Bundle[messages.size()];
4804 final int N = messages.size();
4805 for (int i = 0; i < N; i++) {
4806 bundles[i] = messages.get(i).toBundle();
4807 }
4808 return bundles;
4809 }
4810
Adrian Roosdedd1df2016-04-26 16:38:47 -07004811 static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004812 List<Message> messages = new ArrayList<>(bundles.length);
4813 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07004814 if (bundles[i] instanceof Bundle) {
4815 Message message = getMessageFromBundle((Bundle)bundles[i]);
4816 if (message != null) {
4817 messages.add(message);
4818 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004819 }
4820 }
4821 return messages;
4822 }
4823
4824 static Message getMessageFromBundle(Bundle bundle) {
4825 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07004826 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04004827 return null;
4828 } else {
4829 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
4830 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
4831 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
4832 bundle.containsKey(KEY_DATA_URI)) {
4833
4834 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
4835 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04004836 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04004837 return message;
4838 }
4839 } catch (ClassCastException e) {
4840 return null;
4841 }
4842 }
Alex Hillsfc737de2016-03-23 17:33:02 -04004843 }
4844 }
4845
4846 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04004847 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08004848 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004849 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04004850 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004851 * Notification notif = new Notification.Builder(mContext)
4852 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
4853 * .setContentText(subject)
4854 * .setSmallIcon(R.drawable.new_mail)
4855 * .setLargeIcon(aBitmap)
4856 * .setStyle(new Notification.InboxStyle()
4857 * .addLine(str1)
4858 * .addLine(str2)
4859 * .setContentTitle(&quot;&quot;)
4860 * .setSummaryText(&quot;+3 more&quot;))
4861 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04004862 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004863 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04004864 * @see Notification#bigContentView
4865 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004866 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004867 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
4868
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004869 public InboxStyle() {
4870 }
4871
Adrian Roosf5faf9d2016-05-23 13:56:15 -07004872 /**
4873 * @deprecated use {@code InboxStyle()}.
4874 */
4875 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04004876 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004877 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004878 }
4879
Chris Wrend6297db2012-05-03 16:20:13 -04004880 /**
4881 * Overrides ContentTitle in the big form of the template.
4882 * This defaults to the value passed to setContentTitle().
4883 */
4884 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004885 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004886 return this;
4887 }
4888
4889 /**
4890 * Set the first line of text after the detail section in the big form of the template.
4891 */
4892 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004893 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004894 return this;
4895 }
4896
Chris Wren0bd664d2012-08-01 13:56:56 -04004897 /**
4898 * Append a line to the digest section of the Inbox notification.
4899 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04004900 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004901 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04004902 return this;
4903 }
4904
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004905 /**
4906 * @hide
4907 */
4908 public void addExtras(Bundle extras) {
4909 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004910
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004911 CharSequence[] a = new CharSequence[mTexts.size()];
4912 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
4913 }
4914
Christoph Studer4600f9b2014-07-22 22:44:43 +02004915 /**
4916 * @hide
4917 */
4918 @Override
4919 protected void restoreFromExtras(Bundle extras) {
4920 super.restoreFromExtras(extras);
4921
4922 mTexts.clear();
4923 if (extras.containsKey(EXTRA_TEXT_LINES)) {
4924 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
4925 }
4926 }
4927
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004928 /**
4929 * @hide
4930 */
4931 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004932 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02004933 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004934 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
4935 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004936
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004937 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04004938
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004939 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004940
Chris Wrend6297db2012-05-03 16:20:13 -04004941 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 -04004942 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04004943
Chris Wren4ed80d52012-05-17 09:30:03 -04004944 // Make sure all rows are gone in case we reuse a view.
4945 for (int rowId : rowIds) {
4946 contentView.setViewVisibility(rowId, View.GONE);
4947 }
4948
Daniel Sandler879c5e02012-04-17 16:46:51 -04004949 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07004950 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
4951 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08004952 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07004953 int onlyViewId = 0;
4954 int maxRows = rowIds.length;
4955 if (mBuilder.mActions.size() > 0) {
4956 maxRows--;
4957 }
4958 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004959 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07004960 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004961 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004962 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Selim Cinek07c80172016-04-21 16:40:47 -07004963 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08004964 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07004965 if (first) {
4966 onlyViewId = rowIds[i];
4967 } else {
4968 onlyViewId = 0;
4969 }
Selim Cinek247fa012016-02-18 09:50:48 -08004970 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004971 }
4972 i++;
4973 }
Selim Cinek07c80172016-04-21 16:40:47 -07004974 if (onlyViewId != 0) {
4975 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
4976 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
4977 R.dimen.notification_text_margin_top);
4978 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
4979 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004980
Daniel Sandler879c5e02012-04-17 16:46:51 -04004981 return contentView;
4982 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004983
Selim Cinek247fa012016-02-18 09:50:48 -08004984 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08004985 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08004986 if (first) {
4987 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
4988 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4989 boolean hasProgress = max != 0 || ind;
4990 if (mBuilder.mN.mLargeIcon != null && !hasProgress) {
4991 endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4992 R.dimen.notification_content_picture_margin);
4993 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004994 }
4995 contentView.setViewLayoutMarginEnd(id, endMargin);
4996 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004997 }
Dan Sandler842dd772014-05-15 09:36:47 -04004998
4999 /**
5000 * Notification style for media playback notifications.
5001 *
5002 * In the expanded form, {@link Notification#bigContentView}, up to 5
5003 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04005004 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04005005 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
5006 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
5007 * treated as album artwork.
5008 *
5009 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
5010 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01005011 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04005012 * in the standard view alongside the usual content.
5013 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005014 * Notifications created with MediaStyle will have their category set to
5015 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
5016 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
5017 *
Jeff Browndba34ba2014-06-24 20:46:03 -07005018 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
5019 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04005020 * the System UI can identify this as a notification representing an active media session
5021 * and respond accordingly (by showing album artwork in the lockscreen, for example).
5022 *
5023 * To use this style with your Notification, feed it to
5024 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5025 * <pre class="prettyprint">
5026 * Notification noti = new Notification.Builder()
5027 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01005028 * .setContentTitle(&quot;Track title&quot;)
5029 * .setContentText(&quot;Artist - Album&quot;)
5030 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005031 * .setStyle(<b>new Notification.MediaStyle()</b>
5032 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04005033 * .build();
5034 * </pre>
5035 *
5036 * @see Notification#bigContentView
5037 */
5038 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005039 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04005040 static final int MAX_MEDIA_BUTTONS = 5;
5041
5042 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07005043 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04005044
5045 public MediaStyle() {
5046 }
5047
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005048 /**
5049 * @deprecated use {@code MediaStyle()}.
5050 */
5051 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04005052 public MediaStyle(Builder builder) {
5053 setBuilder(builder);
5054 }
5055
5056 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005057 * 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 -04005058 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005059 *
5060 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04005061 */
5062 public MediaStyle setShowActionsInCompactView(int...actions) {
5063 mActionsToShowInCompact = actions;
5064 return this;
5065 }
5066
5067 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07005068 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
5069 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04005070 */
Jeff Browndba34ba2014-06-24 20:46:03 -07005071 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04005072 mToken = token;
5073 return this;
5074 }
5075
Christoph Studer4600f9b2014-07-22 22:44:43 +02005076 /**
5077 * @hide
5078 */
Dan Sandler842dd772014-05-15 09:36:47 -04005079 @Override
5080 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005081 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01005082 if (wip.category == null) {
5083 wip.category = Notification.CATEGORY_TRANSPORT;
5084 }
Dan Sandler842dd772014-05-15 09:36:47 -04005085 return wip;
5086 }
5087
Christoph Studer4600f9b2014-07-22 22:44:43 +02005088 /**
5089 * @hide
5090 */
5091 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005092 public RemoteViews makeContentView() {
5093 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02005094 }
5095
5096 /**
5097 * @hide
5098 */
5099 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005100 public RemoteViews makeBigContentView() {
5101 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02005102 }
5103
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005104 /**
5105 * @hide
5106 */
5107 @Override
5108 public RemoteViews makeHeadsUpContentView() {
5109 RemoteViews expanded = makeMediaBigContentView();
5110 return expanded != null ? expanded : makeMediaContentView();
5111 }
5112
Dan Sandler842dd772014-05-15 09:36:47 -04005113 /** @hide */
5114 @Override
5115 public void addExtras(Bundle extras) {
5116 super.addExtras(extras);
5117
5118 if (mToken != null) {
5119 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
5120 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01005121 if (mActionsToShowInCompact != null) {
5122 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
5123 }
Dan Sandler842dd772014-05-15 09:36:47 -04005124 }
5125
Christoph Studer4600f9b2014-07-22 22:44:43 +02005126 /**
5127 * @hide
5128 */
5129 @Override
5130 protected void restoreFromExtras(Bundle extras) {
5131 super.restoreFromExtras(extras);
5132
5133 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
5134 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
5135 }
5136 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
5137 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
5138 }
5139 }
5140
Selim Cinek5bf069a2015-11-10 19:14:27 -05005141 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04005142 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07005143 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07005144 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04005145 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05005146 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
5147 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04005148 if (!tombstone) {
5149 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
5150 }
5151 button.setContentDescription(R.id.action0, action.title);
5152 return button;
5153 }
5154
5155 private RemoteViews makeMediaContentView() {
5156 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005157 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04005158
5159 final int numActions = mBuilder.mActions.size();
5160 final int N = mActionsToShowInCompact == null
5161 ? 0
5162 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
5163 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005164 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04005165 for (int i = 0; i < N; i++) {
5166 if (i >= numActions) {
5167 throw new IllegalArgumentException(String.format(
5168 "setShowActionsInCompactView: action %d out of bounds (max %d)",
5169 i, numActions - 1));
5170 }
5171
5172 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05005173 final RemoteViews button = generateMediaActionButton(action,
Adrian Roos4ff3b122016-02-01 12:26:13 -08005174 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005175 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04005176 }
5177 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08005178 handleImage(view);
5179 // handle the content margin
Selim Cinek247fa012016-02-18 09:50:48 -08005180 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
5181 R.dimen.notification_content_margin_end);;
Selim Cinekfdc738f2016-01-27 20:04:27 -08005182 if (mBuilder.mN.mLargeIcon != null) {
Selim Cinek247fa012016-02-18 09:50:48 -08005183 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
5184 R.dimen.notification_content_picture_margin);
Selim Cinekfdc738f2016-01-27 20:04:27 -08005185 }
5186 view.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04005187 return view;
5188 }
5189
5190 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005191 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08005192 // Dont add an expanded view if there is no more content to be revealed
5193 int actionsInCompact = mActionsToShowInCompact == null
5194 ? 0
5195 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
5196 if (mBuilder.mN.mLargeIcon == null && actionCount <= actionsInCompact) {
5197 return null;
5198 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05005199 RemoteViews big = mBuilder.applyStandardTemplate(
5200 R.layout.notification_template_material_big_media,
5201 false);
Dan Sandler842dd772014-05-15 09:36:47 -04005202
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005203 if (actionCount > 0) {
5204 big.removeAllViews(com.android.internal.R.id.media_actions);
5205 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05005206 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Adrian Roos4ff3b122016-02-01 12:26:13 -08005207 mBuilder.resolveContrastColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005208 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04005209 }
5210 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05005211 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04005212 return big;
5213 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005214
Selim Cinek5bf069a2015-11-10 19:14:27 -05005215 private void handleImage(RemoteViews contentView) {
5216 if (mBuilder.mN.mLargeIcon != null) {
5217 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005218 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005219 }
5220 }
5221
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005222 /**
5223 * @hide
5224 */
5225 @Override
5226 protected boolean hasProgress() {
5227 return false;
5228 }
Dan Sandler842dd772014-05-15 09:36:47 -04005229 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005230
Selim Cinek593610c2016-02-16 18:42:57 -08005231 /**
5232 * Notification style for custom views that are decorated by the system
5233 *
5234 * <p>Instead of providing a notification that is completely custom, a developer can set this
5235 * style and still obtain system decorations like the notification header with the expand
5236 * affordance and actions.
5237 *
5238 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5239 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5240 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5241 * corresponding custom views to display.
5242 *
5243 * To use this style with your Notification, feed it to
5244 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5245 * <pre class="prettyprint">
5246 * Notification noti = new Notification.Builder()
5247 * .setSmallIcon(R.drawable.ic_stat_player)
5248 * .setLargeIcon(albumArtBitmap))
5249 * .setCustomContentView(contentView);
5250 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
5251 * .build();
5252 * </pre>
5253 */
5254 public static class DecoratedCustomViewStyle extends Style {
5255
5256 public DecoratedCustomViewStyle() {
5257 }
5258
Selim Cinek593610c2016-02-16 18:42:57 -08005259 /**
5260 * @hide
5261 */
5262 public boolean displayCustomViewInline() {
5263 return true;
5264 }
5265
5266 /**
5267 * @hide
5268 */
5269 @Override
5270 public RemoteViews makeContentView() {
5271 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
5272 }
5273
5274 /**
5275 * @hide
5276 */
5277 @Override
5278 public RemoteViews makeBigContentView() {
5279 return makeDecoratedBigContentView();
5280 }
5281
5282 /**
5283 * @hide
5284 */
5285 @Override
5286 public RemoteViews makeHeadsUpContentView() {
5287 return makeDecoratedHeadsUpContentView();
5288 }
5289
Selim Cinek593610c2016-02-16 18:42:57 -08005290 private RemoteViews makeDecoratedHeadsUpContentView() {
5291 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
5292 ? mBuilder.mN.contentView
5293 : mBuilder.mN.headsUpContentView;
5294 if (mBuilder.mActions.size() == 0) {
5295 return makeStandardTemplateWithCustomContent(headsUpContentView);
5296 }
5297 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5298 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005299 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005300 return remoteViews;
5301 }
5302
Selim Cinek593610c2016-02-16 18:42:57 -08005303 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
5304 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
5305 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005306 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08005307 return remoteViews;
5308 }
5309
Selim Cinek593610c2016-02-16 18:42:57 -08005310 private RemoteViews makeDecoratedBigContentView() {
5311 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
5312 ? mBuilder.mN.contentView
5313 : mBuilder.mN.bigContentView;
5314 if (mBuilder.mActions.size() == 0) {
5315 return makeStandardTemplateWithCustomContent(bigContentView);
5316 }
5317 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
5318 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08005319 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08005320 return remoteViews;
5321 }
Selim Cinek247fa012016-02-18 09:50:48 -08005322
5323 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
5324 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005325 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005326 // Need to clone customContent before adding, because otherwise it can no longer be
5327 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005328 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005329 remoteViews.removeAllViews(R.id.notification_main_column);
5330 remoteViews.addView(R.id.notification_main_column, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005331 }
Selim Cinek247fa012016-02-18 09:50:48 -08005332 // also update the end margin if there is an image
5333 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
5334 R.dimen.notification_content_margin_end);
5335 if (mBuilder.mN.mLargeIcon != null) {
5336 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
5337 R.dimen.notification_content_picture_margin);
5338 }
5339 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
5340 }
Selim Cinek593610c2016-02-16 18:42:57 -08005341 }
5342
Selim Cinek03eb3b72016-02-18 10:39:45 -08005343 /**
5344 * Notification style for media custom views that are decorated by the system
5345 *
5346 * <p>Instead of providing a media notification that is completely custom, a developer can set
5347 * this style and still obtain system decorations like the notification header with the expand
5348 * affordance and actions.
5349 *
5350 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
5351 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
5352 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
5353 * corresponding custom views to display.
5354 *
5355 * To use this style with your Notification, feed it to
5356 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
5357 * <pre class="prettyprint">
5358 * Notification noti = new Notification.Builder()
5359 * .setSmallIcon(R.drawable.ic_stat_player)
5360 * .setLargeIcon(albumArtBitmap))
5361 * .setCustomContentView(contentView);
5362 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
5363 * .setMediaSession(mySession))
5364 * .build();
5365 * </pre>
5366 *
5367 * @see android.app.Notification.DecoratedCustomViewStyle
5368 * @see android.app.Notification.MediaStyle
5369 */
5370 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
5371
5372 public DecoratedMediaCustomViewStyle() {
5373 }
5374
Selim Cinek03eb3b72016-02-18 10:39:45 -08005375 /**
5376 * @hide
5377 */
5378 public boolean displayCustomViewInline() {
5379 return true;
5380 }
5381
5382 /**
5383 * @hide
5384 */
5385 @Override
5386 public RemoteViews makeContentView() {
5387 RemoteViews remoteViews = super.makeContentView();
5388 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5389 mBuilder.mN.contentView);
5390 }
5391
5392 /**
5393 * @hide
5394 */
5395 @Override
5396 public RemoteViews makeBigContentView() {
5397 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
5398 ? mBuilder.mN.bigContentView
5399 : mBuilder.mN.contentView;
5400 return makeBigContentViewWithCustomContent(customRemoteView);
5401 }
5402
5403 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
5404 RemoteViews remoteViews = super.makeBigContentView();
5405 if (remoteViews != null) {
5406 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
5407 customRemoteView);
5408 } else if (customRemoteView != mBuilder.mN.contentView){
5409 remoteViews = super.makeContentView();
5410 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
5411 customRemoteView);
5412 } else {
5413 return null;
5414 }
5415 }
5416
5417 /**
5418 * @hide
5419 */
5420 @Override
5421 public RemoteViews makeHeadsUpContentView() {
5422 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
5423 ? mBuilder.mN.headsUpContentView
5424 : mBuilder.mN.contentView;
5425 return makeBigContentViewWithCustomContent(customRemoteView);
5426 }
5427
5428 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
5429 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08005430 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07005431 // Need to clone customContent before adding, because otherwise it can no longer be
5432 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08005433 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07005434 remoteViews.removeAllViews(id);
5435 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08005436 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08005437 return remoteViews;
5438 }
5439 }
5440
Christoph Studer4600f9b2014-07-22 22:44:43 +02005441 // When adding a new Style subclass here, don't forget to update
5442 // Builder.getNotificationStyleClass.
5443
Griff Hazen61a9e862014-05-22 16:05:19 -07005444 /**
5445 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
5446 * metadata or change options on a notification builder.
5447 */
5448 public interface Extender {
5449 /**
5450 * Apply this extender to a notification builder.
5451 * @param builder the builder to be modified.
5452 * @return the build object for chaining.
5453 */
5454 public Builder extend(Builder builder);
5455 }
5456
5457 /**
5458 * Helper class to add wearable extensions to notifications.
5459 * <p class="note"> See
5460 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
5461 * for Android Wear</a> for more information on how to use this class.
5462 * <p>
5463 * To create a notification with wearable extensions:
5464 * <ol>
5465 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
5466 * properties.
5467 * <li>Create a {@link android.app.Notification.WearableExtender}.
5468 * <li>Set wearable-specific properties using the
5469 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
5470 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
5471 * notification.
5472 * <li>Post the notification to the notification system with the
5473 * {@code NotificationManager.notify(...)} methods.
5474 * </ol>
5475 *
5476 * <pre class="prettyprint">
5477 * Notification notif = new Notification.Builder(mContext)
5478 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5479 * .setContentText(subject)
5480 * .setSmallIcon(R.drawable.new_mail)
5481 * .extend(new Notification.WearableExtender()
5482 * .setContentIcon(R.drawable.new_mail))
5483 * .build();
5484 * NotificationManager notificationManger =
5485 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
5486 * notificationManger.notify(0, notif);</pre>
5487 *
5488 * <p>Wearable extensions can be accessed on an existing notification by using the
5489 * {@code WearableExtender(Notification)} constructor,
5490 * and then using the {@code get} methods to access values.
5491 *
5492 * <pre class="prettyprint">
5493 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
5494 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07005495 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005496 */
5497 public static final class WearableExtender implements Extender {
5498 /**
5499 * Sentinel value for an action index that is unset.
5500 */
5501 public static final int UNSET_ACTION_INDEX = -1;
5502
5503 /**
5504 * Size value for use with {@link #setCustomSizePreset} to show this notification with
5505 * default sizing.
5506 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07005507 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07005508 * on their content.
5509 */
5510 public static final int SIZE_DEFAULT = 0;
5511
5512 /**
5513 * Size value for use with {@link #setCustomSizePreset} to show this notification
5514 * with an extra small size.
5515 * <p>This value is only applicable for custom display notifications created using
5516 * {@link #setDisplayIntent}.
5517 */
5518 public static final int SIZE_XSMALL = 1;
5519
5520 /**
5521 * Size value for use with {@link #setCustomSizePreset} to show this notification
5522 * with a small size.
5523 * <p>This value is only applicable for custom display notifications created using
5524 * {@link #setDisplayIntent}.
5525 */
5526 public static final int SIZE_SMALL = 2;
5527
5528 /**
5529 * Size value for use with {@link #setCustomSizePreset} to show this notification
5530 * with a medium size.
5531 * <p>This value is only applicable for custom display notifications created using
5532 * {@link #setDisplayIntent}.
5533 */
5534 public static final int SIZE_MEDIUM = 3;
5535
5536 /**
5537 * Size value for use with {@link #setCustomSizePreset} to show this notification
5538 * with a large size.
5539 * <p>This value is only applicable for custom display notifications created using
5540 * {@link #setDisplayIntent}.
5541 */
5542 public static final int SIZE_LARGE = 4;
5543
Griff Hazend5f11f92014-05-27 15:40:09 -07005544 /**
5545 * Size value for use with {@link #setCustomSizePreset} to show this notification
5546 * full screen.
5547 * <p>This value is only applicable for custom display notifications created using
5548 * {@link #setDisplayIntent}.
5549 */
5550 public static final int SIZE_FULL_SCREEN = 5;
5551
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005552 /**
5553 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
5554 * short amount of time when this notification is displayed on the screen. This
5555 * is the default value.
5556 */
5557 public static final int SCREEN_TIMEOUT_SHORT = 0;
5558
5559 /**
5560 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
5561 * for a longer amount of time when this notification is displayed on the screen.
5562 */
5563 public static final int SCREEN_TIMEOUT_LONG = -1;
5564
Griff Hazen61a9e862014-05-22 16:05:19 -07005565 /** Notification extra which contains wearable extensions */
5566 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
5567
Pete Gastaf6781d2014-10-07 15:17:05 -04005568 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07005569 private static final String KEY_ACTIONS = "actions";
5570 private static final String KEY_FLAGS = "flags";
5571 private static final String KEY_DISPLAY_INTENT = "displayIntent";
5572 private static final String KEY_PAGES = "pages";
5573 private static final String KEY_BACKGROUND = "background";
5574 private static final String KEY_CONTENT_ICON = "contentIcon";
5575 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
5576 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
5577 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
5578 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
5579 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005580 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04005581 private static final String KEY_DISMISSAL_ID = "dismissalId";
Griff Hazen61a9e862014-05-22 16:05:19 -07005582
5583 // Flags bitwise-ored to mFlags
5584 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
5585 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
5586 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
5587 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005588 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04005589 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04005590 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07005591
5592 // Default value for flags integer
5593 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
5594
5595 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
5596 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
5597
5598 private ArrayList<Action> mActions = new ArrayList<Action>();
5599 private int mFlags = DEFAULT_FLAGS;
5600 private PendingIntent mDisplayIntent;
5601 private ArrayList<Notification> mPages = new ArrayList<Notification>();
5602 private Bitmap mBackground;
5603 private int mContentIcon;
5604 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
5605 private int mContentActionIndex = UNSET_ACTION_INDEX;
5606 private int mCustomSizePreset = SIZE_DEFAULT;
5607 private int mCustomContentHeight;
5608 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005609 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04005610 private String mDismissalId;
Griff Hazen61a9e862014-05-22 16:05:19 -07005611
5612 /**
5613 * Create a {@link android.app.Notification.WearableExtender} with default
5614 * options.
5615 */
5616 public WearableExtender() {
5617 }
5618
5619 public WearableExtender(Notification notif) {
5620 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
5621 if (wearableBundle != null) {
5622 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
5623 if (actions != null) {
5624 mActions.addAll(actions);
5625 }
5626
5627 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
5628 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
5629
5630 Notification[] pages = getNotificationArrayFromBundle(
5631 wearableBundle, KEY_PAGES);
5632 if (pages != null) {
5633 Collections.addAll(mPages, pages);
5634 }
5635
5636 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
5637 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
5638 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
5639 DEFAULT_CONTENT_ICON_GRAVITY);
5640 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
5641 UNSET_ACTION_INDEX);
5642 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
5643 SIZE_DEFAULT);
5644 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
5645 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005646 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04005647 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Griff Hazen61a9e862014-05-22 16:05:19 -07005648 }
5649 }
5650
5651 /**
5652 * Apply wearable extensions to a notification that is being built. This is typically
5653 * called by the {@link android.app.Notification.Builder#extend} method of
5654 * {@link android.app.Notification.Builder}.
5655 */
5656 @Override
5657 public Notification.Builder extend(Notification.Builder builder) {
5658 Bundle wearableBundle = new Bundle();
5659
5660 if (!mActions.isEmpty()) {
5661 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
5662 }
5663 if (mFlags != DEFAULT_FLAGS) {
5664 wearableBundle.putInt(KEY_FLAGS, mFlags);
5665 }
5666 if (mDisplayIntent != null) {
5667 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
5668 }
5669 if (!mPages.isEmpty()) {
5670 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
5671 new Notification[mPages.size()]));
5672 }
5673 if (mBackground != null) {
5674 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
5675 }
5676 if (mContentIcon != 0) {
5677 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
5678 }
5679 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
5680 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
5681 }
5682 if (mContentActionIndex != UNSET_ACTION_INDEX) {
5683 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
5684 mContentActionIndex);
5685 }
5686 if (mCustomSizePreset != SIZE_DEFAULT) {
5687 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
5688 }
5689 if (mCustomContentHeight != 0) {
5690 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
5691 }
5692 if (mGravity != DEFAULT_GRAVITY) {
5693 wearableBundle.putInt(KEY_GRAVITY, mGravity);
5694 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005695 if (mHintScreenTimeout != 0) {
5696 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
5697 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04005698 if (mDismissalId != null) {
5699 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
5700 }
Griff Hazen61a9e862014-05-22 16:05:19 -07005701
5702 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
5703 return builder;
5704 }
5705
5706 @Override
5707 public WearableExtender clone() {
5708 WearableExtender that = new WearableExtender();
5709 that.mActions = new ArrayList<Action>(this.mActions);
5710 that.mFlags = this.mFlags;
5711 that.mDisplayIntent = this.mDisplayIntent;
5712 that.mPages = new ArrayList<Notification>(this.mPages);
5713 that.mBackground = this.mBackground;
5714 that.mContentIcon = this.mContentIcon;
5715 that.mContentIconGravity = this.mContentIconGravity;
5716 that.mContentActionIndex = this.mContentActionIndex;
5717 that.mCustomSizePreset = this.mCustomSizePreset;
5718 that.mCustomContentHeight = this.mCustomContentHeight;
5719 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005720 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04005721 that.mDismissalId = this.mDismissalId;
Griff Hazen61a9e862014-05-22 16:05:19 -07005722 return that;
5723 }
5724
5725 /**
5726 * Add a wearable action to this notification.
5727 *
5728 * <p>When wearable actions are added using this method, the set of actions that
5729 * show on a wearable device splits from devices that only show actions added
5730 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
5731 * of which actions display on different devices.
5732 *
5733 * @param action the action to add to this notification
5734 * @return this object for method chaining
5735 * @see android.app.Notification.Action
5736 */
5737 public WearableExtender addAction(Action action) {
5738 mActions.add(action);
5739 return this;
5740 }
5741
5742 /**
5743 * Adds wearable actions to this notification.
5744 *
5745 * <p>When wearable actions are added using this method, the set of actions that
5746 * show on a wearable device splits from devices that only show actions added
5747 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
5748 * of which actions display on different devices.
5749 *
5750 * @param actions the actions to add to this notification
5751 * @return this object for method chaining
5752 * @see android.app.Notification.Action
5753 */
5754 public WearableExtender addActions(List<Action> actions) {
5755 mActions.addAll(actions);
5756 return this;
5757 }
5758
5759 /**
5760 * Clear all wearable actions present on this builder.
5761 * @return this object for method chaining.
5762 * @see #addAction
5763 */
5764 public WearableExtender clearActions() {
5765 mActions.clear();
5766 return this;
5767 }
5768
5769 /**
5770 * Get the wearable actions present on this notification.
5771 */
5772 public List<Action> getActions() {
5773 return mActions;
5774 }
5775
5776 /**
5777 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07005778 * this notification. The {@link PendingIntent} provided should be for an activity.
5779 *
5780 * <pre class="prettyprint">
5781 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
5782 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
5783 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
5784 * Notification notif = new Notification.Builder(context)
5785 * .extend(new Notification.WearableExtender()
5786 * .setDisplayIntent(displayPendingIntent)
5787 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
5788 * .build();</pre>
5789 *
5790 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07005791 * should have an empty task affinity. It is also recommended to use the device
5792 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07005793 *
5794 * <p>Example AndroidManifest.xml entry:
5795 * <pre class="prettyprint">
5796 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
5797 * android:exported=&quot;true&quot;
5798 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07005799 * android:taskAffinity=&quot;&quot;
5800 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005801 *
5802 * @param intent the {@link PendingIntent} for an activity
5803 * @return this object for method chaining
5804 * @see android.app.Notification.WearableExtender#getDisplayIntent
5805 */
5806 public WearableExtender setDisplayIntent(PendingIntent intent) {
5807 mDisplayIntent = intent;
5808 return this;
5809 }
5810
5811 /**
5812 * Get the intent to launch inside of an activity view when displaying this
5813 * notification. This {@code PendingIntent} should be for an activity.
5814 */
5815 public PendingIntent getDisplayIntent() {
5816 return mDisplayIntent;
5817 }
5818
5819 /**
5820 * Add an additional page of content to display with this notification. The current
5821 * notification forms the first page, and pages added using this function form
5822 * subsequent pages. This field can be used to separate a notification into multiple
5823 * sections.
5824 *
5825 * @param page the notification to add as another page
5826 * @return this object for method chaining
5827 * @see android.app.Notification.WearableExtender#getPages
5828 */
5829 public WearableExtender addPage(Notification page) {
5830 mPages.add(page);
5831 return this;
5832 }
5833
5834 /**
5835 * Add additional pages of content to display with this notification. The current
5836 * notification forms the first page, and pages added using this function form
5837 * subsequent pages. This field can be used to separate a notification into multiple
5838 * sections.
5839 *
5840 * @param pages a list of notifications
5841 * @return this object for method chaining
5842 * @see android.app.Notification.WearableExtender#getPages
5843 */
5844 public WearableExtender addPages(List<Notification> pages) {
5845 mPages.addAll(pages);
5846 return this;
5847 }
5848
5849 /**
5850 * Clear all additional pages present on this builder.
5851 * @return this object for method chaining.
5852 * @see #addPage
5853 */
5854 public WearableExtender clearPages() {
5855 mPages.clear();
5856 return this;
5857 }
5858
5859 /**
5860 * Get the array of additional pages of content for displaying this notification. The
5861 * current notification forms the first page, and elements within this array form
5862 * subsequent pages. This field can be used to separate a notification into multiple
5863 * sections.
5864 * @return the pages for this notification
5865 */
5866 public List<Notification> getPages() {
5867 return mPages;
5868 }
5869
5870 /**
5871 * Set a background image to be displayed behind the notification content.
5872 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5873 * will work with any notification style.
5874 *
5875 * @param background the background bitmap
5876 * @return this object for method chaining
5877 * @see android.app.Notification.WearableExtender#getBackground
5878 */
5879 public WearableExtender setBackground(Bitmap background) {
5880 mBackground = background;
5881 return this;
5882 }
5883
5884 /**
5885 * Get a background image to be displayed behind the notification content.
5886 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5887 * will work with any notification style.
5888 *
5889 * @return the background image
5890 * @see android.app.Notification.WearableExtender#setBackground
5891 */
5892 public Bitmap getBackground() {
5893 return mBackground;
5894 }
5895
5896 /**
5897 * Set an icon that goes with the content of this notification.
5898 */
5899 public WearableExtender setContentIcon(int icon) {
5900 mContentIcon = icon;
5901 return this;
5902 }
5903
5904 /**
5905 * Get an icon that goes with the content of this notification.
5906 */
5907 public int getContentIcon() {
5908 return mContentIcon;
5909 }
5910
5911 /**
5912 * Set the gravity that the content icon should have within the notification display.
5913 * Supported values include {@link android.view.Gravity#START} and
5914 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5915 * @see #setContentIcon
5916 */
5917 public WearableExtender setContentIconGravity(int contentIconGravity) {
5918 mContentIconGravity = contentIconGravity;
5919 return this;
5920 }
5921
5922 /**
5923 * Get the gravity that the content icon should have within the notification display.
5924 * Supported values include {@link android.view.Gravity#START} and
5925 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5926 * @see #getContentIcon
5927 */
5928 public int getContentIconGravity() {
5929 return mContentIconGravity;
5930 }
5931
5932 /**
5933 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07005934 * this notification. This action will no longer display separately from the
5935 * notification's content.
5936 *
Griff Hazenca48d352014-05-28 22:37:13 -07005937 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005938 * set, although the list of available actions comes from the main notification and not
5939 * from the child page's notification.
5940 *
5941 * @param actionIndex The index of the action to hoist onto the current notification page.
5942 * If wearable actions were added to the main notification, this index
5943 * will apply to that list, otherwise it will apply to the regular
5944 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07005945 */
5946 public WearableExtender setContentAction(int actionIndex) {
5947 mContentActionIndex = actionIndex;
5948 return this;
5949 }
5950
5951 /**
Griff Hazenca48d352014-05-28 22:37:13 -07005952 * Get the index of the notification action, if any, that was specified as being clickable
5953 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07005954 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07005955 *
Griff Hazenca48d352014-05-28 22:37:13 -07005956 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005957 * set, although the list of available actions comes from the main notification and not
5958 * from the child page's notification.
5959 *
5960 * <p>If wearable specific actions were added to the main notification, this index will
5961 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07005962 *
5963 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07005964 */
5965 public int getContentAction() {
5966 return mContentActionIndex;
5967 }
5968
5969 /**
5970 * Set the gravity that this notification should have within the available viewport space.
5971 * Supported values include {@link android.view.Gravity#TOP},
5972 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5973 * The default value is {@link android.view.Gravity#BOTTOM}.
5974 */
5975 public WearableExtender setGravity(int gravity) {
5976 mGravity = gravity;
5977 return this;
5978 }
5979
5980 /**
5981 * Get the gravity that this notification should have within the available viewport space.
5982 * Supported values include {@link android.view.Gravity#TOP},
5983 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5984 * The default value is {@link android.view.Gravity#BOTTOM}.
5985 */
5986 public int getGravity() {
5987 return mGravity;
5988 }
5989
5990 /**
5991 * Set the custom size preset for the display of this notification out of the available
5992 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5993 * {@link #SIZE_LARGE}.
5994 * <p>Some custom size presets are only applicable for custom display notifications created
5995 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
5996 * documentation for the preset in question. See also
5997 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
5998 */
5999 public WearableExtender setCustomSizePreset(int sizePreset) {
6000 mCustomSizePreset = sizePreset;
6001 return this;
6002 }
6003
6004 /**
6005 * Get the custom size preset for the display of this notification out of the available
6006 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
6007 * {@link #SIZE_LARGE}.
6008 * <p>Some custom size presets are only applicable for custom display notifications created
6009 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
6010 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
6011 */
6012 public int getCustomSizePreset() {
6013 return mCustomSizePreset;
6014 }
6015
6016 /**
6017 * Set the custom height in pixels for the display of this notification's content.
6018 * <p>This option is only available for custom display notifications created
6019 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
6020 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
6021 * {@link #getCustomContentHeight}.
6022 */
6023 public WearableExtender setCustomContentHeight(int height) {
6024 mCustomContentHeight = height;
6025 return this;
6026 }
6027
6028 /**
6029 * Get the custom height in pixels for the display of this notification's content.
6030 * <p>This option is only available for custom display notifications created
6031 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
6032 * {@link #setCustomContentHeight}.
6033 */
6034 public int getCustomContentHeight() {
6035 return mCustomContentHeight;
6036 }
6037
6038 /**
6039 * Set whether the scrolling position for the contents of this notification should start
6040 * at the bottom of the contents instead of the top when the contents are too long to
6041 * display within the screen. Default is false (start scroll at the top).
6042 */
6043 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
6044 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
6045 return this;
6046 }
6047
6048 /**
6049 * Get whether the scrolling position for the contents of this notification should start
6050 * at the bottom of the contents instead of the top when the contents are too long to
6051 * display within the screen. Default is false (start scroll at the top).
6052 */
6053 public boolean getStartScrollBottom() {
6054 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
6055 }
6056
6057 /**
6058 * Set whether the content intent is available when the wearable device is not connected
6059 * to a companion device. The user can still trigger this intent when the wearable device
6060 * is offline, but a visual hint will indicate that the content intent may not be available.
6061 * Defaults to true.
6062 */
6063 public WearableExtender setContentIntentAvailableOffline(
6064 boolean contentIntentAvailableOffline) {
6065 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
6066 return this;
6067 }
6068
6069 /**
6070 * Get whether the content intent is available when the wearable device is not connected
6071 * to a companion device. The user can still trigger this intent when the wearable device
6072 * is offline, but a visual hint will indicate that the content intent may not be available.
6073 * Defaults to true.
6074 */
6075 public boolean getContentIntentAvailableOffline() {
6076 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
6077 }
6078
6079 /**
6080 * Set a hint that this notification's icon should not be displayed.
6081 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
6082 * @return this object for method chaining
6083 */
6084 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
6085 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
6086 return this;
6087 }
6088
6089 /**
6090 * Get a hint that this notification's icon should not be displayed.
6091 * @return {@code true} if this icon should not be displayed, false otherwise.
6092 * The default value is {@code false} if this was never set.
6093 */
6094 public boolean getHintHideIcon() {
6095 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
6096 }
6097
6098 /**
6099 * Set a visual hint that only the background image of this notification should be
6100 * displayed, and other semantic content should be hidden. This hint is only applicable
6101 * to sub-pages added using {@link #addPage}.
6102 */
6103 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
6104 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
6105 return this;
6106 }
6107
6108 /**
6109 * Get a visual hint that only the background image of this notification should be
6110 * displayed, and other semantic content should be hidden. This hint is only applicable
6111 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
6112 */
6113 public boolean getHintShowBackgroundOnly() {
6114 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
6115 }
6116
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006117 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08006118 * Set a hint that this notification's background should not be clipped if possible,
6119 * and should instead be resized to fully display on the screen, retaining the aspect
6120 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006121 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
6122 * @return this object for method chaining
6123 */
6124 public WearableExtender setHintAvoidBackgroundClipping(
6125 boolean hintAvoidBackgroundClipping) {
6126 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
6127 return this;
6128 }
6129
6130 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08006131 * Get a hint that this notification's background should not be clipped if possible,
6132 * and should instead be resized to fully display on the screen, retaining the aspect
6133 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006134 * @return {@code true} if it's ok if the background is clipped on the screen, false
6135 * otherwise. The default value is {@code false} if this was never set.
6136 */
6137 public boolean getHintAvoidBackgroundClipping() {
6138 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
6139 }
6140
6141 /**
6142 * Set a hint that the screen should remain on for at least this duration when
6143 * this notification is displayed on the screen.
6144 * @param timeout The requested screen timeout in milliseconds. Can also be either
6145 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
6146 * @return this object for method chaining
6147 */
6148 public WearableExtender setHintScreenTimeout(int timeout) {
6149 mHintScreenTimeout = timeout;
6150 return this;
6151 }
6152
6153 /**
6154 * Get the duration, in milliseconds, that the screen should remain on for
6155 * when this notification is displayed.
6156 * @return the duration in milliseconds if > 0, or either one of the sentinel values
6157 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
6158 */
6159 public int getHintScreenTimeout() {
6160 return mHintScreenTimeout;
6161 }
6162
Alex Hills9ab3a232016-04-05 14:54:56 -04006163 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04006164 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
6165 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
6166 * qr codes, as well as other simple black-and-white tickets.
6167 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
6168 * @return this object for method chaining
6169 */
6170 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
6171 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
6172 return this;
6173 }
6174
6175 /**
6176 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
6177 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
6178 * qr codes, as well as other simple black-and-white tickets.
6179 * @return {@code true} if it should be displayed in ambient, false otherwise
6180 * otherwise. The default value is {@code false} if this was never set.
6181 */
6182 public boolean getHintAmbientBigPicture() {
6183 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
6184 }
6185
6186 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04006187 * Set a hint that this notification's content intent will launch an {@link Activity}
6188 * directly, telling the platform that it can generate the appropriate transitions.
6189 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
6190 * an activity and transitions should be generated, false otherwise.
6191 * @return this object for method chaining
6192 */
6193 public WearableExtender setHintContentIntentLaunchesActivity(
6194 boolean hintContentIntentLaunchesActivity) {
6195 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
6196 return this;
6197 }
6198
6199 /**
6200 * Get a hint that this notification's content intent will launch an {@link Activity}
6201 * directly, telling the platform that it can generate the appropriate transitions
6202 * @return {@code true} if the content intent will launch an activity and transitions should
6203 * be generated, false otherwise. The default value is {@code false} if this was never set.
6204 */
6205 public boolean getHintContentIntentLaunchesActivity() {
6206 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
6207 }
6208
Nadia Benbernou948627e2016-04-14 14:41:08 -04006209 /**
6210 * When you post a notification, if you set the dismissal id field, then when that
6211 * notification is canceled, notifications on other wearables and the paired Android phone
6212 * having that same dismissal id will also be canceled. Note that this only works if you
6213 * have notification bridge mode set to NO_BRIDGING in your Wear app manifest. See
6214 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
6215 * Notifications</a> for more information on how to use the bridge mode feature.
6216 * @param dismissalId the dismissal id of the notification.
6217 * @return this object for method chaining
6218 */
6219 public WearableExtender setDismissalId(String dismissalId) {
6220 mDismissalId = dismissalId;
6221 return this;
6222 }
6223
6224 /**
6225 * Returns the dismissal id of the notification.
6226 * @return the dismissal id of the notification or null if it has not been set.
6227 */
6228 public String getDismissalId() {
6229 return mDismissalId;
6230 }
6231
Griff Hazen61a9e862014-05-22 16:05:19 -07006232 private void setFlag(int mask, boolean value) {
6233 if (value) {
6234 mFlags |= mask;
6235 } else {
6236 mFlags &= ~mask;
6237 }
6238 }
6239 }
6240
6241 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006242 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
6243 * with car extensions:
6244 *
6245 * <ol>
6246 * <li>Create an {@link Notification.Builder}, setting any desired
6247 * properties.
6248 * <li>Create a {@link CarExtender}.
6249 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
6250 * {@link CarExtender}.
6251 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
6252 * to apply the extensions to a notification.
6253 * </ol>
6254 *
6255 * <pre class="prettyprint">
6256 * Notification notification = new Notification.Builder(context)
6257 * ...
6258 * .extend(new CarExtender()
6259 * .set*(...))
6260 * .build();
6261 * </pre>
6262 *
6263 * <p>Car extensions can be accessed on an existing notification by using the
6264 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
6265 * to access values.
6266 */
6267 public static final class CarExtender implements Extender {
6268 private static final String TAG = "CarExtender";
6269
6270 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
6271 private static final String EXTRA_LARGE_ICON = "large_icon";
6272 private static final String EXTRA_CONVERSATION = "car_conversation";
6273 private static final String EXTRA_COLOR = "app_color";
6274
6275 private Bitmap mLargeIcon;
6276 private UnreadConversation mUnreadConversation;
6277 private int mColor = Notification.COLOR_DEFAULT;
6278
6279 /**
6280 * Create a {@link CarExtender} with default options.
6281 */
6282 public CarExtender() {
6283 }
6284
6285 /**
6286 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
6287 *
6288 * @param notif The notification from which to copy options.
6289 */
6290 public CarExtender(Notification notif) {
6291 Bundle carBundle = notif.extras == null ?
6292 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
6293 if (carBundle != null) {
6294 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
6295 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
6296
6297 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
6298 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
6299 }
6300 }
6301
6302 /**
6303 * Apply car extensions to a notification that is being built. This is typically called by
6304 * the {@link Notification.Builder#extend(Notification.Extender)}
6305 * method of {@link Notification.Builder}.
6306 */
6307 @Override
6308 public Notification.Builder extend(Notification.Builder builder) {
6309 Bundle carExtensions = new Bundle();
6310
6311 if (mLargeIcon != null) {
6312 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
6313 }
6314 if (mColor != Notification.COLOR_DEFAULT) {
6315 carExtensions.putInt(EXTRA_COLOR, mColor);
6316 }
6317
6318 if (mUnreadConversation != null) {
6319 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
6320 carExtensions.putBundle(EXTRA_CONVERSATION, b);
6321 }
6322
6323 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
6324 return builder;
6325 }
6326
6327 /**
6328 * Sets the accent color to use when Android Auto presents the notification.
6329 *
6330 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
6331 * to accent the displayed notification. However, not all colors are acceptable in an
6332 * automotive setting. This method can be used to override the color provided in the
6333 * notification in such a situation.
6334 */
Tor Norbye80756e32015-03-02 09:39:27 -08006335 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006336 mColor = color;
6337 return this;
6338 }
6339
6340 /**
6341 * Gets the accent color.
6342 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006343 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006344 */
Tor Norbye80756e32015-03-02 09:39:27 -08006345 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08006346 public int getColor() {
6347 return mColor;
6348 }
6349
6350 /**
6351 * Sets the large icon of the car notification.
6352 *
6353 * If no large icon is set in the extender, Android Auto will display the icon
6354 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
6355 *
6356 * @param largeIcon The large icon to use in the car notification.
6357 * @return This object for method chaining.
6358 */
6359 public CarExtender setLargeIcon(Bitmap largeIcon) {
6360 mLargeIcon = largeIcon;
6361 return this;
6362 }
6363
6364 /**
6365 * Gets the large icon used in this car notification, or null if no icon has been set.
6366 *
6367 * @return The large icon for the car notification.
6368 * @see CarExtender#setLargeIcon
6369 */
6370 public Bitmap getLargeIcon() {
6371 return mLargeIcon;
6372 }
6373
6374 /**
6375 * Sets the unread conversation in a message notification.
6376 *
6377 * @param unreadConversation The unread part of the conversation this notification conveys.
6378 * @return This object for method chaining.
6379 */
6380 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
6381 mUnreadConversation = unreadConversation;
6382 return this;
6383 }
6384
6385 /**
6386 * Returns the unread conversation conveyed by this notification.
6387 * @see #setUnreadConversation(UnreadConversation)
6388 */
6389 public UnreadConversation getUnreadConversation() {
6390 return mUnreadConversation;
6391 }
6392
6393 /**
6394 * A class which holds the unread messages from a conversation.
6395 */
6396 public static class UnreadConversation {
6397 private static final String KEY_AUTHOR = "author";
6398 private static final String KEY_TEXT = "text";
6399 private static final String KEY_MESSAGES = "messages";
6400 private static final String KEY_REMOTE_INPUT = "remote_input";
6401 private static final String KEY_ON_REPLY = "on_reply";
6402 private static final String KEY_ON_READ = "on_read";
6403 private static final String KEY_PARTICIPANTS = "participants";
6404 private static final String KEY_TIMESTAMP = "timestamp";
6405
6406 private final String[] mMessages;
6407 private final RemoteInput mRemoteInput;
6408 private final PendingIntent mReplyPendingIntent;
6409 private final PendingIntent mReadPendingIntent;
6410 private final String[] mParticipants;
6411 private final long mLatestTimestamp;
6412
6413 UnreadConversation(String[] messages, RemoteInput remoteInput,
6414 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
6415 String[] participants, long latestTimestamp) {
6416 mMessages = messages;
6417 mRemoteInput = remoteInput;
6418 mReadPendingIntent = readPendingIntent;
6419 mReplyPendingIntent = replyPendingIntent;
6420 mParticipants = participants;
6421 mLatestTimestamp = latestTimestamp;
6422 }
6423
6424 /**
6425 * Gets the list of messages conveyed by this notification.
6426 */
6427 public String[] getMessages() {
6428 return mMessages;
6429 }
6430
6431 /**
6432 * Gets the remote input that will be used to convey the response to a message list, or
6433 * null if no such remote input exists.
6434 */
6435 public RemoteInput getRemoteInput() {
6436 return mRemoteInput;
6437 }
6438
6439 /**
6440 * Gets the pending intent that will be triggered when the user replies to this
6441 * notification.
6442 */
6443 public PendingIntent getReplyPendingIntent() {
6444 return mReplyPendingIntent;
6445 }
6446
6447 /**
6448 * Gets the pending intent that Android Auto will send after it reads aloud all messages
6449 * in this object's message list.
6450 */
6451 public PendingIntent getReadPendingIntent() {
6452 return mReadPendingIntent;
6453 }
6454
6455 /**
6456 * Gets the participants in the conversation.
6457 */
6458 public String[] getParticipants() {
6459 return mParticipants;
6460 }
6461
6462 /**
6463 * Gets the firs participant in the conversation.
6464 */
6465 public String getParticipant() {
6466 return mParticipants.length > 0 ? mParticipants[0] : null;
6467 }
6468
6469 /**
6470 * Gets the timestamp of the conversation.
6471 */
6472 public long getLatestTimestamp() {
6473 return mLatestTimestamp;
6474 }
6475
6476 Bundle getBundleForUnreadConversation() {
6477 Bundle b = new Bundle();
6478 String author = null;
6479 if (mParticipants != null && mParticipants.length > 1) {
6480 author = mParticipants[0];
6481 }
6482 Parcelable[] messages = new Parcelable[mMessages.length];
6483 for (int i = 0; i < messages.length; i++) {
6484 Bundle m = new Bundle();
6485 m.putString(KEY_TEXT, mMessages[i]);
6486 m.putString(KEY_AUTHOR, author);
6487 messages[i] = m;
6488 }
6489 b.putParcelableArray(KEY_MESSAGES, messages);
6490 if (mRemoteInput != null) {
6491 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
6492 }
6493 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
6494 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
6495 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
6496 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
6497 return b;
6498 }
6499
6500 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
6501 if (b == null) {
6502 return null;
6503 }
6504 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
6505 String[] messages = null;
6506 if (parcelableMessages != null) {
6507 String[] tmp = new String[parcelableMessages.length];
6508 boolean success = true;
6509 for (int i = 0; i < tmp.length; i++) {
6510 if (!(parcelableMessages[i] instanceof Bundle)) {
6511 success = false;
6512 break;
6513 }
6514 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
6515 if (tmp[i] == null) {
6516 success = false;
6517 break;
6518 }
6519 }
6520 if (success) {
6521 messages = tmp;
6522 } else {
6523 return null;
6524 }
6525 }
6526
6527 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
6528 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
6529
6530 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
6531
6532 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
6533 if (participants == null || participants.length != 1) {
6534 return null;
6535 }
6536
6537 return new UnreadConversation(messages,
6538 remoteInput,
6539 onReply,
6540 onRead,
6541 participants, b.getLong(KEY_TIMESTAMP));
6542 }
6543 };
6544
6545 /**
6546 * Builder class for {@link CarExtender.UnreadConversation} objects.
6547 */
6548 public static class Builder {
6549 private final List<String> mMessages = new ArrayList<String>();
6550 private final String mParticipant;
6551 private RemoteInput mRemoteInput;
6552 private PendingIntent mReadPendingIntent;
6553 private PendingIntent mReplyPendingIntent;
6554 private long mLatestTimestamp;
6555
6556 /**
6557 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
6558 *
6559 * @param name The name of the other participant in the conversation.
6560 */
6561 public Builder(String name) {
6562 mParticipant = name;
6563 }
6564
6565 /**
6566 * Appends a new unread message to the list of messages for this conversation.
6567 *
6568 * The messages should be added from oldest to newest.
6569 *
6570 * @param message The text of the new unread message.
6571 * @return This object for method chaining.
6572 */
6573 public Builder addMessage(String message) {
6574 mMessages.add(message);
6575 return this;
6576 }
6577
6578 /**
6579 * Sets the pending intent and remote input which will convey the reply to this
6580 * notification.
6581 *
6582 * @param pendingIntent The pending intent which will be triggered on a reply.
6583 * @param remoteInput The remote input parcelable which will carry the reply.
6584 * @return This object for method chaining.
6585 *
6586 * @see CarExtender.UnreadConversation#getRemoteInput
6587 * @see CarExtender.UnreadConversation#getReplyPendingIntent
6588 */
6589 public Builder setReplyAction(
6590 PendingIntent pendingIntent, RemoteInput remoteInput) {
6591 mRemoteInput = remoteInput;
6592 mReplyPendingIntent = pendingIntent;
6593
6594 return this;
6595 }
6596
6597 /**
6598 * Sets the pending intent that will be sent once the messages in this notification
6599 * are read.
6600 *
6601 * @param pendingIntent The pending intent to use.
6602 * @return This object for method chaining.
6603 */
6604 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
6605 mReadPendingIntent = pendingIntent;
6606 return this;
6607 }
6608
6609 /**
6610 * Sets the timestamp of the most recent message in an unread conversation.
6611 *
6612 * If a messaging notification has been posted by your application and has not
6613 * yet been cancelled, posting a later notification with the same id and tag
6614 * but without a newer timestamp may result in Android Auto not displaying a
6615 * heads up notification for the later notification.
6616 *
6617 * @param timestamp The timestamp of the most recent message in the conversation.
6618 * @return This object for method chaining.
6619 */
6620 public Builder setLatestTimestamp(long timestamp) {
6621 mLatestTimestamp = timestamp;
6622 return this;
6623 }
6624
6625 /**
6626 * Builds a new unread conversation object.
6627 *
6628 * @return The new unread conversation object.
6629 */
6630 public UnreadConversation build() {
6631 String[] messages = mMessages.toArray(new String[mMessages.size()]);
6632 String[] participants = { mParticipant };
6633 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
6634 mReadPendingIntent, participants, mLatestTimestamp);
6635 }
6636 }
6637 }
6638
6639 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07006640 * Get an array of Notification objects from a parcelable array bundle field.
6641 * Update the bundle to have a typed array so fetches in the future don't need
6642 * to do an array copy.
6643 */
6644 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
6645 Parcelable[] array = bundle.getParcelableArray(key);
6646 if (array instanceof Notification[] || array == null) {
6647 return (Notification[]) array;
6648 }
6649 Notification[] typedArray = Arrays.copyOf(array, array.length,
6650 Notification[].class);
6651 bundle.putParcelableArray(key, typedArray);
6652 return typedArray;
6653 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02006654
6655 private static class BuilderRemoteViews extends RemoteViews {
6656 public BuilderRemoteViews(Parcel parcel) {
6657 super(parcel);
6658 }
6659
Kenny Guy77320062014-08-27 21:37:15 +01006660 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
6661 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006662 }
6663
6664 @Override
6665 public BuilderRemoteViews clone() {
6666 Parcel p = Parcel.obtain();
6667 writeToParcel(p, 0);
6668 p.setDataPosition(0);
6669 BuilderRemoteViews brv = new BuilderRemoteViews(p);
6670 p.recycle();
6671 return brv;
6672 }
6673 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08006674}