blob: 88603f127148ee6ec1c126b2b987067087e65dc9 [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 Reynolds233a5f92015-10-19 13:51:23 -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;
Selim Cinek65b2e7c2015-10-26 14:11:31 -070028import 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;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010033import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010034import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040035import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040036import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070037import android.media.AudioManager;
Jeff Browndba34ba2014-06-24 20:46:03 -070038import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040040import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020041import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050042import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Parcel;
44import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040045import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070046import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.text.TextUtils;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040048import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050049import android.util.SparseArray;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040050import android.util.TypedValue;
Griff Hazen61a9e862014-05-22 16:05:19 -070051import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080052import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080053import android.view.View;
Jeff Sharkey1c400132011-08-05 14:50:13 -070054import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.widget.RemoteViews;
56
Griff Hazen959591e2014-05-15 22:26:18 -070057import com.android.internal.R;
Griff Hazenc091ba82014-05-16 10:13:26 -070058import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070059
Tor Norbyed9273d62013-05-30 15:59:53 -070060import java.lang.annotation.Retention;
61import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020062import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050063import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070064import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070065import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070066import java.util.List;
Julia Reynolds74303cf2015-10-16 11:37:55 -040067import java.util.Objects;
Dan Sandler50128532015-12-08 15:42:41 -050068import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080069
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070/**
71 * A class that represents how a persistent notification is to be presented to
72 * the user using the {@link android.app.NotificationManager}.
73 *
Joe Onoratocb109a02011-01-18 17:57:41 -080074 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
75 * easier to construct Notifications.</p>
76 *
Joe Fernandez558459f2011-10-13 16:47:36 -070077 * <div class="special reference">
78 * <h3>Developer Guides</h3>
79 * <p>For a guide to creating notifications, read the
80 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
81 * developer guide.</p>
82 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 */
84public class Notification implements Parcelable
85{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040086 private static final String TAG = "Notification";
87
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -040089 * An activity that provides a user interface for adjusting notification preferences for its
90 * containing application. Optional but recommended for apps that post
91 * {@link android.app.Notification Notifications}.
92 */
93 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
94 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
95 = "android.intent.category.NOTIFICATION_PREFERENCES";
96
97 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 * Use all default values (where applicable).
99 */
100 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 /**
103 * Use the default notification sound. This will ignore any given
104 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500105 *
Chris Wren47c20a12014-06-18 17:27:29 -0400106 * <p>
107 * A notification that is noisy is more likely to be presented as a heads-up notification.
108 * </p>
109 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500111 */
112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 public static final int DEFAULT_SOUND = 1;
114
115 /**
116 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500117 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700118 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500119 *
Chris Wren47c20a12014-06-18 17:27:29 -0400120 * <p>
121 * A notification that vibrates is more likely to be presented as a heads-up notification.
122 * </p>
123 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500125 */
126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 /**
130 * Use the default notification lights. This will ignore the
131 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
132 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500133 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500135 */
136
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500138
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200140 * Maximum length of CharSequences accepted by Builder and friends.
141 *
142 * <p>
143 * Avoids spamming the system with overly large strings such as full e-mails.
144 */
145 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
146
147 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800148 * Maximum entries of reply text that are accepted by Builder and friends.
149 */
150 private static final int MAX_REPLY_HISTORY = 5;
151
152 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500153 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800154 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500155 * Default value: {@link System#currentTimeMillis() Now}.
156 *
157 * Choose a timestamp that will be most relevant to the user. For most finite events, this
158 * corresponds to the time the event happened (or will happen, in the case of events that have
159 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800160 * timestamped according to when the activity began.
161 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500162 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800163 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500164 * <ul>
165 * <li>Notification of a new chat message should be stamped when the message was received.</li>
166 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
167 * <li>Notification of a completed file download should be stamped when the download finished.</li>
168 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
169 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
170 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800171 * </ul>
172 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 */
174 public long when;
175
176 /**
177 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400178 *
179 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 */
Dan Sandler86647982015-05-13 23:41:13 -0400181 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700182 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 public int icon;
184
185 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800186 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
187 * leave it at its default value of 0.
188 *
189 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700190 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800191 */
192 public int iconLevel;
193
194 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500195 * The number of events that this notification represents. For example, in a new mail
196 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800197 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500198 * The system may or may not use this field to modify the appearance of the notification. For
199 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
200 * superimposed over the icon in the status bar. Starting with
201 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
202 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800203 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500204 * If the number is 0 or negative, it is never shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 */
206 public int number;
207
208 /**
209 * The intent to execute when the expanded status entry is clicked. If
210 * this is an activity, it must include the
211 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800212 * that you take care of task management as described in the
213 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800214 * Stack</a> document. In particular, make sure to read the notification section
215 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
216 * Notifications</a> for the correct ways to launch an application from a
217 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 */
219 public PendingIntent contentIntent;
220
221 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500222 * The intent to execute when the notification is explicitly dismissed by the user, either with
223 * the "Clear All" button or by swiping it away individually.
224 *
225 * This probably shouldn't be launching an activity since several of those will be sent
226 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 */
228 public PendingIntent deleteIntent;
229
230 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700231 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800232 *
Chris Wren47c20a12014-06-18 17:27:29 -0400233 * <p>
234 * The system UI may choose to display a heads-up notification, instead of
235 * launching this intent, while the user is using the device.
236 * </p>
237 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800238 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400239 */
240 public PendingIntent fullScreenIntent;
241
242 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400243 * Text that summarizes this notification for accessibility services.
244 *
245 * As of the L release, this text is no longer shown on screen, but it is still useful to
246 * accessibility services (where it serves as an audible announcement of the notification's
247 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400248 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800249 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 */
251 public CharSequence tickerText;
252
253 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400254 * Formerly, a view showing the {@link #tickerText}.
255 *
256 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400257 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400258 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800259 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400260
261 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400262 * The view that will represent this notification in the notification list (which is pulled
263 * down from the status bar).
264 *
265 * As of N, this field is not used. The notification view is determined by the inputs to
266 * {@link Notification.Builder}; a custom RemoteViews can optionally be
267 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400269 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800270 public RemoteViews contentView;
271
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400272 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400273 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400274 * opportunity to show more detail. The system UI may choose to show this
275 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400276 *
277 * As of N, this field is not used. The expanded notification view is determined by the
278 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
279 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400280 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400281 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400282 public RemoteViews bigContentView;
283
Chris Wren8fd39ec2014-02-27 17:43:26 -0500284
285 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400286 * A medium-format version of {@link #contentView}, providing the Notification an
287 * opportunity to add action buttons to contentView. At its discretion, the system UI may
288 * choose to show this as a heads-up notification, which will pop up so the user can see
289 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400290 *
291 * As of N, this field is not used. The heads-up notification view is determined by the
292 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
293 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500294 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400295 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500296 public RemoteViews headsUpContentView;
297
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400298 /**
Dan Sandler86647982015-05-13 23:41:13 -0400299 * A large bitmap to be shown in the notification content area.
300 *
301 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 */
Dan Sandler86647982015-05-13 23:41:13 -0400303 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800304 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305
306 /**
307 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500308 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800309 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400310 * A notification that is noisy is more likely to be presented as a heads-up notification.
311 * </p>
312 *
313 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500314 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 * </p>
316 */
317 public Uri sound;
318
319 /**
320 * Use this constant as the value for audioStreamType to request that
321 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700322 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400323 *
324 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700326 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 public static final int STREAM_DEFAULT = -1;
328
329 /**
330 * The audio stream type to use when playing the sound.
331 * Should be one of the STREAM_ constants from
332 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400333 *
334 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800335 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700336 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800337 public int audioStreamType = STREAM_DEFAULT;
338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400340 * The default value of {@link #audioAttributes}.
341 */
342 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
343 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
344 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
345 .build();
346
347 /**
348 * The {@link AudioAttributes audio attributes} to use when playing the sound.
349 */
350 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
351
352 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500353 * The pattern with which to vibrate.
354 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800355 * <p>
356 * To vibrate the default pattern, see {@link #defaults}.
357 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500358 *
Chris Wren47c20a12014-06-18 17:27:29 -0400359 * <p>
360 * A notification that vibrates is more likely to be presented as a heads-up notification.
361 * </p>
362 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800363 * @see android.os.Vibrator#vibrate(long[],int)
364 */
365 public long[] vibrate;
366
367 /**
368 * The color of the led. The hardware will do its best approximation.
369 *
370 * @see #FLAG_SHOW_LIGHTS
371 * @see #flags
372 */
Tor Norbye80756e32015-03-02 09:39:27 -0800373 @ColorInt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 public int ledARGB;
375
376 /**
377 * The number of milliseconds for the LED to be on while it's flashing.
378 * The hardware will do its best approximation.
379 *
380 * @see #FLAG_SHOW_LIGHTS
381 * @see #flags
382 */
383 public int ledOnMS;
384
385 /**
386 * The number of milliseconds for the LED to be off while it's flashing.
387 * The hardware will do its best approximation.
388 *
389 * @see #FLAG_SHOW_LIGHTS
390 * @see #flags
391 */
392 public int ledOffMS;
393
394 /**
395 * Specifies which values should be taken from the defaults.
396 * <p>
397 * To set, OR the desired from {@link #DEFAULT_SOUND},
398 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
399 * values, use {@link #DEFAULT_ALL}.
400 * </p>
401 */
402 public int defaults;
403
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800404 /**
405 * Bit to be bitwise-ored into the {@link #flags} field that should be
406 * set if you want the LED on for this notification.
407 * <ul>
408 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
409 * or 0 for both ledOnMS and ledOffMS.</li>
410 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
411 * <li>To flash the LED, pass the number of milliseconds that it should
412 * be on and off to ledOnMS and ledOffMS.</li>
413 * </ul>
414 * <p>
415 * Since hardware varies, you are not guaranteed that any of the values
416 * you pass are honored exactly. Use the system defaults (TODO) if possible
417 * because they will be set to values that work on any given hardware.
418 * <p>
419 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500420 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 */
422 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
423
424 /**
425 * Bit to be bitwise-ored into the {@link #flags} field that should be
426 * set if this notification is in reference to something that is ongoing,
427 * like a phone call. It should not be set if this notification is in
428 * reference to something that happened at a particular point in time,
429 * like a missed phone call.
430 */
431 public static final int FLAG_ONGOING_EVENT = 0x00000002;
432
433 /**
434 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700435 * the audio will be repeated until the notification is
436 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800437 */
438 public static final int FLAG_INSISTENT = 0x00000004;
439
440 /**
441 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700442 * set if you would only like the sound, vibrate and ticker to be played
443 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 */
445 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
446
447 /**
448 * Bit to be bitwise-ored into the {@link #flags} field that should be
449 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500450 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 */
452 public static final int FLAG_AUTO_CANCEL = 0x00000010;
453
454 /**
455 * Bit to be bitwise-ored into the {@link #flags} field that should be
456 * set if the notification should not be canceled when the user clicks
457 * the Clear all button.
458 */
459 public static final int FLAG_NO_CLEAR = 0x00000020;
460
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700461 /**
462 * Bit to be bitwise-ored into the {@link #flags} field that should be
463 * set if this notification represents a currently running service. This
464 * will normally be set for you by {@link Service#startForeground}.
465 */
466 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
467
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400468 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500469 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800470 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500471 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400472 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500473 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400474
Griff Hazendfcb0802014-02-11 12:00:00 -0800475 /**
476 * Bit to be bitswise-ored into the {@link #flags} field that should be
477 * set if this notification is relevant to the current device only
478 * and it is not recommended that it bridge to other devices.
479 */
480 public static final int FLAG_LOCAL_ONLY = 0x00000100;
481
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700482 /**
483 * Bit to be bitswise-ored into the {@link #flags} field that should be
484 * set if this notification is the group summary for a group of notifications.
485 * Grouped notifications may display in a cluster or stack on devices which
486 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
487 */
488 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
489
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800490 public int flags;
491
Tor Norbyed9273d62013-05-30 15:59:53 -0700492 /** @hide */
493 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
494 @Retention(RetentionPolicy.SOURCE)
495 public @interface Priority {}
496
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800497 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500498 * Default notification {@link #priority}. If your application does not prioritize its own
499 * notifications, use this value for all notifications.
500 */
501 public static final int PRIORITY_DEFAULT = 0;
502
503 /**
504 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
505 * items smaller, or at a different position in the list, compared with your app's
506 * {@link #PRIORITY_DEFAULT} items.
507 */
508 public static final int PRIORITY_LOW = -1;
509
510 /**
511 * Lowest {@link #priority}; these items might not be shown to the user except under special
512 * circumstances, such as detailed notification logs.
513 */
514 public static final int PRIORITY_MIN = -2;
515
516 /**
517 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
518 * show these items larger, or at a different position in notification lists, compared with
519 * your app's {@link #PRIORITY_DEFAULT} items.
520 */
521 public static final int PRIORITY_HIGH = 1;
522
523 /**
524 * Highest {@link #priority}, for your application's most important items that require the
525 * user's prompt attention or input.
526 */
527 public static final int PRIORITY_MAX = 2;
528
529 /**
530 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800531 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500532 * Priority is an indication of how much of the user's valuable attention should be consumed by
533 * this notification. Low-priority notifications may be hidden from the user in certain
534 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500535 * system will make a determination about how to interpret this priority when presenting
536 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400537 *
538 * <p>
539 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
540 * as a heads-up notification.
541 * </p>
542 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500543 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700544 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500545 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800546
Dan Sandler26e81cf2014-05-06 10:01:27 -0400547 /**
548 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
549 * to be applied by the standard Style templates when presenting this notification.
550 *
551 * The current template design constructs a colorful header image by overlaying the
552 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
553 * ignored.
554 */
Tor Norbye80756e32015-03-02 09:39:27 -0800555 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400556 public int color = COLOR_DEFAULT;
557
558 /**
559 * Special value of {@link #color} telling the system not to decorate this notification with
560 * any special color but instead use default colors when presenting this notification.
561 */
Tor Norbye80756e32015-03-02 09:39:27 -0800562 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400563 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600564
565 /**
566 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
567 * the notification's presence and contents in untrusted situations (namely, on the secure
568 * lockscreen).
569 *
570 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
571 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
572 * shown in all situations, but the contents are only available if the device is unlocked for
573 * the appropriate user.
574 *
575 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
576 * can be read even in an "insecure" context (that is, above a secure lockscreen).
577 * To modify the public version of this notification—for example, to redact some portions—see
578 * {@link Builder#setPublicVersion(Notification)}.
579 *
580 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
581 * and ticker until the user has bypassed the lockscreen.
582 */
583 public int visibility;
584
Griff Hazenfc3922d2014-08-20 11:56:44 -0700585 /**
586 * Notification visibility: Show this notification in its entirety on all lockscreens.
587 *
588 * {@see #visibility}
589 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600590 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700591
592 /**
593 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
594 * private information on secure lockscreens.
595 *
596 * {@see #visibility}
597 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600598 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700599
600 /**
601 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
602 *
603 * {@see #visibility}
604 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600605 public static final int VISIBILITY_SECRET = -1;
606
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500607 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400608 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500609 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400610 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500611
612 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400613 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500614 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400615 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500616
617 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400618 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500619 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400620 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500621
622 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400623 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500624 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400625 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500626
627 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400628 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500629 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400630 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500631
632 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400633 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500634 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400635 public static final String CATEGORY_ALARM = "alarm";
636
637 /**
638 * Notification category: progress of a long-running background operation.
639 */
640 public static final String CATEGORY_PROGRESS = "progress";
641
642 /**
643 * Notification category: social network or sharing update.
644 */
645 public static final String CATEGORY_SOCIAL = "social";
646
647 /**
648 * Notification category: error in background operation or authentication status.
649 */
650 public static final String CATEGORY_ERROR = "err";
651
652 /**
653 * Notification category: media transport control for playback.
654 */
655 public static final String CATEGORY_TRANSPORT = "transport";
656
657 /**
658 * Notification category: system or device status update. Reserved for system use.
659 */
660 public static final String CATEGORY_SYSTEM = "sys";
661
662 /**
663 * Notification category: indication of running background service.
664 */
665 public static final String CATEGORY_SERVICE = "service";
666
667 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400668 * Notification category: a specific, timely recommendation for a single thing.
669 * For example, a news app might want to recommend a news story it believes the user will
670 * want to read next.
671 */
672 public static final String CATEGORY_RECOMMENDATION = "recommendation";
673
674 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400675 * Notification category: ongoing information about device or contextual status.
676 */
677 public static final String CATEGORY_STATUS = "status";
678
679 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400680 * Notification category: user-scheduled reminder.
681 */
682 public static final String CATEGORY_REMINDER = "reminder";
683
684 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400685 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
686 * that best describes this Notification. May be used by the system for ranking and filtering.
687 */
688 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500689
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700690 private String mGroupKey;
691
692 /**
693 * Get the key used to group this notification into a cluster or stack
694 * with other notifications on devices which support such rendering.
695 */
696 public String getGroup() {
697 return mGroupKey;
698 }
699
700 private String mSortKey;
701
702 /**
703 * Get a sort key that orders this notification among other notifications from the
704 * same package. This can be useful if an external sort was already applied and an app
705 * would like to preserve this. Notifications will be sorted lexicographically using this
706 * value, although providing different priorities in addition to providing sort key may
707 * cause this value to be ignored.
708 *
709 * <p>This sort key can also be used to order members of a notification group. See
710 * {@link Builder#setGroup}.
711 *
712 * @see String#compareTo(String)
713 */
714 public String getSortKey() {
715 return mSortKey;
716 }
717
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500718 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400719 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400720 * <p>
721 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
722 * APIs, and are intended to be used by
723 * {@link android.service.notification.NotificationListenerService} implementations to extract
724 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500725 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400726 public Bundle extras = new Bundle();
727
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400728 /**
729 * {@link #extras} key: this is the title of the notification,
730 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
731 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500732 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400733
734 /**
735 * {@link #extras} key: this is the title of the notification when shown in expanded form,
736 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
737 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400738 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400739
740 /**
741 * {@link #extras} key: this is the main text payload, as supplied to
742 * {@link Builder#setContentText(CharSequence)}.
743 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500744 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400745
746 /**
747 * {@link #extras} key: this is a third line of text, as supplied to
748 * {@link Builder#setSubText(CharSequence)}.
749 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400750 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400751
752 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800753 * {@link #extras} key: this is the remote input history, as supplied to
754 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
755 */
756 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
757
758 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400759 * {@link #extras} key: this is a small piece of additional text as supplied to
760 * {@link Builder#setContentInfo(CharSequence)}.
761 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400762 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400763
764 /**
765 * {@link #extras} key: this is a line of summary information intended to be shown
766 * alongside expanded notifications, as supplied to (e.g.)
767 * {@link BigTextStyle#setSummaryText(CharSequence)}.
768 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400769 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400770
771 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200772 * {@link #extras} key: this is the longer text shown in the big form of a
773 * {@link BigTextStyle} notification, as supplied to
774 * {@link BigTextStyle#bigText(CharSequence)}.
775 */
776 public static final String EXTRA_BIG_TEXT = "android.bigText";
777
778 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400779 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
780 * supplied to {@link Builder#setSmallIcon(int)}.
781 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500782 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400783
784 /**
785 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
786 * notification payload, as
787 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
788 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400789 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400790
791 /**
792 * {@link #extras} key: this is a bitmap to be used instead of the one from
793 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
794 * shown in its expanded form, as supplied to
795 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
796 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400797 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400798
799 /**
800 * {@link #extras} key: this is the progress value supplied to
801 * {@link Builder#setProgress(int, int, boolean)}.
802 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400803 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400804
805 /**
806 * {@link #extras} key: this is the maximum value supplied to
807 * {@link Builder#setProgress(int, int, boolean)}.
808 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400809 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400810
811 /**
812 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
813 * {@link Builder#setProgress(int, int, boolean)}.
814 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400815 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400816
817 /**
818 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
819 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
820 * {@link Builder#setUsesChronometer(boolean)}.
821 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400822 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400823
824 /**
825 * {@link #extras} key: whether {@link #when} should be shown,
826 * as supplied to {@link Builder#setShowWhen(boolean)}.
827 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400828 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400829
830 /**
831 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
832 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
833 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400834 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400835
836 /**
837 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
838 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
839 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400840 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400841
842 /**
843 * {@link #extras} key: A string representing the name of the specific
844 * {@link android.app.Notification.Style} used to create this notification.
845 */
Chris Wren91ad5632013-06-05 15:05:57 -0400846 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400847
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400848 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400849 * {@link #extras} key: A String array containing the people that this notification relates to,
850 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400851 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400852 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500853
854 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400855 * Allow certain system-generated notifications to appear before the device is provisioned.
856 * Only available to notifications coming from the android package.
857 * @hide
858 */
859 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
860
861 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700862 * {@link #extras} key: A
863 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
864 * in the background when the notification is selected. The URI must point to an image stream
865 * suitable for passing into
866 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
867 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
868 * URI used for this purpose must require no permissions to read the image data.
869 */
870 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
871
872 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400873 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700874 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400875 * {@link android.app.Notification.MediaStyle} notification.
876 */
877 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
878
879 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100880 * {@link #extras} key: the indices of actions to be shown in the compact view,
881 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
882 */
883 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
884
Christoph Studer943aa672014-08-03 20:31:16 +0200885 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100886 * {@link #extras} key: the user that built the notification.
887 *
888 * @hide
889 */
890 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
891
892 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400893 * @hide
894 */
895 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
896
Dan Sandlerd63f9322015-05-06 15:18:49 -0400897 private Icon mSmallIcon;
898 private Icon mLargeIcon;
899
Chris Wren51c75102013-07-16 20:49:17 -0400900 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400901 * Structure to encapsulate a named action that can be shown as part of this notification.
902 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
903 * selected by the user.
904 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700905 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
906 * or {@link Notification.Builder#addAction(Notification.Action)}
907 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400908 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500909 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700910 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -0400911 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -0700912 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700913
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400914 /**
915 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -0400916 *
917 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400918 */
Dan Sandler86647982015-05-13 23:41:13 -0400919 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400920 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700921
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400922 /**
923 * Title of the action.
924 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400925 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700926
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400927 /**
928 * Intent to send when the user invokes this action. May be null, in which case the action
929 * may be rendered in a disabled presentation by the system UI.
930 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400931 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -0700932
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400933 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -0400934 if (in.readInt() != 0) {
935 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -0400936 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
937 icon = mIcon.getResId();
938 }
Dan Sandler86647982015-05-13 23:41:13 -0400939 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400940 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
941 if (in.readInt() == 1) {
942 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
943 }
Griff Hazen959591e2014-05-15 22:26:18 -0700944 mExtras = in.readBundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700945 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400946 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700947
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400948 /**
Dan Sandler86647982015-05-13 23:41:13 -0400949 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400950 */
Dan Sandler86647982015-05-13 23:41:13 -0400951 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400952 public Action(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -0400953 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700954 }
955
Dan Sandler86647982015-05-13 23:41:13 -0400956 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700957 RemoteInput[] remoteInputs) {
Dan Sandler86647982015-05-13 23:41:13 -0400958 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -0400959 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
960 this.icon = icon.getResId();
961 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400962 this.title = title;
963 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -0700964 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700965 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700966 }
967
968 /**
Dan Sandler86647982015-05-13 23:41:13 -0400969 * Return an icon representing the action.
970 */
971 public Icon getIcon() {
972 if (mIcon == null && icon != 0) {
973 // you snuck an icon in here without using the builder; let's try to keep it
974 mIcon = Icon.createWithResource("", icon);
975 }
976 return mIcon;
977 }
978
979 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700980 * Get additional metadata carried around with this Action.
981 */
982 public Bundle getExtras() {
983 return mExtras;
984 }
985
986 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700987 * Get the list of inputs to be collected from the user when this action is sent.
988 * May return null if no remote inputs were added.
989 */
990 public RemoteInput[] getRemoteInputs() {
991 return mRemoteInputs;
992 }
993
994 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700995 * Builder class for {@link Action} objects.
996 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700997 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -0400998 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -0700999 private final CharSequence mTitle;
1000 private final PendingIntent mIntent;
1001 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001002 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001003
1004 /**
1005 * Construct a new builder for {@link Action} object.
1006 * @param icon icon to show for this action
1007 * @param title the title of the action
1008 * @param intent the {@link PendingIntent} to fire when users trigger this action
1009 */
Dan Sandler86647982015-05-13 23:41:13 -04001010 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001011 public Builder(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -04001012 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
1013 }
1014
1015 /**
1016 * Construct a new builder for {@link Action} object.
1017 * @param icon icon to show for this action
1018 * @param title the title of the action
1019 * @param intent the {@link PendingIntent} to fire when users trigger this action
1020 */
1021 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001022 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -07001023 }
1024
1025 /**
1026 * Construct a new builder for {@link Action} object using the fields from an
1027 * {@link Action}.
1028 * @param action the action to read fields from.
1029 */
1030 public Builder(Action action) {
Dan Sandler86647982015-05-13 23:41:13 -04001031 this(action.getIcon(), action.title, action.actionIntent, new Bundle(action.mExtras),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001032 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -07001033 }
1034
Dan Sandler86647982015-05-13 23:41:13 -04001035 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001036 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -07001037 mIcon = icon;
1038 mTitle = title;
1039 mIntent = intent;
1040 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001041 if (remoteInputs != null) {
1042 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1043 Collections.addAll(mRemoteInputs, remoteInputs);
1044 }
Griff Hazen959591e2014-05-15 22:26:18 -07001045 }
1046
1047 /**
1048 * Merge additional metadata into this builder.
1049 *
1050 * <p>Values within the Bundle will replace existing extras values in this Builder.
1051 *
1052 * @see Notification.Action#extras
1053 */
1054 public Builder addExtras(Bundle extras) {
1055 if (extras != null) {
1056 mExtras.putAll(extras);
1057 }
1058 return this;
1059 }
1060
1061 /**
1062 * Get the metadata Bundle used by this Builder.
1063 *
1064 * <p>The returned Bundle is shared with this Builder.
1065 */
1066 public Bundle getExtras() {
1067 return mExtras;
1068 }
1069
1070 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001071 * Add an input to be collected from the user when this action is sent.
1072 * Response values can be retrieved from the fired intent by using the
1073 * {@link RemoteInput#getResultsFromIntent} function.
1074 * @param remoteInput a {@link RemoteInput} to add to the action
1075 * @return this object for method chaining
1076 */
1077 public Builder addRemoteInput(RemoteInput remoteInput) {
1078 if (mRemoteInputs == null) {
1079 mRemoteInputs = new ArrayList<RemoteInput>();
1080 }
1081 mRemoteInputs.add(remoteInput);
1082 return this;
1083 }
1084
1085 /**
1086 * Apply an extender to this action builder. Extenders may be used to add
1087 * metadata or change options on this builder.
1088 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001089 public Builder extend(Extender extender) {
1090 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001091 return this;
1092 }
1093
1094 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001095 * Combine all of the options that have been set and return a new {@link Action}
1096 * object.
1097 * @return the built action
1098 */
1099 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001100 RemoteInput[] remoteInputs = mRemoteInputs != null
1101 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
1102 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -07001103 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001104 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001105
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001106 @Override
1107 public Action clone() {
1108 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001109 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001110 title,
1111 actionIntent, // safe to alias
1112 new Bundle(mExtras),
1113 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001114 }
1115 @Override
1116 public int describeContents() {
1117 return 0;
1118 }
1119 @Override
1120 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001121 final Icon ic = getIcon();
1122 if (ic != null) {
1123 out.writeInt(1);
1124 ic.writeToParcel(out, 0);
1125 } else {
1126 out.writeInt(0);
1127 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001128 TextUtils.writeToParcel(title, out, flags);
1129 if (actionIntent != null) {
1130 out.writeInt(1);
1131 actionIntent.writeToParcel(out, flags);
1132 } else {
1133 out.writeInt(0);
1134 }
Griff Hazen959591e2014-05-15 22:26:18 -07001135 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001136 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001137 }
Griff Hazen959591e2014-05-15 22:26:18 -07001138 public static final Parcelable.Creator<Action> CREATOR =
1139 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001140 public Action createFromParcel(Parcel in) {
1141 return new Action(in);
1142 }
1143 public Action[] newArray(int size) {
1144 return new Action[size];
1145 }
1146 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001147
1148 /**
1149 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1150 * metadata or change options on an action builder.
1151 */
1152 public interface Extender {
1153 /**
1154 * Apply this extender to a notification action builder.
1155 * @param builder the builder to be modified.
1156 * @return the build object for chaining.
1157 */
1158 public Builder extend(Builder builder);
1159 }
1160
1161 /**
1162 * Wearable extender for notification actions. To add extensions to an action,
1163 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1164 * the {@code WearableExtender()} constructor and apply it to a
1165 * {@link android.app.Notification.Action.Builder} using
1166 * {@link android.app.Notification.Action.Builder#extend}.
1167 *
1168 * <pre class="prettyprint">
1169 * Notification.Action action = new Notification.Action.Builder(
1170 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001171 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001172 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001173 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001174 */
1175 public static final class WearableExtender implements Extender {
1176 /** Notification action extra which contains wearable extensions */
1177 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1178
Pete Gastaf6781d2014-10-07 15:17:05 -04001179 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001180 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001181 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1182 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1183 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001184
1185 // Flags bitwise-ored to mFlags
1186 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
1187
1188 // Default value for flags integer
1189 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1190
1191 private int mFlags = DEFAULT_FLAGS;
1192
Pete Gastaf6781d2014-10-07 15:17:05 -04001193 private CharSequence mInProgressLabel;
1194 private CharSequence mConfirmLabel;
1195 private CharSequence mCancelLabel;
1196
Griff Hazen61a9e862014-05-22 16:05:19 -07001197 /**
1198 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1199 * options.
1200 */
1201 public WearableExtender() {
1202 }
1203
1204 /**
1205 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1206 * wearable options present in an existing notification action.
1207 * @param action the notification action to inspect.
1208 */
1209 public WearableExtender(Action action) {
1210 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1211 if (wearableBundle != null) {
1212 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001213 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1214 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1215 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001216 }
1217 }
1218
1219 /**
1220 * Apply wearable extensions to a notification action that is being built. This is
1221 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1222 * method of {@link android.app.Notification.Action.Builder}.
1223 */
1224 @Override
1225 public Action.Builder extend(Action.Builder builder) {
1226 Bundle wearableBundle = new Bundle();
1227
1228 if (mFlags != DEFAULT_FLAGS) {
1229 wearableBundle.putInt(KEY_FLAGS, mFlags);
1230 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001231 if (mInProgressLabel != null) {
1232 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1233 }
1234 if (mConfirmLabel != null) {
1235 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1236 }
1237 if (mCancelLabel != null) {
1238 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1239 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001240
1241 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1242 return builder;
1243 }
1244
1245 @Override
1246 public WearableExtender clone() {
1247 WearableExtender that = new WearableExtender();
1248 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001249 that.mInProgressLabel = this.mInProgressLabel;
1250 that.mConfirmLabel = this.mConfirmLabel;
1251 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001252 return that;
1253 }
1254
1255 /**
1256 * Set whether this action is available when the wearable device is not connected to
1257 * a companion device. The user can still trigger this action when the wearable device is
1258 * offline, but a visual hint will indicate that the action may not be available.
1259 * Defaults to true.
1260 */
1261 public WearableExtender setAvailableOffline(boolean availableOffline) {
1262 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1263 return this;
1264 }
1265
1266 /**
1267 * Get whether this action is available when the wearable device is not connected to
1268 * a companion device. The user can still trigger this action when the wearable device is
1269 * offline, but a visual hint will indicate that the action may not be available.
1270 * Defaults to true.
1271 */
1272 public boolean isAvailableOffline() {
1273 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1274 }
1275
1276 private void setFlag(int mask, boolean value) {
1277 if (value) {
1278 mFlags |= mask;
1279 } else {
1280 mFlags &= ~mask;
1281 }
1282 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001283
1284 /**
1285 * Set a label to display while the wearable is preparing to automatically execute the
1286 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1287 *
1288 * @param label the label to display while the action is being prepared to execute
1289 * @return this object for method chaining
1290 */
1291 public WearableExtender setInProgressLabel(CharSequence label) {
1292 mInProgressLabel = label;
1293 return this;
1294 }
1295
1296 /**
1297 * Get the label to display while the wearable is preparing to automatically execute
1298 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1299 *
1300 * @return the label to display while the action is being prepared to execute
1301 */
1302 public CharSequence getInProgressLabel() {
1303 return mInProgressLabel;
1304 }
1305
1306 /**
1307 * Set a label to display to confirm that the action should be executed.
1308 * This is usually an imperative verb like "Send".
1309 *
1310 * @param label the label to confirm the action should be executed
1311 * @return this object for method chaining
1312 */
1313 public WearableExtender setConfirmLabel(CharSequence label) {
1314 mConfirmLabel = label;
1315 return this;
1316 }
1317
1318 /**
1319 * Get the label to display to confirm that the action should be executed.
1320 * This is usually an imperative verb like "Send".
1321 *
1322 * @return the label to confirm the action should be executed
1323 */
1324 public CharSequence getConfirmLabel() {
1325 return mConfirmLabel;
1326 }
1327
1328 /**
1329 * Set a label to display to cancel the action.
1330 * This is usually an imperative verb, like "Cancel".
1331 *
1332 * @param label the label to display to cancel the action
1333 * @return this object for method chaining
1334 */
1335 public WearableExtender setCancelLabel(CharSequence label) {
1336 mCancelLabel = label;
1337 return this;
1338 }
1339
1340 /**
1341 * Get the label to display to cancel the action.
1342 * This is usually an imperative verb like "Cancel".
1343 *
1344 * @return the label to display to cancel the action
1345 */
1346 public CharSequence getCancelLabel() {
1347 return mCancelLabel;
1348 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001349 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001350 }
1351
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001352 /**
1353 * Array of all {@link Action} structures attached to this notification by
1354 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1355 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1356 * interface for invoking actions.
1357 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001358 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001359
1360 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001361 * Replacement version of this notification whose content will be shown
1362 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1363 * and {@link #VISIBILITY_PUBLIC}.
1364 */
1365 public Notification publicVersion;
1366
1367 /**
Julia Reynolds74303cf2015-10-16 11:37:55 -04001368 * Structure to encapsulate a topic that is shown in Notification settings.
1369 * It must include an id and label.
1370 */
1371 public static class Topic implements Parcelable {
1372 private final String id;
1373 private final CharSequence label;
1374
1375 public Topic(String id, CharSequence label) {
1376 this.id = id;
1377 this.label = safeCharSequence(label);
1378 }
1379
1380 private Topic(Parcel in) {
1381 if (in.readInt() != 0) {
1382 id = in.readString();
1383 } else {
1384 id = null;
1385 }
1386 label = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1387 }
1388
1389 public String getId() {
1390 return id;
1391 }
1392
1393 public CharSequence getLabel() {
1394 return label;
1395 }
1396
1397 @Override
1398 public String toString() {
1399 return new StringBuilder(Topic.class.getSimpleName()).append('[')
1400 .append("id=").append(id)
1401 .append(",label=").append(label)
1402 .append(']').toString();
1403 }
1404
1405 @Override
1406 public boolean equals(Object o) {
1407 if (!(o instanceof Topic)) return false;
1408 if (o == this) return true;
1409 final Topic other = (Topic) o;
1410 return Objects.equals(other.id, id)
1411 && Objects.equals(other.label, label);
1412 }
1413
1414 @Override
1415 public int hashCode() {
1416 return Objects.hash(id, label);
1417 }
1418
1419 @Override
1420 public Topic clone() {
1421 return new Topic(id, label);
1422 }
1423
1424 @Override
1425 public int describeContents() {
1426 return 0;
1427 }
1428
1429 @Override
1430 public void writeToParcel(Parcel out, int flags) {
1431 if (id != null) {
1432 out.writeInt(1);
1433 out.writeString(id);
1434 } else {
1435 out.writeInt(0);
1436 }
1437 TextUtils.writeToParcel(label, out, flags);
1438 }
1439 public static final Parcelable.Creator<Topic> CREATOR =
1440 new Parcelable.Creator<Topic>() {
1441 public Topic createFromParcel(Parcel in) {
1442 return new Topic(in);
1443 }
1444 public Topic[] newArray(int size) {
1445 return new Topic[size];
1446 }
1447 };
1448 }
1449
Julia Reynolds233a5f92015-10-19 13:51:23 -04001450 @SystemApi
1451 public static final String TOPIC_DEFAULT = "system_default_topic";
1452
Julia Reynolds054c5dc2015-11-17 15:36:30 -05001453 private Topic topic;
Julia Reynolds74303cf2015-10-16 11:37:55 -04001454
Julia Reynolds054c5dc2015-11-17 15:36:30 -05001455 public Topic getTopic() {
1456 return topic;
Julia Reynolds74303cf2015-10-16 11:37:55 -04001457 }
1458
1459 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001460 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001461 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001462 */
1463 public Notification()
1464 {
1465 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001466 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001467 }
1468
1469 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001470 * @hide
1471 */
1472 public Notification(Context context, int icon, CharSequence tickerText, long when,
1473 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1474 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001475 new Builder(context)
1476 .setWhen(when)
1477 .setSmallIcon(icon)
1478 .setTicker(tickerText)
1479 .setContentTitle(contentTitle)
1480 .setContentText(contentText)
1481 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1482 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001483 }
1484
1485 /**
1486 * Constructs a Notification object with the information needed to
1487 * have a status bar icon without the standard expanded view.
1488 *
1489 * @param icon The resource id of the icon to put in the status bar.
1490 * @param tickerText The text that flows by in the status bar when the notification first
1491 * activates.
1492 * @param when The time to show in the time field. In the System.currentTimeMillis
1493 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001494 *
1495 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001496 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001497 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001498 public Notification(int icon, CharSequence tickerText, long when)
1499 {
1500 this.icon = icon;
1501 this.tickerText = tickerText;
1502 this.when = when;
1503 }
1504
1505 /**
1506 * Unflatten the notification from a parcel.
1507 */
1508 public Notification(Parcel parcel)
1509 {
1510 int version = parcel.readInt();
1511
1512 when = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001513 if (parcel.readInt() != 0) {
1514 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001515 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1516 icon = mSmallIcon.getResId();
1517 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001518 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 number = parcel.readInt();
1520 if (parcel.readInt() != 0) {
1521 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1522 }
1523 if (parcel.readInt() != 0) {
1524 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1525 }
1526 if (parcel.readInt() != 0) {
1527 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1528 }
1529 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001530 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001531 }
1532 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001533 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1534 }
Joe Onorato561d3852010-11-20 18:09:34 -08001535 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001536 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001537 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001538 defaults = parcel.readInt();
1539 flags = parcel.readInt();
1540 if (parcel.readInt() != 0) {
1541 sound = Uri.CREATOR.createFromParcel(parcel);
1542 }
1543
1544 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001545 if (parcel.readInt() != 0) {
1546 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1547 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001548 vibrate = parcel.createLongArray();
1549 ledARGB = parcel.readInt();
1550 ledOnMS = parcel.readInt();
1551 ledOffMS = parcel.readInt();
1552 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001553
1554 if (parcel.readInt() != 0) {
1555 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1556 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001557
1558 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001559
John Spurlockfd7f1e02014-03-18 16:41:57 -04001560 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001561
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001562 mGroupKey = parcel.readString();
1563
1564 mSortKey = parcel.readString();
1565
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001566 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001567
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001568 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1569
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001570 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001571 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1572 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001573
Chris Wren8fd39ec2014-02-27 17:43:26 -05001574 if (parcel.readInt() != 0) {
1575 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1576 }
1577
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001578 visibility = parcel.readInt();
1579
1580 if (parcel.readInt() != 0) {
1581 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1582 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001583
1584 color = parcel.readInt();
Julia Reynolds74303cf2015-10-16 11:37:55 -04001585
Julia Reynolds054c5dc2015-11-17 15:36:30 -05001586 if (parcel.readInt() != 0) {
1587 topic = Topic.CREATOR.createFromParcel(parcel);
1588 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 }
1590
Andy Stadler110988c2010-12-03 14:29:16 -08001591 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001592 public Notification clone() {
1593 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001594 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001595 return that;
1596 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001597
Daniel Sandler1a497d32013-04-18 14:52:45 -04001598 /**
1599 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1600 * of this into that.
1601 * @hide
1602 */
1603 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001604 that.when = this.when;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001605 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001606 that.number = this.number;
1607
1608 // PendingIntents are global, so there's no reason (or way) to clone them.
1609 that.contentIntent = this.contentIntent;
1610 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001611 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001612
1613 if (this.tickerText != null) {
1614 that.tickerText = this.tickerText.toString();
1615 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001616 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001617 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001618 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001619 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001620 that.contentView = this.contentView.clone();
1621 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001622 if (heavy && this.mLargeIcon != null) {
1623 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001624 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001625 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001626 that.sound = this.sound; // android.net.Uri is immutable
1627 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001628 if (this.audioAttributes != null) {
1629 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1630 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001631
1632 final long[] vibrate = this.vibrate;
1633 if (vibrate != null) {
1634 final int N = vibrate.length;
1635 final long[] vib = that.vibrate = new long[N];
1636 System.arraycopy(vibrate, 0, vib, 0, N);
1637 }
1638
1639 that.ledARGB = this.ledARGB;
1640 that.ledOnMS = this.ledOnMS;
1641 that.ledOffMS = this.ledOffMS;
1642 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001643
Joe Onorato18e69df2010-05-17 22:26:12 -07001644 that.flags = this.flags;
1645
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001646 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001647
John Spurlockfd7f1e02014-03-18 16:41:57 -04001648 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001649
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001650 that.mGroupKey = this.mGroupKey;
1651
1652 that.mSortKey = this.mSortKey;
1653
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001654 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001655 try {
1656 that.extras = new Bundle(this.extras);
1657 // will unparcel
1658 that.extras.size();
1659 } catch (BadParcelableException e) {
1660 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1661 that.extras = null;
1662 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001663 }
1664
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001665 if (this.actions != null) {
1666 that.actions = new Action[this.actions.length];
1667 for(int i=0; i<this.actions.length; i++) {
1668 that.actions[i] = this.actions[i].clone();
1669 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001670 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001671
Daniel Sandler1a497d32013-04-18 14:52:45 -04001672 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001673 that.bigContentView = this.bigContentView.clone();
1674 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001675
Chris Wren8fd39ec2014-02-27 17:43:26 -05001676 if (heavy && this.headsUpContentView != null) {
1677 that.headsUpContentView = this.headsUpContentView.clone();
1678 }
1679
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001680 that.visibility = this.visibility;
1681
1682 if (this.publicVersion != null) {
1683 that.publicVersion = new Notification();
1684 this.publicVersion.cloneInto(that.publicVersion, heavy);
1685 }
1686
Dan Sandler26e81cf2014-05-06 10:01:27 -04001687 that.color = this.color;
1688
Julia Reynolds054c5dc2015-11-17 15:36:30 -05001689 if (this.topic != null) {
1690 that.topic = this.topic.clone();
Julia Reynolds74303cf2015-10-16 11:37:55 -04001691 }
1692
Daniel Sandler1a497d32013-04-18 14:52:45 -04001693 if (!heavy) {
1694 that.lightenPayload(); // will clean out extras
1695 }
1696 }
1697
1698 /**
1699 * Removes heavyweight parts of the Notification object for archival or for sending to
1700 * listeners when the full contents are not necessary.
1701 * @hide
1702 */
1703 public final void lightenPayload() {
1704 tickerView = null;
1705 contentView = null;
1706 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001707 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001708 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05001709 if (extras != null && !extras.isEmpty()) {
1710 final Set<String> keyset = extras.keySet();
1711 final int N = keyset.size();
1712 final String[] keys = keyset.toArray(new String[N]);
1713 for (int i=0; i<N; i++) {
1714 final String key = keys[i];
1715 final Object obj = extras.get(key);
1716 if (obj != null &&
1717 ( obj instanceof Parcelable
1718 || obj instanceof Parcelable[]
1719 || obj instanceof SparseArray
1720 || obj instanceof ArrayList)) {
1721 extras.remove(key);
1722 }
1723 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001724 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001725 }
1726
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001727 /**
1728 * Make sure this CharSequence is safe to put into a bundle, which basically
1729 * means it had better not be some custom Parcelable implementation.
1730 * @hide
1731 */
1732 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001733 if (cs == null) return cs;
1734 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1735 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1736 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001737 if (cs instanceof Parcelable) {
1738 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1739 + " instance is a custom Parcelable and not allowed in Notification");
1740 return cs.toString();
1741 }
1742
1743 return cs;
1744 }
1745
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001746 public int describeContents() {
1747 return 0;
1748 }
1749
1750 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001751 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 */
1753 public void writeToParcel(Parcel parcel, int flags)
1754 {
1755 parcel.writeInt(1);
1756
1757 parcel.writeLong(when);
Dan Sandler4e787062015-06-17 15:09:48 -04001758 if (mSmallIcon == null && icon != 0) {
1759 // you snuck an icon in here without using the builder; let's try to keep it
1760 mSmallIcon = Icon.createWithResource("", icon);
1761 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001762 if (mSmallIcon != null) {
1763 parcel.writeInt(1);
1764 mSmallIcon.writeToParcel(parcel, 0);
1765 } else {
1766 parcel.writeInt(0);
1767 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001768 parcel.writeInt(number);
1769 if (contentIntent != null) {
1770 parcel.writeInt(1);
1771 contentIntent.writeToParcel(parcel, 0);
1772 } else {
1773 parcel.writeInt(0);
1774 }
1775 if (deleteIntent != null) {
1776 parcel.writeInt(1);
1777 deleteIntent.writeToParcel(parcel, 0);
1778 } else {
1779 parcel.writeInt(0);
1780 }
1781 if (tickerText != null) {
1782 parcel.writeInt(1);
1783 TextUtils.writeToParcel(tickerText, parcel, flags);
1784 } else {
1785 parcel.writeInt(0);
1786 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001787 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001788 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001789 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001790 } else {
1791 parcel.writeInt(0);
1792 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 if (contentView != null) {
1794 parcel.writeInt(1);
1795 contentView.writeToParcel(parcel, 0);
1796 } else {
1797 parcel.writeInt(0);
1798 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001799 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001800 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001801 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001802 } else {
1803 parcel.writeInt(0);
1804 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805
1806 parcel.writeInt(defaults);
1807 parcel.writeInt(this.flags);
1808
1809 if (sound != null) {
1810 parcel.writeInt(1);
1811 sound.writeToParcel(parcel, 0);
1812 } else {
1813 parcel.writeInt(0);
1814 }
1815 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001816
1817 if (audioAttributes != null) {
1818 parcel.writeInt(1);
1819 audioAttributes.writeToParcel(parcel, 0);
1820 } else {
1821 parcel.writeInt(0);
1822 }
1823
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 parcel.writeLongArray(vibrate);
1825 parcel.writeInt(ledARGB);
1826 parcel.writeInt(ledOnMS);
1827 parcel.writeInt(ledOffMS);
1828 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001829
1830 if (fullScreenIntent != null) {
1831 parcel.writeInt(1);
1832 fullScreenIntent.writeToParcel(parcel, 0);
1833 } else {
1834 parcel.writeInt(0);
1835 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001836
1837 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001838
John Spurlockfd7f1e02014-03-18 16:41:57 -04001839 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001840
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001841 parcel.writeString(mGroupKey);
1842
1843 parcel.writeString(mSortKey);
1844
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001845 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001846
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001847 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001848
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001849 if (bigContentView != null) {
1850 parcel.writeInt(1);
1851 bigContentView.writeToParcel(parcel, 0);
1852 } else {
1853 parcel.writeInt(0);
1854 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001855
Chris Wren8fd39ec2014-02-27 17:43:26 -05001856 if (headsUpContentView != null) {
1857 parcel.writeInt(1);
1858 headsUpContentView.writeToParcel(parcel, 0);
1859 } else {
1860 parcel.writeInt(0);
1861 }
1862
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001863 parcel.writeInt(visibility);
1864
1865 if (publicVersion != null) {
1866 parcel.writeInt(1);
1867 publicVersion.writeToParcel(parcel, 0);
1868 } else {
1869 parcel.writeInt(0);
1870 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001871
1872 parcel.writeInt(color);
Julia Reynolds74303cf2015-10-16 11:37:55 -04001873
Julia Reynolds054c5dc2015-11-17 15:36:30 -05001874 if (topic != null) {
1875 parcel.writeInt(1);
1876 topic.writeToParcel(parcel, 0);
1877 } else {
1878 parcel.writeInt(0);
1879 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 }
1881
1882 /**
1883 * Parcelable.Creator that instantiates Notification objects
1884 */
1885 public static final Parcelable.Creator<Notification> CREATOR
1886 = new Parcelable.Creator<Notification>()
1887 {
1888 public Notification createFromParcel(Parcel parcel)
1889 {
1890 return new Notification(parcel);
1891 }
1892
1893 public Notification[] newArray(int size)
1894 {
1895 return new Notification[size];
1896 }
1897 };
1898
1899 /**
1900 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1901 * layout.
1902 *
1903 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1904 * in the view.</p>
1905 * @param context The context for your application / activity.
1906 * @param contentTitle The title that goes in the expanded entry.
1907 * @param contentText The text that goes in the expanded entry.
1908 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1909 * If this is an activity, it must include the
1910 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001911 * that you take care of task management as described in the
1912 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1913 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001914 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001915 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04001916 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001917 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001918 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001919 public void setLatestEventInfo(Context context,
1920 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001921 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
1922 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
1923 new Throwable());
1924 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001925
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001926 // ensure that any information already set directly is preserved
1927 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001928
1929 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001930 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001931 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001932 }
1933 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001934 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001935 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001936 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001937
1938 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001939 }
1940
Julia Reynoldsda303542015-11-23 14:00:20 -05001941 /**
1942 * @hide
1943 */
1944 public static void addFieldsFromContext(Context context, Notification notification) {
1945 if (notification.extras.getParcelable(EXTRA_BUILDER_APPLICATION_INFO) == null) {
1946 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO,
1947 context.getApplicationInfo());
1948 }
1949 if (!notification.extras.containsKey(EXTRA_ORIGINATING_USERID)) {
1950 notification.extras.putInt(EXTRA_ORIGINATING_USERID, context.getUserId());
1951 }
1952 }
1953
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001954 @Override
1955 public String toString() {
1956 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001957 sb.append("Notification(pri=");
1958 sb.append(priority);
1959 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001960 if (contentView != null) {
1961 sb.append(contentView.getPackage());
1962 sb.append("/0x");
1963 sb.append(Integer.toHexString(contentView.getLayoutId()));
1964 } else {
1965 sb.append("null");
1966 }
1967 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001968 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1969 sb.append("default");
1970 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 int N = this.vibrate.length-1;
1972 sb.append("[");
1973 for (int i=0; i<N; i++) {
1974 sb.append(this.vibrate[i]);
1975 sb.append(',');
1976 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001977 if (N != -1) {
1978 sb.append(this.vibrate[N]);
1979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 } else {
1982 sb.append("null");
1983 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001984 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001985 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001987 } else if (this.sound != null) {
1988 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001989 } else {
1990 sb.append("null");
1991 }
Chris Wren365b6d32015-07-16 10:39:26 -04001992 if (this.tickerText != null) {
1993 sb.append(" tick");
1994 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001995 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001996 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001997 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001998 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04001999 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002000 if (this.category != null) {
2001 sb.append(" category=");
2002 sb.append(this.category);
2003 }
2004 if (this.mGroupKey != null) {
2005 sb.append(" groupKey=");
2006 sb.append(this.mGroupKey);
2007 }
2008 if (this.mSortKey != null) {
2009 sb.append(" sortKey=");
2010 sb.append(this.mSortKey);
2011 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002012 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002013 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002014 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002015 }
2016 sb.append(" vis=");
2017 sb.append(visibilityToString(this.visibility));
2018 if (this.publicVersion != null) {
2019 sb.append(" publicVersion=");
2020 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002021 }
Julia Reynolds054c5dc2015-11-17 15:36:30 -05002022 if (topic != null) {
2023 sb.append("topic=");
2024 sb.append(topic.toString());
Julia Reynolds74303cf2015-10-16 11:37:55 -04002025 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002026 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002027 return sb.toString();
2028 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002029
Dan Sandler1b718782014-07-18 12:43:45 -04002030 /**
2031 * {@hide}
2032 */
2033 public static String visibilityToString(int vis) {
2034 switch (vis) {
2035 case VISIBILITY_PRIVATE:
2036 return "PRIVATE";
2037 case VISIBILITY_PUBLIC:
2038 return "PUBLIC";
2039 case VISIBILITY_SECRET:
2040 return "SECRET";
2041 default:
2042 return "UNKNOWN(" + String.valueOf(vis) + ")";
2043 }
2044 }
2045
Joe Onoratocb109a02011-01-18 17:57:41 -08002046 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002047 * {@hide}
2048 */
2049 public static String priorityToString(@Priority int pri) {
2050 switch (pri) {
2051 case PRIORITY_MIN:
2052 return "MIN";
2053 case PRIORITY_LOW:
2054 return "LOW";
2055 case PRIORITY_DEFAULT:
2056 return "DEFAULT";
2057 case PRIORITY_HIGH:
2058 return "HIGH";
2059 case PRIORITY_MAX:
2060 return "MAX";
2061 default:
2062 return "UNKNOWN(" + String.valueOf(pri) + ")";
2063 }
2064 }
2065
2066 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002067 * The small icon representing this notification in the status bar and content view.
2068 *
2069 * @return the small icon representing this notification.
2070 *
2071 * @see Builder#getSmallIcon()
2072 * @see Builder#setSmallIcon(Icon)
2073 */
2074 public Icon getSmallIcon() {
2075 return mSmallIcon;
2076 }
2077
2078 /**
2079 * Used when notifying to clean up legacy small icons.
2080 * @hide
2081 */
2082 public void setSmallIcon(Icon icon) {
2083 mSmallIcon = icon;
2084 }
2085
2086 /**
2087 * The large icon shown in this notification's content view.
2088 * @see Builder#getLargeIcon()
2089 * @see Builder#setLargeIcon(Icon)
2090 */
2091 public Icon getLargeIcon() {
2092 return mLargeIcon;
2093 }
2094
2095 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002096 * @hide
2097 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002098 public boolean isGroupSummary() {
2099 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2100 }
2101
2102 /**
2103 * @hide
2104 */
2105 public boolean isGroupChild() {
2106 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2107 }
2108
2109 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002110 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002111 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002112 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002113 * content views using the platform's notification layout template. If your app supports
2114 * versions of Android as old as API level 4, you can instead use
2115 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2116 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2117 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002118 *
Scott Main183bf112012-08-13 19:12:13 -07002119 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002120 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002121 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002122 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002123 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2124 * .setContentText(subject)
2125 * .setSmallIcon(R.drawable.new_mail)
2126 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002127 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002128 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002129 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002130 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002131 private static final int MAX_ACTION_BUTTONS = 3;
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002132 private static final float LARGE_TEXT_SCALE = 1.3f;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002133
Joe Onorato46439ce2010-11-19 13:56:21 -08002134 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002135 private Notification mN;
2136 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002137 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002138 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2139 private ArrayList<String> mPersonList = new ArrayList<String>();
2140 private NotificationColorUtil mColorUtil;
2141 private boolean mColorUtilInited = false;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002142
2143 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002144 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002145 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002146
2147 * <table>
2148 * <tr><th align=right>priority</th>
2149 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2150 * <tr><th align=right>when</th>
2151 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2152 * <tr><th align=right>audio stream</th>
2153 * <td>{@link #STREAM_DEFAULT}</td></tr>
2154 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002155 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002156
2157 * @param context
2158 * A {@link Context} that will be used by the Builder to construct the
2159 * RemoteViews. The Context will not be held past the lifetime of this Builder
2160 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002161 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002162 public Builder(Context context) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002163 this(context, null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002164 }
2165
Joe Onoratocb109a02011-01-18 17:57:41 -08002166 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002167 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002168 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002169 public Builder(Context context, Notification toAdopt) {
2170 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002171
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002172 if (toAdopt == null) {
2173 mN = new Notification();
2174 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2175 mN.priority = PRIORITY_DEFAULT;
2176 mN.visibility = VISIBILITY_PRIVATE;
2177 } else {
2178 mN = toAdopt;
2179 if (mN.actions != null) {
2180 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002181 }
2182
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002183 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2184 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2185 }
2186
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002187 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2188 if (!TextUtils.isEmpty(templateClass)) {
2189 final Class<? extends Style> styleClass
2190 = getNotificationStyleClass(templateClass);
2191 if (styleClass == null) {
2192 Log.d(TAG, "Unknown style class: " + templateClass);
2193 } else {
2194 try {
2195 final Constructor<? extends Style> ctor = styleClass.getConstructor();
2196 ctor.setAccessible(true);
2197 final Style style = ctor.newInstance();
2198 style.restoreFromExtras(mN.extras);
2199
2200 if (style != null) {
2201 setStyle(style);
2202 }
2203 } catch (Throwable t) {
2204 Log.e(TAG, "Could not create Style", t);
2205 }
2206 }
2207 }
2208
2209 }
2210 }
2211
2212 private NotificationColorUtil getColorUtil() {
2213 if (!mColorUtilInited) {
2214 mColorUtilInited = true;
2215 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
2216 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002217 }
2218 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002219 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002220 }
2221
2222 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002223 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07002224 * It will be shown in the notification content view by default; use
Griff Hazen50c11652014-05-16 09:46:31 -07002225 * {@link #setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002226 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002227 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002228 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002229 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002230 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002231 return this;
2232 }
2233
Joe Onoratocb109a02011-01-18 17:57:41 -08002234 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002235 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002236 * in the content view.
2237 */
2238 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002239 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002240 return this;
2241 }
2242
2243 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002244 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002245 *
2246 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002247 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002248 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002249 * Useful when showing an elapsed time (like an ongoing phone call).
2250 *
2251 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002252 * @see Notification#when
2253 */
2254 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002255 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002256 return this;
2257 }
2258
2259 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002260 * Set the small icon resource, which will be used to represent the notification in the
2261 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002262 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002263
2264 * The platform template for the expanded view will draw this icon in the left, unless a
2265 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2266 * icon will be moved to the right-hand side.
2267 *
2268
2269 * @param icon
2270 * A resource ID in the application's package of the drawable to use.
2271 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002272 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002273 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002274 return setSmallIcon(icon != 0
2275 ? Icon.createWithResource(mContext, icon)
2276 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002277 }
2278
Joe Onoratocb109a02011-01-18 17:57:41 -08002279 /**
2280 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2281 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2282 * LevelListDrawable}.
2283 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002284 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002285 * @param level The level to use for the icon.
2286 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002287 * @see Notification#icon
2288 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002289 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002290 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002291 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002292 return setSmallIcon(icon);
2293 }
2294
2295 /**
2296 * Set the small icon, which will be used to represent the notification in the
2297 * status bar and content view (unless overriden there by a
2298 * {@link #setLargeIcon(Bitmap) large icon}).
2299 *
2300 * @param icon An Icon object to use.
2301 * @see Notification#icon
2302 */
2303 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002304 mN.setSmallIcon(icon);
2305 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2306 mN.icon = icon.getResId();
2307 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002308 return this;
2309 }
2310
Joe Onoratocb109a02011-01-18 17:57:41 -08002311 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002312 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002313 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002314 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002315 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002316 return this;
2317 }
2318
Joe Onoratocb109a02011-01-18 17:57:41 -08002319 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002320 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002321 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002322 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002323 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002324 return this;
2325 }
2326
Joe Onoratocb109a02011-01-18 17:57:41 -08002327 /**
Joe Malin8d40d042012-11-05 11:36:40 -08002328 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002329 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
2330 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002331 */
2332 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002333 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002334 return this;
2335 }
2336
2337 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002338 * Set the remote input history.
2339 *
2340 * This should be set to the most recent inputs that have been sent
2341 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2342 * longer relevant (e.g. for chat notifications once the other party has responded).
2343 *
2344 * The most recent input must be stored at the 0 index, the second most recent at the
2345 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2346 * and how much of each individual input is shown.
2347 *
2348 * <p>Note: The reply text will only be shown on notifications that have least one action
2349 * with a {@code RemoteInput}.</p>
2350 */
2351 public Builder setRemoteInputHistory(CharSequence[] text) {
2352 if (text == null) {
2353 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2354 } else {
2355 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2356 CharSequence[] safe = new CharSequence[N];
2357 for (int i = 0; i < N; i++) {
2358 safe[i] = safeCharSequence(text[i]);
2359 }
2360 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2361 }
2362 return this;
2363 }
2364
2365 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002366 * Set the large number at the right-hand side of the notification. This is
2367 * equivalent to setContentInfo, although it might show the number in a different
2368 * font size for readability.
2369 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002370 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002371 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002372 return this;
2373 }
2374
Joe Onoratocb109a02011-01-18 17:57:41 -08002375 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002376 * A small piece of additional information pertaining to this notification.
2377 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002378 * The platform template will draw this on the last line of the notification, at the far
2379 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08002380 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002381 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002382 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002383 return this;
2384 }
2385
Joe Onoratocb109a02011-01-18 17:57:41 -08002386 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002387 * Set the progress this notification represents.
2388 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002389 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002390 */
2391 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002392 mN.extras.putInt(EXTRA_PROGRESS, progress);
2393 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
2394 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002395 return this;
2396 }
2397
2398 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002399 * Supply a custom RemoteViews to use instead of the platform template.
2400 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002401 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08002402 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002403 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002404 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002405 return setCustomContentView(views);
2406 }
2407
2408 /**
2409 * Supply custom RemoteViews to use instead of the platform template.
2410 *
2411 * This will override the layout that would otherwise be constructed by this Builder
2412 * object.
2413 */
2414 public Builder setCustomContentView(RemoteViews contentView) {
2415 mN.contentView = contentView;
2416 return this;
2417 }
2418
2419 /**
2420 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
2421 *
2422 * This will override the expanded layout that would otherwise be constructed by this
2423 * Builder object.
2424 */
2425 public Builder setCustomBigContentView(RemoteViews contentView) {
2426 mN.bigContentView = contentView;
2427 return this;
2428 }
2429
2430 /**
2431 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
2432 *
2433 * This will override the heads-up layout that would otherwise be constructed by this
2434 * Builder object.
2435 */
2436 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
2437 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002438 return this;
2439 }
2440
Joe Onoratocb109a02011-01-18 17:57:41 -08002441 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002442 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2443 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002444 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2445 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2446 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002447 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002448 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002449 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002450 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002451 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002452 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002453 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002454 return this;
2455 }
2456
Joe Onoratocb109a02011-01-18 17:57:41 -08002457 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002458 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2459 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002460 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002461 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002462 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002463 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002464 return this;
2465 }
2466
Joe Onoratocb109a02011-01-18 17:57:41 -08002467 /**
2468 * An intent to launch instead of posting the notification to the status bar.
2469 * Only for use with extremely high-priority notifications demanding the user's
2470 * <strong>immediate</strong> attention, such as an incoming phone call or
2471 * alarm clock that the user has explicitly set to a particular time.
2472 * If this facility is used for something else, please give the user an option
2473 * to turn it off and use a normal notification, as this can be extremely
2474 * disruptive.
2475 *
Chris Wren47c20a12014-06-18 17:27:29 -04002476 * <p>
2477 * The system UI may choose to display a heads-up notification, instead of
2478 * launching this intent, while the user is using the device.
2479 * </p>
2480 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002481 * @param intent The pending intent to launch.
2482 * @param highPriority Passing true will cause this notification to be sent
2483 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002484 *
2485 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002486 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002487 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002488 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002489 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2490 return this;
2491 }
2492
Joe Onoratocb109a02011-01-18 17:57:41 -08002493 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002494 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002495 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002496 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002497 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002498 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002499 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002500 return this;
2501 }
2502
Joe Onoratocb109a02011-01-18 17:57:41 -08002503 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002504 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002505 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002506 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002507 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002508 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002509 setTicker(tickerText);
2510 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08002511 return this;
2512 }
2513
Joe Onoratocb109a02011-01-18 17:57:41 -08002514 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002515 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002516 *
2517 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002518 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2519 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002520 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002521 public Builder setLargeIcon(Bitmap b) {
2522 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2523 }
2524
2525 /**
2526 * Add a large icon to the notification content view.
2527 *
2528 * In the platform template, this image will be shown on the left of the notification view
2529 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2530 * badge atop the large icon).
2531 */
2532 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002533 mN.mLargeIcon = icon;
2534 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08002535 return this;
2536 }
2537
Joe Onoratocb109a02011-01-18 17:57:41 -08002538 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002539 * Set the sound to play.
2540 *
John Spurlockc0650f022014-07-19 13:22:39 -04002541 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2542 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002543 *
Chris Wren47c20a12014-06-18 17:27:29 -04002544 * <p>
2545 * A notification that is noisy is more likely to be presented as a heads-up notification.
2546 * </p>
2547 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002548 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002549 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002550 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002551 mN.sound = sound;
2552 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002553 return this;
2554 }
2555
Joe Onoratocb109a02011-01-18 17:57:41 -08002556 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002557 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002558 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002559 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2560 *
Chris Wren47c20a12014-06-18 17:27:29 -04002561 * <p>
2562 * A notification that is noisy is more likely to be presented as a heads-up notification.
2563 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002564 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002565 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002566 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002567 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002568 public Builder setSound(Uri sound, int streamType) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002569 mN.sound = sound;
2570 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08002571 return this;
2572 }
2573
Joe Onoratocb109a02011-01-18 17:57:41 -08002574 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002575 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2576 * use during playback.
2577 *
2578 * <p>
2579 * A notification that is noisy is more likely to be presented as a heads-up notification.
2580 * </p>
2581 *
2582 * @see Notification#sound
2583 */
2584 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002585 mN.sound = sound;
2586 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04002587 return this;
2588 }
2589
2590 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002591 * Set the vibration pattern to use.
2592 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002593 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2594 * <code>pattern</code> parameter.
2595 *
Chris Wren47c20a12014-06-18 17:27:29 -04002596 * <p>
2597 * A notification that vibrates is more likely to be presented as a heads-up notification.
2598 * </p>
2599 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002600 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002601 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002602 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002603 mN.vibrate = pattern;
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 * Set the desired color for the indicator LED on the device, as well as the
2609 * blink duty cycle (specified in milliseconds).
2610 *
2611
2612 * Not all devices will honor all (or even any) of these values.
2613 *
2614
2615 * @see Notification#ledARGB
2616 * @see Notification#ledOnMS
2617 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002618 */
Tor Norbye80756e32015-03-02 09:39:27 -08002619 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002620 mN.ledARGB = argb;
2621 mN.ledOnMS = onMs;
2622 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002623 if (onMs != 0 || offMs != 0) {
2624 mN.flags |= FLAG_SHOW_LIGHTS;
2625 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002626 return this;
2627 }
2628
Joe Onoratocb109a02011-01-18 17:57:41 -08002629 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002630 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002631 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002632
2633 * Ongoing notifications cannot be dismissed by the user, so your application or service
2634 * must take care of canceling them.
2635 *
2636
2637 * They are typically used to indicate a background task that the user is actively engaged
2638 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2639 * (e.g., a file download, sync operation, active network connection).
2640 *
2641
2642 * @see Notification#FLAG_ONGOING_EVENT
2643 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002644 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002645 public Builder setOngoing(boolean ongoing) {
2646 setFlag(FLAG_ONGOING_EVENT, ongoing);
2647 return this;
2648 }
2649
Joe Onoratocb109a02011-01-18 17:57:41 -08002650 /**
2651 * Set this flag if you would only like the sound, vibrate
2652 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002653 *
2654 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002655 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002656 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2657 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2658 return this;
2659 }
2660
Joe Onoratocb109a02011-01-18 17:57:41 -08002661 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002662 * Make this notification automatically dismissed when the user touches it. The
2663 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2664 *
2665 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002666 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002667 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002668 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002669 return this;
2670 }
2671
Joe Onoratocb109a02011-01-18 17:57:41 -08002672 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002673 * Set whether or not this notification should not bridge to other devices.
2674 *
2675 * <p>Some notifications can be bridged to other devices for remote display.
2676 * This hint can be set to recommend this notification not be bridged.
2677 */
2678 public Builder setLocalOnly(boolean localOnly) {
2679 setFlag(FLAG_LOCAL_ONLY, localOnly);
2680 return this;
2681 }
2682
2683 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002684 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002685 * <p>
2686 * The value should be one or more of the following fields combined with
2687 * bitwise-or:
2688 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2689 * <p>
2690 * For all default values, use {@link #DEFAULT_ALL}.
2691 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002692 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002693 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08002694 return this;
2695 }
2696
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002697 /**
2698 * Set the priority of this notification.
2699 *
2700 * @see Notification#priority
2701 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002702 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002703 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002704 return this;
2705 }
Joe Malin8d40d042012-11-05 11:36:40 -08002706
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002707 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002708 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002709 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002710 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002711 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002712 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002713 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002714 return this;
2715 }
2716
2717 /**
Chris Wrendde75302014-03-26 17:24:15 -04002718 * Add a person that is relevant to this notification.
2719 *
Chris Wrene6c48932014-09-29 17:19:27 -04002720 * <P>
2721 * Depending on user preferences, this annotation may allow the notification to pass
2722 * through interruption filters, and to appear more prominently in the user interface.
2723 * </P>
2724 *
2725 * <P>
2726 * The person should be specified by the {@code String} representation of a
2727 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2728 * </P>
2729 *
2730 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2731 * URIs. The path part of these URIs must exist in the contacts database, in the
2732 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2733 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2734 * </P>
2735 *
2736 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002737 * @see Notification#EXTRA_PEOPLE
2738 */
Chris Wrene6c48932014-09-29 17:19:27 -04002739 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002740 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002741 return this;
2742 }
2743
2744 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002745 * Set this notification to be part of a group of notifications sharing the same key.
2746 * Grouped notifications may display in a cluster or stack on devices which
2747 * support such rendering.
2748 *
2749 * <p>To make this notification the summary for its group, also call
2750 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2751 * {@link #setSortKey}.
2752 * @param groupKey The group key of the group.
2753 * @return this object for method chaining
2754 */
2755 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002756 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002757 return this;
2758 }
2759
2760 /**
2761 * Set this notification to be the group summary for a group of notifications.
2762 * Grouped notifications may display in a cluster or stack on devices which
2763 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2764 * @param isGroupSummary Whether this notification should be a group summary.
2765 * @return this object for method chaining
2766 */
2767 public Builder setGroupSummary(boolean isGroupSummary) {
2768 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2769 return this;
2770 }
2771
2772 /**
2773 * Set a sort key that orders this notification among other notifications from the
2774 * same package. This can be useful if an external sort was already applied and an app
2775 * would like to preserve this. Notifications will be sorted lexicographically using this
2776 * value, although providing different priorities in addition to providing sort key may
2777 * cause this value to be ignored.
2778 *
2779 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002780 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002781 *
2782 * @see String#compareTo(String)
2783 */
2784 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002785 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002786 return this;
2787 }
2788
2789 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002790 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002791 *
Griff Hazen720042b2014-02-24 15:46:56 -08002792 * <p>Values within the Bundle will replace existing extras values in this Builder.
2793 *
2794 * @see Notification#extras
2795 */
Griff Hazen959591e2014-05-15 22:26:18 -07002796 public Builder addExtras(Bundle extras) {
2797 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002798 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08002799 }
2800 return this;
2801 }
2802
2803 /**
2804 * Set metadata for this notification.
2805 *
2806 * <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 -04002807 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002808 * called.
2809 *
Griff Hazen720042b2014-02-24 15:46:56 -08002810 * <p>Replaces any existing extras values with those from the provided Bundle.
2811 * Use {@link #addExtras} to merge in metadata instead.
2812 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002813 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002814 */
Griff Hazen959591e2014-05-15 22:26:18 -07002815 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002816 if (extras != null) {
2817 mUserExtras = extras;
2818 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002819 return this;
2820 }
2821
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002822 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002823 * Get the current metadata Bundle used by this notification Builder.
2824 *
2825 * <p>The returned Bundle is shared with this Builder.
2826 *
2827 * <p>The current contents of this Bundle are copied into the Notification each time
2828 * {@link #build()} is called.
2829 *
2830 * @see Notification#extras
2831 */
2832 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002833 return mUserExtras;
2834 }
2835
2836 private Bundle getAllExtras() {
2837 final Bundle saveExtras = (Bundle) mUserExtras.clone();
2838 saveExtras.putAll(mN.extras);
2839 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08002840 }
2841
2842 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002843 * Add an action to this notification. Actions are typically displayed by
2844 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002845 * <p>
2846 * Every action must have an icon (32dp square and matching the
2847 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2848 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2849 * <p>
2850 * A notification in its expanded form can display up to 3 actions, from left to right in
2851 * the order they were added. Actions will not be displayed when the notification is
2852 * collapsed, however, so be sure that any essential functions may be accessed by the user
2853 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002854 *
2855 * @param icon Resource ID of a drawable that represents the action.
2856 * @param title Text describing the action.
2857 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04002858 *
2859 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002860 */
Dan Sandler86647982015-05-13 23:41:13 -04002861 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002862 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002863 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002864 return this;
2865 }
2866
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002867 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002868 * Add an action to this notification. Actions are typically displayed by
2869 * the system as a button adjacent to the notification content.
2870 * <p>
2871 * Every action must have an icon (32dp square and matching the
2872 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2873 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2874 * <p>
2875 * A notification in its expanded form can display up to 3 actions, from left to right in
2876 * the order they were added. Actions will not be displayed when the notification is
2877 * collapsed, however, so be sure that any essential functions may be accessed by the user
2878 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2879 *
2880 * @param action The action to add.
2881 */
2882 public Builder addAction(Action action) {
2883 mActions.add(action);
2884 return this;
2885 }
2886
2887 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002888 * Alter the complete list of actions attached to this notification.
2889 * @see #addAction(Action).
2890 *
2891 * @param actions
2892 * @return
2893 */
2894 public Builder setActions(Action... actions) {
2895 mActions.clear();
2896 for (int i = 0; i < actions.length; i++) {
2897 mActions.add(actions[i]);
2898 }
2899 return this;
2900 }
2901
2902 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002903 * Add a rich notification style to be applied at build time.
2904 *
2905 * @param style Object responsible for modifying the notification style.
2906 */
2907 public Builder setStyle(Style style) {
2908 if (mStyle != style) {
2909 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002910 if (mStyle != null) {
2911 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002912 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
2913 } else {
2914 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002915 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002916 }
2917 return this;
2918 }
2919
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002920 /**
2921 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002922 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002923 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2924 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
2925 *
2926 * @return The same Builder.
2927 */
2928 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002929 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002930 return this;
2931 }
2932
2933 /**
2934 * Supply a replacement Notification whose contents should be shown in insecure contexts
2935 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
2936 * @param n A replacement notification, presumably with some or all info redacted.
2937 * @return The same Builder.
2938 */
2939 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002940 if (n != null) {
2941 mN.publicVersion = new Notification();
2942 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
2943 } else {
2944 mN.publicVersion = null;
2945 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002946 return this;
2947 }
2948
Griff Hazenb720abe2014-05-20 13:15:30 -07002949 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002950 * Apply an extender to this notification builder. Extenders may be used to add
2951 * metadata or change options on this builder.
2952 */
Griff Hazen61a9e862014-05-22 16:05:19 -07002953 public Builder extend(Extender extender) {
2954 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002955 return this;
2956 }
2957
Dan Sandler4e787062015-06-17 15:09:48 -04002958 /**
2959 * @hide
2960 */
2961 public void setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002962 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002963 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08002964 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002965 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08002966 }
2967 }
2968
Dan Sandler26e81cf2014-05-06 10:01:27 -04002969 /**
2970 * Sets {@link Notification#color}.
2971 *
2972 * @param argb The accent color to use
2973 *
2974 * @return The same Builder.
2975 */
Tor Norbye80756e32015-03-02 09:39:27 -08002976 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002977 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002978 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04002979 return this;
2980 }
2981
Julia Reynolds74303cf2015-10-16 11:37:55 -04002982 /**
Julia Reynolds054c5dc2015-11-17 15:36:30 -05002983 * Sets the topic of this notification. Topics are typically displayed in Notification
Julia Reynolds74303cf2015-10-16 11:37:55 -04002984 * settings.
2985 * <p>
2986 * Every topic must have an id and a textual label.
2987 *
2988 * @param topic The topic to add.
2989 */
Julia Reynolds054c5dc2015-11-17 15:36:30 -05002990 public Builder setTopic(Topic topic) {
2991 mN.topic = topic;
Julia Reynolds74303cf2015-10-16 11:37:55 -04002992 return this;
2993 }
2994
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002995 private Drawable getProfileBadgeDrawable() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02002996 // Note: This assumes that the current user can read the profile badge of the
2997 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08002998 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05002999 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003000 }
3001
3002 private Bitmap getProfileBadge() {
3003 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003004 if (badge == null) {
3005 return null;
3006 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003007 final int size = mContext.getResources().getDimensionPixelSize(
3008 R.dimen.notification_badge_size);
3009 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003010 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003011 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003012 badge.draw(canvas);
3013 return bitmap;
3014 }
3015
Selim Cinekc848c3a2016-01-13 15:27:30 -08003016 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003017 Bitmap profileBadge = getProfileBadge();
3018
Kenny Guy98193ea2014-07-24 19:54:37 +01003019 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003020 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3021 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Kenny Guy98193ea2014-07-24 19:54:37 +01003022 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003023 }
3024
Christoph Studerfe718432014-09-01 18:21:18 +02003025 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003026 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003027 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003028 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003029 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003030 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003031 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003032 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003033 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003034 contentView.setTextViewText(R.id.text_line_1, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003035 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003036 }
3037
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003038 /**
3039 * Resets the notification header to its original state
3040 */
3041 private void resetNotificationHeader(RemoteViews contentView) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003042 contentView.setImageViewResource(R.id.icon, 0);
Selim Cinek7b836392015-12-04 20:02:59 -08003043 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003044 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003045 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003046 contentView.setViewVisibility(R.id.header_sub_text, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003047 contentView.setViewVisibility(R.id.header_content_info, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003048 contentView.setViewVisibility(R.id.number_of_children, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003049 contentView.setViewVisibility(R.id.sub_text_divider, View.GONE);
3050 contentView.setViewVisibility(R.id.content_info_divider, View.GONE);
3051 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003052 contentView.setImageViewIcon(R.id.profile_badge, null);
3053 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003054 }
3055
3056 private void resetContentMargins(RemoteViews contentView) {
3057 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003058 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003059 }
3060
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003061 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003062 return applyStandardTemplate(resId, true /* hasProgress */);
3063 }
3064
3065 /**
3066 * @param hasProgress whether the progress bar should be shown and set
3067 */
3068 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Kenny Guy77320062014-08-27 21:37:15 +01003069 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003070
Christoph Studerfe718432014-09-01 18:21:18 +02003071 resetStandardTemplate(contentView);
3072
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003073 final Bundle ex = mN.extras;
Dan Sandler190d58d2014-05-15 09:33:39 -04003074
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003075 bindNotificationHeader(contentView);
3076 bindLargeIcon(contentView);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003077 if (ex.getCharSequence(EXTRA_TITLE) != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003078 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003079 contentView.setTextViewText(R.id.title,
3080 processLegacyText(ex.getCharSequence(EXTRA_TITLE)));
Joe Onorato561d3852010-11-20 18:09:34 -08003081 }
Selim Cinek29603462015-11-17 19:04:39 -08003082 boolean showProgress = handleProgressBar(hasProgress, contentView, ex);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003083 if (ex.getCharSequence(EXTRA_TEXT) != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003084 int textId = showProgress ? com.android.internal.R.id.text_line_1
3085 : com.android.internal.R.id.text;
3086 contentView.setTextViewText(textId, processLegacyText(
3087 ex.getCharSequence(EXTRA_TEXT)));
3088 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003089 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003090
Selim Cinek860b6da2015-12-16 19:02:19 -08003091 setContentMinHeight(contentView, showProgress || mN.mLargeIcon != null);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003092
Selim Cinek29603462015-11-17 19:04:39 -08003093 return contentView;
3094 }
3095
Selim Cinek860b6da2015-12-16 19:02:19 -08003096 /**
3097 * @param remoteView the remote view to update the minheight in
3098 * @param hasMinHeight does it have a mimHeight
3099 * @hide
3100 */
3101 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3102 int minHeight = 0;
3103 if (hasMinHeight) {
3104 // we need to set the minHeight of the notification
3105 minHeight = mContext.getResources().getDimensionPixelSize(
3106 com.android.internal.R.dimen.notification_min_content_height);
3107 }
3108 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3109 }
3110
Selim Cinek29603462015-11-17 19:04:39 -08003111 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003112 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3113 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3114 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3115 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003116 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003117 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003118 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003119 contentView.setProgressBackgroundTintList(
3120 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3121 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003122 if (mN.color != COLOR_DEFAULT) {
3123 ColorStateList colorStateList = ColorStateList.valueOf(mN.color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003124 contentView.setProgressTintList(R.id.progress, colorStateList);
3125 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003126 }
Selim Cinek29603462015-11-17 19:04:39 -08003127 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003128 } else {
3129 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003130 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003131 }
Joe Onorato561d3852010-11-20 18:09:34 -08003132 }
3133
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003134 private void bindLargeIcon(RemoteViews contentView) {
3135 if (mN.mLargeIcon != null) {
3136 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3137 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3138 processLargeLegacyIcon(mN.mLargeIcon, contentView);
3139 int endMargin = mContext.getResources().getDimensionPixelSize(
3140 R.dimen.notification_content_picture_margin);
3141 contentView.setViewLayoutMarginEnd(R.id.line1, endMargin);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003142 contentView.setViewLayoutMarginEnd(R.id.text, endMargin);
Selim Cinek29603462015-11-17 19:04:39 -08003143 contentView.setViewLayoutMarginEnd(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003144 }
3145 }
3146
3147 private void bindNotificationHeader(RemoteViews contentView) {
3148 bindSmallIcon(contentView);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003149 bindChildCountColor(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003150 bindHeaderAppName(contentView);
Selim Cinek03d0d652015-11-13 13:18:09 -05003151 bindHeaderSubText(contentView);
Selim Cinek29603462015-11-17 19:04:39 -08003152 bindContentInfo(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003153 bindHeaderChronometerAndTime(contentView);
3154 bindExpandButton(contentView);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003155 bindProfileBadge(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003156 }
3157
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003158 private void bindChildCountColor(RemoteViews contentView) {
3159 contentView.setTextColor(R.id.number_of_children, resolveColor());
3160 }
3161
Selim Cinek29603462015-11-17 19:04:39 -08003162 private void bindContentInfo(RemoteViews contentView) {
3163 boolean visible = false;
3164 if (mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3165 contentView.setTextViewText(R.id.header_content_info,
3166 processLegacyText(mN.extras.getCharSequence(EXTRA_INFO_TEXT)));
3167 contentView.setViewVisibility(R.id.header_content_info, View.VISIBLE);
3168 visible = true;
3169 } else if (mN.number > 0) {
3170 final int tooBig = mContext.getResources().getInteger(
3171 R.integer.status_bar_notification_info_maxnum);
3172 if (mN.number > tooBig) {
3173 contentView.setTextViewText(R.id.header_content_info, processLegacyText(
3174 mContext.getResources().getString(
3175 R.string.status_bar_notification_info_overflow)));
3176 } else {
3177 contentView.setTextViewText(R.id.header_content_info,
3178 processLegacyText(String.valueOf(mN.number)));
3179 }
3180 contentView.setViewVisibility(R.id.header_content_info, View.VISIBLE);
3181 visible = true;
3182 }
3183 if (visible) {
3184 contentView.setViewVisibility(R.id.content_info_divider, View.VISIBLE);
3185 }
3186 }
3187
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003188 private void bindExpandButton(RemoteViews contentView) {
3189 contentView.setDrawableParameters(R.id.expand_button, false, -1, resolveColor(),
3190 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003191 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
3192 resolveColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003193 }
3194
3195 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3196 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003197 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003198 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3199 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3200 contentView.setLong(R.id.chronometer, "setBase",
3201 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3202 contentView.setBoolean(R.id.chronometer, "setStarted", true);
3203 } else {
3204 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3205 contentView.setLong(R.id.time, "setTime", mN.when);
3206 }
3207 }
3208 }
3209
Selim Cinek03d0d652015-11-13 13:18:09 -05003210 private void bindHeaderSubText(RemoteViews contentView) {
3211 CharSequence subText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3212 if (subText == null && mStyle != null && mStyle.mSummaryTextSet
3213 && mStyle.hasSummaryInHeader()) {
3214 subText = mStyle.mSummaryText;
3215 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003216 if (subText != null) {
3217 // TODO: Remove the span entirely to only have the string with propper formating.
3218 contentView.setTextViewText(R.id.header_sub_text, processLegacyText(subText));
3219 contentView.setViewVisibility(R.id.header_sub_text, View.VISIBLE);
Selim Cinek29603462015-11-17 19:04:39 -08003220 contentView.setViewVisibility(R.id.sub_text_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003221 }
3222 }
3223
3224 private void bindHeaderAppName(RemoteViews contentView) {
3225 PackageManager packageManager = mContext.getPackageManager();
3226 ApplicationInfo info = null;
3227 try {
3228 info = packageManager.getApplicationInfo(mContext.getApplicationInfo().packageName,
3229 0);
3230 } catch (final NameNotFoundException e) {
3231 return;
3232 }
3233 CharSequence appName = info != null ? packageManager.getApplicationLabel(info)
3234 : null;
3235 if (TextUtils.isEmpty(appName)) {
3236 return;
3237 }
3238 contentView.setTextViewText(R.id.app_name_text, appName);
Selim Cinekaef485a2016-02-05 10:31:20 -08003239 contentView.setTextColor(R.id.app_name_text, resolveColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003240 }
3241
3242 private void bindSmallIcon(RemoteViews contentView) {
3243 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
3244 processSmallIconColor(mN.mSmallIcon, contentView);
3245 }
3246
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003247 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003248 * @return true if the built notification will show the time or the chronometer; false
3249 * otherwise
3250 */
3251 private boolean showsTimeOrChronometer() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003252 return mN.when != 0 && mN.extras.getBoolean(EXTRA_SHOW_WHEN);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003253 }
3254
Christoph Studerfe718432014-09-01 18:21:18 +02003255 private void resetStandardTemplateWithActions(RemoteViews big) {
3256 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003257 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003258
3259 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3260 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3261
3262 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3263 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3264 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3265 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003266 }
3267
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003268 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003269 RemoteViews big = applyStandardTemplate(layoutId);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003270
Christoph Studerfe718432014-09-01 18:21:18 +02003271 resetStandardTemplateWithActions(big);
3272
Adrian Roose458aa82015-12-08 16:17:19 -08003273 boolean validRemoteInput = false;
3274
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003275 int N = mActions.size();
3276 if (N > 0) {
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003277 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003278 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003279 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003280 Action action = mActions.get(i);
3281 validRemoteInput |= hasValidRemoteInput(action);
3282
3283 final RemoteViews button = generateActionButton(action);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003284 big.addView(R.id.actions, button);
3285 }
3286 }
Adrian Roose458aa82015-12-08 16:17:19 -08003287
3288 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
3289 if (validRemoteInput && replyText != null
3290 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3291 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3292 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
3293
3294 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3295 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3296 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
3297
3298 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3299 big.setViewVisibility(
3300 R.id.notification_material_reply_text_3, View.VISIBLE);
3301 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
3302 }
3303 }
3304 }
3305
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003306 return big;
3307 }
3308
Adrian Roose458aa82015-12-08 16:17:19 -08003309 private boolean hasValidRemoteInput(Action action) {
3310 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
3311 // Weird actions
3312 return false;
3313 }
3314
3315 RemoteInput[] remoteInputs = action.getRemoteInputs();
3316 if (remoteInputs == null) {
3317 return false;
3318 }
3319
3320 for (RemoteInput r : remoteInputs) {
3321 CharSequence[] choices = r.getChoices();
3322 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
3323 return true;
3324 }
3325 }
3326 return false;
3327 }
3328
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003329 /**
3330 * Construct a RemoteViews for the final 1U notification layout. In order:
3331 * 1. Custom contentView from the caller
3332 * 2. Style's proposed content view
3333 * 3. Standard template view
3334 */
3335 public RemoteViews makeContentView() {
3336 if (mN.contentView != null) {
3337 return mN.contentView;
3338 } else if (mStyle != null) {
3339 final RemoteViews styleView = mStyle.makeContentView();
3340 if (styleView != null) {
3341 return styleView;
3342 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003343 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003344 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003345 }
3346
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003347 /**
3348 * Construct a RemoteViews for the final big notification layout.
3349 */
3350 public RemoteViews makeBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05003351 RemoteViews result = null;
Julia Reynolds089e3e42015-11-18 09:59:57 -05003352 if (mN.bigContentView != null) {
3353 return mN.bigContentView;
3354 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05003355 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08003356 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003357 } else if (mActions.size() != 0) {
3358 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05003359 }
3360 adaptNotificationHeaderForBigContentView(result);
3361 return result;
3362 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003363
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003364 /**
3365 * Construct a RemoteViews for the final notification header only
3366 *
3367 * @hide
3368 */
3369 public RemoteViews makeNotificationHeader() {
3370 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
3371 R.layout.notification_template_header);
3372 resetNotificationHeader(header);
3373 bindNotificationHeader(header);
3374 return header;
3375 }
3376
Selim Cinek29603462015-11-17 19:04:39 -08003377 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003378 if (result != null) {
3379 result.setViewVisibility(R.id.text_line_1, View.GONE);
3380 }
Selim Cinek29603462015-11-17 19:04:39 -08003381 }
3382
Selim Cinek850a8542015-11-11 11:48:36 -05003383 private void adaptNotificationHeaderForBigContentView(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003384 if (result != null) {
3385 result.setBoolean(R.id.notification_header, "setExpanded", true);
3386 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003387 }
3388
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003389 /**
3390 * Construct a RemoteViews for the final heads-up notification layout.
3391 */
3392 public RemoteViews makeHeadsUpContentView() {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003393 if (mN.headsUpContentView != null) {
3394 return mN.headsUpContentView;
3395 } else if (mStyle != null) {
3396 final RemoteViews styleView = mStyle.makeHeadsUpContentView();
3397 if (styleView != null) {
3398 return styleView;
3399 }
3400 } else if (mActions.size() == 0) {
3401 return null;
3402 }
3403
Chris Wren8fd39ec2014-02-27 17:43:26 -05003404
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003405 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003406 }
3407
Selim Cinek624c02db2015-12-14 21:00:02 -08003408 /**
3409 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
3410 *
3411 * @hide
3412 */
3413 public RemoteViews makePublicContentView() {
3414 if (mN.publicVersion != null) {
3415 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
3416 return builder.makeContentView();
3417 }
3418 Bundle savedBundle = mN.extras;
3419 Style style = mStyle;
3420 mStyle = null;
3421 Icon largeIcon = mN.mLargeIcon;
3422 mN.mLargeIcon = null;
3423 Bundle publicExtras = new Bundle();
3424 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
3425 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
3426 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
3427 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
3428 publicExtras.putCharSequence(EXTRA_TITLE,
3429 mContext.getString(R.string.notification_hidden_text));
3430 mN.extras = publicExtras;
3431 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
3432 mN.extras = savedBundle;
3433 mN.mLargeIcon = largeIcon;
3434 mStyle = style;
3435 return publicView;
3436 }
3437
3438
Chris Wren8fd39ec2014-02-27 17:43:26 -05003439
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003440 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003441 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07003442 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003443 tombstone ? getActionTombstoneLayoutResource()
3444 : getActionLayoutResource());
Dan Sandler68079d52015-07-22 10:45:30 -04003445 final Icon ai = action.getIcon();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003446 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04003447 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003448 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003449 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003450 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003451 if (action.mRemoteInputs != null) {
3452 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
3453 }
3454 if (mN.color != COLOR_DEFAULT) {
3455 button.setTextColor(R.id.action0, mN.color);
3456 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003457 return button;
3458 }
3459
Joe Onoratocb109a02011-01-18 17:57:41 -08003460 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003461 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003462 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003463 */
3464 private boolean isLegacy() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003465 return getColorUtil() != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003466 }
3467
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003468 private CharSequence processLegacyText(CharSequence charSequence) {
3469 if (isLegacy()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003470 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003471 } else {
3472 return charSequence;
3473 }
3474 }
3475
Dan Sandler26e81cf2014-05-06 10:01:27 -04003476 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003477 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04003478 */
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003479 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView) {
Selim Cinekea4bef72015-12-02 15:51:10 -08003480 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
3481 if (colorable) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003482 contentView.setDrawableParameters(R.id.icon, false, -1, resolveColor(),
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003483 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003484
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003485 }
Selim Cinekea4bef72015-12-02 15:51:10 -08003486 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
3487 colorable ? resolveColor() : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003488 }
3489
Dan Sandler26e81cf2014-05-06 10:01:27 -04003490 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003491 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04003492 * if it's grayscale).
3493 */
3494 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003495 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3496 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003497 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003498 // resolve color will fall back to the default when legacy
3499 contentView.setDrawableParameters(R.id.icon, false, -1, resolveColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003500 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003501 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003502 }
3503
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003504 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003505 if (mN.color != COLOR_DEFAULT) {
3506 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02003507 }
Jorim Jaggi74419312014-06-10 20:57:21 +02003508 }
3509
Selim Cinek5bf069a2015-11-10 19:14:27 -05003510 int resolveColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003511 if (mN.color == COLOR_DEFAULT) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003512 return mContext.getColor(R.color.notification_icon_default_color);
Dan Sandler26e81cf2014-05-06 10:01:27 -04003513 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003514 return mN.color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003515 }
3516
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003517 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003518 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003519 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003520 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003521 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003522 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003523 mN.actions = new Action[mActions.size()];
3524 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003525 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003526 if (!mPersonList.isEmpty()) {
3527 mN.extras.putStringArray(EXTRA_PEOPLE,
3528 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003529 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003530 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08003531 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003532
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003533 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003534 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003535 ApplicationInfo applicationInfo = n.extras.getParcelable(
3536 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003537 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05003538 if (applicationInfo != null) {
3539 try {
3540 builderContext = context.createApplicationContext(applicationInfo,
3541 Context.CONTEXT_RESTRICTED);
3542 } catch (NameNotFoundException e) {
3543 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
3544 builderContext = context; // try with our context
3545 }
3546 } else {
3547 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02003548 }
3549
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003550 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003551 }
3552
3553 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
3554 Class<? extends Style>[] classes = new Class[]{
3555 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class};
3556 for (Class<? extends Style> innerClass : classes) {
3557 if (templateClass.equals(innerClass.getName())) {
3558 return innerClass;
3559 }
3560 }
3561 return null;
3562 }
3563
3564 private void setBuilderContentView(Notification n, RemoteViews contentView) {
3565 n.contentView = contentView;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003566 }
3567
3568 private void setBuilderBigContentView(Notification n, RemoteViews bigContentView) {
3569 n.bigContentView = bigContentView;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003570 }
3571
3572 private void setBuilderHeadsUpContentView(Notification n,
3573 RemoteViews headsUpContentView) {
3574 n.headsUpContentView = headsUpContentView;
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003575 }
3576
3577 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003578 * @deprecated Use {@link #build()} instead.
3579 */
3580 @Deprecated
3581 public Notification getNotification() {
3582 return build();
3583 }
3584
3585 /**
3586 * Combine all of the options that have been set and return a new {@link Notification}
3587 * object.
3588 */
3589 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003590 // first, add any extras from the calling code
3591 if (mUserExtras != null) {
3592 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003593 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003594
3595 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05003596 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02003597
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003598 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003599
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003600 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003601 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003602 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003603
Julia Reynolds4c0c2022016-02-02 15:11:59 -05003604 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
3605 mN.flags |= FLAG_SHOW_LIGHTS;
3606 }
3607
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003608 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003609 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003610
3611 /**
3612 * Apply this Builder to an existing {@link Notification} object.
3613 *
3614 * @hide
3615 */
3616 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003617 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003618 return n;
3619 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003620
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003621 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003622 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003623 }
3624
3625 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003626 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003627 }
3628
3629 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003630 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003631 }
3632
3633 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003634 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003635 }
3636
3637 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003638 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003639 }
3640
3641 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003642 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003643 }
3644
3645 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003646 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003647 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003648 }
3649
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003650 /**
3651 * An object that can apply a rich notification style to a {@link Notification.Builder}
3652 * object.
3653 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003654 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003655 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003656
3657 /**
3658 * @hide
3659 */
3660 protected CharSequence mSummaryText = null;
3661
3662 /**
3663 * @hide
3664 */
3665 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003666
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003667 protected Builder mBuilder;
3668
Chris Wrend6297db2012-05-03 16:20:13 -04003669 /**
3670 * Overrides ContentTitle in the big form of the template.
3671 * This defaults to the value passed to setContentTitle().
3672 */
3673 protected void internalSetBigContentTitle(CharSequence title) {
3674 mBigContentTitle = title;
3675 }
3676
3677 /**
3678 * Set the first line of text after the detail section in the big form of the template.
3679 */
3680 protected void internalSetSummaryText(CharSequence cs) {
3681 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003682 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003683 }
3684
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003685 public void setBuilder(Builder builder) {
3686 if (mBuilder != builder) {
3687 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003688 if (mBuilder != null) {
3689 mBuilder.setStyle(this);
3690 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003691 }
3692 }
3693
Chris Wrend6297db2012-05-03 16:20:13 -04003694 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003695 if (mBuilder == null) {
3696 throw new IllegalArgumentException("Style requires a valid Builder object");
3697 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003698 }
Chris Wrend6297db2012-05-03 16:20:13 -04003699
3700 protected RemoteViews getStandardView(int layoutId) {
3701 checkBuilder();
3702
Christoph Studer4600f9b2014-07-22 22:44:43 +02003703 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003704 CharSequence oldBuilderContentTitle =
3705 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003706 if (mBigContentTitle != null) {
3707 mBuilder.setContentTitle(mBigContentTitle);
3708 }
3709
Chris Wrend6297db2012-05-03 16:20:13 -04003710 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3711
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003712 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003713
Chris Wrend6297db2012-05-03 16:20:13 -04003714 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3715 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003716 } else {
3717 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003718 }
3719
Chris Wrend6297db2012-05-03 16:20:13 -04003720 return contentView;
3721 }
3722
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003723 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003724 * Construct a Style-specific RemoteViews for the final 1U notification layout.
3725 * The default implementation has nothing additional to add.
3726 * @hide
3727 */
3728 public RemoteViews makeContentView() {
3729 return null;
3730 }
3731
3732 /**
3733 * Construct a Style-specific RemoteViews for the final big notification layout.
3734 * @hide
3735 */
3736 public RemoteViews makeBigContentView() {
3737 return null;
3738 }
3739
3740 /**
3741 * Construct a Style-specific RemoteViews for the final HUN layout.
3742 * @hide
3743 */
3744 public RemoteViews makeHeadsUpContentView() {
3745 return null;
3746 }
3747
3748 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003749 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003750 * @hide
3751 */
3752 public void addExtras(Bundle extras) {
3753 if (mSummaryTextSet) {
3754 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
3755 }
3756 if (mBigContentTitle != null) {
3757 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
3758 }
Chris Wren91ad5632013-06-05 15:05:57 -04003759 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003760 }
3761
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003762 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003763 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003764 * @hide
3765 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02003766 protected void restoreFromExtras(Bundle extras) {
3767 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
3768 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
3769 mSummaryTextSet = true;
3770 }
3771 if (extras.containsKey(EXTRA_TITLE_BIG)) {
3772 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
3773 }
3774 }
3775
3776
3777 /**
3778 * @hide
3779 */
3780 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003781 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003782 return wip;
3783 }
3784
Daniel Sandler0ec46202015-06-24 01:27:05 -04003785 /**
3786 * @hide
3787 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003788 public void purgeResources() {}
3789
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003790 /**
3791 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
3792 * attached to.
3793 *
3794 * @return the fully constructed Notification.
3795 */
3796 public Notification build() {
3797 checkBuilder();
3798 return mBuilder.build();
3799 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003800
3801 /**
3802 * @hide
3803 * @return true if the style positions the progress bar on the second line; false if the
3804 * style hides the progress bar
3805 */
3806 protected boolean hasProgress() {
3807 return true;
3808 }
Selim Cinek03d0d652015-11-13 13:18:09 -05003809
3810 /**
3811 * @hide
3812 * @return Whether we should put the summary be put into the notification header
3813 */
3814 public boolean hasSummaryInHeader() {
3815 return true;
3816 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003817 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003818
3819 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003820 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08003821 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003822 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003823 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003824 * Notification notif = new Notification.Builder(mContext)
3825 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
3826 * .setContentText(subject)
3827 * .setSmallIcon(R.drawable.new_post)
3828 * .setLargeIcon(aBitmap)
3829 * .setStyle(new Notification.BigPictureStyle()
3830 * .bigPicture(aBigBitmap))
3831 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003832 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003833 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003834 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003835 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003836 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003837 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003838 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003839 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003840
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003841 public BigPictureStyle() {
3842 }
3843
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003844 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003845 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003846 }
3847
Chris Wrend6297db2012-05-03 16:20:13 -04003848 /**
3849 * Overrides ContentTitle in the big form of the template.
3850 * This defaults to the value passed to setContentTitle().
3851 */
3852 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003853 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003854 return this;
3855 }
3856
3857 /**
3858 * Set the first line of text after the detail section in the big form of the template.
3859 */
3860 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003861 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003862 return this;
3863 }
3864
Chris Wren0bd664d2012-08-01 13:56:56 -04003865 /**
3866 * Provide the bitmap to be used as the payload for the BigPicture notification.
3867 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003868 public BigPictureStyle bigPicture(Bitmap b) {
3869 mPicture = b;
3870 return this;
3871 }
3872
Chris Wren3745a3d2012-05-22 15:11:52 -04003873 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04003874 * Override the large icon when the big notification is shown.
3875 */
3876 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003877 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3878 }
3879
3880 /**
3881 * Override the large icon when the big notification is shown.
3882 */
3883 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04003884 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003885 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003886 return this;
3887 }
3888
Riley Andrews0394a0c2015-11-03 23:36:52 -08003889 /** @hide */
3890 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
3891
Daniel Sandler0ec46202015-06-24 01:27:05 -04003892 /**
3893 * @hide
3894 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003895 @Override
3896 public void purgeResources() {
3897 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08003898 if (mPicture != null &&
3899 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08003900 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003901 mPicture = mPicture.createAshmemBitmap();
3902 }
3903 if (mBigLargeIcon != null) {
3904 mBigLargeIcon.convertToAshmem();
3905 }
3906 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01003907
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003908 /**
3909 * @hide
3910 */
3911 public RemoteViews makeBigContentView() {
3912 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01003913 // This covers the following cases:
3914 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003915 // mN.mLargeIcon
3916 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04003917 Icon oldLargeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003918 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003919 oldLargeIcon = mBuilder.mN.mLargeIcon;
3920 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003921 }
3922
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003923 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05003924 if (mSummaryTextSet) {
3925 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinekc848c3a2016-01-13 15:27:30 -08003926 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05003927 }
Selim Cinek860b6da2015-12-16 19:02:19 -08003928 mBuilder.setContentMinHeight(contentView, mBuilder.mN.mLargeIcon != null);
Selim Cinek53e64a42015-11-16 10:40:56 -08003929
Christoph Studer5c510ee2014-12-15 16:32:27 +01003930 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003931 mBuilder.mN.mLargeIcon = oldLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003932 }
3933
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003934 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003935 return contentView;
3936 }
3937
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003938 /**
3939 * @hide
3940 */
3941 public void addExtras(Bundle extras) {
3942 super.addExtras(extras);
3943
3944 if (mBigLargeIconSet) {
3945 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
3946 }
3947 extras.putParcelable(EXTRA_PICTURE, mPicture);
3948 }
3949
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003950 /**
3951 * @hide
3952 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003953 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003954 protected void restoreFromExtras(Bundle extras) {
3955 super.restoreFromExtras(extras);
3956
3957 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01003958 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003959 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04003960 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003961 mPicture = extras.getParcelable(EXTRA_PICTURE);
3962 }
Selim Cinek03d0d652015-11-13 13:18:09 -05003963
3964 /**
3965 * @hide
3966 */
3967 @Override
3968 public boolean hasSummaryInHeader() {
3969 return false;
3970 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003971 }
3972
3973 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003974 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08003975 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003976 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003977 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003978 * Notification notif = new Notification.Builder(mContext)
3979 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3980 * .setContentText(subject)
3981 * .setSmallIcon(R.drawable.new_mail)
3982 * .setLargeIcon(aBitmap)
3983 * .setStyle(new Notification.BigTextStyle()
3984 * .bigText(aVeryLongString))
3985 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003986 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003987 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003988 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003989 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003990 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003991
3992 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08003993 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003994
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003995 private CharSequence mBigText;
3996
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003997 public BigTextStyle() {
3998 }
3999
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004000 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004001 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004002 }
4003
Chris Wrend6297db2012-05-03 16:20:13 -04004004 /**
4005 * Overrides ContentTitle in the big form of the template.
4006 * This defaults to the value passed to setContentTitle().
4007 */
4008 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004009 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004010 return this;
4011 }
4012
4013 /**
4014 * Set the first line of text after the detail section in the big form of the template.
4015 */
4016 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004017 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004018 return this;
4019 }
4020
Chris Wren0bd664d2012-08-01 13:56:56 -04004021 /**
4022 * Provide the longer text to be displayed in the big form of the
4023 * template in place of the content text.
4024 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004025 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004026 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004027 return this;
4028 }
4029
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004030 /**
4031 * @hide
4032 */
4033 public void addExtras(Bundle extras) {
4034 super.addExtras(extras);
4035
Christoph Studer4600f9b2014-07-22 22:44:43 +02004036 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4037 }
4038
4039 /**
4040 * @hide
4041 */
4042 @Override
4043 protected void restoreFromExtras(Bundle extras) {
4044 super.restoreFromExtras(extras);
4045
4046 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004047 }
4048
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004049 /**
4050 * @hide
4051 */
4052 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004053
4054 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004055 CharSequence oldBuilderContentText =
4056 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
4057 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004058
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004059 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004060
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004061 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004062
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004063 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
4064 contentView.setTextViewText(R.id.big_text, bigTextText);
4065 contentView.setViewVisibility(R.id.big_text,
4066 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004067 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines());
Selim Cinek4fb12d32015-11-19 18:10:48 -08004068 contentView.setBoolean(R.id.big_text, "setHasImage", mBuilder.mN.mLargeIcon != null);
4069
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004070 return contentView;
4071 }
4072
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004073 private int calculateMaxLines() {
4074 int lineCount = MAX_LINES;
4075 boolean hasActions = mBuilder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004076 if (hasActions) {
4077 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4078 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004079 return lineCount;
4080 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004081 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004082
4083 /**
4084 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08004085 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004086 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04004087 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004088 * Notification notif = new Notification.Builder(mContext)
4089 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
4090 * .setContentText(subject)
4091 * .setSmallIcon(R.drawable.new_mail)
4092 * .setLargeIcon(aBitmap)
4093 * .setStyle(new Notification.InboxStyle()
4094 * .addLine(str1)
4095 * .addLine(str2)
4096 * .setContentTitle(&quot;&quot;)
4097 * .setSummaryText(&quot;+3 more&quot;))
4098 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04004099 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004100 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04004101 * @see Notification#bigContentView
4102 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004103 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004104 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
4105
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004106 public InboxStyle() {
4107 }
4108
Daniel Sandler879c5e02012-04-17 16:46:51 -04004109 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004110 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004111 }
4112
Chris Wrend6297db2012-05-03 16:20:13 -04004113 /**
4114 * Overrides ContentTitle in the big form of the template.
4115 * This defaults to the value passed to setContentTitle().
4116 */
4117 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004118 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004119 return this;
4120 }
4121
4122 /**
4123 * Set the first line of text after the detail section in the big form of the template.
4124 */
4125 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004126 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004127 return this;
4128 }
4129
Chris Wren0bd664d2012-08-01 13:56:56 -04004130 /**
4131 * Append a line to the digest section of the Inbox notification.
4132 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04004133 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004134 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04004135 return this;
4136 }
4137
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004138 /**
4139 * @hide
4140 */
4141 public void addExtras(Bundle extras) {
4142 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004143
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004144 CharSequence[] a = new CharSequence[mTexts.size()];
4145 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
4146 }
4147
Christoph Studer4600f9b2014-07-22 22:44:43 +02004148 /**
4149 * @hide
4150 */
4151 @Override
4152 protected void restoreFromExtras(Bundle extras) {
4153 super.restoreFromExtras(extras);
4154
4155 mTexts.clear();
4156 if (extras.containsKey(EXTRA_TEXT_LINES)) {
4157 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
4158 }
4159 }
4160
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004161 /**
4162 * @hide
4163 */
4164 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004165 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02004166 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004167 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
4168 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004169
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004170 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04004171
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004172 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004173
Chris Wrend6297db2012-05-03 16:20:13 -04004174 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 -04004175 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04004176
Chris Wren4ed80d52012-05-17 09:30:03 -04004177 // Make sure all rows are gone in case we reuse a view.
4178 for (int rowId : rowIds) {
4179 contentView.setViewVisibility(rowId, View.GONE);
4180 }
4181
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004182 final boolean largeText =
4183 mBuilder.mContext.getResources().getConfiguration().fontScale > 1f;
4184 final float subTextSize = mBuilder.mContext.getResources().getDimensionPixelSize(
4185 R.dimen.notification_subtext_size);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004186 int i=0;
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004187 final float density = mBuilder.mContext.getResources().getDisplayMetrics().density;
4188 int topPadding = (int) (5 * density);
4189 int bottomPadding = (int) (13 * density);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004190 while (i < mTexts.size() && i < rowIds.length) {
4191 CharSequence str = mTexts.get(i);
4192 if (str != null && !str.equals("")) {
4193 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004194 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004195 if (largeText) {
4196 contentView.setTextViewTextSize(rowIds[i], TypedValue.COMPLEX_UNIT_PX,
4197 subTextSize);
4198 }
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004199 contentView.setViewPadding(rowIds[i], 0, topPadding, 0,
4200 i == rowIds.length - 1 || i == mTexts.size() - 1 ? bottomPadding : 0);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004201 }
4202 i++;
4203 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004204
Selim Cinek1e0bf612015-11-20 15:57:26 -08004205 handleInboxImageMargin(contentView, rowIds[0]);
4206
Daniel Sandler879c5e02012-04-17 16:46:51 -04004207 return contentView;
4208 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004209
4210 private void handleInboxImageMargin(RemoteViews contentView, int id) {
4211 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
4212 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4213 boolean hasProgress = max != 0 || ind;
4214 int endMargin = 0;
4215 if (mTexts.size() > 0 && mBuilder.mN.mLargeIcon != null && !hasProgress) {
4216 endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4217 R.dimen.notification_content_picture_margin);
4218 }
4219 contentView.setViewLayoutMarginEnd(id, endMargin);
4220 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004221 }
Dan Sandler842dd772014-05-15 09:36:47 -04004222
4223 /**
4224 * Notification style for media playback notifications.
4225 *
4226 * In the expanded form, {@link Notification#bigContentView}, up to 5
4227 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04004228 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04004229 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
4230 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
4231 * treated as album artwork.
4232 *
4233 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
4234 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01004235 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04004236 * in the standard view alongside the usual content.
4237 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004238 * Notifications created with MediaStyle will have their category set to
4239 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
4240 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
4241 *
Jeff Browndba34ba2014-06-24 20:46:03 -07004242 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
4243 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04004244 * the System UI can identify this as a notification representing an active media session
4245 * and respond accordingly (by showing album artwork in the lockscreen, for example).
4246 *
4247 * To use this style with your Notification, feed it to
4248 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4249 * <pre class="prettyprint">
4250 * Notification noti = new Notification.Builder()
4251 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01004252 * .setContentTitle(&quot;Track title&quot;)
4253 * .setContentText(&quot;Artist - Album&quot;)
4254 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004255 * .setStyle(<b>new Notification.MediaStyle()</b>
4256 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04004257 * .build();
4258 * </pre>
4259 *
4260 * @see Notification#bigContentView
4261 */
4262 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004263 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04004264 static final int MAX_MEDIA_BUTTONS = 5;
4265
4266 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07004267 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04004268
4269 public MediaStyle() {
4270 }
4271
4272 public MediaStyle(Builder builder) {
4273 setBuilder(builder);
4274 }
4275
4276 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004277 * 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 -04004278 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004279 *
4280 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04004281 */
4282 public MediaStyle setShowActionsInCompactView(int...actions) {
4283 mActionsToShowInCompact = actions;
4284 return this;
4285 }
4286
4287 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07004288 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
4289 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04004290 */
Jeff Browndba34ba2014-06-24 20:46:03 -07004291 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04004292 mToken = token;
4293 return this;
4294 }
4295
Christoph Studer4600f9b2014-07-22 22:44:43 +02004296 /**
4297 * @hide
4298 */
Dan Sandler842dd772014-05-15 09:36:47 -04004299 @Override
4300 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004301 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004302 if (wip.category == null) {
4303 wip.category = Notification.CATEGORY_TRANSPORT;
4304 }
Dan Sandler842dd772014-05-15 09:36:47 -04004305 return wip;
4306 }
4307
Christoph Studer4600f9b2014-07-22 22:44:43 +02004308 /**
4309 * @hide
4310 */
4311 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004312 public RemoteViews makeContentView() {
4313 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004314 }
4315
4316 /**
4317 * @hide
4318 */
4319 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004320 public RemoteViews makeBigContentView() {
4321 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004322 }
4323
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004324 /**
4325 * @hide
4326 */
4327 @Override
4328 public RemoteViews makeHeadsUpContentView() {
4329 RemoteViews expanded = makeMediaBigContentView();
4330 return expanded != null ? expanded : makeMediaContentView();
4331 }
4332
Dan Sandler842dd772014-05-15 09:36:47 -04004333 /** @hide */
4334 @Override
4335 public void addExtras(Bundle extras) {
4336 super.addExtras(extras);
4337
4338 if (mToken != null) {
4339 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
4340 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01004341 if (mActionsToShowInCompact != null) {
4342 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
4343 }
Dan Sandler842dd772014-05-15 09:36:47 -04004344 }
4345
Christoph Studer4600f9b2014-07-22 22:44:43 +02004346 /**
4347 * @hide
4348 */
4349 @Override
4350 protected void restoreFromExtras(Bundle extras) {
4351 super.restoreFromExtras(extras);
4352
4353 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
4354 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
4355 }
4356 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
4357 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
4358 }
4359 }
4360
Selim Cinek5bf069a2015-11-10 19:14:27 -05004361 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04004362 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004363 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07004364 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04004365 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05004366 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
4367 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04004368 if (!tombstone) {
4369 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
4370 }
4371 button.setContentDescription(R.id.action0, action.title);
4372 return button;
4373 }
4374
4375 private RemoteViews makeMediaContentView() {
4376 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004377 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04004378
4379 final int numActions = mBuilder.mActions.size();
4380 final int N = mActionsToShowInCompact == null
4381 ? 0
4382 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4383 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004384 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04004385 for (int i = 0; i < N; i++) {
4386 if (i >= numActions) {
4387 throw new IllegalArgumentException(String.format(
4388 "setShowActionsInCompactView: action %d out of bounds (max %d)",
4389 i, numActions - 1));
4390 }
4391
4392 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05004393 final RemoteViews button = generateMediaActionButton(action,
4394 mBuilder.resolveColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004395 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004396 }
4397 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08004398 handleImage(view);
4399 // handle the content margin
4400 int endMargin;
4401 if (mBuilder.mN.mLargeIcon != null) {
4402 endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4403 R.dimen.notification_content_picture_margin_media);
4404 } else {
4405 endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4406 R.dimen.notification_content_margin_end);
4407 }
4408 view.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04004409 return view;
4410 }
4411
4412 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004413 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004414 // Dont add an expanded view if there is no more content to be revealed
4415 int actionsInCompact = mActionsToShowInCompact == null
4416 ? 0
4417 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4418 if (mBuilder.mN.mLargeIcon == null && actionCount <= actionsInCompact) {
4419 return null;
4420 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004421 RemoteViews big = mBuilder.applyStandardTemplate(
4422 R.layout.notification_template_material_big_media,
4423 false);
Dan Sandler842dd772014-05-15 09:36:47 -04004424
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004425 if (actionCount > 0) {
4426 big.removeAllViews(com.android.internal.R.id.media_actions);
4427 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05004428 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
4429 mBuilder.resolveColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004430 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004431 }
4432 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004433 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04004434 return big;
4435 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004436
Selim Cinek5bf069a2015-11-10 19:14:27 -05004437 private void handleImage(RemoteViews contentView) {
4438 if (mBuilder.mN.mLargeIcon != null) {
4439 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004440 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004441 }
4442 }
4443
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004444 /**
4445 * @hide
4446 */
4447 @Override
4448 protected boolean hasProgress() {
4449 return false;
4450 }
Dan Sandler842dd772014-05-15 09:36:47 -04004451 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004452
Christoph Studer4600f9b2014-07-22 22:44:43 +02004453 // When adding a new Style subclass here, don't forget to update
4454 // Builder.getNotificationStyleClass.
4455
Griff Hazen61a9e862014-05-22 16:05:19 -07004456 /**
4457 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
4458 * metadata or change options on a notification builder.
4459 */
4460 public interface Extender {
4461 /**
4462 * Apply this extender to a notification builder.
4463 * @param builder the builder to be modified.
4464 * @return the build object for chaining.
4465 */
4466 public Builder extend(Builder builder);
4467 }
4468
4469 /**
4470 * Helper class to add wearable extensions to notifications.
4471 * <p class="note"> See
4472 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
4473 * for Android Wear</a> for more information on how to use this class.
4474 * <p>
4475 * To create a notification with wearable extensions:
4476 * <ol>
4477 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
4478 * properties.
4479 * <li>Create a {@link android.app.Notification.WearableExtender}.
4480 * <li>Set wearable-specific properties using the
4481 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
4482 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
4483 * notification.
4484 * <li>Post the notification to the notification system with the
4485 * {@code NotificationManager.notify(...)} methods.
4486 * </ol>
4487 *
4488 * <pre class="prettyprint">
4489 * Notification notif = new Notification.Builder(mContext)
4490 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4491 * .setContentText(subject)
4492 * .setSmallIcon(R.drawable.new_mail)
4493 * .extend(new Notification.WearableExtender()
4494 * .setContentIcon(R.drawable.new_mail))
4495 * .build();
4496 * NotificationManager notificationManger =
4497 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
4498 * notificationManger.notify(0, notif);</pre>
4499 *
4500 * <p>Wearable extensions can be accessed on an existing notification by using the
4501 * {@code WearableExtender(Notification)} constructor,
4502 * and then using the {@code get} methods to access values.
4503 *
4504 * <pre class="prettyprint">
4505 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
4506 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07004507 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004508 */
4509 public static final class WearableExtender implements Extender {
4510 /**
4511 * Sentinel value for an action index that is unset.
4512 */
4513 public static final int UNSET_ACTION_INDEX = -1;
4514
4515 /**
4516 * Size value for use with {@link #setCustomSizePreset} to show this notification with
4517 * default sizing.
4518 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07004519 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07004520 * on their content.
4521 */
4522 public static final int SIZE_DEFAULT = 0;
4523
4524 /**
4525 * Size value for use with {@link #setCustomSizePreset} to show this notification
4526 * with an extra small size.
4527 * <p>This value is only applicable for custom display notifications created using
4528 * {@link #setDisplayIntent}.
4529 */
4530 public static final int SIZE_XSMALL = 1;
4531
4532 /**
4533 * Size value for use with {@link #setCustomSizePreset} to show this notification
4534 * with a small size.
4535 * <p>This value is only applicable for custom display notifications created using
4536 * {@link #setDisplayIntent}.
4537 */
4538 public static final int SIZE_SMALL = 2;
4539
4540 /**
4541 * Size value for use with {@link #setCustomSizePreset} to show this notification
4542 * with a medium size.
4543 * <p>This value is only applicable for custom display notifications created using
4544 * {@link #setDisplayIntent}.
4545 */
4546 public static final int SIZE_MEDIUM = 3;
4547
4548 /**
4549 * Size value for use with {@link #setCustomSizePreset} to show this notification
4550 * with a large size.
4551 * <p>This value is only applicable for custom display notifications created using
4552 * {@link #setDisplayIntent}.
4553 */
4554 public static final int SIZE_LARGE = 4;
4555
Griff Hazend5f11f92014-05-27 15:40:09 -07004556 /**
4557 * Size value for use with {@link #setCustomSizePreset} to show this notification
4558 * full screen.
4559 * <p>This value is only applicable for custom display notifications created using
4560 * {@link #setDisplayIntent}.
4561 */
4562 public static final int SIZE_FULL_SCREEN = 5;
4563
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004564 /**
4565 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
4566 * short amount of time when this notification is displayed on the screen. This
4567 * is the default value.
4568 */
4569 public static final int SCREEN_TIMEOUT_SHORT = 0;
4570
4571 /**
4572 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
4573 * for a longer amount of time when this notification is displayed on the screen.
4574 */
4575 public static final int SCREEN_TIMEOUT_LONG = -1;
4576
Griff Hazen61a9e862014-05-22 16:05:19 -07004577 /** Notification extra which contains wearable extensions */
4578 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
4579
Pete Gastaf6781d2014-10-07 15:17:05 -04004580 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07004581 private static final String KEY_ACTIONS = "actions";
4582 private static final String KEY_FLAGS = "flags";
4583 private static final String KEY_DISPLAY_INTENT = "displayIntent";
4584 private static final String KEY_PAGES = "pages";
4585 private static final String KEY_BACKGROUND = "background";
4586 private static final String KEY_CONTENT_ICON = "contentIcon";
4587 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
4588 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
4589 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
4590 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
4591 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004592 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Griff Hazen61a9e862014-05-22 16:05:19 -07004593
4594 // Flags bitwise-ored to mFlags
4595 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
4596 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
4597 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
4598 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004599 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Griff Hazen61a9e862014-05-22 16:05:19 -07004600
4601 // Default value for flags integer
4602 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
4603
4604 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
4605 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
4606
4607 private ArrayList<Action> mActions = new ArrayList<Action>();
4608 private int mFlags = DEFAULT_FLAGS;
4609 private PendingIntent mDisplayIntent;
4610 private ArrayList<Notification> mPages = new ArrayList<Notification>();
4611 private Bitmap mBackground;
4612 private int mContentIcon;
4613 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
4614 private int mContentActionIndex = UNSET_ACTION_INDEX;
4615 private int mCustomSizePreset = SIZE_DEFAULT;
4616 private int mCustomContentHeight;
4617 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004618 private int mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004619
4620 /**
4621 * Create a {@link android.app.Notification.WearableExtender} with default
4622 * options.
4623 */
4624 public WearableExtender() {
4625 }
4626
4627 public WearableExtender(Notification notif) {
4628 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
4629 if (wearableBundle != null) {
4630 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
4631 if (actions != null) {
4632 mActions.addAll(actions);
4633 }
4634
4635 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
4636 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
4637
4638 Notification[] pages = getNotificationArrayFromBundle(
4639 wearableBundle, KEY_PAGES);
4640 if (pages != null) {
4641 Collections.addAll(mPages, pages);
4642 }
4643
4644 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
4645 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
4646 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
4647 DEFAULT_CONTENT_ICON_GRAVITY);
4648 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
4649 UNSET_ACTION_INDEX);
4650 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
4651 SIZE_DEFAULT);
4652 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
4653 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004654 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Griff Hazen61a9e862014-05-22 16:05:19 -07004655 }
4656 }
4657
4658 /**
4659 * Apply wearable extensions to a notification that is being built. This is typically
4660 * called by the {@link android.app.Notification.Builder#extend} method of
4661 * {@link android.app.Notification.Builder}.
4662 */
4663 @Override
4664 public Notification.Builder extend(Notification.Builder builder) {
4665 Bundle wearableBundle = new Bundle();
4666
4667 if (!mActions.isEmpty()) {
4668 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
4669 }
4670 if (mFlags != DEFAULT_FLAGS) {
4671 wearableBundle.putInt(KEY_FLAGS, mFlags);
4672 }
4673 if (mDisplayIntent != null) {
4674 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
4675 }
4676 if (!mPages.isEmpty()) {
4677 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
4678 new Notification[mPages.size()]));
4679 }
4680 if (mBackground != null) {
4681 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
4682 }
4683 if (mContentIcon != 0) {
4684 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
4685 }
4686 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
4687 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
4688 }
4689 if (mContentActionIndex != UNSET_ACTION_INDEX) {
4690 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
4691 mContentActionIndex);
4692 }
4693 if (mCustomSizePreset != SIZE_DEFAULT) {
4694 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
4695 }
4696 if (mCustomContentHeight != 0) {
4697 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
4698 }
4699 if (mGravity != DEFAULT_GRAVITY) {
4700 wearableBundle.putInt(KEY_GRAVITY, mGravity);
4701 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004702 if (mHintScreenTimeout != 0) {
4703 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
4704 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004705
4706 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
4707 return builder;
4708 }
4709
4710 @Override
4711 public WearableExtender clone() {
4712 WearableExtender that = new WearableExtender();
4713 that.mActions = new ArrayList<Action>(this.mActions);
4714 that.mFlags = this.mFlags;
4715 that.mDisplayIntent = this.mDisplayIntent;
4716 that.mPages = new ArrayList<Notification>(this.mPages);
4717 that.mBackground = this.mBackground;
4718 that.mContentIcon = this.mContentIcon;
4719 that.mContentIconGravity = this.mContentIconGravity;
4720 that.mContentActionIndex = this.mContentActionIndex;
4721 that.mCustomSizePreset = this.mCustomSizePreset;
4722 that.mCustomContentHeight = this.mCustomContentHeight;
4723 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004724 that.mHintScreenTimeout = this.mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004725 return that;
4726 }
4727
4728 /**
4729 * Add a wearable action to this notification.
4730 *
4731 * <p>When wearable actions are added using this method, the set of actions that
4732 * show on a wearable device splits from devices that only show actions added
4733 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4734 * of which actions display on different devices.
4735 *
4736 * @param action the action to add to this notification
4737 * @return this object for method chaining
4738 * @see android.app.Notification.Action
4739 */
4740 public WearableExtender addAction(Action action) {
4741 mActions.add(action);
4742 return this;
4743 }
4744
4745 /**
4746 * Adds wearable actions to this notification.
4747 *
4748 * <p>When wearable actions are added using this method, the set of actions that
4749 * show on a wearable device splits from devices that only show actions added
4750 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4751 * of which actions display on different devices.
4752 *
4753 * @param actions the actions to add to this notification
4754 * @return this object for method chaining
4755 * @see android.app.Notification.Action
4756 */
4757 public WearableExtender addActions(List<Action> actions) {
4758 mActions.addAll(actions);
4759 return this;
4760 }
4761
4762 /**
4763 * Clear all wearable actions present on this builder.
4764 * @return this object for method chaining.
4765 * @see #addAction
4766 */
4767 public WearableExtender clearActions() {
4768 mActions.clear();
4769 return this;
4770 }
4771
4772 /**
4773 * Get the wearable actions present on this notification.
4774 */
4775 public List<Action> getActions() {
4776 return mActions;
4777 }
4778
4779 /**
4780 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07004781 * this notification. The {@link PendingIntent} provided should be for an activity.
4782 *
4783 * <pre class="prettyprint">
4784 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
4785 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
4786 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
4787 * Notification notif = new Notification.Builder(context)
4788 * .extend(new Notification.WearableExtender()
4789 * .setDisplayIntent(displayPendingIntent)
4790 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
4791 * .build();</pre>
4792 *
4793 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07004794 * should have an empty task affinity. It is also recommended to use the device
4795 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07004796 *
4797 * <p>Example AndroidManifest.xml entry:
4798 * <pre class="prettyprint">
4799 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
4800 * android:exported=&quot;true&quot;
4801 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07004802 * android:taskAffinity=&quot;&quot;
4803 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004804 *
4805 * @param intent the {@link PendingIntent} for an activity
4806 * @return this object for method chaining
4807 * @see android.app.Notification.WearableExtender#getDisplayIntent
4808 */
4809 public WearableExtender setDisplayIntent(PendingIntent intent) {
4810 mDisplayIntent = intent;
4811 return this;
4812 }
4813
4814 /**
4815 * Get the intent to launch inside of an activity view when displaying this
4816 * notification. This {@code PendingIntent} should be for an activity.
4817 */
4818 public PendingIntent getDisplayIntent() {
4819 return mDisplayIntent;
4820 }
4821
4822 /**
4823 * Add an additional page of content to display with this notification. The current
4824 * notification forms the first page, and pages added using this function form
4825 * subsequent pages. This field can be used to separate a notification into multiple
4826 * sections.
4827 *
4828 * @param page the notification to add as another page
4829 * @return this object for method chaining
4830 * @see android.app.Notification.WearableExtender#getPages
4831 */
4832 public WearableExtender addPage(Notification page) {
4833 mPages.add(page);
4834 return this;
4835 }
4836
4837 /**
4838 * Add additional pages of content to display with this notification. The current
4839 * notification forms the first page, and pages added using this function form
4840 * subsequent pages. This field can be used to separate a notification into multiple
4841 * sections.
4842 *
4843 * @param pages a list of notifications
4844 * @return this object for method chaining
4845 * @see android.app.Notification.WearableExtender#getPages
4846 */
4847 public WearableExtender addPages(List<Notification> pages) {
4848 mPages.addAll(pages);
4849 return this;
4850 }
4851
4852 /**
4853 * Clear all additional pages present on this builder.
4854 * @return this object for method chaining.
4855 * @see #addPage
4856 */
4857 public WearableExtender clearPages() {
4858 mPages.clear();
4859 return this;
4860 }
4861
4862 /**
4863 * Get the array of additional pages of content for displaying this notification. The
4864 * current notification forms the first page, and elements within this array form
4865 * subsequent pages. This field can be used to separate a notification into multiple
4866 * sections.
4867 * @return the pages for this notification
4868 */
4869 public List<Notification> getPages() {
4870 return mPages;
4871 }
4872
4873 /**
4874 * Set a background image to be displayed behind the notification content.
4875 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
4876 * will work with any notification style.
4877 *
4878 * @param background the background bitmap
4879 * @return this object for method chaining
4880 * @see android.app.Notification.WearableExtender#getBackground
4881 */
4882 public WearableExtender setBackground(Bitmap background) {
4883 mBackground = background;
4884 return this;
4885 }
4886
4887 /**
4888 * Get a background image to be displayed behind the notification content.
4889 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
4890 * will work with any notification style.
4891 *
4892 * @return the background image
4893 * @see android.app.Notification.WearableExtender#setBackground
4894 */
4895 public Bitmap getBackground() {
4896 return mBackground;
4897 }
4898
4899 /**
4900 * Set an icon that goes with the content of this notification.
4901 */
4902 public WearableExtender setContentIcon(int icon) {
4903 mContentIcon = icon;
4904 return this;
4905 }
4906
4907 /**
4908 * Get an icon that goes with the content of this notification.
4909 */
4910 public int getContentIcon() {
4911 return mContentIcon;
4912 }
4913
4914 /**
4915 * Set the gravity that the content icon should have within the notification display.
4916 * Supported values include {@link android.view.Gravity#START} and
4917 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
4918 * @see #setContentIcon
4919 */
4920 public WearableExtender setContentIconGravity(int contentIconGravity) {
4921 mContentIconGravity = contentIconGravity;
4922 return this;
4923 }
4924
4925 /**
4926 * Get the gravity that the content icon should have within the notification display.
4927 * Supported values include {@link android.view.Gravity#START} and
4928 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
4929 * @see #getContentIcon
4930 */
4931 public int getContentIconGravity() {
4932 return mContentIconGravity;
4933 }
4934
4935 /**
4936 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07004937 * this notification. This action will no longer display separately from the
4938 * notification's content.
4939 *
Griff Hazenca48d352014-05-28 22:37:13 -07004940 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07004941 * set, although the list of available actions comes from the main notification and not
4942 * from the child page's notification.
4943 *
4944 * @param actionIndex The index of the action to hoist onto the current notification page.
4945 * If wearable actions were added to the main notification, this index
4946 * will apply to that list, otherwise it will apply to the regular
4947 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07004948 */
4949 public WearableExtender setContentAction(int actionIndex) {
4950 mContentActionIndex = actionIndex;
4951 return this;
4952 }
4953
4954 /**
Griff Hazenca48d352014-05-28 22:37:13 -07004955 * Get the index of the notification action, if any, that was specified as being clickable
4956 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07004957 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07004958 *
Griff Hazenca48d352014-05-28 22:37:13 -07004959 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07004960 * set, although the list of available actions comes from the main notification and not
4961 * from the child page's notification.
4962 *
4963 * <p>If wearable specific actions were added to the main notification, this index will
4964 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07004965 *
4966 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07004967 */
4968 public int getContentAction() {
4969 return mContentActionIndex;
4970 }
4971
4972 /**
4973 * Set the gravity that this notification should have within the available viewport space.
4974 * Supported values include {@link android.view.Gravity#TOP},
4975 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
4976 * The default value is {@link android.view.Gravity#BOTTOM}.
4977 */
4978 public WearableExtender setGravity(int gravity) {
4979 mGravity = gravity;
4980 return this;
4981 }
4982
4983 /**
4984 * Get the gravity that this notification should have within the available viewport space.
4985 * Supported values include {@link android.view.Gravity#TOP},
4986 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
4987 * The default value is {@link android.view.Gravity#BOTTOM}.
4988 */
4989 public int getGravity() {
4990 return mGravity;
4991 }
4992
4993 /**
4994 * Set the custom size preset for the display of this notification out of the available
4995 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
4996 * {@link #SIZE_LARGE}.
4997 * <p>Some custom size presets are only applicable for custom display notifications created
4998 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
4999 * documentation for the preset in question. See also
5000 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
5001 */
5002 public WearableExtender setCustomSizePreset(int sizePreset) {
5003 mCustomSizePreset = sizePreset;
5004 return this;
5005 }
5006
5007 /**
5008 * Get the custom size preset for the display of this notification out of the available
5009 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5010 * {@link #SIZE_LARGE}.
5011 * <p>Some custom size presets are only applicable for custom display notifications created
5012 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
5013 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
5014 */
5015 public int getCustomSizePreset() {
5016 return mCustomSizePreset;
5017 }
5018
5019 /**
5020 * Set the custom height in pixels for the display of this notification's content.
5021 * <p>This option is only available for custom display notifications created
5022 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
5023 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
5024 * {@link #getCustomContentHeight}.
5025 */
5026 public WearableExtender setCustomContentHeight(int height) {
5027 mCustomContentHeight = height;
5028 return this;
5029 }
5030
5031 /**
5032 * Get the custom height in pixels for the display of this notification's content.
5033 * <p>This option is only available for custom display notifications created
5034 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
5035 * {@link #setCustomContentHeight}.
5036 */
5037 public int getCustomContentHeight() {
5038 return mCustomContentHeight;
5039 }
5040
5041 /**
5042 * Set whether the scrolling position for the contents of this notification should start
5043 * at the bottom of the contents instead of the top when the contents are too long to
5044 * display within the screen. Default is false (start scroll at the top).
5045 */
5046 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
5047 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
5048 return this;
5049 }
5050
5051 /**
5052 * Get whether the scrolling position for the contents of this notification should start
5053 * at the bottom of the contents instead of the top when the contents are too long to
5054 * display within the screen. Default is false (start scroll at the top).
5055 */
5056 public boolean getStartScrollBottom() {
5057 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
5058 }
5059
5060 /**
5061 * Set whether the content intent is available when the wearable device is not connected
5062 * to a companion device. The user can still trigger this intent when the wearable device
5063 * is offline, but a visual hint will indicate that the content intent may not be available.
5064 * Defaults to true.
5065 */
5066 public WearableExtender setContentIntentAvailableOffline(
5067 boolean contentIntentAvailableOffline) {
5068 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
5069 return this;
5070 }
5071
5072 /**
5073 * Get whether the content intent is available when the wearable device is not connected
5074 * to a companion device. The user can still trigger this intent when the wearable device
5075 * is offline, but a visual hint will indicate that the content intent may not be available.
5076 * Defaults to true.
5077 */
5078 public boolean getContentIntentAvailableOffline() {
5079 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
5080 }
5081
5082 /**
5083 * Set a hint that this notification's icon should not be displayed.
5084 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
5085 * @return this object for method chaining
5086 */
5087 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
5088 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
5089 return this;
5090 }
5091
5092 /**
5093 * Get a hint that this notification's icon should not be displayed.
5094 * @return {@code true} if this icon should not be displayed, false otherwise.
5095 * The default value is {@code false} if this was never set.
5096 */
5097 public boolean getHintHideIcon() {
5098 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
5099 }
5100
5101 /**
5102 * Set a visual hint that only the background image of this notification should be
5103 * displayed, and other semantic content should be hidden. This hint is only applicable
5104 * to sub-pages added using {@link #addPage}.
5105 */
5106 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
5107 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
5108 return this;
5109 }
5110
5111 /**
5112 * Get a visual hint that only the background image of this notification should be
5113 * displayed, and other semantic content should be hidden. This hint is only applicable
5114 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
5115 */
5116 public boolean getHintShowBackgroundOnly() {
5117 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
5118 }
5119
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005120 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005121 * Set a hint that this notification's background should not be clipped if possible,
5122 * and should instead be resized to fully display on the screen, retaining the aspect
5123 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005124 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
5125 * @return this object for method chaining
5126 */
5127 public WearableExtender setHintAvoidBackgroundClipping(
5128 boolean hintAvoidBackgroundClipping) {
5129 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
5130 return this;
5131 }
5132
5133 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005134 * Get a hint that this notification's background should not be clipped if possible,
5135 * and should instead be resized to fully display on the screen, retaining the aspect
5136 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005137 * @return {@code true} if it's ok if the background is clipped on the screen, false
5138 * otherwise. The default value is {@code false} if this was never set.
5139 */
5140 public boolean getHintAvoidBackgroundClipping() {
5141 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
5142 }
5143
5144 /**
5145 * Set a hint that the screen should remain on for at least this duration when
5146 * this notification is displayed on the screen.
5147 * @param timeout The requested screen timeout in milliseconds. Can also be either
5148 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5149 * @return this object for method chaining
5150 */
5151 public WearableExtender setHintScreenTimeout(int timeout) {
5152 mHintScreenTimeout = timeout;
5153 return this;
5154 }
5155
5156 /**
5157 * Get the duration, in milliseconds, that the screen should remain on for
5158 * when this notification is displayed.
5159 * @return the duration in milliseconds if > 0, or either one of the sentinel values
5160 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5161 */
5162 public int getHintScreenTimeout() {
5163 return mHintScreenTimeout;
5164 }
5165
Griff Hazen61a9e862014-05-22 16:05:19 -07005166 private void setFlag(int mask, boolean value) {
5167 if (value) {
5168 mFlags |= mask;
5169 } else {
5170 mFlags &= ~mask;
5171 }
5172 }
5173 }
5174
5175 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005176 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
5177 * with car extensions:
5178 *
5179 * <ol>
5180 * <li>Create an {@link Notification.Builder}, setting any desired
5181 * properties.
5182 * <li>Create a {@link CarExtender}.
5183 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
5184 * {@link CarExtender}.
5185 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
5186 * to apply the extensions to a notification.
5187 * </ol>
5188 *
5189 * <pre class="prettyprint">
5190 * Notification notification = new Notification.Builder(context)
5191 * ...
5192 * .extend(new CarExtender()
5193 * .set*(...))
5194 * .build();
5195 * </pre>
5196 *
5197 * <p>Car extensions can be accessed on an existing notification by using the
5198 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
5199 * to access values.
5200 */
5201 public static final class CarExtender implements Extender {
5202 private static final String TAG = "CarExtender";
5203
5204 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
5205 private static final String EXTRA_LARGE_ICON = "large_icon";
5206 private static final String EXTRA_CONVERSATION = "car_conversation";
5207 private static final String EXTRA_COLOR = "app_color";
5208
5209 private Bitmap mLargeIcon;
5210 private UnreadConversation mUnreadConversation;
5211 private int mColor = Notification.COLOR_DEFAULT;
5212
5213 /**
5214 * Create a {@link CarExtender} with default options.
5215 */
5216 public CarExtender() {
5217 }
5218
5219 /**
5220 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
5221 *
5222 * @param notif The notification from which to copy options.
5223 */
5224 public CarExtender(Notification notif) {
5225 Bundle carBundle = notif.extras == null ?
5226 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
5227 if (carBundle != null) {
5228 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
5229 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
5230
5231 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
5232 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
5233 }
5234 }
5235
5236 /**
5237 * Apply car extensions to a notification that is being built. This is typically called by
5238 * the {@link Notification.Builder#extend(Notification.Extender)}
5239 * method of {@link Notification.Builder}.
5240 */
5241 @Override
5242 public Notification.Builder extend(Notification.Builder builder) {
5243 Bundle carExtensions = new Bundle();
5244
5245 if (mLargeIcon != null) {
5246 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
5247 }
5248 if (mColor != Notification.COLOR_DEFAULT) {
5249 carExtensions.putInt(EXTRA_COLOR, mColor);
5250 }
5251
5252 if (mUnreadConversation != null) {
5253 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
5254 carExtensions.putBundle(EXTRA_CONVERSATION, b);
5255 }
5256
5257 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
5258 return builder;
5259 }
5260
5261 /**
5262 * Sets the accent color to use when Android Auto presents the notification.
5263 *
5264 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
5265 * to accent the displayed notification. However, not all colors are acceptable in an
5266 * automotive setting. This method can be used to override the color provided in the
5267 * notification in such a situation.
5268 */
Tor Norbye80756e32015-03-02 09:39:27 -08005269 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005270 mColor = color;
5271 return this;
5272 }
5273
5274 /**
5275 * Gets the accent color.
5276 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005277 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005278 */
Tor Norbye80756e32015-03-02 09:39:27 -08005279 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005280 public int getColor() {
5281 return mColor;
5282 }
5283
5284 /**
5285 * Sets the large icon of the car notification.
5286 *
5287 * If no large icon is set in the extender, Android Auto will display the icon
5288 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
5289 *
5290 * @param largeIcon The large icon to use in the car notification.
5291 * @return This object for method chaining.
5292 */
5293 public CarExtender setLargeIcon(Bitmap largeIcon) {
5294 mLargeIcon = largeIcon;
5295 return this;
5296 }
5297
5298 /**
5299 * Gets the large icon used in this car notification, or null if no icon has been set.
5300 *
5301 * @return The large icon for the car notification.
5302 * @see CarExtender#setLargeIcon
5303 */
5304 public Bitmap getLargeIcon() {
5305 return mLargeIcon;
5306 }
5307
5308 /**
5309 * Sets the unread conversation in a message notification.
5310 *
5311 * @param unreadConversation The unread part of the conversation this notification conveys.
5312 * @return This object for method chaining.
5313 */
5314 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
5315 mUnreadConversation = unreadConversation;
5316 return this;
5317 }
5318
5319 /**
5320 * Returns the unread conversation conveyed by this notification.
5321 * @see #setUnreadConversation(UnreadConversation)
5322 */
5323 public UnreadConversation getUnreadConversation() {
5324 return mUnreadConversation;
5325 }
5326
5327 /**
5328 * A class which holds the unread messages from a conversation.
5329 */
5330 public static class UnreadConversation {
5331 private static final String KEY_AUTHOR = "author";
5332 private static final String KEY_TEXT = "text";
5333 private static final String KEY_MESSAGES = "messages";
5334 private static final String KEY_REMOTE_INPUT = "remote_input";
5335 private static final String KEY_ON_REPLY = "on_reply";
5336 private static final String KEY_ON_READ = "on_read";
5337 private static final String KEY_PARTICIPANTS = "participants";
5338 private static final String KEY_TIMESTAMP = "timestamp";
5339
5340 private final String[] mMessages;
5341 private final RemoteInput mRemoteInput;
5342 private final PendingIntent mReplyPendingIntent;
5343 private final PendingIntent mReadPendingIntent;
5344 private final String[] mParticipants;
5345 private final long mLatestTimestamp;
5346
5347 UnreadConversation(String[] messages, RemoteInput remoteInput,
5348 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
5349 String[] participants, long latestTimestamp) {
5350 mMessages = messages;
5351 mRemoteInput = remoteInput;
5352 mReadPendingIntent = readPendingIntent;
5353 mReplyPendingIntent = replyPendingIntent;
5354 mParticipants = participants;
5355 mLatestTimestamp = latestTimestamp;
5356 }
5357
5358 /**
5359 * Gets the list of messages conveyed by this notification.
5360 */
5361 public String[] getMessages() {
5362 return mMessages;
5363 }
5364
5365 /**
5366 * Gets the remote input that will be used to convey the response to a message list, or
5367 * null if no such remote input exists.
5368 */
5369 public RemoteInput getRemoteInput() {
5370 return mRemoteInput;
5371 }
5372
5373 /**
5374 * Gets the pending intent that will be triggered when the user replies to this
5375 * notification.
5376 */
5377 public PendingIntent getReplyPendingIntent() {
5378 return mReplyPendingIntent;
5379 }
5380
5381 /**
5382 * Gets the pending intent that Android Auto will send after it reads aloud all messages
5383 * in this object's message list.
5384 */
5385 public PendingIntent getReadPendingIntent() {
5386 return mReadPendingIntent;
5387 }
5388
5389 /**
5390 * Gets the participants in the conversation.
5391 */
5392 public String[] getParticipants() {
5393 return mParticipants;
5394 }
5395
5396 /**
5397 * Gets the firs participant in the conversation.
5398 */
5399 public String getParticipant() {
5400 return mParticipants.length > 0 ? mParticipants[0] : null;
5401 }
5402
5403 /**
5404 * Gets the timestamp of the conversation.
5405 */
5406 public long getLatestTimestamp() {
5407 return mLatestTimestamp;
5408 }
5409
5410 Bundle getBundleForUnreadConversation() {
5411 Bundle b = new Bundle();
5412 String author = null;
5413 if (mParticipants != null && mParticipants.length > 1) {
5414 author = mParticipants[0];
5415 }
5416 Parcelable[] messages = new Parcelable[mMessages.length];
5417 for (int i = 0; i < messages.length; i++) {
5418 Bundle m = new Bundle();
5419 m.putString(KEY_TEXT, mMessages[i]);
5420 m.putString(KEY_AUTHOR, author);
5421 messages[i] = m;
5422 }
5423 b.putParcelableArray(KEY_MESSAGES, messages);
5424 if (mRemoteInput != null) {
5425 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
5426 }
5427 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
5428 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
5429 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
5430 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
5431 return b;
5432 }
5433
5434 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
5435 if (b == null) {
5436 return null;
5437 }
5438 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
5439 String[] messages = null;
5440 if (parcelableMessages != null) {
5441 String[] tmp = new String[parcelableMessages.length];
5442 boolean success = true;
5443 for (int i = 0; i < tmp.length; i++) {
5444 if (!(parcelableMessages[i] instanceof Bundle)) {
5445 success = false;
5446 break;
5447 }
5448 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
5449 if (tmp[i] == null) {
5450 success = false;
5451 break;
5452 }
5453 }
5454 if (success) {
5455 messages = tmp;
5456 } else {
5457 return null;
5458 }
5459 }
5460
5461 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
5462 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
5463
5464 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
5465
5466 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
5467 if (participants == null || participants.length != 1) {
5468 return null;
5469 }
5470
5471 return new UnreadConversation(messages,
5472 remoteInput,
5473 onReply,
5474 onRead,
5475 participants, b.getLong(KEY_TIMESTAMP));
5476 }
5477 };
5478
5479 /**
5480 * Builder class for {@link CarExtender.UnreadConversation} objects.
5481 */
5482 public static class Builder {
5483 private final List<String> mMessages = new ArrayList<String>();
5484 private final String mParticipant;
5485 private RemoteInput mRemoteInput;
5486 private PendingIntent mReadPendingIntent;
5487 private PendingIntent mReplyPendingIntent;
5488 private long mLatestTimestamp;
5489
5490 /**
5491 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
5492 *
5493 * @param name The name of the other participant in the conversation.
5494 */
5495 public Builder(String name) {
5496 mParticipant = name;
5497 }
5498
5499 /**
5500 * Appends a new unread message to the list of messages for this conversation.
5501 *
5502 * The messages should be added from oldest to newest.
5503 *
5504 * @param message The text of the new unread message.
5505 * @return This object for method chaining.
5506 */
5507 public Builder addMessage(String message) {
5508 mMessages.add(message);
5509 return this;
5510 }
5511
5512 /**
5513 * Sets the pending intent and remote input which will convey the reply to this
5514 * notification.
5515 *
5516 * @param pendingIntent The pending intent which will be triggered on a reply.
5517 * @param remoteInput The remote input parcelable which will carry the reply.
5518 * @return This object for method chaining.
5519 *
5520 * @see CarExtender.UnreadConversation#getRemoteInput
5521 * @see CarExtender.UnreadConversation#getReplyPendingIntent
5522 */
5523 public Builder setReplyAction(
5524 PendingIntent pendingIntent, RemoteInput remoteInput) {
5525 mRemoteInput = remoteInput;
5526 mReplyPendingIntent = pendingIntent;
5527
5528 return this;
5529 }
5530
5531 /**
5532 * Sets the pending intent that will be sent once the messages in this notification
5533 * are read.
5534 *
5535 * @param pendingIntent The pending intent to use.
5536 * @return This object for method chaining.
5537 */
5538 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
5539 mReadPendingIntent = pendingIntent;
5540 return this;
5541 }
5542
5543 /**
5544 * Sets the timestamp of the most recent message in an unread conversation.
5545 *
5546 * If a messaging notification has been posted by your application and has not
5547 * yet been cancelled, posting a later notification with the same id and tag
5548 * but without a newer timestamp may result in Android Auto not displaying a
5549 * heads up notification for the later notification.
5550 *
5551 * @param timestamp The timestamp of the most recent message in the conversation.
5552 * @return This object for method chaining.
5553 */
5554 public Builder setLatestTimestamp(long timestamp) {
5555 mLatestTimestamp = timestamp;
5556 return this;
5557 }
5558
5559 /**
5560 * Builds a new unread conversation object.
5561 *
5562 * @return The new unread conversation object.
5563 */
5564 public UnreadConversation build() {
5565 String[] messages = mMessages.toArray(new String[mMessages.size()]);
5566 String[] participants = { mParticipant };
5567 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
5568 mReadPendingIntent, participants, mLatestTimestamp);
5569 }
5570 }
5571 }
5572
5573 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07005574 * Get an array of Notification objects from a parcelable array bundle field.
5575 * Update the bundle to have a typed array so fetches in the future don't need
5576 * to do an array copy.
5577 */
5578 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
5579 Parcelable[] array = bundle.getParcelableArray(key);
5580 if (array instanceof Notification[] || array == null) {
5581 return (Notification[]) array;
5582 }
5583 Notification[] typedArray = Arrays.copyOf(array, array.length,
5584 Notification[].class);
5585 bundle.putParcelableArray(key, typedArray);
5586 return typedArray;
5587 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005588
5589 private static class BuilderRemoteViews extends RemoteViews {
5590 public BuilderRemoteViews(Parcel parcel) {
5591 super(parcel);
5592 }
5593
Kenny Guy77320062014-08-27 21:37:15 +01005594 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
5595 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005596 }
5597
5598 @Override
5599 public BuilderRemoteViews clone() {
5600 Parcel p = Parcel.obtain();
5601 writeToParcel(p, 0);
5602 p.setDataPosition(0);
5603 BuilderRemoteViews brv = new BuilderRemoteViews(p);
5604 p.recycle();
5605 return brv;
5606 }
5607 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005608}