blob: 01a9ff9864f3236cef0ab461359a72d9a84886fa [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;
Adrian Roos9b123cf2016-02-04 14:55:57 -080054import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070055import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.widget.RemoteViews;
57
Griff Hazen959591e2014-05-15 22:26:18 -070058import com.android.internal.R;
Griff Hazenc091ba82014-05-16 10:13:26 -070059import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070060
Tor Norbyed9273d62013-05-30 15:59:53 -070061import java.lang.annotation.Retention;
62import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020063import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050064import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070065import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070066import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070067import java.util.List;
Julia Reynolds74303cf2015-10-16 11:37:55 -040068import java.util.Objects;
Dan Sandler50128532015-12-08 15:42:41 -050069import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071/**
72 * A class that represents how a persistent notification is to be presented to
73 * the user using the {@link android.app.NotificationManager}.
74 *
Joe Onoratocb109a02011-01-18 17:57:41 -080075 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
76 * easier to construct Notifications.</p>
77 *
Joe Fernandez558459f2011-10-13 16:47:36 -070078 * <div class="special reference">
79 * <h3>Developer Guides</h3>
80 * <p>For a guide to creating notifications, read the
81 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
82 * developer guide.</p>
83 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 */
85public class Notification implements Parcelable
86{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040087 private static final String TAG = "Notification";
88
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -040090 * An activity that provides a user interface for adjusting notification preferences for its
91 * containing application. Optional but recommended for apps that post
92 * {@link android.app.Notification Notifications}.
93 */
94 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
95 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
96 = "android.intent.category.NOTIFICATION_PREFERENCES";
97
98 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 * Use all default values (where applicable).
100 */
101 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 /**
104 * Use the default notification sound. This will ignore any given
105 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500106 *
Chris Wren47c20a12014-06-18 17:27:29 -0400107 * <p>
108 * A notification that is noisy is more likely to be presented as a heads-up notification.
109 * </p>
110 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500112 */
113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 public static final int DEFAULT_SOUND = 1;
115
116 /**
117 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500118 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700119 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500120 *
Chris Wren47c20a12014-06-18 17:27:29 -0400121 * <p>
122 * A notification that vibrates is more likely to be presented as a heads-up notification.
123 * </p>
124 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500126 */
127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 /**
131 * Use the default notification lights. This will ignore the
132 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
133 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500134 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500136 */
137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200141 * Maximum length of CharSequences accepted by Builder and friends.
142 *
143 * <p>
144 * Avoids spamming the system with overly large strings such as full e-mails.
145 */
146 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
147
148 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800149 * Maximum entries of reply text that are accepted by Builder and friends.
150 */
151 private static final int MAX_REPLY_HISTORY = 5;
152
153 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500154 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800155 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500156 * Default value: {@link System#currentTimeMillis() Now}.
157 *
158 * Choose a timestamp that will be most relevant to the user. For most finite events, this
159 * corresponds to the time the event happened (or will happen, in the case of events that have
160 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800161 * timestamped according to when the activity began.
162 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500163 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800164 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500165 * <ul>
166 * <li>Notification of a new chat message should be stamped when the message was received.</li>
167 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
168 * <li>Notification of a completed file download should be stamped when the download finished.</li>
169 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
170 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
171 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800172 * </ul>
173 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 */
175 public long when;
176
177 /**
178 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400179 *
180 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 */
Dan Sandler86647982015-05-13 23:41:13 -0400182 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700183 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 public int icon;
185
186 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800187 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
188 * leave it at its default value of 0.
189 *
190 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700191 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800192 */
193 public int iconLevel;
194
195 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500196 * The number of events that this notification represents. For example, in a new mail
197 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800198 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500199 * The system may or may not use this field to modify the appearance of the notification. For
200 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
201 * superimposed over the icon in the status bar. Starting with
202 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
203 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800204 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500205 * If the number is 0 or negative, it is never shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 */
207 public int number;
208
209 /**
210 * The intent to execute when the expanded status entry is clicked. If
211 * this is an activity, it must include the
212 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800213 * that you take care of task management as described in the
214 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800215 * Stack</a> document. In particular, make sure to read the notification section
216 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
217 * Notifications</a> for the correct ways to launch an application from a
218 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 */
220 public PendingIntent contentIntent;
221
222 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500223 * The intent to execute when the notification is explicitly dismissed by the user, either with
224 * the "Clear All" button or by swiping it away individually.
225 *
226 * This probably shouldn't be launching an activity since several of those will be sent
227 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 */
229 public PendingIntent deleteIntent;
230
231 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700232 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800233 *
Chris Wren47c20a12014-06-18 17:27:29 -0400234 * <p>
235 * The system UI may choose to display a heads-up notification, instead of
236 * launching this intent, while the user is using the device.
237 * </p>
238 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800239 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400240 */
241 public PendingIntent fullScreenIntent;
242
243 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400244 * Text that summarizes this notification for accessibility services.
245 *
246 * As of the L release, this text is no longer shown on screen, but it is still useful to
247 * accessibility services (where it serves as an audible announcement of the notification's
248 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400249 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800250 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 */
252 public CharSequence tickerText;
253
254 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400255 * Formerly, a view showing the {@link #tickerText}.
256 *
257 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400258 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400259 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800260 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400261
262 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400263 * The view that will represent this notification in the notification list (which is pulled
264 * down from the status bar).
265 *
266 * As of N, this field is not used. The notification view is determined by the inputs to
267 * {@link Notification.Builder}; a custom RemoteViews can optionally be
268 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400270 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 public RemoteViews contentView;
272
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400273 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400274 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400275 * opportunity to show more detail. The system UI may choose to show this
276 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400277 *
278 * As of N, this field is not used. The expanded notification view is determined by the
279 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
280 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400281 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400282 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400283 public RemoteViews bigContentView;
284
Chris Wren8fd39ec2014-02-27 17:43:26 -0500285
286 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400287 * A medium-format version of {@link #contentView}, providing the Notification an
288 * opportunity to add action buttons to contentView. At its discretion, the system UI may
289 * choose to show this as a heads-up notification, which will pop up so the user can see
290 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400291 *
292 * As of N, this field is not used. The heads-up notification view is determined by the
293 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
294 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500295 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400296 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500297 public RemoteViews headsUpContentView;
298
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400299 /**
Dan Sandler86647982015-05-13 23:41:13 -0400300 * A large bitmap to be shown in the notification content area.
301 *
302 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 */
Dan Sandler86647982015-05-13 23:41:13 -0400304 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800305 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306
307 /**
308 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500309 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400311 * A notification that is noisy is more likely to be presented as a heads-up notification.
312 * </p>
313 *
314 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500315 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 * </p>
317 */
318 public Uri sound;
319
320 /**
321 * Use this constant as the value for audioStreamType to request that
322 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700323 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400324 *
325 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700327 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 public static final int STREAM_DEFAULT = -1;
329
330 /**
331 * The audio stream type to use when playing the sound.
332 * Should be one of the STREAM_ constants from
333 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400334 *
335 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700337 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 public int audioStreamType = STREAM_DEFAULT;
339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400341 * The default value of {@link #audioAttributes}.
342 */
343 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
344 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
345 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
346 .build();
347
348 /**
349 * The {@link AudioAttributes audio attributes} to use when playing the sound.
350 */
351 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
352
353 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500354 * The pattern with which to vibrate.
355 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 * <p>
357 * To vibrate the default pattern, see {@link #defaults}.
358 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500359 *
Chris Wren47c20a12014-06-18 17:27:29 -0400360 * <p>
361 * A notification that vibrates is more likely to be presented as a heads-up notification.
362 * </p>
363 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 * @see android.os.Vibrator#vibrate(long[],int)
365 */
366 public long[] vibrate;
367
368 /**
369 * The color of the led. The hardware will do its best approximation.
370 *
371 * @see #FLAG_SHOW_LIGHTS
372 * @see #flags
373 */
Tor Norbye80756e32015-03-02 09:39:27 -0800374 @ColorInt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 public int ledARGB;
376
377 /**
378 * The number of milliseconds for the LED to be on while it's flashing.
379 * The hardware will do its best approximation.
380 *
381 * @see #FLAG_SHOW_LIGHTS
382 * @see #flags
383 */
384 public int ledOnMS;
385
386 /**
387 * The number of milliseconds for the LED to be off while it's flashing.
388 * The hardware will do its best approximation.
389 *
390 * @see #FLAG_SHOW_LIGHTS
391 * @see #flags
392 */
393 public int ledOffMS;
394
395 /**
396 * Specifies which values should be taken from the defaults.
397 * <p>
398 * To set, OR the desired from {@link #DEFAULT_SOUND},
399 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
400 * values, use {@link #DEFAULT_ALL}.
401 * </p>
402 */
403 public int defaults;
404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 /**
406 * Bit to be bitwise-ored into the {@link #flags} field that should be
407 * set if you want the LED on for this notification.
408 * <ul>
409 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
410 * or 0 for both ledOnMS and ledOffMS.</li>
411 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
412 * <li>To flash the LED, pass the number of milliseconds that it should
413 * be on and off to ledOnMS and ledOffMS.</li>
414 * </ul>
415 * <p>
416 * Since hardware varies, you are not guaranteed that any of the values
417 * you pass are honored exactly. Use the system defaults (TODO) if possible
418 * because they will be set to values that work on any given hardware.
419 * <p>
420 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500421 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 */
423 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
424
425 /**
426 * Bit to be bitwise-ored into the {@link #flags} field that should be
427 * set if this notification is in reference to something that is ongoing,
428 * like a phone call. It should not be set if this notification is in
429 * reference to something that happened at a particular point in time,
430 * like a missed phone call.
431 */
432 public static final int FLAG_ONGOING_EVENT = 0x00000002;
433
434 /**
435 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700436 * the audio will be repeated until the notification is
437 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 */
439 public static final int FLAG_INSISTENT = 0x00000004;
440
441 /**
442 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700443 * set if you would only like the sound, vibrate and ticker to be played
444 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 */
446 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
447
448 /**
449 * Bit to be bitwise-ored into the {@link #flags} field that should be
450 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500451 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 */
453 public static final int FLAG_AUTO_CANCEL = 0x00000010;
454
455 /**
456 * Bit to be bitwise-ored into the {@link #flags} field that should be
457 * set if the notification should not be canceled when the user clicks
458 * the Clear all button.
459 */
460 public static final int FLAG_NO_CLEAR = 0x00000020;
461
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700462 /**
463 * Bit to be bitwise-ored into the {@link #flags} field that should be
464 * set if this notification represents a currently running service. This
465 * will normally be set for you by {@link Service#startForeground}.
466 */
467 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
468
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400469 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500470 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800471 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500472 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400473 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500474 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400475
Griff Hazendfcb0802014-02-11 12:00:00 -0800476 /**
477 * Bit to be bitswise-ored into the {@link #flags} field that should be
478 * set if this notification is relevant to the current device only
479 * and it is not recommended that it bridge to other devices.
480 */
481 public static final int FLAG_LOCAL_ONLY = 0x00000100;
482
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700483 /**
484 * Bit to be bitswise-ored into the {@link #flags} field that should be
485 * set if this notification is the group summary for a group of notifications.
486 * Grouped notifications may display in a cluster or stack on devices which
487 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
488 */
489 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 public int flags;
492
Tor Norbyed9273d62013-05-30 15:59:53 -0700493 /** @hide */
494 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
495 @Retention(RetentionPolicy.SOURCE)
496 public @interface Priority {}
497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500499 * Default notification {@link #priority}. If your application does not prioritize its own
500 * notifications, use this value for all notifications.
501 */
502 public static final int PRIORITY_DEFAULT = 0;
503
504 /**
505 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
506 * items smaller, or at a different position in the list, compared with your app's
507 * {@link #PRIORITY_DEFAULT} items.
508 */
509 public static final int PRIORITY_LOW = -1;
510
511 /**
512 * Lowest {@link #priority}; these items might not be shown to the user except under special
513 * circumstances, such as detailed notification logs.
514 */
515 public static final int PRIORITY_MIN = -2;
516
517 /**
518 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
519 * show these items larger, or at a different position in notification lists, compared with
520 * your app's {@link #PRIORITY_DEFAULT} items.
521 */
522 public static final int PRIORITY_HIGH = 1;
523
524 /**
525 * Highest {@link #priority}, for your application's most important items that require the
526 * user's prompt attention or input.
527 */
528 public static final int PRIORITY_MAX = 2;
529
530 /**
531 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800532 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500533 * Priority is an indication of how much of the user's valuable attention should be consumed by
534 * this notification. Low-priority notifications may be hidden from the user in certain
535 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500536 * system will make a determination about how to interpret this priority when presenting
537 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400538 *
539 * <p>
540 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
541 * as a heads-up notification.
542 * </p>
543 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500544 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700545 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500546 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800547
Dan Sandler26e81cf2014-05-06 10:01:27 -0400548 /**
549 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
550 * to be applied by the standard Style templates when presenting this notification.
551 *
552 * The current template design constructs a colorful header image by overlaying the
553 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
554 * ignored.
555 */
Tor Norbye80756e32015-03-02 09:39:27 -0800556 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400557 public int color = COLOR_DEFAULT;
558
559 /**
560 * Special value of {@link #color} telling the system not to decorate this notification with
561 * any special color but instead use default colors when presenting this notification.
562 */
Tor Norbye80756e32015-03-02 09:39:27 -0800563 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400564 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600565
566 /**
567 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
568 * the notification's presence and contents in untrusted situations (namely, on the secure
569 * lockscreen).
570 *
571 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
572 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
573 * shown in all situations, but the contents are only available if the device is unlocked for
574 * the appropriate user.
575 *
576 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
577 * can be read even in an "insecure" context (that is, above a secure lockscreen).
578 * To modify the public version of this notification—for example, to redact some portions—see
579 * {@link Builder#setPublicVersion(Notification)}.
580 *
581 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
582 * and ticker until the user has bypassed the lockscreen.
583 */
584 public int visibility;
585
Griff Hazenfc3922d2014-08-20 11:56:44 -0700586 /**
587 * Notification visibility: Show this notification in its entirety on all lockscreens.
588 *
589 * {@see #visibility}
590 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600591 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700592
593 /**
594 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
595 * private information on secure lockscreens.
596 *
597 * {@see #visibility}
598 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600599 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700600
601 /**
602 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
603 *
604 * {@see #visibility}
605 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600606 public static final int VISIBILITY_SECRET = -1;
607
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500608 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400609 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500610 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400611 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500612
613 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400614 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500615 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400616 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500617
618 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400619 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500620 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400621 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500622
623 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400624 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500625 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400626 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500627
628 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400629 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500630 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400631 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500632
633 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400634 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500635 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400636 public static final String CATEGORY_ALARM = "alarm";
637
638 /**
639 * Notification category: progress of a long-running background operation.
640 */
641 public static final String CATEGORY_PROGRESS = "progress";
642
643 /**
644 * Notification category: social network or sharing update.
645 */
646 public static final String CATEGORY_SOCIAL = "social";
647
648 /**
649 * Notification category: error in background operation or authentication status.
650 */
651 public static final String CATEGORY_ERROR = "err";
652
653 /**
654 * Notification category: media transport control for playback.
655 */
656 public static final String CATEGORY_TRANSPORT = "transport";
657
658 /**
659 * Notification category: system or device status update. Reserved for system use.
660 */
661 public static final String CATEGORY_SYSTEM = "sys";
662
663 /**
664 * Notification category: indication of running background service.
665 */
666 public static final String CATEGORY_SERVICE = "service";
667
668 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400669 * Notification category: a specific, timely recommendation for a single thing.
670 * For example, a news app might want to recommend a news story it believes the user will
671 * want to read next.
672 */
673 public static final String CATEGORY_RECOMMENDATION = "recommendation";
674
675 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400676 * Notification category: ongoing information about device or contextual status.
677 */
678 public static final String CATEGORY_STATUS = "status";
679
680 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400681 * Notification category: user-scheduled reminder.
682 */
683 public static final String CATEGORY_REMINDER = "reminder";
684
685 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400686 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
687 * that best describes this Notification. May be used by the system for ranking and filtering.
688 */
689 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500690
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700691 private String mGroupKey;
692
693 /**
694 * Get the key used to group this notification into a cluster or stack
695 * with other notifications on devices which support such rendering.
696 */
697 public String getGroup() {
698 return mGroupKey;
699 }
700
701 private String mSortKey;
702
703 /**
704 * Get a sort key that orders this notification among other notifications from the
705 * same package. This can be useful if an external sort was already applied and an app
706 * would like to preserve this. Notifications will be sorted lexicographically using this
707 * value, although providing different priorities in addition to providing sort key may
708 * cause this value to be ignored.
709 *
710 * <p>This sort key can also be used to order members of a notification group. See
711 * {@link Builder#setGroup}.
712 *
713 * @see String#compareTo(String)
714 */
715 public String getSortKey() {
716 return mSortKey;
717 }
718
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500719 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400720 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400721 * <p>
722 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
723 * APIs, and are intended to be used by
724 * {@link android.service.notification.NotificationListenerService} implementations to extract
725 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500726 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400727 public Bundle extras = new Bundle();
728
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400729 /**
730 * {@link #extras} key: this is the title of the notification,
731 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
732 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500733 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400734
735 /**
736 * {@link #extras} key: this is the title of the notification when shown in expanded form,
737 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
738 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400739 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400740
741 /**
742 * {@link #extras} key: this is the main text payload, as supplied to
743 * {@link Builder#setContentText(CharSequence)}.
744 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500745 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400746
747 /**
748 * {@link #extras} key: this is a third line of text, as supplied to
749 * {@link Builder#setSubText(CharSequence)}.
750 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400751 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400752
753 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800754 * {@link #extras} key: this is the remote input history, as supplied to
755 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
756 */
757 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
758
759 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400760 * {@link #extras} key: this is a small piece of additional text as supplied to
761 * {@link Builder#setContentInfo(CharSequence)}.
762 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400763 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400764
765 /**
766 * {@link #extras} key: this is a line of summary information intended to be shown
767 * alongside expanded notifications, as supplied to (e.g.)
768 * {@link BigTextStyle#setSummaryText(CharSequence)}.
769 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400770 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400771
772 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200773 * {@link #extras} key: this is the longer text shown in the big form of a
774 * {@link BigTextStyle} notification, as supplied to
775 * {@link BigTextStyle#bigText(CharSequence)}.
776 */
777 public static final String EXTRA_BIG_TEXT = "android.bigText";
778
779 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400780 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
781 * supplied to {@link Builder#setSmallIcon(int)}.
782 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500783 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400784
785 /**
786 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
787 * notification payload, as
788 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
789 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400790 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400791
792 /**
793 * {@link #extras} key: this is a bitmap to be used instead of the one from
794 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
795 * shown in its expanded form, as supplied to
796 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
797 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400798 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400799
800 /**
801 * {@link #extras} key: this is the progress value supplied to
802 * {@link Builder#setProgress(int, int, boolean)}.
803 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400804 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400805
806 /**
807 * {@link #extras} key: this is the maximum value supplied to
808 * {@link Builder#setProgress(int, int, boolean)}.
809 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400810 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400811
812 /**
813 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
814 * {@link Builder#setProgress(int, int, boolean)}.
815 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400816 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400817
818 /**
819 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
820 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
821 * {@link Builder#setUsesChronometer(boolean)}.
822 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400823 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400824
825 /**
826 * {@link #extras} key: whether {@link #when} should be shown,
827 * as supplied to {@link Builder#setShowWhen(boolean)}.
828 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400829 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400830
831 /**
832 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
833 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
834 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400835 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400836
837 /**
838 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
839 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
840 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400841 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400842
843 /**
844 * {@link #extras} key: A string representing the name of the specific
845 * {@link android.app.Notification.Style} used to create this notification.
846 */
Chris Wren91ad5632013-06-05 15:05:57 -0400847 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400848
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400849 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400850 * {@link #extras} key: A String array containing the people that this notification relates to,
851 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400852 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400853 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500854
855 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400856 * Allow certain system-generated notifications to appear before the device is provisioned.
857 * Only available to notifications coming from the android package.
858 * @hide
859 */
860 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
861
862 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700863 * {@link #extras} key: A
864 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
865 * in the background when the notification is selected. The URI must point to an image stream
866 * suitable for passing into
867 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
868 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
869 * URI used for this purpose must require no permissions to read the image data.
870 */
871 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
872
873 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400874 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700875 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400876 * {@link android.app.Notification.MediaStyle} notification.
877 */
878 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
879
880 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100881 * {@link #extras} key: the indices of actions to be shown in the compact view,
882 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
883 */
884 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
885
Christoph Studer943aa672014-08-03 20:31:16 +0200886 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100887 * {@link #extras} key: the user that built the notification.
888 *
889 * @hide
890 */
891 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
892
893 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400894 * @hide
895 */
896 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
897
Dan Sandlerd63f9322015-05-06 15:18:49 -0400898 private Icon mSmallIcon;
899 private Icon mLargeIcon;
900
Chris Wren51c75102013-07-16 20:49:17 -0400901 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400902 * Structure to encapsulate a named action that can be shown as part of this notification.
903 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
904 * selected by the user.
905 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700906 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
907 * or {@link Notification.Builder#addAction(Notification.Action)}
908 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400909 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500910 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700911 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -0400912 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -0700913 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700914
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400915 /**
916 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -0400917 *
918 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400919 */
Dan Sandler86647982015-05-13 23:41:13 -0400920 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400921 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700922
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400923 /**
924 * Title of the action.
925 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400926 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700927
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400928 /**
929 * Intent to send when the user invokes this action. May be null, in which case the action
930 * may be rendered in a disabled presentation by the system UI.
931 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400932 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -0700933
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400934 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -0400935 if (in.readInt() != 0) {
936 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -0400937 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
938 icon = mIcon.getResId();
939 }
Dan Sandler86647982015-05-13 23:41:13 -0400940 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400941 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
942 if (in.readInt() == 1) {
943 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
944 }
Griff Hazen959591e2014-05-15 22:26:18 -0700945 mExtras = in.readBundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700946 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400947 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700948
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400949 /**
Dan Sandler86647982015-05-13 23:41:13 -0400950 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400951 */
Dan Sandler86647982015-05-13 23:41:13 -0400952 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400953 public Action(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -0400954 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700955 }
956
Dan Sandler86647982015-05-13 23:41:13 -0400957 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700958 RemoteInput[] remoteInputs) {
Dan Sandler86647982015-05-13 23:41:13 -0400959 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -0400960 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
961 this.icon = icon.getResId();
962 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400963 this.title = title;
964 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -0700965 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700966 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700967 }
968
969 /**
Dan Sandler86647982015-05-13 23:41:13 -0400970 * Return an icon representing the action.
971 */
972 public Icon getIcon() {
973 if (mIcon == null && icon != 0) {
974 // you snuck an icon in here without using the builder; let's try to keep it
975 mIcon = Icon.createWithResource("", icon);
976 }
977 return mIcon;
978 }
979
980 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700981 * Get additional metadata carried around with this Action.
982 */
983 public Bundle getExtras() {
984 return mExtras;
985 }
986
987 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700988 * Get the list of inputs to be collected from the user when this action is sent.
989 * May return null if no remote inputs were added.
990 */
991 public RemoteInput[] getRemoteInputs() {
992 return mRemoteInputs;
993 }
994
995 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700996 * Builder class for {@link Action} objects.
997 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700998 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -0400999 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001000 private final CharSequence mTitle;
1001 private final PendingIntent mIntent;
1002 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001003 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001004
1005 /**
1006 * Construct a new builder for {@link Action} object.
1007 * @param icon icon to show for this action
1008 * @param title the title of the action
1009 * @param intent the {@link PendingIntent} to fire when users trigger this action
1010 */
Dan Sandler86647982015-05-13 23:41:13 -04001011 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001012 public Builder(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -04001013 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
1014 }
1015
1016 /**
1017 * Construct a new builder for {@link Action} object.
1018 * @param icon icon to show for this action
1019 * @param title the title of the action
1020 * @param intent the {@link PendingIntent} to fire when users trigger this action
1021 */
1022 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001023 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -07001024 }
1025
1026 /**
1027 * Construct a new builder for {@link Action} object using the fields from an
1028 * {@link Action}.
1029 * @param action the action to read fields from.
1030 */
1031 public Builder(Action action) {
Dan Sandler86647982015-05-13 23:41:13 -04001032 this(action.getIcon(), action.title, action.actionIntent, new Bundle(action.mExtras),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001033 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -07001034 }
1035
Dan Sandler86647982015-05-13 23:41:13 -04001036 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001037 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -07001038 mIcon = icon;
1039 mTitle = title;
1040 mIntent = intent;
1041 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001042 if (remoteInputs != null) {
1043 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1044 Collections.addAll(mRemoteInputs, remoteInputs);
1045 }
Griff Hazen959591e2014-05-15 22:26:18 -07001046 }
1047
1048 /**
1049 * Merge additional metadata into this builder.
1050 *
1051 * <p>Values within the Bundle will replace existing extras values in this Builder.
1052 *
1053 * @see Notification.Action#extras
1054 */
1055 public Builder addExtras(Bundle extras) {
1056 if (extras != null) {
1057 mExtras.putAll(extras);
1058 }
1059 return this;
1060 }
1061
1062 /**
1063 * Get the metadata Bundle used by this Builder.
1064 *
1065 * <p>The returned Bundle is shared with this Builder.
1066 */
1067 public Bundle getExtras() {
1068 return mExtras;
1069 }
1070
1071 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001072 * Add an input to be collected from the user when this action is sent.
1073 * Response values can be retrieved from the fired intent by using the
1074 * {@link RemoteInput#getResultsFromIntent} function.
1075 * @param remoteInput a {@link RemoteInput} to add to the action
1076 * @return this object for method chaining
1077 */
1078 public Builder addRemoteInput(RemoteInput remoteInput) {
1079 if (mRemoteInputs == null) {
1080 mRemoteInputs = new ArrayList<RemoteInput>();
1081 }
1082 mRemoteInputs.add(remoteInput);
1083 return this;
1084 }
1085
1086 /**
1087 * Apply an extender to this action builder. Extenders may be used to add
1088 * metadata or change options on this builder.
1089 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001090 public Builder extend(Extender extender) {
1091 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001092 return this;
1093 }
1094
1095 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001096 * Combine all of the options that have been set and return a new {@link Action}
1097 * object.
1098 * @return the built action
1099 */
1100 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001101 RemoteInput[] remoteInputs = mRemoteInputs != null
1102 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
1103 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -07001104 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001105 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001106
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001107 @Override
1108 public Action clone() {
1109 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001110 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001111 title,
1112 actionIntent, // safe to alias
1113 new Bundle(mExtras),
1114 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001115 }
1116 @Override
1117 public int describeContents() {
1118 return 0;
1119 }
1120 @Override
1121 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001122 final Icon ic = getIcon();
1123 if (ic != null) {
1124 out.writeInt(1);
1125 ic.writeToParcel(out, 0);
1126 } else {
1127 out.writeInt(0);
1128 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001129 TextUtils.writeToParcel(title, out, flags);
1130 if (actionIntent != null) {
1131 out.writeInt(1);
1132 actionIntent.writeToParcel(out, flags);
1133 } else {
1134 out.writeInt(0);
1135 }
Griff Hazen959591e2014-05-15 22:26:18 -07001136 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001137 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001138 }
Griff Hazen959591e2014-05-15 22:26:18 -07001139 public static final Parcelable.Creator<Action> CREATOR =
1140 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001141 public Action createFromParcel(Parcel in) {
1142 return new Action(in);
1143 }
1144 public Action[] newArray(int size) {
1145 return new Action[size];
1146 }
1147 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001148
1149 /**
1150 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1151 * metadata or change options on an action builder.
1152 */
1153 public interface Extender {
1154 /**
1155 * Apply this extender to a notification action builder.
1156 * @param builder the builder to be modified.
1157 * @return the build object for chaining.
1158 */
1159 public Builder extend(Builder builder);
1160 }
1161
1162 /**
1163 * Wearable extender for notification actions. To add extensions to an action,
1164 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1165 * the {@code WearableExtender()} constructor and apply it to a
1166 * {@link android.app.Notification.Action.Builder} using
1167 * {@link android.app.Notification.Action.Builder#extend}.
1168 *
1169 * <pre class="prettyprint">
1170 * Notification.Action action = new Notification.Action.Builder(
1171 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001172 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001173 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001174 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001175 */
1176 public static final class WearableExtender implements Extender {
1177 /** Notification action extra which contains wearable extensions */
1178 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1179
Pete Gastaf6781d2014-10-07 15:17:05 -04001180 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001181 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001182 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1183 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1184 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001185
1186 // Flags bitwise-ored to mFlags
1187 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
1188
1189 // Default value for flags integer
1190 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1191
1192 private int mFlags = DEFAULT_FLAGS;
1193
Pete Gastaf6781d2014-10-07 15:17:05 -04001194 private CharSequence mInProgressLabel;
1195 private CharSequence mConfirmLabel;
1196 private CharSequence mCancelLabel;
1197
Griff Hazen61a9e862014-05-22 16:05:19 -07001198 /**
1199 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1200 * options.
1201 */
1202 public WearableExtender() {
1203 }
1204
1205 /**
1206 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1207 * wearable options present in an existing notification action.
1208 * @param action the notification action to inspect.
1209 */
1210 public WearableExtender(Action action) {
1211 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1212 if (wearableBundle != null) {
1213 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001214 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1215 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1216 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001217 }
1218 }
1219
1220 /**
1221 * Apply wearable extensions to a notification action that is being built. This is
1222 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1223 * method of {@link android.app.Notification.Action.Builder}.
1224 */
1225 @Override
1226 public Action.Builder extend(Action.Builder builder) {
1227 Bundle wearableBundle = new Bundle();
1228
1229 if (mFlags != DEFAULT_FLAGS) {
1230 wearableBundle.putInt(KEY_FLAGS, mFlags);
1231 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001232 if (mInProgressLabel != null) {
1233 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1234 }
1235 if (mConfirmLabel != null) {
1236 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1237 }
1238 if (mCancelLabel != null) {
1239 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1240 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001241
1242 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1243 return builder;
1244 }
1245
1246 @Override
1247 public WearableExtender clone() {
1248 WearableExtender that = new WearableExtender();
1249 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001250 that.mInProgressLabel = this.mInProgressLabel;
1251 that.mConfirmLabel = this.mConfirmLabel;
1252 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001253 return that;
1254 }
1255
1256 /**
1257 * Set whether this action is available when the wearable device is not connected to
1258 * a companion device. The user can still trigger this action when the wearable device is
1259 * offline, but a visual hint will indicate that the action may not be available.
1260 * Defaults to true.
1261 */
1262 public WearableExtender setAvailableOffline(boolean availableOffline) {
1263 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1264 return this;
1265 }
1266
1267 /**
1268 * Get whether this action is available when the wearable device is not connected to
1269 * a companion device. The user can still trigger this action when the wearable device is
1270 * offline, but a visual hint will indicate that the action may not be available.
1271 * Defaults to true.
1272 */
1273 public boolean isAvailableOffline() {
1274 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1275 }
1276
1277 private void setFlag(int mask, boolean value) {
1278 if (value) {
1279 mFlags |= mask;
1280 } else {
1281 mFlags &= ~mask;
1282 }
1283 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001284
1285 /**
1286 * Set a label to display while the wearable is preparing to automatically execute the
1287 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1288 *
1289 * @param label the label to display while the action is being prepared to execute
1290 * @return this object for method chaining
1291 */
1292 public WearableExtender setInProgressLabel(CharSequence label) {
1293 mInProgressLabel = label;
1294 return this;
1295 }
1296
1297 /**
1298 * Get the label to display while the wearable is preparing to automatically execute
1299 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1300 *
1301 * @return the label to display while the action is being prepared to execute
1302 */
1303 public CharSequence getInProgressLabel() {
1304 return mInProgressLabel;
1305 }
1306
1307 /**
1308 * Set a label to display to confirm that the action should be executed.
1309 * This is usually an imperative verb like "Send".
1310 *
1311 * @param label the label to confirm the action should be executed
1312 * @return this object for method chaining
1313 */
1314 public WearableExtender setConfirmLabel(CharSequence label) {
1315 mConfirmLabel = label;
1316 return this;
1317 }
1318
1319 /**
1320 * Get the label to display to confirm that the action should be executed.
1321 * This is usually an imperative verb like "Send".
1322 *
1323 * @return the label to confirm the action should be executed
1324 */
1325 public CharSequence getConfirmLabel() {
1326 return mConfirmLabel;
1327 }
1328
1329 /**
1330 * Set a label to display to cancel the action.
1331 * This is usually an imperative verb, like "Cancel".
1332 *
1333 * @param label the label to display to cancel the action
1334 * @return this object for method chaining
1335 */
1336 public WearableExtender setCancelLabel(CharSequence label) {
1337 mCancelLabel = label;
1338 return this;
1339 }
1340
1341 /**
1342 * Get the label to display to cancel the action.
1343 * This is usually an imperative verb like "Cancel".
1344 *
1345 * @return the label to display to cancel the action
1346 */
1347 public CharSequence getCancelLabel() {
1348 return mCancelLabel;
1349 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001350 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001351 }
1352
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001353 /**
1354 * Array of all {@link Action} structures attached to this notification by
1355 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1356 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1357 * interface for invoking actions.
1358 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001359 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001360
1361 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001362 * Replacement version of this notification whose content will be shown
1363 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1364 * and {@link #VISIBILITY_PUBLIC}.
1365 */
1366 public Notification publicVersion;
1367
1368 /**
Julia Reynolds74303cf2015-10-16 11:37:55 -04001369 * Structure to encapsulate a topic that is shown in Notification settings.
1370 * It must include an id and label.
1371 */
1372 public static class Topic implements Parcelable {
1373 private final String id;
1374 private final CharSequence label;
1375
1376 public Topic(String id, CharSequence label) {
1377 this.id = id;
1378 this.label = safeCharSequence(label);
1379 }
1380
1381 private Topic(Parcel in) {
1382 if (in.readInt() != 0) {
1383 id = in.readString();
1384 } else {
1385 id = null;
1386 }
1387 label = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1388 }
1389
1390 public String getId() {
1391 return id;
1392 }
1393
1394 public CharSequence getLabel() {
1395 return label;
1396 }
1397
1398 @Override
1399 public String toString() {
1400 return new StringBuilder(Topic.class.getSimpleName()).append('[')
1401 .append("id=").append(id)
1402 .append(",label=").append(label)
1403 .append(']').toString();
1404 }
1405
1406 @Override
1407 public boolean equals(Object o) {
1408 if (!(o instanceof Topic)) return false;
1409 if (o == this) return true;
1410 final Topic other = (Topic) o;
1411 return Objects.equals(other.id, id)
1412 && Objects.equals(other.label, label);
1413 }
1414
1415 @Override
1416 public int hashCode() {
1417 return Objects.hash(id, label);
1418 }
1419
1420 @Override
1421 public Topic clone() {
1422 return new Topic(id, label);
1423 }
1424
1425 @Override
1426 public int describeContents() {
1427 return 0;
1428 }
1429
1430 @Override
1431 public void writeToParcel(Parcel out, int flags) {
1432 if (id != null) {
1433 out.writeInt(1);
1434 out.writeString(id);
1435 } else {
1436 out.writeInt(0);
1437 }
1438 TextUtils.writeToParcel(label, out, flags);
1439 }
1440 public static final Parcelable.Creator<Topic> CREATOR =
1441 new Parcelable.Creator<Topic>() {
1442 public Topic createFromParcel(Parcel in) {
1443 return new Topic(in);
1444 }
1445 public Topic[] newArray(int size) {
1446 return new Topic[size];
1447 }
1448 };
1449 }
1450
Julia Reynolds233a5f92015-10-19 13:51:23 -04001451 @SystemApi
1452 public static final String TOPIC_DEFAULT = "system_default_topic";
1453
Julia Reynolds054c5dc2015-11-17 15:36:30 -05001454 private Topic topic;
Julia Reynolds74303cf2015-10-16 11:37:55 -04001455
Julia Reynolds054c5dc2015-11-17 15:36:30 -05001456 public Topic getTopic() {
1457 return topic;
Julia Reynolds74303cf2015-10-16 11:37:55 -04001458 }
1459
1460 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001461 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001462 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 */
1464 public Notification()
1465 {
1466 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001467 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 }
1469
1470 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 * @hide
1472 */
1473 public Notification(Context context, int icon, CharSequence tickerText, long when,
1474 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1475 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001476 new Builder(context)
1477 .setWhen(when)
1478 .setSmallIcon(icon)
1479 .setTicker(tickerText)
1480 .setContentTitle(contentTitle)
1481 .setContentText(contentText)
1482 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1483 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 }
1485
1486 /**
1487 * Constructs a Notification object with the information needed to
1488 * have a status bar icon without the standard expanded view.
1489 *
1490 * @param icon The resource id of the icon to put in the status bar.
1491 * @param tickerText The text that flows by in the status bar when the notification first
1492 * activates.
1493 * @param when The time to show in the time field. In the System.currentTimeMillis
1494 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001495 *
1496 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001498 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 public Notification(int icon, CharSequence tickerText, long when)
1500 {
1501 this.icon = icon;
1502 this.tickerText = tickerText;
1503 this.when = when;
1504 }
1505
1506 /**
1507 * Unflatten the notification from a parcel.
1508 */
1509 public Notification(Parcel parcel)
1510 {
1511 int version = parcel.readInt();
1512
1513 when = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001514 if (parcel.readInt() != 0) {
1515 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001516 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1517 icon = mSmallIcon.getResId();
1518 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 number = parcel.readInt();
1521 if (parcel.readInt() != 0) {
1522 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1523 }
1524 if (parcel.readInt() != 0) {
1525 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1526 }
1527 if (parcel.readInt() != 0) {
1528 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1529 }
1530 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001531 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001532 }
1533 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1535 }
Joe Onorato561d3852010-11-20 18:09:34 -08001536 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001537 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001538 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 defaults = parcel.readInt();
1540 flags = parcel.readInt();
1541 if (parcel.readInt() != 0) {
1542 sound = Uri.CREATOR.createFromParcel(parcel);
1543 }
1544
1545 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001546 if (parcel.readInt() != 0) {
1547 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1548 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 vibrate = parcel.createLongArray();
1550 ledARGB = parcel.readInt();
1551 ledOnMS = parcel.readInt();
1552 ledOffMS = parcel.readInt();
1553 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001554
1555 if (parcel.readInt() != 0) {
1556 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1557 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001558
1559 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001560
John Spurlockfd7f1e02014-03-18 16:41:57 -04001561 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001562
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001563 mGroupKey = parcel.readString();
1564
1565 mSortKey = parcel.readString();
1566
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001567 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001568
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001569 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1570
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001571 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001572 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1573 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001574
Chris Wren8fd39ec2014-02-27 17:43:26 -05001575 if (parcel.readInt() != 0) {
1576 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1577 }
1578
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001579 visibility = parcel.readInt();
1580
1581 if (parcel.readInt() != 0) {
1582 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1583 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001584
1585 color = parcel.readInt();
Julia Reynolds74303cf2015-10-16 11:37:55 -04001586
Julia Reynolds054c5dc2015-11-17 15:36:30 -05001587 if (parcel.readInt() != 0) {
1588 topic = Topic.CREATOR.createFromParcel(parcel);
1589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 }
1591
Andy Stadler110988c2010-12-03 14:29:16 -08001592 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001593 public Notification clone() {
1594 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001595 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001596 return that;
1597 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001598
Daniel Sandler1a497d32013-04-18 14:52:45 -04001599 /**
1600 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1601 * of this into that.
1602 * @hide
1603 */
1604 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001605 that.when = this.when;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001606 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001607 that.number = this.number;
1608
1609 // PendingIntents are global, so there's no reason (or way) to clone them.
1610 that.contentIntent = this.contentIntent;
1611 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001612 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001613
1614 if (this.tickerText != null) {
1615 that.tickerText = this.tickerText.toString();
1616 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001617 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001618 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001619 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001620 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001621 that.contentView = this.contentView.clone();
1622 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001623 if (heavy && this.mLargeIcon != null) {
1624 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001625 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001626 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001627 that.sound = this.sound; // android.net.Uri is immutable
1628 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001629 if (this.audioAttributes != null) {
1630 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1631 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001632
1633 final long[] vibrate = this.vibrate;
1634 if (vibrate != null) {
1635 final int N = vibrate.length;
1636 final long[] vib = that.vibrate = new long[N];
1637 System.arraycopy(vibrate, 0, vib, 0, N);
1638 }
1639
1640 that.ledARGB = this.ledARGB;
1641 that.ledOnMS = this.ledOnMS;
1642 that.ledOffMS = this.ledOffMS;
1643 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001644
Joe Onorato18e69df2010-05-17 22:26:12 -07001645 that.flags = this.flags;
1646
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001647 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001648
John Spurlockfd7f1e02014-03-18 16:41:57 -04001649 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001650
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001651 that.mGroupKey = this.mGroupKey;
1652
1653 that.mSortKey = this.mSortKey;
1654
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001655 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001656 try {
1657 that.extras = new Bundle(this.extras);
1658 // will unparcel
1659 that.extras.size();
1660 } catch (BadParcelableException e) {
1661 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1662 that.extras = null;
1663 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001664 }
1665
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001666 if (this.actions != null) {
1667 that.actions = new Action[this.actions.length];
1668 for(int i=0; i<this.actions.length; i++) {
1669 that.actions[i] = this.actions[i].clone();
1670 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001671 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001672
Daniel Sandler1a497d32013-04-18 14:52:45 -04001673 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001674 that.bigContentView = this.bigContentView.clone();
1675 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001676
Chris Wren8fd39ec2014-02-27 17:43:26 -05001677 if (heavy && this.headsUpContentView != null) {
1678 that.headsUpContentView = this.headsUpContentView.clone();
1679 }
1680
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001681 that.visibility = this.visibility;
1682
1683 if (this.publicVersion != null) {
1684 that.publicVersion = new Notification();
1685 this.publicVersion.cloneInto(that.publicVersion, heavy);
1686 }
1687
Dan Sandler26e81cf2014-05-06 10:01:27 -04001688 that.color = this.color;
1689
Julia Reynolds054c5dc2015-11-17 15:36:30 -05001690 if (this.topic != null) {
1691 that.topic = this.topic.clone();
Julia Reynolds74303cf2015-10-16 11:37:55 -04001692 }
1693
Daniel Sandler1a497d32013-04-18 14:52:45 -04001694 if (!heavy) {
1695 that.lightenPayload(); // will clean out extras
1696 }
1697 }
1698
1699 /**
1700 * Removes heavyweight parts of the Notification object for archival or for sending to
1701 * listeners when the full contents are not necessary.
1702 * @hide
1703 */
1704 public final void lightenPayload() {
1705 tickerView = null;
1706 contentView = null;
1707 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001708 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001709 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05001710 if (extras != null && !extras.isEmpty()) {
1711 final Set<String> keyset = extras.keySet();
1712 final int N = keyset.size();
1713 final String[] keys = keyset.toArray(new String[N]);
1714 for (int i=0; i<N; i++) {
1715 final String key = keys[i];
1716 final Object obj = extras.get(key);
1717 if (obj != null &&
1718 ( obj instanceof Parcelable
1719 || obj instanceof Parcelable[]
1720 || obj instanceof SparseArray
1721 || obj instanceof ArrayList)) {
1722 extras.remove(key);
1723 }
1724 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001725 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001726 }
1727
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001728 /**
1729 * Make sure this CharSequence is safe to put into a bundle, which basically
1730 * means it had better not be some custom Parcelable implementation.
1731 * @hide
1732 */
1733 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001734 if (cs == null) return cs;
1735 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1736 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1737 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001738 if (cs instanceof Parcelable) {
1739 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1740 + " instance is a custom Parcelable and not allowed in Notification");
1741 return cs.toString();
1742 }
1743
1744 return cs;
1745 }
1746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 public int describeContents() {
1748 return 0;
1749 }
1750
1751 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001752 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 */
1754 public void writeToParcel(Parcel parcel, int flags)
1755 {
1756 parcel.writeInt(1);
1757
1758 parcel.writeLong(when);
Dan Sandler4e787062015-06-17 15:09:48 -04001759 if (mSmallIcon == null && icon != 0) {
1760 // you snuck an icon in here without using the builder; let's try to keep it
1761 mSmallIcon = Icon.createWithResource("", icon);
1762 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001763 if (mSmallIcon != null) {
1764 parcel.writeInt(1);
1765 mSmallIcon.writeToParcel(parcel, 0);
1766 } else {
1767 parcel.writeInt(0);
1768 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 parcel.writeInt(number);
1770 if (contentIntent != null) {
1771 parcel.writeInt(1);
1772 contentIntent.writeToParcel(parcel, 0);
1773 } else {
1774 parcel.writeInt(0);
1775 }
1776 if (deleteIntent != null) {
1777 parcel.writeInt(1);
1778 deleteIntent.writeToParcel(parcel, 0);
1779 } else {
1780 parcel.writeInt(0);
1781 }
1782 if (tickerText != null) {
1783 parcel.writeInt(1);
1784 TextUtils.writeToParcel(tickerText, parcel, flags);
1785 } else {
1786 parcel.writeInt(0);
1787 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001788 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001789 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001790 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001791 } else {
1792 parcel.writeInt(0);
1793 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 if (contentView != null) {
1795 parcel.writeInt(1);
1796 contentView.writeToParcel(parcel, 0);
1797 } else {
1798 parcel.writeInt(0);
1799 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001800 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001801 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001802 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001803 } else {
1804 parcel.writeInt(0);
1805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806
1807 parcel.writeInt(defaults);
1808 parcel.writeInt(this.flags);
1809
1810 if (sound != null) {
1811 parcel.writeInt(1);
1812 sound.writeToParcel(parcel, 0);
1813 } else {
1814 parcel.writeInt(0);
1815 }
1816 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001817
1818 if (audioAttributes != null) {
1819 parcel.writeInt(1);
1820 audioAttributes.writeToParcel(parcel, 0);
1821 } else {
1822 parcel.writeInt(0);
1823 }
1824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 parcel.writeLongArray(vibrate);
1826 parcel.writeInt(ledARGB);
1827 parcel.writeInt(ledOnMS);
1828 parcel.writeInt(ledOffMS);
1829 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001830
1831 if (fullScreenIntent != null) {
1832 parcel.writeInt(1);
1833 fullScreenIntent.writeToParcel(parcel, 0);
1834 } else {
1835 parcel.writeInt(0);
1836 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001837
1838 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001839
John Spurlockfd7f1e02014-03-18 16:41:57 -04001840 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001841
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001842 parcel.writeString(mGroupKey);
1843
1844 parcel.writeString(mSortKey);
1845
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001846 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001847
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001848 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001849
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001850 if (bigContentView != null) {
1851 parcel.writeInt(1);
1852 bigContentView.writeToParcel(parcel, 0);
1853 } else {
1854 parcel.writeInt(0);
1855 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001856
Chris Wren8fd39ec2014-02-27 17:43:26 -05001857 if (headsUpContentView != null) {
1858 parcel.writeInt(1);
1859 headsUpContentView.writeToParcel(parcel, 0);
1860 } else {
1861 parcel.writeInt(0);
1862 }
1863
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001864 parcel.writeInt(visibility);
1865
1866 if (publicVersion != null) {
1867 parcel.writeInt(1);
1868 publicVersion.writeToParcel(parcel, 0);
1869 } else {
1870 parcel.writeInt(0);
1871 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001872
1873 parcel.writeInt(color);
Julia Reynolds74303cf2015-10-16 11:37:55 -04001874
Julia Reynolds054c5dc2015-11-17 15:36:30 -05001875 if (topic != null) {
1876 parcel.writeInt(1);
1877 topic.writeToParcel(parcel, 0);
1878 } else {
1879 parcel.writeInt(0);
1880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 }
1882
1883 /**
1884 * Parcelable.Creator that instantiates Notification objects
1885 */
1886 public static final Parcelable.Creator<Notification> CREATOR
1887 = new Parcelable.Creator<Notification>()
1888 {
1889 public Notification createFromParcel(Parcel parcel)
1890 {
1891 return new Notification(parcel);
1892 }
1893
1894 public Notification[] newArray(int size)
1895 {
1896 return new Notification[size];
1897 }
1898 };
1899
1900 /**
1901 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1902 * layout.
1903 *
1904 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1905 * in the view.</p>
1906 * @param context The context for your application / activity.
1907 * @param contentTitle The title that goes in the expanded entry.
1908 * @param contentText The text that goes in the expanded entry.
1909 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1910 * If this is an activity, it must include the
1911 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001912 * that you take care of task management as described in the
1913 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1914 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001915 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001916 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04001917 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001919 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 public void setLatestEventInfo(Context context,
1921 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001922 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
1923 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
1924 new Throwable());
1925 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001926
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001927 // ensure that any information already set directly is preserved
1928 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001929
1930 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001932 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 }
1934 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001935 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001937 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001938
1939 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 }
1941
Julia Reynoldsda303542015-11-23 14:00:20 -05001942 /**
1943 * @hide
1944 */
1945 public static void addFieldsFromContext(Context context, Notification notification) {
1946 if (notification.extras.getParcelable(EXTRA_BUILDER_APPLICATION_INFO) == null) {
1947 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO,
1948 context.getApplicationInfo());
1949 }
1950 if (!notification.extras.containsKey(EXTRA_ORIGINATING_USERID)) {
1951 notification.extras.putInt(EXTRA_ORIGINATING_USERID, context.getUserId());
1952 }
1953 }
1954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 @Override
1956 public String toString() {
1957 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001958 sb.append("Notification(pri=");
1959 sb.append(priority);
1960 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001961 if (contentView != null) {
1962 sb.append(contentView.getPackage());
1963 sb.append("/0x");
1964 sb.append(Integer.toHexString(contentView.getLayoutId()));
1965 } else {
1966 sb.append("null");
1967 }
1968 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001969 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1970 sb.append("default");
1971 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 int N = this.vibrate.length-1;
1973 sb.append("[");
1974 for (int i=0; i<N; i++) {
1975 sb.append(this.vibrate[i]);
1976 sb.append(',');
1977 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001978 if (N != -1) {
1979 sb.append(this.vibrate[N]);
1980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 } else {
1983 sb.append("null");
1984 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001985 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001986 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001988 } else if (this.sound != null) {
1989 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 } else {
1991 sb.append("null");
1992 }
Chris Wren365b6d32015-07-16 10:39:26 -04001993 if (this.tickerText != null) {
1994 sb.append(" tick");
1995 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001996 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001998 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001999 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002000 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002001 if (this.category != null) {
2002 sb.append(" category=");
2003 sb.append(this.category);
2004 }
2005 if (this.mGroupKey != null) {
2006 sb.append(" groupKey=");
2007 sb.append(this.mGroupKey);
2008 }
2009 if (this.mSortKey != null) {
2010 sb.append(" sortKey=");
2011 sb.append(this.mSortKey);
2012 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002013 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002014 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002015 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002016 }
2017 sb.append(" vis=");
2018 sb.append(visibilityToString(this.visibility));
2019 if (this.publicVersion != null) {
2020 sb.append(" publicVersion=");
2021 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002022 }
Julia Reynolds054c5dc2015-11-17 15:36:30 -05002023 if (topic != null) {
2024 sb.append("topic=");
2025 sb.append(topic.toString());
Julia Reynolds74303cf2015-10-16 11:37:55 -04002026 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002027 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 return sb.toString();
2029 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002030
Dan Sandler1b718782014-07-18 12:43:45 -04002031 /**
2032 * {@hide}
2033 */
2034 public static String visibilityToString(int vis) {
2035 switch (vis) {
2036 case VISIBILITY_PRIVATE:
2037 return "PRIVATE";
2038 case VISIBILITY_PUBLIC:
2039 return "PUBLIC";
2040 case VISIBILITY_SECRET:
2041 return "SECRET";
2042 default:
2043 return "UNKNOWN(" + String.valueOf(vis) + ")";
2044 }
2045 }
2046
Joe Onoratocb109a02011-01-18 17:57:41 -08002047 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002048 * {@hide}
2049 */
2050 public static String priorityToString(@Priority int pri) {
2051 switch (pri) {
2052 case PRIORITY_MIN:
2053 return "MIN";
2054 case PRIORITY_LOW:
2055 return "LOW";
2056 case PRIORITY_DEFAULT:
2057 return "DEFAULT";
2058 case PRIORITY_HIGH:
2059 return "HIGH";
2060 case PRIORITY_MAX:
2061 return "MAX";
2062 default:
2063 return "UNKNOWN(" + String.valueOf(pri) + ")";
2064 }
2065 }
2066
2067 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002068 * The small icon representing this notification in the status bar and content view.
2069 *
2070 * @return the small icon representing this notification.
2071 *
2072 * @see Builder#getSmallIcon()
2073 * @see Builder#setSmallIcon(Icon)
2074 */
2075 public Icon getSmallIcon() {
2076 return mSmallIcon;
2077 }
2078
2079 /**
2080 * Used when notifying to clean up legacy small icons.
2081 * @hide
2082 */
2083 public void setSmallIcon(Icon icon) {
2084 mSmallIcon = icon;
2085 }
2086
2087 /**
2088 * The large icon shown in this notification's content view.
2089 * @see Builder#getLargeIcon()
2090 * @see Builder#setLargeIcon(Icon)
2091 */
2092 public Icon getLargeIcon() {
2093 return mLargeIcon;
2094 }
2095
2096 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002097 * @hide
2098 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002099 public boolean isGroupSummary() {
2100 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2101 }
2102
2103 /**
2104 * @hide
2105 */
2106 public boolean isGroupChild() {
2107 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2108 }
2109
2110 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002111 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002112 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002113 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002114 * content views using the platform's notification layout template. If your app supports
2115 * versions of Android as old as API level 4, you can instead use
2116 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2117 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2118 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002119 *
Scott Main183bf112012-08-13 19:12:13 -07002120 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002121 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002122 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002123 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002124 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2125 * .setContentText(subject)
2126 * .setSmallIcon(R.drawable.new_mail)
2127 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002128 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002129 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002130 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002131 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002132 private static final int MAX_ACTION_BUTTONS = 3;
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002133 private static final float LARGE_TEXT_SCALE = 1.3f;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002134
Joe Onorato46439ce2010-11-19 13:56:21 -08002135 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002136 private Notification mN;
2137 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002138 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002139 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2140 private ArrayList<String> mPersonList = new ArrayList<String>();
2141 private NotificationColorUtil mColorUtil;
2142 private boolean mColorUtilInited = false;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002143
2144 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002145 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002146 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002147
2148 * <table>
2149 * <tr><th align=right>priority</th>
2150 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2151 * <tr><th align=right>when</th>
2152 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2153 * <tr><th align=right>audio stream</th>
2154 * <td>{@link #STREAM_DEFAULT}</td></tr>
2155 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002156 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002157
2158 * @param context
2159 * A {@link Context} that will be used by the Builder to construct the
2160 * RemoteViews. The Context will not be held past the lifetime of this Builder
2161 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002162 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002163 public Builder(Context context) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002164 this(context, null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002165 }
2166
Joe Onoratocb109a02011-01-18 17:57:41 -08002167 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002168 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002169 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002170 public Builder(Context context, Notification toAdopt) {
2171 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002172
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002173 if (toAdopt == null) {
2174 mN = new Notification();
2175 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2176 mN.priority = PRIORITY_DEFAULT;
2177 mN.visibility = VISIBILITY_PRIVATE;
2178 } else {
2179 mN = toAdopt;
2180 if (mN.actions != null) {
2181 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002182 }
2183
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002184 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2185 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2186 }
2187
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002188 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2189 if (!TextUtils.isEmpty(templateClass)) {
2190 final Class<? extends Style> styleClass
2191 = getNotificationStyleClass(templateClass);
2192 if (styleClass == null) {
2193 Log.d(TAG, "Unknown style class: " + templateClass);
2194 } else {
2195 try {
2196 final Constructor<? extends Style> ctor = styleClass.getConstructor();
2197 ctor.setAccessible(true);
2198 final Style style = ctor.newInstance();
2199 style.restoreFromExtras(mN.extras);
2200
2201 if (style != null) {
2202 setStyle(style);
2203 }
2204 } catch (Throwable t) {
2205 Log.e(TAG, "Could not create Style", t);
2206 }
2207 }
2208 }
2209
2210 }
2211 }
2212
2213 private NotificationColorUtil getColorUtil() {
2214 if (!mColorUtilInited) {
2215 mColorUtilInited = true;
2216 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
2217 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002218 }
2219 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002220 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002221 }
2222
2223 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002224 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07002225 * It will be shown in the notification content view by default; use
Griff Hazen50c11652014-05-16 09:46:31 -07002226 * {@link #setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002227 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002228 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002229 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002230 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002231 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002232 return this;
2233 }
2234
Joe Onoratocb109a02011-01-18 17:57:41 -08002235 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002236 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002237 * in the content view.
2238 */
2239 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002240 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002241 return this;
2242 }
2243
2244 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002245 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002246 *
2247 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002248 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002249 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002250 * Useful when showing an elapsed time (like an ongoing phone call).
2251 *
2252 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002253 * @see Notification#when
2254 */
2255 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002256 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002257 return this;
2258 }
2259
2260 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002261 * Set the small icon resource, which will be used to represent the notification in the
2262 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002263 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002264
2265 * The platform template for the expanded view will draw this icon in the left, unless a
2266 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2267 * icon will be moved to the right-hand side.
2268 *
2269
2270 * @param icon
2271 * A resource ID in the application's package of the drawable to use.
2272 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002273 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002274 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002275 return setSmallIcon(icon != 0
2276 ? Icon.createWithResource(mContext, icon)
2277 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002278 }
2279
Joe Onoratocb109a02011-01-18 17:57:41 -08002280 /**
2281 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2282 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2283 * LevelListDrawable}.
2284 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002285 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002286 * @param level The level to use for the icon.
2287 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002288 * @see Notification#icon
2289 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002290 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002291 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002292 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002293 return setSmallIcon(icon);
2294 }
2295
2296 /**
2297 * Set the small icon, which will be used to represent the notification in the
2298 * status bar and content view (unless overriden there by a
2299 * {@link #setLargeIcon(Bitmap) large icon}).
2300 *
2301 * @param icon An Icon object to use.
2302 * @see Notification#icon
2303 */
2304 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002305 mN.setSmallIcon(icon);
2306 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2307 mN.icon = icon.getResId();
2308 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002309 return this;
2310 }
2311
Joe Onoratocb109a02011-01-18 17:57:41 -08002312 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002313 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002314 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002315 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002316 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002317 return this;
2318 }
2319
Joe Onoratocb109a02011-01-18 17:57:41 -08002320 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002321 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002322 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002323 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002324 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002325 return this;
2326 }
2327
Joe Onoratocb109a02011-01-18 17:57:41 -08002328 /**
Joe Malin8d40d042012-11-05 11:36:40 -08002329 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002330 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
2331 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002332 */
2333 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002334 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002335 return this;
2336 }
2337
2338 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002339 * Set the remote input history.
2340 *
2341 * This should be set to the most recent inputs that have been sent
2342 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2343 * longer relevant (e.g. for chat notifications once the other party has responded).
2344 *
2345 * The most recent input must be stored at the 0 index, the second most recent at the
2346 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2347 * and how much of each individual input is shown.
2348 *
2349 * <p>Note: The reply text will only be shown on notifications that have least one action
2350 * with a {@code RemoteInput}.</p>
2351 */
2352 public Builder setRemoteInputHistory(CharSequence[] text) {
2353 if (text == null) {
2354 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2355 } else {
2356 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2357 CharSequence[] safe = new CharSequence[N];
2358 for (int i = 0; i < N; i++) {
2359 safe[i] = safeCharSequence(text[i]);
2360 }
2361 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2362 }
2363 return this;
2364 }
2365
2366 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002367 * Set the large number at the right-hand side of the notification. This is
2368 * equivalent to setContentInfo, although it might show the number in a different
2369 * font size for readability.
2370 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002371 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002372 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002373 return this;
2374 }
2375
Joe Onoratocb109a02011-01-18 17:57:41 -08002376 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002377 * A small piece of additional information pertaining to this notification.
2378 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002379 * The platform template will draw this on the last line of the notification, at the far
2380 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08002381 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002382 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002383 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002384 return this;
2385 }
2386
Joe Onoratocb109a02011-01-18 17:57:41 -08002387 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002388 * Set the progress this notification represents.
2389 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002390 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002391 */
2392 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002393 mN.extras.putInt(EXTRA_PROGRESS, progress);
2394 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
2395 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002396 return this;
2397 }
2398
2399 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002400 * Supply a custom RemoteViews to use instead of the platform template.
2401 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002402 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08002403 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002404 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002405 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002406 return setCustomContentView(views);
2407 }
2408
2409 /**
2410 * Supply custom RemoteViews to use instead of the platform template.
2411 *
2412 * This will override the layout that would otherwise be constructed by this Builder
2413 * object.
2414 */
2415 public Builder setCustomContentView(RemoteViews contentView) {
2416 mN.contentView = contentView;
2417 return this;
2418 }
2419
2420 /**
2421 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
2422 *
2423 * This will override the expanded layout that would otherwise be constructed by this
2424 * Builder object.
2425 */
2426 public Builder setCustomBigContentView(RemoteViews contentView) {
2427 mN.bigContentView = contentView;
2428 return this;
2429 }
2430
2431 /**
2432 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
2433 *
2434 * This will override the heads-up layout that would otherwise be constructed by this
2435 * Builder object.
2436 */
2437 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
2438 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002439 return this;
2440 }
2441
Joe Onoratocb109a02011-01-18 17:57:41 -08002442 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002443 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2444 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002445 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2446 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2447 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002448 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002449 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002450 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002451 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002452 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002453 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002454 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002455 return this;
2456 }
2457
Joe Onoratocb109a02011-01-18 17:57:41 -08002458 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002459 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2460 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002461 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002462 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002463 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002464 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002465 return this;
2466 }
2467
Joe Onoratocb109a02011-01-18 17:57:41 -08002468 /**
2469 * An intent to launch instead of posting the notification to the status bar.
2470 * Only for use with extremely high-priority notifications demanding the user's
2471 * <strong>immediate</strong> attention, such as an incoming phone call or
2472 * alarm clock that the user has explicitly set to a particular time.
2473 * If this facility is used for something else, please give the user an option
2474 * to turn it off and use a normal notification, as this can be extremely
2475 * disruptive.
2476 *
Chris Wren47c20a12014-06-18 17:27:29 -04002477 * <p>
2478 * The system UI may choose to display a heads-up notification, instead of
2479 * launching this intent, while the user is using the device.
2480 * </p>
2481 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002482 * @param intent The pending intent to launch.
2483 * @param highPriority Passing true will cause this notification to be sent
2484 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002485 *
2486 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002487 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002488 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002489 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002490 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2491 return this;
2492 }
2493
Joe Onoratocb109a02011-01-18 17:57:41 -08002494 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002495 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002496 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002497 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002498 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002499 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002500 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002501 return this;
2502 }
2503
Joe Onoratocb109a02011-01-18 17:57:41 -08002504 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002505 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002506 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002507 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002508 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002509 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002510 setTicker(tickerText);
2511 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08002512 return this;
2513 }
2514
Joe Onoratocb109a02011-01-18 17:57:41 -08002515 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002516 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002517 *
2518 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002519 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2520 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002521 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002522 public Builder setLargeIcon(Bitmap b) {
2523 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2524 }
2525
2526 /**
2527 * Add a large icon to the notification content view.
2528 *
2529 * In the platform template, this image will be shown on the left of the notification view
2530 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2531 * badge atop the large icon).
2532 */
2533 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002534 mN.mLargeIcon = icon;
2535 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08002536 return this;
2537 }
2538
Joe Onoratocb109a02011-01-18 17:57:41 -08002539 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002540 * Set the sound to play.
2541 *
John Spurlockc0650f022014-07-19 13:22:39 -04002542 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2543 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002544 *
Chris Wren47c20a12014-06-18 17:27:29 -04002545 * <p>
2546 * A notification that is noisy is more likely to be presented as a heads-up notification.
2547 * </p>
2548 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002549 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002550 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002551 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002552 mN.sound = sound;
2553 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002554 return this;
2555 }
2556
Joe Onoratocb109a02011-01-18 17:57:41 -08002557 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002558 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002559 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002560 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2561 *
Chris Wren47c20a12014-06-18 17:27:29 -04002562 * <p>
2563 * A notification that is noisy is more likely to be presented as a heads-up notification.
2564 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002565 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002566 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002567 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002568 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002569 public Builder setSound(Uri sound, int streamType) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002570 mN.sound = sound;
2571 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08002572 return this;
2573 }
2574
Joe Onoratocb109a02011-01-18 17:57:41 -08002575 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002576 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2577 * use during playback.
2578 *
2579 * <p>
2580 * A notification that is noisy is more likely to be presented as a heads-up notification.
2581 * </p>
2582 *
2583 * @see Notification#sound
2584 */
2585 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002586 mN.sound = sound;
2587 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04002588 return this;
2589 }
2590
2591 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002592 * Set the vibration pattern to use.
2593 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002594 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2595 * <code>pattern</code> parameter.
2596 *
Chris Wren47c20a12014-06-18 17:27:29 -04002597 * <p>
2598 * A notification that vibrates is more likely to be presented as a heads-up notification.
2599 * </p>
2600 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002601 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002602 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002603 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002604 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08002605 return this;
2606 }
2607
Joe Onoratocb109a02011-01-18 17:57:41 -08002608 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002609 * Set the desired color for the indicator LED on the device, as well as the
2610 * blink duty cycle (specified in milliseconds).
2611 *
2612
2613 * Not all devices will honor all (or even any) of these values.
2614 *
2615
2616 * @see Notification#ledARGB
2617 * @see Notification#ledOnMS
2618 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002619 */
Tor Norbye80756e32015-03-02 09:39:27 -08002620 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002621 mN.ledARGB = argb;
2622 mN.ledOnMS = onMs;
2623 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002624 if (onMs != 0 || offMs != 0) {
2625 mN.flags |= FLAG_SHOW_LIGHTS;
2626 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002627 return this;
2628 }
2629
Joe Onoratocb109a02011-01-18 17:57:41 -08002630 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002631 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002632 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002633
2634 * Ongoing notifications cannot be dismissed by the user, so your application or service
2635 * must take care of canceling them.
2636 *
2637
2638 * They are typically used to indicate a background task that the user is actively engaged
2639 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2640 * (e.g., a file download, sync operation, active network connection).
2641 *
2642
2643 * @see Notification#FLAG_ONGOING_EVENT
2644 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002645 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002646 public Builder setOngoing(boolean ongoing) {
2647 setFlag(FLAG_ONGOING_EVENT, ongoing);
2648 return this;
2649 }
2650
Joe Onoratocb109a02011-01-18 17:57:41 -08002651 /**
2652 * Set this flag if you would only like the sound, vibrate
2653 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002654 *
2655 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002656 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002657 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2658 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2659 return this;
2660 }
2661
Joe Onoratocb109a02011-01-18 17:57:41 -08002662 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002663 * Make this notification automatically dismissed when the user touches it. The
2664 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2665 *
2666 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002667 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002668 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002669 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002670 return this;
2671 }
2672
Joe Onoratocb109a02011-01-18 17:57:41 -08002673 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002674 * Set whether or not this notification should not bridge to other devices.
2675 *
2676 * <p>Some notifications can be bridged to other devices for remote display.
2677 * This hint can be set to recommend this notification not be bridged.
2678 */
2679 public Builder setLocalOnly(boolean localOnly) {
2680 setFlag(FLAG_LOCAL_ONLY, localOnly);
2681 return this;
2682 }
2683
2684 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002685 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002686 * <p>
2687 * The value should be one or more of the following fields combined with
2688 * bitwise-or:
2689 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2690 * <p>
2691 * For all default values, use {@link #DEFAULT_ALL}.
2692 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002693 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002694 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08002695 return this;
2696 }
2697
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002698 /**
2699 * Set the priority of this notification.
2700 *
2701 * @see Notification#priority
2702 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002703 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002704 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002705 return this;
2706 }
Joe Malin8d40d042012-11-05 11:36:40 -08002707
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002708 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002709 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002710 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002711 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002712 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002713 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002714 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002715 return this;
2716 }
2717
2718 /**
Chris Wrendde75302014-03-26 17:24:15 -04002719 * Add a person that is relevant to this notification.
2720 *
Chris Wrene6c48932014-09-29 17:19:27 -04002721 * <P>
2722 * Depending on user preferences, this annotation may allow the notification to pass
2723 * through interruption filters, and to appear more prominently in the user interface.
2724 * </P>
2725 *
2726 * <P>
2727 * The person should be specified by the {@code String} representation of a
2728 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2729 * </P>
2730 *
2731 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2732 * URIs. The path part of these URIs must exist in the contacts database, in the
2733 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2734 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2735 * </P>
2736 *
2737 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002738 * @see Notification#EXTRA_PEOPLE
2739 */
Chris Wrene6c48932014-09-29 17:19:27 -04002740 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002741 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002742 return this;
2743 }
2744
2745 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002746 * Set this notification to be part of a group of notifications sharing the same key.
2747 * Grouped notifications may display in a cluster or stack on devices which
2748 * support such rendering.
2749 *
2750 * <p>To make this notification the summary for its group, also call
2751 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2752 * {@link #setSortKey}.
2753 * @param groupKey The group key of the group.
2754 * @return this object for method chaining
2755 */
2756 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002757 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002758 return this;
2759 }
2760
2761 /**
2762 * Set this notification to be the group summary for a group of notifications.
2763 * Grouped notifications may display in a cluster or stack on devices which
2764 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2765 * @param isGroupSummary Whether this notification should be a group summary.
2766 * @return this object for method chaining
2767 */
2768 public Builder setGroupSummary(boolean isGroupSummary) {
2769 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2770 return this;
2771 }
2772
2773 /**
2774 * Set a sort key that orders this notification among other notifications from the
2775 * same package. This can be useful if an external sort was already applied and an app
2776 * would like to preserve this. Notifications will be sorted lexicographically using this
2777 * value, although providing different priorities in addition to providing sort key may
2778 * cause this value to be ignored.
2779 *
2780 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002781 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002782 *
2783 * @see String#compareTo(String)
2784 */
2785 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002786 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002787 return this;
2788 }
2789
2790 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002791 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002792 *
Griff Hazen720042b2014-02-24 15:46:56 -08002793 * <p>Values within the Bundle will replace existing extras values in this Builder.
2794 *
2795 * @see Notification#extras
2796 */
Griff Hazen959591e2014-05-15 22:26:18 -07002797 public Builder addExtras(Bundle extras) {
2798 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002799 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08002800 }
2801 return this;
2802 }
2803
2804 /**
2805 * Set metadata for this notification.
2806 *
2807 * <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 -04002808 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002809 * called.
2810 *
Griff Hazen720042b2014-02-24 15:46:56 -08002811 * <p>Replaces any existing extras values with those from the provided Bundle.
2812 * Use {@link #addExtras} to merge in metadata instead.
2813 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002814 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002815 */
Griff Hazen959591e2014-05-15 22:26:18 -07002816 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002817 if (extras != null) {
2818 mUserExtras = extras;
2819 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002820 return this;
2821 }
2822
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002823 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002824 * Get the current metadata Bundle used by this notification Builder.
2825 *
2826 * <p>The returned Bundle is shared with this Builder.
2827 *
2828 * <p>The current contents of this Bundle are copied into the Notification each time
2829 * {@link #build()} is called.
2830 *
2831 * @see Notification#extras
2832 */
2833 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002834 return mUserExtras;
2835 }
2836
2837 private Bundle getAllExtras() {
2838 final Bundle saveExtras = (Bundle) mUserExtras.clone();
2839 saveExtras.putAll(mN.extras);
2840 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08002841 }
2842
2843 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002844 * Add an action to this notification. Actions are typically displayed by
2845 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002846 * <p>
2847 * Every action must have an icon (32dp square and matching the
2848 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2849 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2850 * <p>
2851 * A notification in its expanded form can display up to 3 actions, from left to right in
2852 * the order they were added. Actions will not be displayed when the notification is
2853 * collapsed, however, so be sure that any essential functions may be accessed by the user
2854 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002855 *
2856 * @param icon Resource ID of a drawable that represents the action.
2857 * @param title Text describing the action.
2858 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04002859 *
2860 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002861 */
Dan Sandler86647982015-05-13 23:41:13 -04002862 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002863 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002864 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002865 return this;
2866 }
2867
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002868 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002869 * Add an action to this notification. Actions are typically displayed by
2870 * the system as a button adjacent to the notification content.
2871 * <p>
2872 * Every action must have an icon (32dp square and matching the
2873 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2874 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2875 * <p>
2876 * A notification in its expanded form can display up to 3 actions, from left to right in
2877 * the order they were added. Actions will not be displayed when the notification is
2878 * collapsed, however, so be sure that any essential functions may be accessed by the user
2879 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2880 *
2881 * @param action The action to add.
2882 */
2883 public Builder addAction(Action action) {
2884 mActions.add(action);
2885 return this;
2886 }
2887
2888 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002889 * Alter the complete list of actions attached to this notification.
2890 * @see #addAction(Action).
2891 *
2892 * @param actions
2893 * @return
2894 */
2895 public Builder setActions(Action... actions) {
2896 mActions.clear();
2897 for (int i = 0; i < actions.length; i++) {
2898 mActions.add(actions[i]);
2899 }
2900 return this;
2901 }
2902
2903 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002904 * Add a rich notification style to be applied at build time.
2905 *
2906 * @param style Object responsible for modifying the notification style.
2907 */
2908 public Builder setStyle(Style style) {
2909 if (mStyle != style) {
2910 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002911 if (mStyle != null) {
2912 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002913 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
2914 } else {
2915 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002916 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002917 }
2918 return this;
2919 }
2920
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002921 /**
2922 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002923 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002924 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2925 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
2926 *
2927 * @return The same Builder.
2928 */
2929 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002930 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002931 return this;
2932 }
2933
2934 /**
2935 * Supply a replacement Notification whose contents should be shown in insecure contexts
2936 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
2937 * @param n A replacement notification, presumably with some or all info redacted.
2938 * @return The same Builder.
2939 */
2940 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002941 if (n != null) {
2942 mN.publicVersion = new Notification();
2943 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
2944 } else {
2945 mN.publicVersion = null;
2946 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002947 return this;
2948 }
2949
Griff Hazenb720abe2014-05-20 13:15:30 -07002950 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002951 * Apply an extender to this notification builder. Extenders may be used to add
2952 * metadata or change options on this builder.
2953 */
Griff Hazen61a9e862014-05-22 16:05:19 -07002954 public Builder extend(Extender extender) {
2955 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002956 return this;
2957 }
2958
Dan Sandler4e787062015-06-17 15:09:48 -04002959 /**
2960 * @hide
2961 */
2962 public void setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002963 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002964 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08002965 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002966 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08002967 }
2968 }
2969
Dan Sandler26e81cf2014-05-06 10:01:27 -04002970 /**
2971 * Sets {@link Notification#color}.
2972 *
2973 * @param argb The accent color to use
2974 *
2975 * @return The same Builder.
2976 */
Tor Norbye80756e32015-03-02 09:39:27 -08002977 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002978 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002979 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04002980 return this;
2981 }
2982
Julia Reynolds74303cf2015-10-16 11:37:55 -04002983 /**
Julia Reynolds054c5dc2015-11-17 15:36:30 -05002984 * Sets the topic of this notification. Topics are typically displayed in Notification
Julia Reynolds74303cf2015-10-16 11:37:55 -04002985 * settings.
2986 * <p>
2987 * Every topic must have an id and a textual label.
2988 *
2989 * @param topic The topic to add.
2990 */
Julia Reynolds054c5dc2015-11-17 15:36:30 -05002991 public Builder setTopic(Topic topic) {
2992 mN.topic = topic;
Julia Reynolds74303cf2015-10-16 11:37:55 -04002993 return this;
2994 }
2995
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002996 private Drawable getProfileBadgeDrawable() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02002997 // Note: This assumes that the current user can read the profile badge of the
2998 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08002999 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003000 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003001 }
3002
3003 private Bitmap getProfileBadge() {
3004 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003005 if (badge == null) {
3006 return null;
3007 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003008 final int size = mContext.getResources().getDimensionPixelSize(
3009 R.dimen.notification_badge_size);
3010 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003011 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003012 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003013 badge.draw(canvas);
3014 return bitmap;
3015 }
3016
Selim Cinekc848c3a2016-01-13 15:27:30 -08003017 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003018 Bitmap profileBadge = getProfileBadge();
3019
Kenny Guy98193ea2014-07-24 19:54:37 +01003020 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003021 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3022 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Kenny Guy98193ea2014-07-24 19:54:37 +01003023 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003024 }
3025
Christoph Studerfe718432014-09-01 18:21:18 +02003026 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003027 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003028 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003029 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003030 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003031 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003032 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003033 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003034 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003035 contentView.setTextViewText(R.id.text_line_1, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003036 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003037 }
3038
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003039 /**
3040 * Resets the notification header to its original state
3041 */
3042 private void resetNotificationHeader(RemoteViews contentView) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003043 contentView.setImageViewResource(R.id.icon, 0);
Selim Cinek7b836392015-12-04 20:02:59 -08003044 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003045 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003046 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003047 contentView.setViewVisibility(R.id.header_sub_text, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003048 contentView.setViewVisibility(R.id.header_content_info, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003049 contentView.setViewVisibility(R.id.number_of_children, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003050 contentView.setViewVisibility(R.id.sub_text_divider, View.GONE);
3051 contentView.setViewVisibility(R.id.content_info_divider, View.GONE);
3052 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003053 contentView.setImageViewIcon(R.id.profile_badge, null);
3054 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003055 }
3056
3057 private void resetContentMargins(RemoteViews contentView) {
3058 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003059 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003060 }
3061
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003062 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003063 return applyStandardTemplate(resId, true /* hasProgress */);
3064 }
3065
3066 /**
3067 * @param hasProgress whether the progress bar should be shown and set
3068 */
3069 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Kenny Guy77320062014-08-27 21:37:15 +01003070 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003071
Christoph Studerfe718432014-09-01 18:21:18 +02003072 resetStandardTemplate(contentView);
3073
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003074 final Bundle ex = mN.extras;
Dan Sandler190d58d2014-05-15 09:33:39 -04003075
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003076 bindNotificationHeader(contentView);
3077 bindLargeIcon(contentView);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003078 if (ex.getCharSequence(EXTRA_TITLE) != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003079 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003080 contentView.setTextViewText(R.id.title,
3081 processLegacyText(ex.getCharSequence(EXTRA_TITLE)));
Joe Onorato561d3852010-11-20 18:09:34 -08003082 }
Selim Cinek29603462015-11-17 19:04:39 -08003083 boolean showProgress = handleProgressBar(hasProgress, contentView, ex);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003084 if (ex.getCharSequence(EXTRA_TEXT) != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003085 int textId = showProgress ? com.android.internal.R.id.text_line_1
3086 : com.android.internal.R.id.text;
3087 contentView.setTextViewText(textId, processLegacyText(
3088 ex.getCharSequence(EXTRA_TEXT)));
3089 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003090 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003091
Selim Cinek860b6da2015-12-16 19:02:19 -08003092 setContentMinHeight(contentView, showProgress || mN.mLargeIcon != null);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003093
Selim Cinek29603462015-11-17 19:04:39 -08003094 return contentView;
3095 }
3096
Selim Cinek860b6da2015-12-16 19:02:19 -08003097 /**
3098 * @param remoteView the remote view to update the minheight in
3099 * @param hasMinHeight does it have a mimHeight
3100 * @hide
3101 */
3102 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3103 int minHeight = 0;
3104 if (hasMinHeight) {
3105 // we need to set the minHeight of the notification
3106 minHeight = mContext.getResources().getDimensionPixelSize(
3107 com.android.internal.R.dimen.notification_min_content_height);
3108 }
3109 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3110 }
3111
Selim Cinek29603462015-11-17 19:04:39 -08003112 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003113 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3114 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3115 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3116 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003117 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003118 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003119 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003120 contentView.setProgressBackgroundTintList(
3121 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3122 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003123 if (mN.color != COLOR_DEFAULT) {
3124 ColorStateList colorStateList = ColorStateList.valueOf(mN.color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003125 contentView.setProgressTintList(R.id.progress, colorStateList);
3126 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003127 }
Selim Cinek29603462015-11-17 19:04:39 -08003128 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003129 } else {
3130 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003131 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003132 }
Joe Onorato561d3852010-11-20 18:09:34 -08003133 }
3134
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003135 private void bindLargeIcon(RemoteViews contentView) {
3136 if (mN.mLargeIcon != null) {
3137 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3138 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3139 processLargeLegacyIcon(mN.mLargeIcon, contentView);
3140 int endMargin = mContext.getResources().getDimensionPixelSize(
3141 R.dimen.notification_content_picture_margin);
3142 contentView.setViewLayoutMarginEnd(R.id.line1, endMargin);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003143 contentView.setViewLayoutMarginEnd(R.id.text, endMargin);
Selim Cinek29603462015-11-17 19:04:39 -08003144 contentView.setViewLayoutMarginEnd(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003145 }
3146 }
3147
3148 private void bindNotificationHeader(RemoteViews contentView) {
3149 bindSmallIcon(contentView);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003150 bindChildCountColor(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003151 bindHeaderAppName(contentView);
Selim Cinek03d0d652015-11-13 13:18:09 -05003152 bindHeaderSubText(contentView);
Selim Cinek29603462015-11-17 19:04:39 -08003153 bindContentInfo(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003154 bindHeaderChronometerAndTime(contentView);
3155 bindExpandButton(contentView);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003156 bindProfileBadge(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003157 }
3158
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003159 private void bindChildCountColor(RemoteViews contentView) {
3160 contentView.setTextColor(R.id.number_of_children, resolveColor());
3161 }
3162
Selim Cinek29603462015-11-17 19:04:39 -08003163 private void bindContentInfo(RemoteViews contentView) {
3164 boolean visible = false;
3165 if (mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3166 contentView.setTextViewText(R.id.header_content_info,
3167 processLegacyText(mN.extras.getCharSequence(EXTRA_INFO_TEXT)));
3168 contentView.setViewVisibility(R.id.header_content_info, View.VISIBLE);
3169 visible = true;
3170 } else if (mN.number > 0) {
3171 final int tooBig = mContext.getResources().getInteger(
3172 R.integer.status_bar_notification_info_maxnum);
3173 if (mN.number > tooBig) {
3174 contentView.setTextViewText(R.id.header_content_info, processLegacyText(
3175 mContext.getResources().getString(
3176 R.string.status_bar_notification_info_overflow)));
3177 } else {
3178 contentView.setTextViewText(R.id.header_content_info,
3179 processLegacyText(String.valueOf(mN.number)));
3180 }
3181 contentView.setViewVisibility(R.id.header_content_info, View.VISIBLE);
3182 visible = true;
3183 }
3184 if (visible) {
3185 contentView.setViewVisibility(R.id.content_info_divider, View.VISIBLE);
3186 }
3187 }
3188
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003189 private void bindExpandButton(RemoteViews contentView) {
3190 contentView.setDrawableParameters(R.id.expand_button, false, -1, resolveColor(),
3191 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003192 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
3193 resolveColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003194 }
3195
3196 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3197 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003198 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003199 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3200 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3201 contentView.setLong(R.id.chronometer, "setBase",
3202 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3203 contentView.setBoolean(R.id.chronometer, "setStarted", true);
3204 } else {
3205 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3206 contentView.setLong(R.id.time, "setTime", mN.when);
3207 }
3208 }
3209 }
3210
Selim Cinek03d0d652015-11-13 13:18:09 -05003211 private void bindHeaderSubText(RemoteViews contentView) {
3212 CharSequence subText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3213 if (subText == null && mStyle != null && mStyle.mSummaryTextSet
3214 && mStyle.hasSummaryInHeader()) {
3215 subText = mStyle.mSummaryText;
3216 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003217 if (subText != null) {
3218 // TODO: Remove the span entirely to only have the string with propper formating.
3219 contentView.setTextViewText(R.id.header_sub_text, processLegacyText(subText));
3220 contentView.setViewVisibility(R.id.header_sub_text, View.VISIBLE);
Selim Cinek29603462015-11-17 19:04:39 -08003221 contentView.setViewVisibility(R.id.sub_text_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003222 }
3223 }
3224
3225 private void bindHeaderAppName(RemoteViews contentView) {
3226 PackageManager packageManager = mContext.getPackageManager();
3227 ApplicationInfo info = null;
3228 try {
3229 info = packageManager.getApplicationInfo(mContext.getApplicationInfo().packageName,
3230 0);
3231 } catch (final NameNotFoundException e) {
3232 return;
3233 }
3234 CharSequence appName = info != null ? packageManager.getApplicationLabel(info)
3235 : null;
3236 if (TextUtils.isEmpty(appName)) {
3237 return;
3238 }
3239 contentView.setTextViewText(R.id.app_name_text, appName);
Selim Cinekaef485a2016-02-05 10:31:20 -08003240 contentView.setTextColor(R.id.app_name_text, resolveColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003241 }
3242
3243 private void bindSmallIcon(RemoteViews contentView) {
3244 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
3245 processSmallIconColor(mN.mSmallIcon, contentView);
3246 }
3247
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003248 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003249 * @return true if the built notification will show the time or the chronometer; false
3250 * otherwise
3251 */
3252 private boolean showsTimeOrChronometer() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003253 return mN.when != 0 && mN.extras.getBoolean(EXTRA_SHOW_WHEN);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003254 }
3255
Christoph Studerfe718432014-09-01 18:21:18 +02003256 private void resetStandardTemplateWithActions(RemoteViews big) {
3257 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003258 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003259
3260 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3261 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3262
3263 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3264 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3265 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3266 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003267 }
3268
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003269 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003270 RemoteViews big = applyStandardTemplate(layoutId);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003271
Christoph Studerfe718432014-09-01 18:21:18 +02003272 resetStandardTemplateWithActions(big);
3273
Adrian Roose458aa82015-12-08 16:17:19 -08003274 boolean validRemoteInput = false;
3275
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003276 int N = mActions.size();
3277 if (N > 0) {
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003278 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003279 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003280 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003281 Action action = mActions.get(i);
3282 validRemoteInput |= hasValidRemoteInput(action);
3283
3284 final RemoteViews button = generateActionButton(action);
Adrian Roos9b123cf2016-02-04 14:55:57 -08003285 if (i == N - 1) {
3286 button.setViewLayoutWidth(com.android.internal.R.id.action0,
3287 ViewGroup.LayoutParams.MATCH_PARENT);
3288 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003289 big.addView(R.id.actions, button);
3290 }
3291 }
Adrian Roose458aa82015-12-08 16:17:19 -08003292
3293 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
3294 if (validRemoteInput && replyText != null
3295 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3296 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3297 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
3298
3299 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3300 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3301 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
3302
3303 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3304 big.setViewVisibility(
3305 R.id.notification_material_reply_text_3, View.VISIBLE);
3306 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
3307 }
3308 }
3309 }
3310
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003311 return big;
3312 }
3313
Adrian Roose458aa82015-12-08 16:17:19 -08003314 private boolean hasValidRemoteInput(Action action) {
3315 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
3316 // Weird actions
3317 return false;
3318 }
3319
3320 RemoteInput[] remoteInputs = action.getRemoteInputs();
3321 if (remoteInputs == null) {
3322 return false;
3323 }
3324
3325 for (RemoteInput r : remoteInputs) {
3326 CharSequence[] choices = r.getChoices();
3327 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
3328 return true;
3329 }
3330 }
3331 return false;
3332 }
3333
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003334 /**
3335 * Construct a RemoteViews for the final 1U notification layout. In order:
3336 * 1. Custom contentView from the caller
3337 * 2. Style's proposed content view
3338 * 3. Standard template view
3339 */
3340 public RemoteViews makeContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003341 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003342 return mN.contentView;
3343 } else if (mStyle != null) {
3344 final RemoteViews styleView = mStyle.makeContentView();
3345 if (styleView != null) {
3346 return styleView;
3347 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003348 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003349 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003350 }
3351
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003352 /**
3353 * Construct a RemoteViews for the final big notification layout.
3354 */
3355 public RemoteViews makeBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05003356 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08003357 if (mN.bigContentView != null
3358 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003359 return mN.bigContentView;
3360 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05003361 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08003362 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003363 } else if (mActions.size() != 0) {
3364 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05003365 }
3366 adaptNotificationHeaderForBigContentView(result);
3367 return result;
3368 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003369
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003370 /**
3371 * Construct a RemoteViews for the final notification header only
3372 *
3373 * @hide
3374 */
3375 public RemoteViews makeNotificationHeader() {
3376 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
3377 R.layout.notification_template_header);
3378 resetNotificationHeader(header);
3379 bindNotificationHeader(header);
3380 return header;
3381 }
3382
Selim Cinek29603462015-11-17 19:04:39 -08003383 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003384 if (result != null) {
3385 result.setViewVisibility(R.id.text_line_1, View.GONE);
3386 }
Selim Cinek29603462015-11-17 19:04:39 -08003387 }
3388
Selim Cinek850a8542015-11-11 11:48:36 -05003389 private void adaptNotificationHeaderForBigContentView(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003390 if (result != null) {
3391 result.setBoolean(R.id.notification_header, "setExpanded", true);
3392 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003393 }
3394
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003395 /**
3396 * Construct a RemoteViews for the final heads-up notification layout.
3397 */
3398 public RemoteViews makeHeadsUpContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003399 if (mN.headsUpContentView != null
3400 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003401 return mN.headsUpContentView;
3402 } else if (mStyle != null) {
3403 final RemoteViews styleView = mStyle.makeHeadsUpContentView();
3404 if (styleView != null) {
3405 return styleView;
3406 }
3407 } else if (mActions.size() == 0) {
3408 return null;
3409 }
3410
Chris Wren8fd39ec2014-02-27 17:43:26 -05003411
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003412 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003413 }
3414
Selim Cinek624c02db2015-12-14 21:00:02 -08003415 /**
3416 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
3417 *
3418 * @hide
3419 */
3420 public RemoteViews makePublicContentView() {
3421 if (mN.publicVersion != null) {
3422 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
3423 return builder.makeContentView();
3424 }
3425 Bundle savedBundle = mN.extras;
3426 Style style = mStyle;
3427 mStyle = null;
3428 Icon largeIcon = mN.mLargeIcon;
3429 mN.mLargeIcon = null;
3430 Bundle publicExtras = new Bundle();
3431 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
3432 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
3433 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
3434 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
3435 publicExtras.putCharSequence(EXTRA_TITLE,
3436 mContext.getString(R.string.notification_hidden_text));
3437 mN.extras = publicExtras;
3438 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
3439 mN.extras = savedBundle;
3440 mN.mLargeIcon = largeIcon;
3441 mStyle = style;
3442 return publicView;
3443 }
3444
3445
Chris Wren8fd39ec2014-02-27 17:43:26 -05003446
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003447 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003448 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07003449 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003450 tombstone ? getActionTombstoneLayoutResource()
3451 : getActionLayoutResource());
Dan Sandler68079d52015-07-22 10:45:30 -04003452 final Icon ai = action.getIcon();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003453 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04003454 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003455 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003456 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003457 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003458 if (action.mRemoteInputs != null) {
3459 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
3460 }
3461 if (mN.color != COLOR_DEFAULT) {
3462 button.setTextColor(R.id.action0, mN.color);
3463 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003464 return button;
3465 }
3466
Joe Onoratocb109a02011-01-18 17:57:41 -08003467 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003468 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003469 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003470 */
3471 private boolean isLegacy() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003472 return getColorUtil() != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003473 }
3474
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003475 private CharSequence processLegacyText(CharSequence charSequence) {
3476 if (isLegacy()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003477 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003478 } else {
3479 return charSequence;
3480 }
3481 }
3482
Dan Sandler26e81cf2014-05-06 10:01:27 -04003483 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003484 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04003485 */
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003486 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView) {
Selim Cinekea4bef72015-12-02 15:51:10 -08003487 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
3488 if (colorable) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003489 contentView.setDrawableParameters(R.id.icon, false, -1, resolveColor(),
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003490 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003491
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003492 }
Selim Cinekea4bef72015-12-02 15:51:10 -08003493 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
3494 colorable ? resolveColor() : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003495 }
3496
Dan Sandler26e81cf2014-05-06 10:01:27 -04003497 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003498 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04003499 * if it's grayscale).
3500 */
3501 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003502 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3503 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003504 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003505 // resolve color will fall back to the default when legacy
3506 contentView.setDrawableParameters(R.id.icon, false, -1, resolveColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003507 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003508 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003509 }
3510
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003511 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003512 if (mN.color != COLOR_DEFAULT) {
3513 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02003514 }
Jorim Jaggi74419312014-06-10 20:57:21 +02003515 }
3516
Selim Cinek5bf069a2015-11-10 19:14:27 -05003517 int resolveColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003518 if (mN.color == COLOR_DEFAULT) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003519 return mContext.getColor(R.color.notification_icon_default_color);
Dan Sandler26e81cf2014-05-06 10:01:27 -04003520 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003521 return mN.color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003522 }
3523
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003524 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003525 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003526 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003527 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003528 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003529 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003530 mN.actions = new Action[mActions.size()];
3531 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003532 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003533 if (!mPersonList.isEmpty()) {
3534 mN.extras.putStringArray(EXTRA_PEOPLE,
3535 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003536 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003537 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08003538 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003539
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003540 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003541 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003542 ApplicationInfo applicationInfo = n.extras.getParcelable(
3543 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003544 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05003545 if (applicationInfo != null) {
3546 try {
3547 builderContext = context.createApplicationContext(applicationInfo,
3548 Context.CONTEXT_RESTRICTED);
3549 } catch (NameNotFoundException e) {
3550 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
3551 builderContext = context; // try with our context
3552 }
3553 } else {
3554 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02003555 }
3556
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003557 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003558 }
3559
3560 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
Selim Cinek593610c2016-02-16 18:42:57 -08003561 Class<? extends Style>[] classes = new Class[] {
3562 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
3563 DecoratedCustomViewStyle.class };
Christoph Studer4600f9b2014-07-22 22:44:43 +02003564 for (Class<? extends Style> innerClass : classes) {
3565 if (templateClass.equals(innerClass.getName())) {
3566 return innerClass;
3567 }
3568 }
3569 return null;
3570 }
3571
3572 private void setBuilderContentView(Notification n, RemoteViews contentView) {
3573 n.contentView = contentView;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003574 }
3575
3576 private void setBuilderBigContentView(Notification n, RemoteViews bigContentView) {
3577 n.bigContentView = bigContentView;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003578 }
3579
3580 private void setBuilderHeadsUpContentView(Notification n,
3581 RemoteViews headsUpContentView) {
3582 n.headsUpContentView = headsUpContentView;
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003583 }
3584
3585 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003586 * @deprecated Use {@link #build()} instead.
3587 */
3588 @Deprecated
3589 public Notification getNotification() {
3590 return build();
3591 }
3592
3593 /**
3594 * Combine all of the options that have been set and return a new {@link Notification}
3595 * object.
3596 */
3597 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003598 // first, add any extras from the calling code
3599 if (mUserExtras != null) {
3600 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003601 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003602
3603 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05003604 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02003605
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003606 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003607
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003608 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003609 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003610 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003611
Julia Reynolds4c0c2022016-02-02 15:11:59 -05003612 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
3613 mN.flags |= FLAG_SHOW_LIGHTS;
3614 }
3615
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003616 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003617 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003618
3619 /**
3620 * Apply this Builder to an existing {@link Notification} object.
3621 *
3622 * @hide
3623 */
3624 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003625 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003626 return n;
3627 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003628
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003629 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003630 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003631 }
3632
3633 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003634 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003635 }
3636
3637 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003638 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003639 }
3640
3641 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003642 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003643 }
3644
3645 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003646 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003647 }
3648
3649 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003650 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003651 }
3652
3653 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003654 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003655 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003656 }
3657
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003658 /**
3659 * An object that can apply a rich notification style to a {@link Notification.Builder}
3660 * object.
3661 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003662 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003663 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003664
3665 /**
3666 * @hide
3667 */
3668 protected CharSequence mSummaryText = null;
3669
3670 /**
3671 * @hide
3672 */
3673 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003674
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003675 protected Builder mBuilder;
3676
Chris Wrend6297db2012-05-03 16:20:13 -04003677 /**
3678 * Overrides ContentTitle in the big form of the template.
3679 * This defaults to the value passed to setContentTitle().
3680 */
3681 protected void internalSetBigContentTitle(CharSequence title) {
3682 mBigContentTitle = title;
3683 }
3684
3685 /**
3686 * Set the first line of text after the detail section in the big form of the template.
3687 */
3688 protected void internalSetSummaryText(CharSequence cs) {
3689 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003690 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003691 }
3692
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003693 public void setBuilder(Builder builder) {
3694 if (mBuilder != builder) {
3695 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003696 if (mBuilder != null) {
3697 mBuilder.setStyle(this);
3698 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003699 }
3700 }
3701
Chris Wrend6297db2012-05-03 16:20:13 -04003702 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003703 if (mBuilder == null) {
3704 throw new IllegalArgumentException("Style requires a valid Builder object");
3705 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003706 }
Chris Wrend6297db2012-05-03 16:20:13 -04003707
3708 protected RemoteViews getStandardView(int layoutId) {
3709 checkBuilder();
3710
Christoph Studer4600f9b2014-07-22 22:44:43 +02003711 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003712 CharSequence oldBuilderContentTitle =
3713 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003714 if (mBigContentTitle != null) {
3715 mBuilder.setContentTitle(mBigContentTitle);
3716 }
3717
Chris Wrend6297db2012-05-03 16:20:13 -04003718 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3719
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003720 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003721
Chris Wrend6297db2012-05-03 16:20:13 -04003722 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3723 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003724 } else {
3725 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003726 }
3727
Chris Wrend6297db2012-05-03 16:20:13 -04003728 return contentView;
3729 }
3730
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003731 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003732 * Construct a Style-specific RemoteViews for the final 1U notification layout.
3733 * The default implementation has nothing additional to add.
3734 * @hide
3735 */
3736 public RemoteViews makeContentView() {
3737 return null;
3738 }
3739
3740 /**
3741 * Construct a Style-specific RemoteViews for the final big notification layout.
3742 * @hide
3743 */
3744 public RemoteViews makeBigContentView() {
3745 return null;
3746 }
3747
3748 /**
3749 * Construct a Style-specific RemoteViews for the final HUN layout.
3750 * @hide
3751 */
3752 public RemoteViews makeHeadsUpContentView() {
3753 return null;
3754 }
3755
3756 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003757 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003758 * @hide
3759 */
3760 public void addExtras(Bundle extras) {
3761 if (mSummaryTextSet) {
3762 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
3763 }
3764 if (mBigContentTitle != null) {
3765 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
3766 }
Chris Wren91ad5632013-06-05 15:05:57 -04003767 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003768 }
3769
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003770 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003771 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003772 * @hide
3773 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02003774 protected void restoreFromExtras(Bundle extras) {
3775 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
3776 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
3777 mSummaryTextSet = true;
3778 }
3779 if (extras.containsKey(EXTRA_TITLE_BIG)) {
3780 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
3781 }
3782 }
3783
3784
3785 /**
3786 * @hide
3787 */
3788 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003789 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003790 return wip;
3791 }
3792
Daniel Sandler0ec46202015-06-24 01:27:05 -04003793 /**
3794 * @hide
3795 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003796 public void purgeResources() {}
3797
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003798 /**
3799 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
3800 * attached to.
3801 *
3802 * @return the fully constructed Notification.
3803 */
3804 public Notification build() {
3805 checkBuilder();
3806 return mBuilder.build();
3807 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003808
3809 /**
3810 * @hide
3811 * @return true if the style positions the progress bar on the second line; false if the
3812 * style hides the progress bar
3813 */
3814 protected boolean hasProgress() {
3815 return true;
3816 }
Selim Cinek03d0d652015-11-13 13:18:09 -05003817
3818 /**
3819 * @hide
3820 * @return Whether we should put the summary be put into the notification header
3821 */
3822 public boolean hasSummaryInHeader() {
3823 return true;
3824 }
Selim Cinek593610c2016-02-16 18:42:57 -08003825
3826 /**
3827 * @hide
3828 * @return Whether custom content views are displayed inline in the style
3829 */
3830 public boolean displayCustomViewInline() {
3831 return false;
3832 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003833 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003834
3835 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003836 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08003837 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003838 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003839 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003840 * Notification notif = new Notification.Builder(mContext)
3841 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
3842 * .setContentText(subject)
3843 * .setSmallIcon(R.drawable.new_post)
3844 * .setLargeIcon(aBitmap)
3845 * .setStyle(new Notification.BigPictureStyle()
3846 * .bigPicture(aBigBitmap))
3847 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003848 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003849 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003850 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003851 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003852 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003853 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003854 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003855 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003856
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003857 public BigPictureStyle() {
3858 }
3859
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003860 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003861 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003862 }
3863
Chris Wrend6297db2012-05-03 16:20:13 -04003864 /**
3865 * Overrides ContentTitle in the big form of the template.
3866 * This defaults to the value passed to setContentTitle().
3867 */
3868 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003869 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003870 return this;
3871 }
3872
3873 /**
3874 * Set the first line of text after the detail section in the big form of the template.
3875 */
3876 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003877 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003878 return this;
3879 }
3880
Chris Wren0bd664d2012-08-01 13:56:56 -04003881 /**
3882 * Provide the bitmap to be used as the payload for the BigPicture notification.
3883 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003884 public BigPictureStyle bigPicture(Bitmap b) {
3885 mPicture = b;
3886 return this;
3887 }
3888
Chris Wren3745a3d2012-05-22 15:11:52 -04003889 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04003890 * Override the large icon when the big notification is shown.
3891 */
3892 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003893 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3894 }
3895
3896 /**
3897 * Override the large icon when the big notification is shown.
3898 */
3899 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04003900 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003901 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003902 return this;
3903 }
3904
Riley Andrews0394a0c2015-11-03 23:36:52 -08003905 /** @hide */
3906 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
3907
Daniel Sandler0ec46202015-06-24 01:27:05 -04003908 /**
3909 * @hide
3910 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003911 @Override
3912 public void purgeResources() {
3913 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08003914 if (mPicture != null &&
3915 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08003916 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003917 mPicture = mPicture.createAshmemBitmap();
3918 }
3919 if (mBigLargeIcon != null) {
3920 mBigLargeIcon.convertToAshmem();
3921 }
3922 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01003923
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003924 /**
3925 * @hide
3926 */
3927 public RemoteViews makeBigContentView() {
3928 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01003929 // This covers the following cases:
3930 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003931 // mN.mLargeIcon
3932 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04003933 Icon oldLargeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003934 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003935 oldLargeIcon = mBuilder.mN.mLargeIcon;
3936 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003937 }
3938
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003939 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05003940 if (mSummaryTextSet) {
3941 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinekc848c3a2016-01-13 15:27:30 -08003942 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05003943 }
Selim Cinek860b6da2015-12-16 19:02:19 -08003944 mBuilder.setContentMinHeight(contentView, mBuilder.mN.mLargeIcon != null);
Selim Cinek53e64a42015-11-16 10:40:56 -08003945
Christoph Studer5c510ee2014-12-15 16:32:27 +01003946 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003947 mBuilder.mN.mLargeIcon = oldLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003948 }
3949
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003950 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003951 return contentView;
3952 }
3953
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003954 /**
3955 * @hide
3956 */
3957 public void addExtras(Bundle extras) {
3958 super.addExtras(extras);
3959
3960 if (mBigLargeIconSet) {
3961 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
3962 }
3963 extras.putParcelable(EXTRA_PICTURE, mPicture);
3964 }
3965
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003966 /**
3967 * @hide
3968 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003969 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003970 protected void restoreFromExtras(Bundle extras) {
3971 super.restoreFromExtras(extras);
3972
3973 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01003974 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003975 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04003976 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003977 mPicture = extras.getParcelable(EXTRA_PICTURE);
3978 }
Selim Cinek03d0d652015-11-13 13:18:09 -05003979
3980 /**
3981 * @hide
3982 */
3983 @Override
3984 public boolean hasSummaryInHeader() {
3985 return false;
3986 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003987 }
3988
3989 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003990 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08003991 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003992 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003993 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003994 * Notification notif = new Notification.Builder(mContext)
3995 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3996 * .setContentText(subject)
3997 * .setSmallIcon(R.drawable.new_mail)
3998 * .setLargeIcon(aBitmap)
3999 * .setStyle(new Notification.BigTextStyle()
4000 * .bigText(aVeryLongString))
4001 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004002 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004003 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004004 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004005 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004006 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004007
4008 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004009 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004010
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004011 private CharSequence mBigText;
4012
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004013 public BigTextStyle() {
4014 }
4015
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004016 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004017 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004018 }
4019
Chris Wrend6297db2012-05-03 16:20:13 -04004020 /**
4021 * Overrides ContentTitle in the big form of the template.
4022 * This defaults to the value passed to setContentTitle().
4023 */
4024 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004025 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004026 return this;
4027 }
4028
4029 /**
4030 * Set the first line of text after the detail section in the big form of the template.
4031 */
4032 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004033 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004034 return this;
4035 }
4036
Chris Wren0bd664d2012-08-01 13:56:56 -04004037 /**
4038 * Provide the longer text to be displayed in the big form of the
4039 * template in place of the content text.
4040 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004041 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004042 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004043 return this;
4044 }
4045
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004046 /**
4047 * @hide
4048 */
4049 public void addExtras(Bundle extras) {
4050 super.addExtras(extras);
4051
Christoph Studer4600f9b2014-07-22 22:44:43 +02004052 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4053 }
4054
4055 /**
4056 * @hide
4057 */
4058 @Override
4059 protected void restoreFromExtras(Bundle extras) {
4060 super.restoreFromExtras(extras);
4061
4062 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004063 }
4064
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004065 /**
4066 * @hide
4067 */
4068 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004069
4070 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004071 CharSequence oldBuilderContentText =
4072 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
4073 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004074
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004075 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004076
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004077 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004078
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004079 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
4080 contentView.setTextViewText(R.id.big_text, bigTextText);
4081 contentView.setViewVisibility(R.id.big_text,
4082 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004083 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines());
Selim Cinek4fb12d32015-11-19 18:10:48 -08004084 contentView.setBoolean(R.id.big_text, "setHasImage", mBuilder.mN.mLargeIcon != null);
4085
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004086 return contentView;
4087 }
4088
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004089 private int calculateMaxLines() {
4090 int lineCount = MAX_LINES;
4091 boolean hasActions = mBuilder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004092 if (hasActions) {
4093 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4094 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004095 return lineCount;
4096 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004097 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004098
4099 /**
4100 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08004101 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004102 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04004103 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004104 * Notification notif = new Notification.Builder(mContext)
4105 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
4106 * .setContentText(subject)
4107 * .setSmallIcon(R.drawable.new_mail)
4108 * .setLargeIcon(aBitmap)
4109 * .setStyle(new Notification.InboxStyle()
4110 * .addLine(str1)
4111 * .addLine(str2)
4112 * .setContentTitle(&quot;&quot;)
4113 * .setSummaryText(&quot;+3 more&quot;))
4114 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04004115 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004116 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04004117 * @see Notification#bigContentView
4118 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004119 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004120 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
4121
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004122 public InboxStyle() {
4123 }
4124
Daniel Sandler879c5e02012-04-17 16:46:51 -04004125 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004126 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004127 }
4128
Chris Wrend6297db2012-05-03 16:20:13 -04004129 /**
4130 * Overrides ContentTitle in the big form of the template.
4131 * This defaults to the value passed to setContentTitle().
4132 */
4133 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004134 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004135 return this;
4136 }
4137
4138 /**
4139 * Set the first line of text after the detail section in the big form of the template.
4140 */
4141 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004142 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004143 return this;
4144 }
4145
Chris Wren0bd664d2012-08-01 13:56:56 -04004146 /**
4147 * Append a line to the digest section of the Inbox notification.
4148 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04004149 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004150 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04004151 return this;
4152 }
4153
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004154 /**
4155 * @hide
4156 */
4157 public void addExtras(Bundle extras) {
4158 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004159
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004160 CharSequence[] a = new CharSequence[mTexts.size()];
4161 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
4162 }
4163
Christoph Studer4600f9b2014-07-22 22:44:43 +02004164 /**
4165 * @hide
4166 */
4167 @Override
4168 protected void restoreFromExtras(Bundle extras) {
4169 super.restoreFromExtras(extras);
4170
4171 mTexts.clear();
4172 if (extras.containsKey(EXTRA_TEXT_LINES)) {
4173 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
4174 }
4175 }
4176
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004177 /**
4178 * @hide
4179 */
4180 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004181 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02004182 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004183 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
4184 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004185
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004186 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04004187
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004188 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004189
Chris Wrend6297db2012-05-03 16:20:13 -04004190 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 -04004191 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04004192
Chris Wren4ed80d52012-05-17 09:30:03 -04004193 // Make sure all rows are gone in case we reuse a view.
4194 for (int rowId : rowIds) {
4195 contentView.setViewVisibility(rowId, View.GONE);
4196 }
4197
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004198 final boolean largeText =
4199 mBuilder.mContext.getResources().getConfiguration().fontScale > 1f;
4200 final float subTextSize = mBuilder.mContext.getResources().getDimensionPixelSize(
4201 R.dimen.notification_subtext_size);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004202 int i=0;
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004203 final float density = mBuilder.mContext.getResources().getDisplayMetrics().density;
4204 int topPadding = (int) (5 * density);
4205 int bottomPadding = (int) (13 * density);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004206 while (i < mTexts.size() && i < rowIds.length) {
4207 CharSequence str = mTexts.get(i);
4208 if (str != null && !str.equals("")) {
4209 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004210 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004211 if (largeText) {
4212 contentView.setTextViewTextSize(rowIds[i], TypedValue.COMPLEX_UNIT_PX,
4213 subTextSize);
4214 }
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004215 contentView.setViewPadding(rowIds[i], 0, topPadding, 0,
4216 i == rowIds.length - 1 || i == mTexts.size() - 1 ? bottomPadding : 0);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004217 }
4218 i++;
4219 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004220
Selim Cinek1e0bf612015-11-20 15:57:26 -08004221 handleInboxImageMargin(contentView, rowIds[0]);
4222
Daniel Sandler879c5e02012-04-17 16:46:51 -04004223 return contentView;
4224 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004225
4226 private void handleInboxImageMargin(RemoteViews contentView, int id) {
4227 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
4228 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4229 boolean hasProgress = max != 0 || ind;
4230 int endMargin = 0;
4231 if (mTexts.size() > 0 && mBuilder.mN.mLargeIcon != null && !hasProgress) {
4232 endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4233 R.dimen.notification_content_picture_margin);
4234 }
4235 contentView.setViewLayoutMarginEnd(id, endMargin);
4236 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004237 }
Dan Sandler842dd772014-05-15 09:36:47 -04004238
4239 /**
4240 * Notification style for media playback notifications.
4241 *
4242 * In the expanded form, {@link Notification#bigContentView}, up to 5
4243 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04004244 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04004245 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
4246 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
4247 * treated as album artwork.
4248 *
4249 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
4250 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01004251 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04004252 * in the standard view alongside the usual content.
4253 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004254 * Notifications created with MediaStyle will have their category set to
4255 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
4256 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
4257 *
Jeff Browndba34ba2014-06-24 20:46:03 -07004258 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
4259 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04004260 * the System UI can identify this as a notification representing an active media session
4261 * and respond accordingly (by showing album artwork in the lockscreen, for example).
4262 *
4263 * To use this style with your Notification, feed it to
4264 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4265 * <pre class="prettyprint">
4266 * Notification noti = new Notification.Builder()
4267 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01004268 * .setContentTitle(&quot;Track title&quot;)
4269 * .setContentText(&quot;Artist - Album&quot;)
4270 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004271 * .setStyle(<b>new Notification.MediaStyle()</b>
4272 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04004273 * .build();
4274 * </pre>
4275 *
4276 * @see Notification#bigContentView
4277 */
4278 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004279 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04004280 static final int MAX_MEDIA_BUTTONS = 5;
4281
4282 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07004283 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04004284
4285 public MediaStyle() {
4286 }
4287
4288 public MediaStyle(Builder builder) {
4289 setBuilder(builder);
4290 }
4291
4292 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004293 * 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 -04004294 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004295 *
4296 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04004297 */
4298 public MediaStyle setShowActionsInCompactView(int...actions) {
4299 mActionsToShowInCompact = actions;
4300 return this;
4301 }
4302
4303 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07004304 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
4305 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04004306 */
Jeff Browndba34ba2014-06-24 20:46:03 -07004307 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04004308 mToken = token;
4309 return this;
4310 }
4311
Christoph Studer4600f9b2014-07-22 22:44:43 +02004312 /**
4313 * @hide
4314 */
Dan Sandler842dd772014-05-15 09:36:47 -04004315 @Override
4316 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004317 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004318 if (wip.category == null) {
4319 wip.category = Notification.CATEGORY_TRANSPORT;
4320 }
Dan Sandler842dd772014-05-15 09:36:47 -04004321 return wip;
4322 }
4323
Christoph Studer4600f9b2014-07-22 22:44:43 +02004324 /**
4325 * @hide
4326 */
4327 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004328 public RemoteViews makeContentView() {
4329 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004330 }
4331
4332 /**
4333 * @hide
4334 */
4335 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004336 public RemoteViews makeBigContentView() {
4337 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004338 }
4339
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004340 /**
4341 * @hide
4342 */
4343 @Override
4344 public RemoteViews makeHeadsUpContentView() {
4345 RemoteViews expanded = makeMediaBigContentView();
4346 return expanded != null ? expanded : makeMediaContentView();
4347 }
4348
Dan Sandler842dd772014-05-15 09:36:47 -04004349 /** @hide */
4350 @Override
4351 public void addExtras(Bundle extras) {
4352 super.addExtras(extras);
4353
4354 if (mToken != null) {
4355 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
4356 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01004357 if (mActionsToShowInCompact != null) {
4358 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
4359 }
Dan Sandler842dd772014-05-15 09:36:47 -04004360 }
4361
Christoph Studer4600f9b2014-07-22 22:44:43 +02004362 /**
4363 * @hide
4364 */
4365 @Override
4366 protected void restoreFromExtras(Bundle extras) {
4367 super.restoreFromExtras(extras);
4368
4369 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
4370 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
4371 }
4372 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
4373 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
4374 }
4375 }
4376
Selim Cinek5bf069a2015-11-10 19:14:27 -05004377 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04004378 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004379 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07004380 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04004381 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05004382 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
4383 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04004384 if (!tombstone) {
4385 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
4386 }
4387 button.setContentDescription(R.id.action0, action.title);
4388 return button;
4389 }
4390
4391 private RemoteViews makeMediaContentView() {
4392 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004393 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04004394
4395 final int numActions = mBuilder.mActions.size();
4396 final int N = mActionsToShowInCompact == null
4397 ? 0
4398 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4399 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004400 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04004401 for (int i = 0; i < N; i++) {
4402 if (i >= numActions) {
4403 throw new IllegalArgumentException(String.format(
4404 "setShowActionsInCompactView: action %d out of bounds (max %d)",
4405 i, numActions - 1));
4406 }
4407
4408 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05004409 final RemoteViews button = generateMediaActionButton(action,
4410 mBuilder.resolveColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004411 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004412 }
4413 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08004414 handleImage(view);
4415 // handle the content margin
4416 int endMargin;
4417 if (mBuilder.mN.mLargeIcon != null) {
4418 endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4419 R.dimen.notification_content_picture_margin_media);
4420 } else {
4421 endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4422 R.dimen.notification_content_margin_end);
4423 }
4424 view.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04004425 return view;
4426 }
4427
4428 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004429 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004430 // Dont add an expanded view if there is no more content to be revealed
4431 int actionsInCompact = mActionsToShowInCompact == null
4432 ? 0
4433 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4434 if (mBuilder.mN.mLargeIcon == null && actionCount <= actionsInCompact) {
4435 return null;
4436 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004437 RemoteViews big = mBuilder.applyStandardTemplate(
4438 R.layout.notification_template_material_big_media,
4439 false);
Dan Sandler842dd772014-05-15 09:36:47 -04004440
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004441 if (actionCount > 0) {
4442 big.removeAllViews(com.android.internal.R.id.media_actions);
4443 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05004444 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
4445 mBuilder.resolveColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004446 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004447 }
4448 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004449 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04004450 return big;
4451 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004452
Selim Cinek5bf069a2015-11-10 19:14:27 -05004453 private void handleImage(RemoteViews contentView) {
4454 if (mBuilder.mN.mLargeIcon != null) {
4455 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004456 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004457 }
4458 }
4459
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004460 /**
4461 * @hide
4462 */
4463 @Override
4464 protected boolean hasProgress() {
4465 return false;
4466 }
Dan Sandler842dd772014-05-15 09:36:47 -04004467 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004468
Selim Cinek593610c2016-02-16 18:42:57 -08004469 /**
4470 * Notification style for custom views that are decorated by the system
4471 *
4472 * <p>Instead of providing a notification that is completely custom, a developer can set this
4473 * style and still obtain system decorations like the notification header with the expand
4474 * affordance and actions.
4475 *
4476 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
4477 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
4478 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
4479 * corresponding custom views to display.
4480 *
4481 * To use this style with your Notification, feed it to
4482 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4483 * <pre class="prettyprint">
4484 * Notification noti = new Notification.Builder()
4485 * .setSmallIcon(R.drawable.ic_stat_player)
4486 * .setLargeIcon(albumArtBitmap))
4487 * .setCustomContentView(contentView);
4488 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
4489 * .build();
4490 * </pre>
4491 */
4492 public static class DecoratedCustomViewStyle extends Style {
4493
4494 public DecoratedCustomViewStyle() {
4495 }
4496
4497 public DecoratedCustomViewStyle(Builder builder) {
4498 setBuilder(builder);
4499 }
4500
4501 /**
4502 * @hide
4503 */
4504 public boolean displayCustomViewInline() {
4505 return true;
4506 }
4507
4508 /**
4509 * @hide
4510 */
4511 @Override
4512 public RemoteViews makeContentView() {
4513 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
4514 }
4515
4516 /**
4517 * @hide
4518 */
4519 @Override
4520 public RemoteViews makeBigContentView() {
4521 return makeDecoratedBigContentView();
4522 }
4523
4524 /**
4525 * @hide
4526 */
4527 @Override
4528 public RemoteViews makeHeadsUpContentView() {
4529 return makeDecoratedHeadsUpContentView();
4530 }
4531
4532 /**
4533 * @hide
4534 */
4535 private RemoteViews makeDecoratedHeadsUpContentView() {
4536 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
4537 ? mBuilder.mN.contentView
4538 : mBuilder.mN.headsUpContentView;
4539 if (mBuilder.mActions.size() == 0) {
4540 return makeStandardTemplateWithCustomContent(headsUpContentView);
4541 }
4542 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
4543 mBuilder.getBigBaseLayoutResource());
4544 remoteViews.removeAllViews(R.id.notification_main_column);
4545 remoteViews.addView(R.id.notification_main_column, headsUpContentView);
4546 return remoteViews;
4547 }
4548
4549 /**
4550 * @hide
4551 */
4552 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
4553 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
4554 mBuilder.getBaseLayoutResource());
4555 remoteViews.removeAllViews(R.id.notification_main_column);
4556 remoteViews.addView(R.id.notification_main_column, customContent);
4557 return remoteViews;
4558 }
4559
4560 /**
4561 * @hide
4562 */
4563 private RemoteViews makeDecoratedBigContentView() {
4564 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
4565 ? mBuilder.mN.contentView
4566 : mBuilder.mN.bigContentView;
4567 if (mBuilder.mActions.size() == 0) {
4568 return makeStandardTemplateWithCustomContent(bigContentView);
4569 }
4570 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
4571 mBuilder.getBigBaseLayoutResource());
4572 remoteViews.removeAllViews(R.id.notification_main_column);
4573 remoteViews.addView(R.id.notification_main_column, bigContentView);
4574 return remoteViews;
4575 }
4576 }
4577
Christoph Studer4600f9b2014-07-22 22:44:43 +02004578 // When adding a new Style subclass here, don't forget to update
4579 // Builder.getNotificationStyleClass.
4580
Griff Hazen61a9e862014-05-22 16:05:19 -07004581 /**
4582 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
4583 * metadata or change options on a notification builder.
4584 */
4585 public interface Extender {
4586 /**
4587 * Apply this extender to a notification builder.
4588 * @param builder the builder to be modified.
4589 * @return the build object for chaining.
4590 */
4591 public Builder extend(Builder builder);
4592 }
4593
4594 /**
4595 * Helper class to add wearable extensions to notifications.
4596 * <p class="note"> See
4597 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
4598 * for Android Wear</a> for more information on how to use this class.
4599 * <p>
4600 * To create a notification with wearable extensions:
4601 * <ol>
4602 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
4603 * properties.
4604 * <li>Create a {@link android.app.Notification.WearableExtender}.
4605 * <li>Set wearable-specific properties using the
4606 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
4607 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
4608 * notification.
4609 * <li>Post the notification to the notification system with the
4610 * {@code NotificationManager.notify(...)} methods.
4611 * </ol>
4612 *
4613 * <pre class="prettyprint">
4614 * Notification notif = new Notification.Builder(mContext)
4615 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4616 * .setContentText(subject)
4617 * .setSmallIcon(R.drawable.new_mail)
4618 * .extend(new Notification.WearableExtender()
4619 * .setContentIcon(R.drawable.new_mail))
4620 * .build();
4621 * NotificationManager notificationManger =
4622 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
4623 * notificationManger.notify(0, notif);</pre>
4624 *
4625 * <p>Wearable extensions can be accessed on an existing notification by using the
4626 * {@code WearableExtender(Notification)} constructor,
4627 * and then using the {@code get} methods to access values.
4628 *
4629 * <pre class="prettyprint">
4630 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
4631 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07004632 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004633 */
4634 public static final class WearableExtender implements Extender {
4635 /**
4636 * Sentinel value for an action index that is unset.
4637 */
4638 public static final int UNSET_ACTION_INDEX = -1;
4639
4640 /**
4641 * Size value for use with {@link #setCustomSizePreset} to show this notification with
4642 * default sizing.
4643 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07004644 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07004645 * on their content.
4646 */
4647 public static final int SIZE_DEFAULT = 0;
4648
4649 /**
4650 * Size value for use with {@link #setCustomSizePreset} to show this notification
4651 * with an extra small size.
4652 * <p>This value is only applicable for custom display notifications created using
4653 * {@link #setDisplayIntent}.
4654 */
4655 public static final int SIZE_XSMALL = 1;
4656
4657 /**
4658 * Size value for use with {@link #setCustomSizePreset} to show this notification
4659 * with a small size.
4660 * <p>This value is only applicable for custom display notifications created using
4661 * {@link #setDisplayIntent}.
4662 */
4663 public static final int SIZE_SMALL = 2;
4664
4665 /**
4666 * Size value for use with {@link #setCustomSizePreset} to show this notification
4667 * with a medium size.
4668 * <p>This value is only applicable for custom display notifications created using
4669 * {@link #setDisplayIntent}.
4670 */
4671 public static final int SIZE_MEDIUM = 3;
4672
4673 /**
4674 * Size value for use with {@link #setCustomSizePreset} to show this notification
4675 * with a large size.
4676 * <p>This value is only applicable for custom display notifications created using
4677 * {@link #setDisplayIntent}.
4678 */
4679 public static final int SIZE_LARGE = 4;
4680
Griff Hazend5f11f92014-05-27 15:40:09 -07004681 /**
4682 * Size value for use with {@link #setCustomSizePreset} to show this notification
4683 * full screen.
4684 * <p>This value is only applicable for custom display notifications created using
4685 * {@link #setDisplayIntent}.
4686 */
4687 public static final int SIZE_FULL_SCREEN = 5;
4688
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004689 /**
4690 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
4691 * short amount of time when this notification is displayed on the screen. This
4692 * is the default value.
4693 */
4694 public static final int SCREEN_TIMEOUT_SHORT = 0;
4695
4696 /**
4697 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
4698 * for a longer amount of time when this notification is displayed on the screen.
4699 */
4700 public static final int SCREEN_TIMEOUT_LONG = -1;
4701
Griff Hazen61a9e862014-05-22 16:05:19 -07004702 /** Notification extra which contains wearable extensions */
4703 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
4704
Pete Gastaf6781d2014-10-07 15:17:05 -04004705 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07004706 private static final String KEY_ACTIONS = "actions";
4707 private static final String KEY_FLAGS = "flags";
4708 private static final String KEY_DISPLAY_INTENT = "displayIntent";
4709 private static final String KEY_PAGES = "pages";
4710 private static final String KEY_BACKGROUND = "background";
4711 private static final String KEY_CONTENT_ICON = "contentIcon";
4712 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
4713 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
4714 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
4715 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
4716 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004717 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Griff Hazen61a9e862014-05-22 16:05:19 -07004718
4719 // Flags bitwise-ored to mFlags
4720 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
4721 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
4722 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
4723 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004724 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Griff Hazen61a9e862014-05-22 16:05:19 -07004725
4726 // Default value for flags integer
4727 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
4728
4729 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
4730 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
4731
4732 private ArrayList<Action> mActions = new ArrayList<Action>();
4733 private int mFlags = DEFAULT_FLAGS;
4734 private PendingIntent mDisplayIntent;
4735 private ArrayList<Notification> mPages = new ArrayList<Notification>();
4736 private Bitmap mBackground;
4737 private int mContentIcon;
4738 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
4739 private int mContentActionIndex = UNSET_ACTION_INDEX;
4740 private int mCustomSizePreset = SIZE_DEFAULT;
4741 private int mCustomContentHeight;
4742 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004743 private int mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004744
4745 /**
4746 * Create a {@link android.app.Notification.WearableExtender} with default
4747 * options.
4748 */
4749 public WearableExtender() {
4750 }
4751
4752 public WearableExtender(Notification notif) {
4753 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
4754 if (wearableBundle != null) {
4755 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
4756 if (actions != null) {
4757 mActions.addAll(actions);
4758 }
4759
4760 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
4761 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
4762
4763 Notification[] pages = getNotificationArrayFromBundle(
4764 wearableBundle, KEY_PAGES);
4765 if (pages != null) {
4766 Collections.addAll(mPages, pages);
4767 }
4768
4769 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
4770 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
4771 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
4772 DEFAULT_CONTENT_ICON_GRAVITY);
4773 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
4774 UNSET_ACTION_INDEX);
4775 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
4776 SIZE_DEFAULT);
4777 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
4778 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004779 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Griff Hazen61a9e862014-05-22 16:05:19 -07004780 }
4781 }
4782
4783 /**
4784 * Apply wearable extensions to a notification that is being built. This is typically
4785 * called by the {@link android.app.Notification.Builder#extend} method of
4786 * {@link android.app.Notification.Builder}.
4787 */
4788 @Override
4789 public Notification.Builder extend(Notification.Builder builder) {
4790 Bundle wearableBundle = new Bundle();
4791
4792 if (!mActions.isEmpty()) {
4793 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
4794 }
4795 if (mFlags != DEFAULT_FLAGS) {
4796 wearableBundle.putInt(KEY_FLAGS, mFlags);
4797 }
4798 if (mDisplayIntent != null) {
4799 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
4800 }
4801 if (!mPages.isEmpty()) {
4802 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
4803 new Notification[mPages.size()]));
4804 }
4805 if (mBackground != null) {
4806 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
4807 }
4808 if (mContentIcon != 0) {
4809 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
4810 }
4811 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
4812 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
4813 }
4814 if (mContentActionIndex != UNSET_ACTION_INDEX) {
4815 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
4816 mContentActionIndex);
4817 }
4818 if (mCustomSizePreset != SIZE_DEFAULT) {
4819 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
4820 }
4821 if (mCustomContentHeight != 0) {
4822 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
4823 }
4824 if (mGravity != DEFAULT_GRAVITY) {
4825 wearableBundle.putInt(KEY_GRAVITY, mGravity);
4826 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004827 if (mHintScreenTimeout != 0) {
4828 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
4829 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004830
4831 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
4832 return builder;
4833 }
4834
4835 @Override
4836 public WearableExtender clone() {
4837 WearableExtender that = new WearableExtender();
4838 that.mActions = new ArrayList<Action>(this.mActions);
4839 that.mFlags = this.mFlags;
4840 that.mDisplayIntent = this.mDisplayIntent;
4841 that.mPages = new ArrayList<Notification>(this.mPages);
4842 that.mBackground = this.mBackground;
4843 that.mContentIcon = this.mContentIcon;
4844 that.mContentIconGravity = this.mContentIconGravity;
4845 that.mContentActionIndex = this.mContentActionIndex;
4846 that.mCustomSizePreset = this.mCustomSizePreset;
4847 that.mCustomContentHeight = this.mCustomContentHeight;
4848 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004849 that.mHintScreenTimeout = this.mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004850 return that;
4851 }
4852
4853 /**
4854 * Add a wearable action to this notification.
4855 *
4856 * <p>When wearable actions are added using this method, the set of actions that
4857 * show on a wearable device splits from devices that only show actions added
4858 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4859 * of which actions display on different devices.
4860 *
4861 * @param action the action to add to this notification
4862 * @return this object for method chaining
4863 * @see android.app.Notification.Action
4864 */
4865 public WearableExtender addAction(Action action) {
4866 mActions.add(action);
4867 return this;
4868 }
4869
4870 /**
4871 * Adds wearable actions to this notification.
4872 *
4873 * <p>When wearable actions are added using this method, the set of actions that
4874 * show on a wearable device splits from devices that only show actions added
4875 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4876 * of which actions display on different devices.
4877 *
4878 * @param actions the actions to add to this notification
4879 * @return this object for method chaining
4880 * @see android.app.Notification.Action
4881 */
4882 public WearableExtender addActions(List<Action> actions) {
4883 mActions.addAll(actions);
4884 return this;
4885 }
4886
4887 /**
4888 * Clear all wearable actions present on this builder.
4889 * @return this object for method chaining.
4890 * @see #addAction
4891 */
4892 public WearableExtender clearActions() {
4893 mActions.clear();
4894 return this;
4895 }
4896
4897 /**
4898 * Get the wearable actions present on this notification.
4899 */
4900 public List<Action> getActions() {
4901 return mActions;
4902 }
4903
4904 /**
4905 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07004906 * this notification. The {@link PendingIntent} provided should be for an activity.
4907 *
4908 * <pre class="prettyprint">
4909 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
4910 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
4911 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
4912 * Notification notif = new Notification.Builder(context)
4913 * .extend(new Notification.WearableExtender()
4914 * .setDisplayIntent(displayPendingIntent)
4915 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
4916 * .build();</pre>
4917 *
4918 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07004919 * should have an empty task affinity. It is also recommended to use the device
4920 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07004921 *
4922 * <p>Example AndroidManifest.xml entry:
4923 * <pre class="prettyprint">
4924 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
4925 * android:exported=&quot;true&quot;
4926 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07004927 * android:taskAffinity=&quot;&quot;
4928 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004929 *
4930 * @param intent the {@link PendingIntent} for an activity
4931 * @return this object for method chaining
4932 * @see android.app.Notification.WearableExtender#getDisplayIntent
4933 */
4934 public WearableExtender setDisplayIntent(PendingIntent intent) {
4935 mDisplayIntent = intent;
4936 return this;
4937 }
4938
4939 /**
4940 * Get the intent to launch inside of an activity view when displaying this
4941 * notification. This {@code PendingIntent} should be for an activity.
4942 */
4943 public PendingIntent getDisplayIntent() {
4944 return mDisplayIntent;
4945 }
4946
4947 /**
4948 * Add an additional page of content to display with this notification. The current
4949 * notification forms the first page, and pages added using this function form
4950 * subsequent pages. This field can be used to separate a notification into multiple
4951 * sections.
4952 *
4953 * @param page the notification to add as another page
4954 * @return this object for method chaining
4955 * @see android.app.Notification.WearableExtender#getPages
4956 */
4957 public WearableExtender addPage(Notification page) {
4958 mPages.add(page);
4959 return this;
4960 }
4961
4962 /**
4963 * Add additional pages of content to display with this notification. The current
4964 * notification forms the first page, and pages added using this function form
4965 * subsequent pages. This field can be used to separate a notification into multiple
4966 * sections.
4967 *
4968 * @param pages a list of notifications
4969 * @return this object for method chaining
4970 * @see android.app.Notification.WearableExtender#getPages
4971 */
4972 public WearableExtender addPages(List<Notification> pages) {
4973 mPages.addAll(pages);
4974 return this;
4975 }
4976
4977 /**
4978 * Clear all additional pages present on this builder.
4979 * @return this object for method chaining.
4980 * @see #addPage
4981 */
4982 public WearableExtender clearPages() {
4983 mPages.clear();
4984 return this;
4985 }
4986
4987 /**
4988 * Get the array of additional pages of content for displaying this notification. The
4989 * current notification forms the first page, and elements within this array form
4990 * subsequent pages. This field can be used to separate a notification into multiple
4991 * sections.
4992 * @return the pages for this notification
4993 */
4994 public List<Notification> getPages() {
4995 return mPages;
4996 }
4997
4998 /**
4999 * Set a background image to be displayed behind the notification content.
5000 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5001 * will work with any notification style.
5002 *
5003 * @param background the background bitmap
5004 * @return this object for method chaining
5005 * @see android.app.Notification.WearableExtender#getBackground
5006 */
5007 public WearableExtender setBackground(Bitmap background) {
5008 mBackground = background;
5009 return this;
5010 }
5011
5012 /**
5013 * Get a background image to be displayed behind the notification content.
5014 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5015 * will work with any notification style.
5016 *
5017 * @return the background image
5018 * @see android.app.Notification.WearableExtender#setBackground
5019 */
5020 public Bitmap getBackground() {
5021 return mBackground;
5022 }
5023
5024 /**
5025 * Set an icon that goes with the content of this notification.
5026 */
5027 public WearableExtender setContentIcon(int icon) {
5028 mContentIcon = icon;
5029 return this;
5030 }
5031
5032 /**
5033 * Get an icon that goes with the content of this notification.
5034 */
5035 public int getContentIcon() {
5036 return mContentIcon;
5037 }
5038
5039 /**
5040 * Set the gravity that the content icon should have within the notification display.
5041 * Supported values include {@link android.view.Gravity#START} and
5042 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5043 * @see #setContentIcon
5044 */
5045 public WearableExtender setContentIconGravity(int contentIconGravity) {
5046 mContentIconGravity = contentIconGravity;
5047 return this;
5048 }
5049
5050 /**
5051 * Get the gravity that the content icon should have within the notification display.
5052 * Supported values include {@link android.view.Gravity#START} and
5053 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5054 * @see #getContentIcon
5055 */
5056 public int getContentIconGravity() {
5057 return mContentIconGravity;
5058 }
5059
5060 /**
5061 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07005062 * this notification. This action will no longer display separately from the
5063 * notification's content.
5064 *
Griff Hazenca48d352014-05-28 22:37:13 -07005065 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005066 * set, although the list of available actions comes from the main notification and not
5067 * from the child page's notification.
5068 *
5069 * @param actionIndex The index of the action to hoist onto the current notification page.
5070 * If wearable actions were added to the main notification, this index
5071 * will apply to that list, otherwise it will apply to the regular
5072 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07005073 */
5074 public WearableExtender setContentAction(int actionIndex) {
5075 mContentActionIndex = actionIndex;
5076 return this;
5077 }
5078
5079 /**
Griff Hazenca48d352014-05-28 22:37:13 -07005080 * Get the index of the notification action, if any, that was specified as being clickable
5081 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07005082 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07005083 *
Griff Hazenca48d352014-05-28 22:37:13 -07005084 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005085 * set, although the list of available actions comes from the main notification and not
5086 * from the child page's notification.
5087 *
5088 * <p>If wearable specific actions were added to the main notification, this index will
5089 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07005090 *
5091 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07005092 */
5093 public int getContentAction() {
5094 return mContentActionIndex;
5095 }
5096
5097 /**
5098 * Set the gravity that this notification should have within the available viewport space.
5099 * Supported values include {@link android.view.Gravity#TOP},
5100 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5101 * The default value is {@link android.view.Gravity#BOTTOM}.
5102 */
5103 public WearableExtender setGravity(int gravity) {
5104 mGravity = gravity;
5105 return this;
5106 }
5107
5108 /**
5109 * Get the gravity that this notification should have within the available viewport space.
5110 * Supported values include {@link android.view.Gravity#TOP},
5111 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5112 * The default value is {@link android.view.Gravity#BOTTOM}.
5113 */
5114 public int getGravity() {
5115 return mGravity;
5116 }
5117
5118 /**
5119 * Set the custom size preset for the display of this notification out of the available
5120 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5121 * {@link #SIZE_LARGE}.
5122 * <p>Some custom size presets are only applicable for custom display notifications created
5123 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
5124 * documentation for the preset in question. See also
5125 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
5126 */
5127 public WearableExtender setCustomSizePreset(int sizePreset) {
5128 mCustomSizePreset = sizePreset;
5129 return this;
5130 }
5131
5132 /**
5133 * Get the custom size preset for the display of this notification out of the available
5134 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5135 * {@link #SIZE_LARGE}.
5136 * <p>Some custom size presets are only applicable for custom display notifications created
5137 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
5138 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
5139 */
5140 public int getCustomSizePreset() {
5141 return mCustomSizePreset;
5142 }
5143
5144 /**
5145 * Set the custom height in pixels for the display of this notification's content.
5146 * <p>This option is only available for custom display notifications created
5147 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
5148 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
5149 * {@link #getCustomContentHeight}.
5150 */
5151 public WearableExtender setCustomContentHeight(int height) {
5152 mCustomContentHeight = height;
5153 return this;
5154 }
5155
5156 /**
5157 * Get the custom height in pixels for the display of this notification's content.
5158 * <p>This option is only available for custom display notifications created
5159 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
5160 * {@link #setCustomContentHeight}.
5161 */
5162 public int getCustomContentHeight() {
5163 return mCustomContentHeight;
5164 }
5165
5166 /**
5167 * Set whether the scrolling position for the contents of this notification should start
5168 * at the bottom of the contents instead of the top when the contents are too long to
5169 * display within the screen. Default is false (start scroll at the top).
5170 */
5171 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
5172 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
5173 return this;
5174 }
5175
5176 /**
5177 * Get whether the scrolling position for the contents of this notification should start
5178 * at the bottom of the contents instead of the top when the contents are too long to
5179 * display within the screen. Default is false (start scroll at the top).
5180 */
5181 public boolean getStartScrollBottom() {
5182 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
5183 }
5184
5185 /**
5186 * Set whether the content intent is available when the wearable device is not connected
5187 * to a companion device. The user can still trigger this intent when the wearable device
5188 * is offline, but a visual hint will indicate that the content intent may not be available.
5189 * Defaults to true.
5190 */
5191 public WearableExtender setContentIntentAvailableOffline(
5192 boolean contentIntentAvailableOffline) {
5193 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
5194 return this;
5195 }
5196
5197 /**
5198 * Get whether the content intent is available when the wearable device is not connected
5199 * to a companion device. The user can still trigger this intent when the wearable device
5200 * is offline, but a visual hint will indicate that the content intent may not be available.
5201 * Defaults to true.
5202 */
5203 public boolean getContentIntentAvailableOffline() {
5204 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
5205 }
5206
5207 /**
5208 * Set a hint that this notification's icon should not be displayed.
5209 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
5210 * @return this object for method chaining
5211 */
5212 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
5213 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
5214 return this;
5215 }
5216
5217 /**
5218 * Get a hint that this notification's icon should not be displayed.
5219 * @return {@code true} if this icon should not be displayed, false otherwise.
5220 * The default value is {@code false} if this was never set.
5221 */
5222 public boolean getHintHideIcon() {
5223 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
5224 }
5225
5226 /**
5227 * Set a visual hint that only the background image of this notification should be
5228 * displayed, and other semantic content should be hidden. This hint is only applicable
5229 * to sub-pages added using {@link #addPage}.
5230 */
5231 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
5232 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
5233 return this;
5234 }
5235
5236 /**
5237 * Get a visual hint that only the background image of this notification should be
5238 * displayed, and other semantic content should be hidden. This hint is only applicable
5239 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
5240 */
5241 public boolean getHintShowBackgroundOnly() {
5242 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
5243 }
5244
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005245 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005246 * Set a hint that this notification's background should not be clipped if possible,
5247 * and should instead be resized to fully display on the screen, retaining the aspect
5248 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005249 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
5250 * @return this object for method chaining
5251 */
5252 public WearableExtender setHintAvoidBackgroundClipping(
5253 boolean hintAvoidBackgroundClipping) {
5254 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
5255 return this;
5256 }
5257
5258 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005259 * Get a hint that this notification's background should not be clipped if possible,
5260 * and should instead be resized to fully display on the screen, retaining the aspect
5261 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005262 * @return {@code true} if it's ok if the background is clipped on the screen, false
5263 * otherwise. The default value is {@code false} if this was never set.
5264 */
5265 public boolean getHintAvoidBackgroundClipping() {
5266 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
5267 }
5268
5269 /**
5270 * Set a hint that the screen should remain on for at least this duration when
5271 * this notification is displayed on the screen.
5272 * @param timeout The requested screen timeout in milliseconds. Can also be either
5273 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5274 * @return this object for method chaining
5275 */
5276 public WearableExtender setHintScreenTimeout(int timeout) {
5277 mHintScreenTimeout = timeout;
5278 return this;
5279 }
5280
5281 /**
5282 * Get the duration, in milliseconds, that the screen should remain on for
5283 * when this notification is displayed.
5284 * @return the duration in milliseconds if > 0, or either one of the sentinel values
5285 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5286 */
5287 public int getHintScreenTimeout() {
5288 return mHintScreenTimeout;
5289 }
5290
Griff Hazen61a9e862014-05-22 16:05:19 -07005291 private void setFlag(int mask, boolean value) {
5292 if (value) {
5293 mFlags |= mask;
5294 } else {
5295 mFlags &= ~mask;
5296 }
5297 }
5298 }
5299
5300 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005301 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
5302 * with car extensions:
5303 *
5304 * <ol>
5305 * <li>Create an {@link Notification.Builder}, setting any desired
5306 * properties.
5307 * <li>Create a {@link CarExtender}.
5308 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
5309 * {@link CarExtender}.
5310 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
5311 * to apply the extensions to a notification.
5312 * </ol>
5313 *
5314 * <pre class="prettyprint">
5315 * Notification notification = new Notification.Builder(context)
5316 * ...
5317 * .extend(new CarExtender()
5318 * .set*(...))
5319 * .build();
5320 * </pre>
5321 *
5322 * <p>Car extensions can be accessed on an existing notification by using the
5323 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
5324 * to access values.
5325 */
5326 public static final class CarExtender implements Extender {
5327 private static final String TAG = "CarExtender";
5328
5329 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
5330 private static final String EXTRA_LARGE_ICON = "large_icon";
5331 private static final String EXTRA_CONVERSATION = "car_conversation";
5332 private static final String EXTRA_COLOR = "app_color";
5333
5334 private Bitmap mLargeIcon;
5335 private UnreadConversation mUnreadConversation;
5336 private int mColor = Notification.COLOR_DEFAULT;
5337
5338 /**
5339 * Create a {@link CarExtender} with default options.
5340 */
5341 public CarExtender() {
5342 }
5343
5344 /**
5345 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
5346 *
5347 * @param notif The notification from which to copy options.
5348 */
5349 public CarExtender(Notification notif) {
5350 Bundle carBundle = notif.extras == null ?
5351 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
5352 if (carBundle != null) {
5353 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
5354 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
5355
5356 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
5357 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
5358 }
5359 }
5360
5361 /**
5362 * Apply car extensions to a notification that is being built. This is typically called by
5363 * the {@link Notification.Builder#extend(Notification.Extender)}
5364 * method of {@link Notification.Builder}.
5365 */
5366 @Override
5367 public Notification.Builder extend(Notification.Builder builder) {
5368 Bundle carExtensions = new Bundle();
5369
5370 if (mLargeIcon != null) {
5371 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
5372 }
5373 if (mColor != Notification.COLOR_DEFAULT) {
5374 carExtensions.putInt(EXTRA_COLOR, mColor);
5375 }
5376
5377 if (mUnreadConversation != null) {
5378 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
5379 carExtensions.putBundle(EXTRA_CONVERSATION, b);
5380 }
5381
5382 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
5383 return builder;
5384 }
5385
5386 /**
5387 * Sets the accent color to use when Android Auto presents the notification.
5388 *
5389 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
5390 * to accent the displayed notification. However, not all colors are acceptable in an
5391 * automotive setting. This method can be used to override the color provided in the
5392 * notification in such a situation.
5393 */
Tor Norbye80756e32015-03-02 09:39:27 -08005394 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005395 mColor = color;
5396 return this;
5397 }
5398
5399 /**
5400 * Gets the accent color.
5401 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005402 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005403 */
Tor Norbye80756e32015-03-02 09:39:27 -08005404 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005405 public int getColor() {
5406 return mColor;
5407 }
5408
5409 /**
5410 * Sets the large icon of the car notification.
5411 *
5412 * If no large icon is set in the extender, Android Auto will display the icon
5413 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
5414 *
5415 * @param largeIcon The large icon to use in the car notification.
5416 * @return This object for method chaining.
5417 */
5418 public CarExtender setLargeIcon(Bitmap largeIcon) {
5419 mLargeIcon = largeIcon;
5420 return this;
5421 }
5422
5423 /**
5424 * Gets the large icon used in this car notification, or null if no icon has been set.
5425 *
5426 * @return The large icon for the car notification.
5427 * @see CarExtender#setLargeIcon
5428 */
5429 public Bitmap getLargeIcon() {
5430 return mLargeIcon;
5431 }
5432
5433 /**
5434 * Sets the unread conversation in a message notification.
5435 *
5436 * @param unreadConversation The unread part of the conversation this notification conveys.
5437 * @return This object for method chaining.
5438 */
5439 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
5440 mUnreadConversation = unreadConversation;
5441 return this;
5442 }
5443
5444 /**
5445 * Returns the unread conversation conveyed by this notification.
5446 * @see #setUnreadConversation(UnreadConversation)
5447 */
5448 public UnreadConversation getUnreadConversation() {
5449 return mUnreadConversation;
5450 }
5451
5452 /**
5453 * A class which holds the unread messages from a conversation.
5454 */
5455 public static class UnreadConversation {
5456 private static final String KEY_AUTHOR = "author";
5457 private static final String KEY_TEXT = "text";
5458 private static final String KEY_MESSAGES = "messages";
5459 private static final String KEY_REMOTE_INPUT = "remote_input";
5460 private static final String KEY_ON_REPLY = "on_reply";
5461 private static final String KEY_ON_READ = "on_read";
5462 private static final String KEY_PARTICIPANTS = "participants";
5463 private static final String KEY_TIMESTAMP = "timestamp";
5464
5465 private final String[] mMessages;
5466 private final RemoteInput mRemoteInput;
5467 private final PendingIntent mReplyPendingIntent;
5468 private final PendingIntent mReadPendingIntent;
5469 private final String[] mParticipants;
5470 private final long mLatestTimestamp;
5471
5472 UnreadConversation(String[] messages, RemoteInput remoteInput,
5473 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
5474 String[] participants, long latestTimestamp) {
5475 mMessages = messages;
5476 mRemoteInput = remoteInput;
5477 mReadPendingIntent = readPendingIntent;
5478 mReplyPendingIntent = replyPendingIntent;
5479 mParticipants = participants;
5480 mLatestTimestamp = latestTimestamp;
5481 }
5482
5483 /**
5484 * Gets the list of messages conveyed by this notification.
5485 */
5486 public String[] getMessages() {
5487 return mMessages;
5488 }
5489
5490 /**
5491 * Gets the remote input that will be used to convey the response to a message list, or
5492 * null if no such remote input exists.
5493 */
5494 public RemoteInput getRemoteInput() {
5495 return mRemoteInput;
5496 }
5497
5498 /**
5499 * Gets the pending intent that will be triggered when the user replies to this
5500 * notification.
5501 */
5502 public PendingIntent getReplyPendingIntent() {
5503 return mReplyPendingIntent;
5504 }
5505
5506 /**
5507 * Gets the pending intent that Android Auto will send after it reads aloud all messages
5508 * in this object's message list.
5509 */
5510 public PendingIntent getReadPendingIntent() {
5511 return mReadPendingIntent;
5512 }
5513
5514 /**
5515 * Gets the participants in the conversation.
5516 */
5517 public String[] getParticipants() {
5518 return mParticipants;
5519 }
5520
5521 /**
5522 * Gets the firs participant in the conversation.
5523 */
5524 public String getParticipant() {
5525 return mParticipants.length > 0 ? mParticipants[0] : null;
5526 }
5527
5528 /**
5529 * Gets the timestamp of the conversation.
5530 */
5531 public long getLatestTimestamp() {
5532 return mLatestTimestamp;
5533 }
5534
5535 Bundle getBundleForUnreadConversation() {
5536 Bundle b = new Bundle();
5537 String author = null;
5538 if (mParticipants != null && mParticipants.length > 1) {
5539 author = mParticipants[0];
5540 }
5541 Parcelable[] messages = new Parcelable[mMessages.length];
5542 for (int i = 0; i < messages.length; i++) {
5543 Bundle m = new Bundle();
5544 m.putString(KEY_TEXT, mMessages[i]);
5545 m.putString(KEY_AUTHOR, author);
5546 messages[i] = m;
5547 }
5548 b.putParcelableArray(KEY_MESSAGES, messages);
5549 if (mRemoteInput != null) {
5550 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
5551 }
5552 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
5553 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
5554 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
5555 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
5556 return b;
5557 }
5558
5559 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
5560 if (b == null) {
5561 return null;
5562 }
5563 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
5564 String[] messages = null;
5565 if (parcelableMessages != null) {
5566 String[] tmp = new String[parcelableMessages.length];
5567 boolean success = true;
5568 for (int i = 0; i < tmp.length; i++) {
5569 if (!(parcelableMessages[i] instanceof Bundle)) {
5570 success = false;
5571 break;
5572 }
5573 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
5574 if (tmp[i] == null) {
5575 success = false;
5576 break;
5577 }
5578 }
5579 if (success) {
5580 messages = tmp;
5581 } else {
5582 return null;
5583 }
5584 }
5585
5586 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
5587 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
5588
5589 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
5590
5591 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
5592 if (participants == null || participants.length != 1) {
5593 return null;
5594 }
5595
5596 return new UnreadConversation(messages,
5597 remoteInput,
5598 onReply,
5599 onRead,
5600 participants, b.getLong(KEY_TIMESTAMP));
5601 }
5602 };
5603
5604 /**
5605 * Builder class for {@link CarExtender.UnreadConversation} objects.
5606 */
5607 public static class Builder {
5608 private final List<String> mMessages = new ArrayList<String>();
5609 private final String mParticipant;
5610 private RemoteInput mRemoteInput;
5611 private PendingIntent mReadPendingIntent;
5612 private PendingIntent mReplyPendingIntent;
5613 private long mLatestTimestamp;
5614
5615 /**
5616 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
5617 *
5618 * @param name The name of the other participant in the conversation.
5619 */
5620 public Builder(String name) {
5621 mParticipant = name;
5622 }
5623
5624 /**
5625 * Appends a new unread message to the list of messages for this conversation.
5626 *
5627 * The messages should be added from oldest to newest.
5628 *
5629 * @param message The text of the new unread message.
5630 * @return This object for method chaining.
5631 */
5632 public Builder addMessage(String message) {
5633 mMessages.add(message);
5634 return this;
5635 }
5636
5637 /**
5638 * Sets the pending intent and remote input which will convey the reply to this
5639 * notification.
5640 *
5641 * @param pendingIntent The pending intent which will be triggered on a reply.
5642 * @param remoteInput The remote input parcelable which will carry the reply.
5643 * @return This object for method chaining.
5644 *
5645 * @see CarExtender.UnreadConversation#getRemoteInput
5646 * @see CarExtender.UnreadConversation#getReplyPendingIntent
5647 */
5648 public Builder setReplyAction(
5649 PendingIntent pendingIntent, RemoteInput remoteInput) {
5650 mRemoteInput = remoteInput;
5651 mReplyPendingIntent = pendingIntent;
5652
5653 return this;
5654 }
5655
5656 /**
5657 * Sets the pending intent that will be sent once the messages in this notification
5658 * are read.
5659 *
5660 * @param pendingIntent The pending intent to use.
5661 * @return This object for method chaining.
5662 */
5663 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
5664 mReadPendingIntent = pendingIntent;
5665 return this;
5666 }
5667
5668 /**
5669 * Sets the timestamp of the most recent message in an unread conversation.
5670 *
5671 * If a messaging notification has been posted by your application and has not
5672 * yet been cancelled, posting a later notification with the same id and tag
5673 * but without a newer timestamp may result in Android Auto not displaying a
5674 * heads up notification for the later notification.
5675 *
5676 * @param timestamp The timestamp of the most recent message in the conversation.
5677 * @return This object for method chaining.
5678 */
5679 public Builder setLatestTimestamp(long timestamp) {
5680 mLatestTimestamp = timestamp;
5681 return this;
5682 }
5683
5684 /**
5685 * Builds a new unread conversation object.
5686 *
5687 * @return The new unread conversation object.
5688 */
5689 public UnreadConversation build() {
5690 String[] messages = mMessages.toArray(new String[mMessages.size()]);
5691 String[] participants = { mParticipant };
5692 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
5693 mReadPendingIntent, participants, mLatestTimestamp);
5694 }
5695 }
5696 }
5697
5698 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07005699 * Get an array of Notification objects from a parcelable array bundle field.
5700 * Update the bundle to have a typed array so fetches in the future don't need
5701 * to do an array copy.
5702 */
5703 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
5704 Parcelable[] array = bundle.getParcelableArray(key);
5705 if (array instanceof Notification[] || array == null) {
5706 return (Notification[]) array;
5707 }
5708 Notification[] typedArray = Arrays.copyOf(array, array.length,
5709 Notification[].class);
5710 bundle.putParcelableArray(key, typedArray);
5711 return typedArray;
5712 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005713
5714 private static class BuilderRemoteViews extends RemoteViews {
5715 public BuilderRemoteViews(Parcel parcel) {
5716 super(parcel);
5717 }
5718
Kenny Guy77320062014-08-27 21:37:15 +01005719 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
5720 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005721 }
5722
5723 @Override
5724 public BuilderRemoteViews clone() {
5725 Parcel p = Parcel.obtain();
5726 writeToParcel(p, 0);
5727 p.setDataPosition(0);
5728 BuilderRemoteViews brv = new BuilderRemoteViews(p);
5729 p.recycle();
5730 return brv;
5731 }
5732 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005733}