blob: d98c7179888f56ddc5d8876dbdc865799ba8a686 [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 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500266 * As of N, this field may be null. The notification view is determined by the inputs
267 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400268 * 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 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500278 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400279 * 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 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500292 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400293 * 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 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800826 * {@link #extras} key: whether the chronometer set on the notification should count down
827 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
828 */
829 public static final String EXTRA_CHRONOMETER_COUNTS_DOWN = "android.chronometerCountsDown";
830
831 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400832 * {@link #extras} key: whether {@link #when} should be shown,
833 * as supplied to {@link Builder#setShowWhen(boolean)}.
834 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400835 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400836
837 /**
838 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
839 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
840 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400841 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400842
843 /**
844 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
845 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
846 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400847 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400848
849 /**
850 * {@link #extras} key: A string representing the name of the specific
851 * {@link android.app.Notification.Style} used to create this notification.
852 */
Chris Wren91ad5632013-06-05 15:05:57 -0400853 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400854
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400855 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400856 * {@link #extras} key: A String array containing the people that this notification relates to,
857 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400858 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400859 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500860
861 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400862 * Allow certain system-generated notifications to appear before the device is provisioned.
863 * Only available to notifications coming from the android package.
864 * @hide
865 */
866 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
867
868 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700869 * {@link #extras} key: A
870 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
871 * in the background when the notification is selected. The URI must point to an image stream
872 * suitable for passing into
873 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
874 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
875 * URI used for this purpose must require no permissions to read the image data.
876 */
877 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
878
879 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400880 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700881 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400882 * {@link android.app.Notification.MediaStyle} notification.
883 */
884 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
885
886 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100887 * {@link #extras} key: the indices of actions to be shown in the compact view,
888 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
889 */
890 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
891
Christoph Studer943aa672014-08-03 20:31:16 +0200892 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100893 * {@link #extras} key: the user that built the notification.
894 *
895 * @hide
896 */
897 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
898
899 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400900 * @hide
901 */
902 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
903
Selim Cinek247fa012016-02-18 09:50:48 -0800904 /**
905 * @hide
906 */
907 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
908
Dan Sandlerd63f9322015-05-06 15:18:49 -0400909 private Icon mSmallIcon;
910 private Icon mLargeIcon;
911
Chris Wren51c75102013-07-16 20:49:17 -0400912 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400913 * Structure to encapsulate a named action that can be shown as part of this notification.
914 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
915 * selected by the user.
916 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700917 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
918 * or {@link Notification.Builder#addAction(Notification.Action)}
919 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400920 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500921 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700922 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -0400923 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -0700924 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700925
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400926 /**
927 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -0400928 *
929 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400930 */
Dan Sandler86647982015-05-13 23:41:13 -0400931 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400932 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700933
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400934 /**
935 * Title of the action.
936 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400937 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700938
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400939 /**
940 * Intent to send when the user invokes this action. May be null, in which case the action
941 * may be rendered in a disabled presentation by the system UI.
942 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400943 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -0700944
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400945 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -0400946 if (in.readInt() != 0) {
947 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -0400948 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
949 icon = mIcon.getResId();
950 }
Dan Sandler86647982015-05-13 23:41:13 -0400951 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400952 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
953 if (in.readInt() == 1) {
954 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
955 }
Griff Hazen959591e2014-05-15 22:26:18 -0700956 mExtras = in.readBundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700957 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400958 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700959
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400960 /**
Dan Sandler86647982015-05-13 23:41:13 -0400961 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400962 */
Dan Sandler86647982015-05-13 23:41:13 -0400963 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400964 public Action(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -0400965 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700966 }
967
Dan Sandler86647982015-05-13 23:41:13 -0400968 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700969 RemoteInput[] remoteInputs) {
Dan Sandler86647982015-05-13 23:41:13 -0400970 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -0400971 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
972 this.icon = icon.getResId();
973 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400974 this.title = title;
975 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -0700976 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700977 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700978 }
979
980 /**
Dan Sandler86647982015-05-13 23:41:13 -0400981 * Return an icon representing the action.
982 */
983 public Icon getIcon() {
984 if (mIcon == null && icon != 0) {
985 // you snuck an icon in here without using the builder; let's try to keep it
986 mIcon = Icon.createWithResource("", icon);
987 }
988 return mIcon;
989 }
990
991 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700992 * Get additional metadata carried around with this Action.
993 */
994 public Bundle getExtras() {
995 return mExtras;
996 }
997
998 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700999 * Get the list of inputs to be collected from the user when this action is sent.
1000 * May return null if no remote inputs were added.
1001 */
1002 public RemoteInput[] getRemoteInputs() {
1003 return mRemoteInputs;
1004 }
1005
1006 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001007 * Builder class for {@link Action} objects.
1008 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001009 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001010 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001011 private final CharSequence mTitle;
1012 private final PendingIntent mIntent;
1013 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001014 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001015
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 */
Dan Sandler86647982015-05-13 23:41:13 -04001022 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001023 public Builder(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -04001024 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
1025 }
1026
1027 /**
1028 * Construct a new builder for {@link Action} object.
1029 * @param icon icon to show for this action
1030 * @param title the title of the action
1031 * @param intent the {@link PendingIntent} to fire when users trigger this action
1032 */
1033 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001034 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -07001035 }
1036
1037 /**
1038 * Construct a new builder for {@link Action} object using the fields from an
1039 * {@link Action}.
1040 * @param action the action to read fields from.
1041 */
1042 public Builder(Action action) {
Dan Sandler86647982015-05-13 23:41:13 -04001043 this(action.getIcon(), action.title, action.actionIntent, new Bundle(action.mExtras),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001044 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -07001045 }
1046
Dan Sandler86647982015-05-13 23:41:13 -04001047 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001048 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -07001049 mIcon = icon;
1050 mTitle = title;
1051 mIntent = intent;
1052 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001053 if (remoteInputs != null) {
1054 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1055 Collections.addAll(mRemoteInputs, remoteInputs);
1056 }
Griff Hazen959591e2014-05-15 22:26:18 -07001057 }
1058
1059 /**
1060 * Merge additional metadata into this builder.
1061 *
1062 * <p>Values within the Bundle will replace existing extras values in this Builder.
1063 *
1064 * @see Notification.Action#extras
1065 */
1066 public Builder addExtras(Bundle extras) {
1067 if (extras != null) {
1068 mExtras.putAll(extras);
1069 }
1070 return this;
1071 }
1072
1073 /**
1074 * Get the metadata Bundle used by this Builder.
1075 *
1076 * <p>The returned Bundle is shared with this Builder.
1077 */
1078 public Bundle getExtras() {
1079 return mExtras;
1080 }
1081
1082 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001083 * Add an input to be collected from the user when this action is sent.
1084 * Response values can be retrieved from the fired intent by using the
1085 * {@link RemoteInput#getResultsFromIntent} function.
1086 * @param remoteInput a {@link RemoteInput} to add to the action
1087 * @return this object for method chaining
1088 */
1089 public Builder addRemoteInput(RemoteInput remoteInput) {
1090 if (mRemoteInputs == null) {
1091 mRemoteInputs = new ArrayList<RemoteInput>();
1092 }
1093 mRemoteInputs.add(remoteInput);
1094 return this;
1095 }
1096
1097 /**
1098 * Apply an extender to this action builder. Extenders may be used to add
1099 * metadata or change options on this builder.
1100 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001101 public Builder extend(Extender extender) {
1102 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001103 return this;
1104 }
1105
1106 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001107 * Combine all of the options that have been set and return a new {@link Action}
1108 * object.
1109 * @return the built action
1110 */
1111 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001112 RemoteInput[] remoteInputs = mRemoteInputs != null
1113 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
1114 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -07001115 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001116 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001117
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001118 @Override
1119 public Action clone() {
1120 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001121 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001122 title,
1123 actionIntent, // safe to alias
1124 new Bundle(mExtras),
1125 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001126 }
1127 @Override
1128 public int describeContents() {
1129 return 0;
1130 }
1131 @Override
1132 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001133 final Icon ic = getIcon();
1134 if (ic != null) {
1135 out.writeInt(1);
1136 ic.writeToParcel(out, 0);
1137 } else {
1138 out.writeInt(0);
1139 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001140 TextUtils.writeToParcel(title, out, flags);
1141 if (actionIntent != null) {
1142 out.writeInt(1);
1143 actionIntent.writeToParcel(out, flags);
1144 } else {
1145 out.writeInt(0);
1146 }
Griff Hazen959591e2014-05-15 22:26:18 -07001147 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001148 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001149 }
Griff Hazen959591e2014-05-15 22:26:18 -07001150 public static final Parcelable.Creator<Action> CREATOR =
1151 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001152 public Action createFromParcel(Parcel in) {
1153 return new Action(in);
1154 }
1155 public Action[] newArray(int size) {
1156 return new Action[size];
1157 }
1158 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001159
1160 /**
1161 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1162 * metadata or change options on an action builder.
1163 */
1164 public interface Extender {
1165 /**
1166 * Apply this extender to a notification action builder.
1167 * @param builder the builder to be modified.
1168 * @return the build object for chaining.
1169 */
1170 public Builder extend(Builder builder);
1171 }
1172
1173 /**
1174 * Wearable extender for notification actions. To add extensions to an action,
1175 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1176 * the {@code WearableExtender()} constructor and apply it to a
1177 * {@link android.app.Notification.Action.Builder} using
1178 * {@link android.app.Notification.Action.Builder#extend}.
1179 *
1180 * <pre class="prettyprint">
1181 * Notification.Action action = new Notification.Action.Builder(
1182 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001183 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001184 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001185 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001186 */
1187 public static final class WearableExtender implements Extender {
1188 /** Notification action extra which contains wearable extensions */
1189 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1190
Pete Gastaf6781d2014-10-07 15:17:05 -04001191 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001192 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001193 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1194 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1195 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001196
1197 // Flags bitwise-ored to mFlags
1198 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
1199
1200 // Default value for flags integer
1201 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1202
1203 private int mFlags = DEFAULT_FLAGS;
1204
Pete Gastaf6781d2014-10-07 15:17:05 -04001205 private CharSequence mInProgressLabel;
1206 private CharSequence mConfirmLabel;
1207 private CharSequence mCancelLabel;
1208
Griff Hazen61a9e862014-05-22 16:05:19 -07001209 /**
1210 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1211 * options.
1212 */
1213 public WearableExtender() {
1214 }
1215
1216 /**
1217 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1218 * wearable options present in an existing notification action.
1219 * @param action the notification action to inspect.
1220 */
1221 public WearableExtender(Action action) {
1222 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1223 if (wearableBundle != null) {
1224 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001225 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1226 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1227 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001228 }
1229 }
1230
1231 /**
1232 * Apply wearable extensions to a notification action that is being built. This is
1233 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1234 * method of {@link android.app.Notification.Action.Builder}.
1235 */
1236 @Override
1237 public Action.Builder extend(Action.Builder builder) {
1238 Bundle wearableBundle = new Bundle();
1239
1240 if (mFlags != DEFAULT_FLAGS) {
1241 wearableBundle.putInt(KEY_FLAGS, mFlags);
1242 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001243 if (mInProgressLabel != null) {
1244 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1245 }
1246 if (mConfirmLabel != null) {
1247 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1248 }
1249 if (mCancelLabel != null) {
1250 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1251 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001252
1253 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1254 return builder;
1255 }
1256
1257 @Override
1258 public WearableExtender clone() {
1259 WearableExtender that = new WearableExtender();
1260 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001261 that.mInProgressLabel = this.mInProgressLabel;
1262 that.mConfirmLabel = this.mConfirmLabel;
1263 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001264 return that;
1265 }
1266
1267 /**
1268 * Set 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 WearableExtender setAvailableOffline(boolean availableOffline) {
1274 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1275 return this;
1276 }
1277
1278 /**
1279 * Get whether this action is available when the wearable device is not connected to
1280 * a companion device. The user can still trigger this action when the wearable device is
1281 * offline, but a visual hint will indicate that the action may not be available.
1282 * Defaults to true.
1283 */
1284 public boolean isAvailableOffline() {
1285 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1286 }
1287
1288 private void setFlag(int mask, boolean value) {
1289 if (value) {
1290 mFlags |= mask;
1291 } else {
1292 mFlags &= ~mask;
1293 }
1294 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001295
1296 /**
1297 * Set a label to display while the wearable is preparing to automatically execute the
1298 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1299 *
1300 * @param label the label to display while the action is being prepared to execute
1301 * @return this object for method chaining
1302 */
1303 public WearableExtender setInProgressLabel(CharSequence label) {
1304 mInProgressLabel = label;
1305 return this;
1306 }
1307
1308 /**
1309 * Get the label to display while the wearable is preparing to automatically execute
1310 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1311 *
1312 * @return the label to display while the action is being prepared to execute
1313 */
1314 public CharSequence getInProgressLabel() {
1315 return mInProgressLabel;
1316 }
1317
1318 /**
1319 * Set a label to display to confirm that the action should be executed.
1320 * This is usually an imperative verb like "Send".
1321 *
1322 * @param label the label to confirm the action should be executed
1323 * @return this object for method chaining
1324 */
1325 public WearableExtender setConfirmLabel(CharSequence label) {
1326 mConfirmLabel = label;
1327 return this;
1328 }
1329
1330 /**
1331 * Get the label to display to confirm that the action should be executed.
1332 * This is usually an imperative verb like "Send".
1333 *
1334 * @return the label to confirm the action should be executed
1335 */
1336 public CharSequence getConfirmLabel() {
1337 return mConfirmLabel;
1338 }
1339
1340 /**
1341 * Set a label to display to cancel the action.
1342 * This is usually an imperative verb, like "Cancel".
1343 *
1344 * @param label the label to display to cancel the action
1345 * @return this object for method chaining
1346 */
1347 public WearableExtender setCancelLabel(CharSequence label) {
1348 mCancelLabel = label;
1349 return this;
1350 }
1351
1352 /**
1353 * Get the label to display to cancel the action.
1354 * This is usually an imperative verb like "Cancel".
1355 *
1356 * @return the label to display to cancel the action
1357 */
1358 public CharSequence getCancelLabel() {
1359 return mCancelLabel;
1360 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001361 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001362 }
1363
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001364 /**
1365 * Array of all {@link Action} structures attached to this notification by
1366 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1367 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1368 * interface for invoking actions.
1369 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001370 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001371
1372 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001373 * Replacement version of this notification whose content will be shown
1374 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1375 * and {@link #VISIBILITY_PUBLIC}.
1376 */
1377 public Notification publicVersion;
1378
1379 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001380 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001381 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 */
1383 public Notification()
1384 {
1385 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001386 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 }
1388
1389 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001390 * @hide
1391 */
1392 public Notification(Context context, int icon, CharSequence tickerText, long when,
1393 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1394 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001395 new Builder(context)
1396 .setWhen(when)
1397 .setSmallIcon(icon)
1398 .setTicker(tickerText)
1399 .setContentTitle(contentTitle)
1400 .setContentText(contentText)
1401 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1402 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 }
1404
1405 /**
1406 * Constructs a Notification object with the information needed to
1407 * have a status bar icon without the standard expanded view.
1408 *
1409 * @param icon The resource id of the icon to put in the status bar.
1410 * @param tickerText The text that flows by in the status bar when the notification first
1411 * activates.
1412 * @param when The time to show in the time field. In the System.currentTimeMillis
1413 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001414 *
1415 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001416 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001417 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001418 public Notification(int icon, CharSequence tickerText, long when)
1419 {
1420 this.icon = icon;
1421 this.tickerText = tickerText;
1422 this.when = when;
1423 }
1424
1425 /**
1426 * Unflatten the notification from a parcel.
1427 */
1428 public Notification(Parcel parcel)
1429 {
1430 int version = parcel.readInt();
1431
1432 when = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001433 if (parcel.readInt() != 0) {
1434 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001435 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1436 icon = mSmallIcon.getResId();
1437 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001438 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 number = parcel.readInt();
1440 if (parcel.readInt() != 0) {
1441 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1442 }
1443 if (parcel.readInt() != 0) {
1444 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1445 }
1446 if (parcel.readInt() != 0) {
1447 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1448 }
1449 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001450 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001451 }
1452 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1454 }
Joe Onorato561d3852010-11-20 18:09:34 -08001455 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001456 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001457 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001458 defaults = parcel.readInt();
1459 flags = parcel.readInt();
1460 if (parcel.readInt() != 0) {
1461 sound = Uri.CREATOR.createFromParcel(parcel);
1462 }
1463
1464 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001465 if (parcel.readInt() != 0) {
1466 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1467 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 vibrate = parcel.createLongArray();
1469 ledARGB = parcel.readInt();
1470 ledOnMS = parcel.readInt();
1471 ledOffMS = parcel.readInt();
1472 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001473
1474 if (parcel.readInt() != 0) {
1475 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1476 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001477
1478 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001479
John Spurlockfd7f1e02014-03-18 16:41:57 -04001480 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001481
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001482 mGroupKey = parcel.readString();
1483
1484 mSortKey = parcel.readString();
1485
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001486 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001487
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001488 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1489
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001490 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001491 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1492 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001493
Chris Wren8fd39ec2014-02-27 17:43:26 -05001494 if (parcel.readInt() != 0) {
1495 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1496 }
1497
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001498 visibility = parcel.readInt();
1499
1500 if (parcel.readInt() != 0) {
1501 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1502 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001503
1504 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001505 }
1506
Andy Stadler110988c2010-12-03 14:29:16 -08001507 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001508 public Notification clone() {
1509 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001510 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001511 return that;
1512 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001513
Daniel Sandler1a497d32013-04-18 14:52:45 -04001514 /**
1515 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1516 * of this into that.
1517 * @hide
1518 */
1519 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001520 that.when = this.when;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001521 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001522 that.number = this.number;
1523
1524 // PendingIntents are global, so there's no reason (or way) to clone them.
1525 that.contentIntent = this.contentIntent;
1526 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001527 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001528
1529 if (this.tickerText != null) {
1530 that.tickerText = this.tickerText.toString();
1531 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001532 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001533 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001534 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001535 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001536 that.contentView = this.contentView.clone();
1537 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001538 if (heavy && this.mLargeIcon != null) {
1539 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001540 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001541 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001542 that.sound = this.sound; // android.net.Uri is immutable
1543 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001544 if (this.audioAttributes != null) {
1545 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1546 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001547
1548 final long[] vibrate = this.vibrate;
1549 if (vibrate != null) {
1550 final int N = vibrate.length;
1551 final long[] vib = that.vibrate = new long[N];
1552 System.arraycopy(vibrate, 0, vib, 0, N);
1553 }
1554
1555 that.ledARGB = this.ledARGB;
1556 that.ledOnMS = this.ledOnMS;
1557 that.ledOffMS = this.ledOffMS;
1558 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001559
Joe Onorato18e69df2010-05-17 22:26:12 -07001560 that.flags = this.flags;
1561
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001562 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001563
John Spurlockfd7f1e02014-03-18 16:41:57 -04001564 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001565
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001566 that.mGroupKey = this.mGroupKey;
1567
1568 that.mSortKey = this.mSortKey;
1569
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001570 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001571 try {
1572 that.extras = new Bundle(this.extras);
1573 // will unparcel
1574 that.extras.size();
1575 } catch (BadParcelableException e) {
1576 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1577 that.extras = null;
1578 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001579 }
1580
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001581 if (this.actions != null) {
1582 that.actions = new Action[this.actions.length];
1583 for(int i=0; i<this.actions.length; i++) {
1584 that.actions[i] = this.actions[i].clone();
1585 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001586 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001587
Daniel Sandler1a497d32013-04-18 14:52:45 -04001588 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001589 that.bigContentView = this.bigContentView.clone();
1590 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001591
Chris Wren8fd39ec2014-02-27 17:43:26 -05001592 if (heavy && this.headsUpContentView != null) {
1593 that.headsUpContentView = this.headsUpContentView.clone();
1594 }
1595
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001596 that.visibility = this.visibility;
1597
1598 if (this.publicVersion != null) {
1599 that.publicVersion = new Notification();
1600 this.publicVersion.cloneInto(that.publicVersion, heavy);
1601 }
1602
Dan Sandler26e81cf2014-05-06 10:01:27 -04001603 that.color = this.color;
1604
Daniel Sandler1a497d32013-04-18 14:52:45 -04001605 if (!heavy) {
1606 that.lightenPayload(); // will clean out extras
1607 }
1608 }
1609
1610 /**
1611 * Removes heavyweight parts of the Notification object for archival or for sending to
1612 * listeners when the full contents are not necessary.
1613 * @hide
1614 */
1615 public final void lightenPayload() {
1616 tickerView = null;
1617 contentView = null;
1618 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001619 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001620 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05001621 if (extras != null && !extras.isEmpty()) {
1622 final Set<String> keyset = extras.keySet();
1623 final int N = keyset.size();
1624 final String[] keys = keyset.toArray(new String[N]);
1625 for (int i=0; i<N; i++) {
1626 final String key = keys[i];
1627 final Object obj = extras.get(key);
1628 if (obj != null &&
1629 ( obj instanceof Parcelable
1630 || obj instanceof Parcelable[]
1631 || obj instanceof SparseArray
1632 || obj instanceof ArrayList)) {
1633 extras.remove(key);
1634 }
1635 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001636 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001637 }
1638
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001639 /**
1640 * Make sure this CharSequence is safe to put into a bundle, which basically
1641 * means it had better not be some custom Parcelable implementation.
1642 * @hide
1643 */
1644 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001645 if (cs == null) return cs;
1646 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1647 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1648 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001649 if (cs instanceof Parcelable) {
1650 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1651 + " instance is a custom Parcelable and not allowed in Notification");
1652 return cs.toString();
1653 }
1654
1655 return cs;
1656 }
1657
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001658 public int describeContents() {
1659 return 0;
1660 }
1661
1662 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001663 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 */
1665 public void writeToParcel(Parcel parcel, int flags)
1666 {
1667 parcel.writeInt(1);
1668
1669 parcel.writeLong(when);
Dan Sandler4e787062015-06-17 15:09:48 -04001670 if (mSmallIcon == null && icon != 0) {
1671 // you snuck an icon in here without using the builder; let's try to keep it
1672 mSmallIcon = Icon.createWithResource("", icon);
1673 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001674 if (mSmallIcon != null) {
1675 parcel.writeInt(1);
1676 mSmallIcon.writeToParcel(parcel, 0);
1677 } else {
1678 parcel.writeInt(0);
1679 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001680 parcel.writeInt(number);
1681 if (contentIntent != null) {
1682 parcel.writeInt(1);
1683 contentIntent.writeToParcel(parcel, 0);
1684 } else {
1685 parcel.writeInt(0);
1686 }
1687 if (deleteIntent != null) {
1688 parcel.writeInt(1);
1689 deleteIntent.writeToParcel(parcel, 0);
1690 } else {
1691 parcel.writeInt(0);
1692 }
1693 if (tickerText != null) {
1694 parcel.writeInt(1);
1695 TextUtils.writeToParcel(tickerText, parcel, flags);
1696 } else {
1697 parcel.writeInt(0);
1698 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001699 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001700 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001701 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001702 } else {
1703 parcel.writeInt(0);
1704 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001705 if (contentView != null) {
1706 parcel.writeInt(1);
1707 contentView.writeToParcel(parcel, 0);
1708 } else {
1709 parcel.writeInt(0);
1710 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001711 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001712 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001713 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001714 } else {
1715 parcel.writeInt(0);
1716 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001717
1718 parcel.writeInt(defaults);
1719 parcel.writeInt(this.flags);
1720
1721 if (sound != null) {
1722 parcel.writeInt(1);
1723 sound.writeToParcel(parcel, 0);
1724 } else {
1725 parcel.writeInt(0);
1726 }
1727 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001728
1729 if (audioAttributes != null) {
1730 parcel.writeInt(1);
1731 audioAttributes.writeToParcel(parcel, 0);
1732 } else {
1733 parcel.writeInt(0);
1734 }
1735
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001736 parcel.writeLongArray(vibrate);
1737 parcel.writeInt(ledARGB);
1738 parcel.writeInt(ledOnMS);
1739 parcel.writeInt(ledOffMS);
1740 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001741
1742 if (fullScreenIntent != null) {
1743 parcel.writeInt(1);
1744 fullScreenIntent.writeToParcel(parcel, 0);
1745 } else {
1746 parcel.writeInt(0);
1747 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001748
1749 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001750
John Spurlockfd7f1e02014-03-18 16:41:57 -04001751 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001752
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001753 parcel.writeString(mGroupKey);
1754
1755 parcel.writeString(mSortKey);
1756
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001757 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001758
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001759 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001760
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001761 if (bigContentView != null) {
1762 parcel.writeInt(1);
1763 bigContentView.writeToParcel(parcel, 0);
1764 } else {
1765 parcel.writeInt(0);
1766 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001767
Chris Wren8fd39ec2014-02-27 17:43:26 -05001768 if (headsUpContentView != null) {
1769 parcel.writeInt(1);
1770 headsUpContentView.writeToParcel(parcel, 0);
1771 } else {
1772 parcel.writeInt(0);
1773 }
1774
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001775 parcel.writeInt(visibility);
1776
1777 if (publicVersion != null) {
1778 parcel.writeInt(1);
1779 publicVersion.writeToParcel(parcel, 0);
1780 } else {
1781 parcel.writeInt(0);
1782 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001783
1784 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 }
1786
1787 /**
1788 * Parcelable.Creator that instantiates Notification objects
1789 */
1790 public static final Parcelable.Creator<Notification> CREATOR
1791 = new Parcelable.Creator<Notification>()
1792 {
1793 public Notification createFromParcel(Parcel parcel)
1794 {
1795 return new Notification(parcel);
1796 }
1797
1798 public Notification[] newArray(int size)
1799 {
1800 return new Notification[size];
1801 }
1802 };
1803
1804 /**
1805 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1806 * layout.
1807 *
1808 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1809 * in the view.</p>
1810 * @param context The context for your application / activity.
1811 * @param contentTitle The title that goes in the expanded entry.
1812 * @param contentText The text that goes in the expanded entry.
1813 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1814 * If this is an activity, it must include the
1815 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001816 * that you take care of task management as described in the
1817 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1818 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001819 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001820 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04001821 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001822 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001823 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 public void setLatestEventInfo(Context context,
1825 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001826 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
1827 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
1828 new Throwable());
1829 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001830
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001831 // ensure that any information already set directly is preserved
1832 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001833
1834 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001835 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001836 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001837 }
1838 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001839 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001840 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001841 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001842
1843 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 }
1845
Julia Reynoldsda303542015-11-23 14:00:20 -05001846 /**
1847 * @hide
1848 */
1849 public static void addFieldsFromContext(Context context, Notification notification) {
1850 if (notification.extras.getParcelable(EXTRA_BUILDER_APPLICATION_INFO) == null) {
1851 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO,
1852 context.getApplicationInfo());
1853 }
1854 if (!notification.extras.containsKey(EXTRA_ORIGINATING_USERID)) {
1855 notification.extras.putInt(EXTRA_ORIGINATING_USERID, context.getUserId());
1856 }
1857 }
1858
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001859 @Override
1860 public String toString() {
1861 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001862 sb.append("Notification(pri=");
1863 sb.append(priority);
1864 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001865 if (contentView != null) {
1866 sb.append(contentView.getPackage());
1867 sb.append("/0x");
1868 sb.append(Integer.toHexString(contentView.getLayoutId()));
1869 } else {
1870 sb.append("null");
1871 }
1872 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001873 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1874 sb.append("default");
1875 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 int N = this.vibrate.length-1;
1877 sb.append("[");
1878 for (int i=0; i<N; i++) {
1879 sb.append(this.vibrate[i]);
1880 sb.append(',');
1881 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001882 if (N != -1) {
1883 sb.append(this.vibrate[N]);
1884 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 } else {
1887 sb.append("null");
1888 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001889 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001890 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001892 } else if (this.sound != null) {
1893 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001894 } else {
1895 sb.append("null");
1896 }
Chris Wren365b6d32015-07-16 10:39:26 -04001897 if (this.tickerText != null) {
1898 sb.append(" tick");
1899 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001900 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001901 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001902 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001903 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04001904 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001905 if (this.category != null) {
1906 sb.append(" category=");
1907 sb.append(this.category);
1908 }
1909 if (this.mGroupKey != null) {
1910 sb.append(" groupKey=");
1911 sb.append(this.mGroupKey);
1912 }
1913 if (this.mSortKey != null) {
1914 sb.append(" sortKey=");
1915 sb.append(this.mSortKey);
1916 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001917 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04001918 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001919 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04001920 }
1921 sb.append(" vis=");
1922 sb.append(visibilityToString(this.visibility));
1923 if (this.publicVersion != null) {
1924 sb.append(" publicVersion=");
1925 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001926 }
1927 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001928 return sb.toString();
1929 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001930
Dan Sandler1b718782014-07-18 12:43:45 -04001931 /**
1932 * {@hide}
1933 */
1934 public static String visibilityToString(int vis) {
1935 switch (vis) {
1936 case VISIBILITY_PRIVATE:
1937 return "PRIVATE";
1938 case VISIBILITY_PUBLIC:
1939 return "PUBLIC";
1940 case VISIBILITY_SECRET:
1941 return "SECRET";
1942 default:
1943 return "UNKNOWN(" + String.valueOf(vis) + ")";
1944 }
1945 }
1946
Joe Onoratocb109a02011-01-18 17:57:41 -08001947 /**
John Spurlock1d881a12015-03-18 19:21:54 -04001948 * {@hide}
1949 */
1950 public static String priorityToString(@Priority int pri) {
1951 switch (pri) {
1952 case PRIORITY_MIN:
1953 return "MIN";
1954 case PRIORITY_LOW:
1955 return "LOW";
1956 case PRIORITY_DEFAULT:
1957 return "DEFAULT";
1958 case PRIORITY_HIGH:
1959 return "HIGH";
1960 case PRIORITY_MAX:
1961 return "MAX";
1962 default:
1963 return "UNKNOWN(" + String.valueOf(pri) + ")";
1964 }
1965 }
1966
1967 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04001968 * The small icon representing this notification in the status bar and content view.
1969 *
1970 * @return the small icon representing this notification.
1971 *
1972 * @see Builder#getSmallIcon()
1973 * @see Builder#setSmallIcon(Icon)
1974 */
1975 public Icon getSmallIcon() {
1976 return mSmallIcon;
1977 }
1978
1979 /**
1980 * Used when notifying to clean up legacy small icons.
1981 * @hide
1982 */
1983 public void setSmallIcon(Icon icon) {
1984 mSmallIcon = icon;
1985 }
1986
1987 /**
1988 * The large icon shown in this notification's content view.
1989 * @see Builder#getLargeIcon()
1990 * @see Builder#setLargeIcon(Icon)
1991 */
1992 public Icon getLargeIcon() {
1993 return mLargeIcon;
1994 }
1995
1996 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02001997 * @hide
1998 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02001999 public boolean isGroupSummary() {
2000 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2001 }
2002
2003 /**
2004 * @hide
2005 */
2006 public boolean isGroupChild() {
2007 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2008 }
2009
2010 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002011 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002012 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002013 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002014 * content views using the platform's notification layout template. If your app supports
2015 * versions of Android as old as API level 4, you can instead use
2016 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2017 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2018 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002019 *
Scott Main183bf112012-08-13 19:12:13 -07002020 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002021 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002022 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002023 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002024 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2025 * .setContentText(subject)
2026 * .setSmallIcon(R.drawable.new_mail)
2027 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002028 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002029 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002030 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002031 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002032 /**
2033 * @hide
2034 */
2035 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2036 "android.rebuild.contentViewActionCount";
2037 /**
2038 * @hide
2039 */
2040 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2041 = "android.rebuild.bigViewActionCount";
2042 /**
2043 * @hide
2044 */
2045 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2046 = "android.rebuild.hudViewActionCount";
2047
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002048 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002049
Joe Onorato46439ce2010-11-19 13:56:21 -08002050 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002051 private Notification mN;
2052 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002053 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002054 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2055 private ArrayList<String> mPersonList = new ArrayList<String>();
2056 private NotificationColorUtil mColorUtil;
2057 private boolean mColorUtilInited = false;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002058
2059 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002060 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002061 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002062
2063 * <table>
2064 * <tr><th align=right>priority</th>
2065 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2066 * <tr><th align=right>when</th>
2067 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2068 * <tr><th align=right>audio stream</th>
2069 * <td>{@link #STREAM_DEFAULT}</td></tr>
2070 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002071 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002072
2073 * @param context
2074 * A {@link Context} that will be used by the Builder to construct the
2075 * RemoteViews. The Context will not be held past the lifetime of this Builder
2076 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002077 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002078 public Builder(Context context) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002079 this(context, null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002080 }
2081
Joe Onoratocb109a02011-01-18 17:57:41 -08002082 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002083 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002084 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002085 public Builder(Context context, Notification toAdopt) {
2086 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002087
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002088 if (toAdopt == null) {
2089 mN = new Notification();
2090 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2091 mN.priority = PRIORITY_DEFAULT;
2092 mN.visibility = VISIBILITY_PRIVATE;
2093 } else {
2094 mN = toAdopt;
2095 if (mN.actions != null) {
2096 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002097 }
2098
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002099 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2100 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2101 }
2102
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002103 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2104 if (!TextUtils.isEmpty(templateClass)) {
2105 final Class<? extends Style> styleClass
2106 = getNotificationStyleClass(templateClass);
2107 if (styleClass == null) {
2108 Log.d(TAG, "Unknown style class: " + templateClass);
2109 } else {
2110 try {
2111 final Constructor<? extends Style> ctor = styleClass.getConstructor();
2112 ctor.setAccessible(true);
2113 final Style style = ctor.newInstance();
2114 style.restoreFromExtras(mN.extras);
2115
2116 if (style != null) {
2117 setStyle(style);
2118 }
2119 } catch (Throwable t) {
2120 Log.e(TAG, "Could not create Style", t);
2121 }
2122 }
2123 }
2124
2125 }
2126 }
2127
2128 private NotificationColorUtil getColorUtil() {
2129 if (!mColorUtilInited) {
2130 mColorUtilInited = true;
2131 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
2132 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002133 }
2134 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002135 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002136 }
2137
2138 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002139 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07002140 * It will be shown in the notification content view by default; use
Griff Hazen50c11652014-05-16 09:46:31 -07002141 * {@link #setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002142 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002143 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002144 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002145 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002146 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002147 return this;
2148 }
2149
Joe Onoratocb109a02011-01-18 17:57:41 -08002150 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002151 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002152 * in the content view.
2153 */
2154 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002155 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002156 return this;
2157 }
2158
2159 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002160 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002161 *
2162 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002163 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002164 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002165 * Useful when showing an elapsed time (like an ongoing phone call).
2166 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002167 * The counter can also be set to count down to <code>when</code> when using
2168 * {@link #setChronometerCountsDown(boolean)}.
2169 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002170 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002171 * @see Notification#when
Selim Cinek81c23aa2016-02-25 16:23:13 -08002172 * @see #setChronometerCountsDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002173 */
2174 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002175 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002176 return this;
2177 }
2178
2179 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002180 * Sets the Chronometer to count down instead of counting up.
2181 *
2182 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2183 * If it isn't set the chronometer will count up.
2184 *
2185 * @see #setUsesChronometer(boolean)
2186 */
2187 public Builder setChronometerCountsDown(boolean countsDown) {
2188 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN, countsDown);
2189 return this;
2190 }
2191
2192 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002193 * Set the small icon resource, which will be used to represent the notification in the
2194 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002195 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002196
2197 * The platform template for the expanded view will draw this icon in the left, unless a
2198 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2199 * icon will be moved to the right-hand side.
2200 *
2201
2202 * @param icon
2203 * A resource ID in the application's package of the drawable to use.
2204 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002205 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002206 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002207 return setSmallIcon(icon != 0
2208 ? Icon.createWithResource(mContext, icon)
2209 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002210 }
2211
Joe Onoratocb109a02011-01-18 17:57:41 -08002212 /**
2213 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2214 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2215 * LevelListDrawable}.
2216 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002217 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002218 * @param level The level to use for the icon.
2219 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002220 * @see Notification#icon
2221 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002222 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002223 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002224 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002225 return setSmallIcon(icon);
2226 }
2227
2228 /**
2229 * Set the small icon, which will be used to represent the notification in the
2230 * status bar and content view (unless overriden there by a
2231 * {@link #setLargeIcon(Bitmap) large icon}).
2232 *
2233 * @param icon An Icon object to use.
2234 * @see Notification#icon
2235 */
2236 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002237 mN.setSmallIcon(icon);
2238 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2239 mN.icon = icon.getResId();
2240 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002241 return this;
2242 }
2243
Joe Onoratocb109a02011-01-18 17:57:41 -08002244 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002245 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002246 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002247 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002248 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002249 return this;
2250 }
2251
Joe Onoratocb109a02011-01-18 17:57:41 -08002252 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002253 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002254 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002255 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002256 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002257 return this;
2258 }
2259
Joe Onoratocb109a02011-01-18 17:57:41 -08002260 /**
Joe Malin8d40d042012-11-05 11:36:40 -08002261 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002262 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
2263 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002264 */
2265 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002266 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002267 return this;
2268 }
2269
2270 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002271 * Set the remote input history.
2272 *
2273 * This should be set to the most recent inputs that have been sent
2274 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2275 * longer relevant (e.g. for chat notifications once the other party has responded).
2276 *
2277 * The most recent input must be stored at the 0 index, the second most recent at the
2278 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2279 * and how much of each individual input is shown.
2280 *
2281 * <p>Note: The reply text will only be shown on notifications that have least one action
2282 * with a {@code RemoteInput}.</p>
2283 */
2284 public Builder setRemoteInputHistory(CharSequence[] text) {
2285 if (text == null) {
2286 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2287 } else {
2288 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2289 CharSequence[] safe = new CharSequence[N];
2290 for (int i = 0; i < N; i++) {
2291 safe[i] = safeCharSequence(text[i]);
2292 }
2293 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2294 }
2295 return this;
2296 }
2297
2298 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002299 * Set the large number at the right-hand side of the notification. This is
2300 * equivalent to setContentInfo, although it might show the number in a different
2301 * font size for readability.
2302 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002303 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002304 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002305 return this;
2306 }
2307
Joe Onoratocb109a02011-01-18 17:57:41 -08002308 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002309 * A small piece of additional information pertaining to this notification.
2310 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002311 * The platform template will draw this on the last line of the notification, at the far
2312 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08002313 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002314 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002315 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002316 return this;
2317 }
2318
Joe Onoratocb109a02011-01-18 17:57:41 -08002319 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002320 * Set the progress this notification represents.
2321 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002322 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002323 */
2324 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002325 mN.extras.putInt(EXTRA_PROGRESS, progress);
2326 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
2327 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002328 return this;
2329 }
2330
2331 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002332 * Supply a custom RemoteViews to use instead of the platform template.
2333 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002334 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08002335 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002336 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002337 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002338 return setCustomContentView(views);
2339 }
2340
2341 /**
2342 * Supply custom RemoteViews to use instead of the platform template.
2343 *
2344 * This will override the layout that would otherwise be constructed by this Builder
2345 * object.
2346 */
2347 public Builder setCustomContentView(RemoteViews contentView) {
2348 mN.contentView = contentView;
2349 return this;
2350 }
2351
2352 /**
2353 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
2354 *
2355 * This will override the expanded layout that would otherwise be constructed by this
2356 * Builder object.
2357 */
2358 public Builder setCustomBigContentView(RemoteViews contentView) {
2359 mN.bigContentView = contentView;
2360 return this;
2361 }
2362
2363 /**
2364 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
2365 *
2366 * This will override the heads-up layout that would otherwise be constructed by this
2367 * Builder object.
2368 */
2369 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
2370 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002371 return this;
2372 }
2373
Joe Onoratocb109a02011-01-18 17:57:41 -08002374 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002375 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2376 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002377 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2378 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2379 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002380 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002381 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002382 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002383 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002384 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002385 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002386 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002387 return this;
2388 }
2389
Joe Onoratocb109a02011-01-18 17:57:41 -08002390 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002391 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2392 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002393 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002394 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002395 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002396 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002397 return this;
2398 }
2399
Joe Onoratocb109a02011-01-18 17:57:41 -08002400 /**
2401 * An intent to launch instead of posting the notification to the status bar.
2402 * Only for use with extremely high-priority notifications demanding the user's
2403 * <strong>immediate</strong> attention, such as an incoming phone call or
2404 * alarm clock that the user has explicitly set to a particular time.
2405 * If this facility is used for something else, please give the user an option
2406 * to turn it off and use a normal notification, as this can be extremely
2407 * disruptive.
2408 *
Chris Wren47c20a12014-06-18 17:27:29 -04002409 * <p>
2410 * The system UI may choose to display a heads-up notification, instead of
2411 * launching this intent, while the user is using the device.
2412 * </p>
2413 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002414 * @param intent The pending intent to launch.
2415 * @param highPriority Passing true will cause this notification to be sent
2416 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002417 *
2418 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002419 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002420 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002421 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002422 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2423 return this;
2424 }
2425
Joe Onoratocb109a02011-01-18 17:57:41 -08002426 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002427 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002428 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002429 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002430 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002431 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002432 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002433 return this;
2434 }
2435
Joe Onoratocb109a02011-01-18 17:57:41 -08002436 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002437 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002438 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002439 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002440 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002441 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002442 setTicker(tickerText);
2443 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08002444 return this;
2445 }
2446
Joe Onoratocb109a02011-01-18 17:57:41 -08002447 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002448 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002449 *
2450 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002451 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2452 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002453 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002454 public Builder setLargeIcon(Bitmap b) {
2455 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2456 }
2457
2458 /**
2459 * Add a large icon to the notification content view.
2460 *
2461 * In the platform template, this image will be shown on the left of the notification view
2462 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2463 * badge atop the large icon).
2464 */
2465 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002466 mN.mLargeIcon = icon;
2467 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08002468 return this;
2469 }
2470
Joe Onoratocb109a02011-01-18 17:57:41 -08002471 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002472 * Set the sound to play.
2473 *
John Spurlockc0650f022014-07-19 13:22:39 -04002474 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2475 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002476 *
Chris Wren47c20a12014-06-18 17:27:29 -04002477 * <p>
2478 * A notification that is noisy is more likely to be presented as a heads-up notification.
2479 * </p>
2480 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002481 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002482 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002483 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002484 mN.sound = sound;
2485 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002486 return this;
2487 }
2488
Joe Onoratocb109a02011-01-18 17:57:41 -08002489 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002490 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002491 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002492 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2493 *
Chris Wren47c20a12014-06-18 17:27:29 -04002494 * <p>
2495 * A notification that is noisy is more likely to be presented as a heads-up notification.
2496 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002497 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002498 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002499 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002500 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002501 public Builder setSound(Uri sound, int streamType) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002502 mN.sound = sound;
2503 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08002504 return this;
2505 }
2506
Joe Onoratocb109a02011-01-18 17:57:41 -08002507 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002508 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2509 * use during playback.
2510 *
2511 * <p>
2512 * A notification that is noisy is more likely to be presented as a heads-up notification.
2513 * </p>
2514 *
2515 * @see Notification#sound
2516 */
2517 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002518 mN.sound = sound;
2519 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04002520 return this;
2521 }
2522
2523 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002524 * Set the vibration pattern to use.
2525 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002526 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2527 * <code>pattern</code> parameter.
2528 *
Chris Wren47c20a12014-06-18 17:27:29 -04002529 * <p>
2530 * A notification that vibrates is more likely to be presented as a heads-up notification.
2531 * </p>
2532 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002533 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002534 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002535 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002536 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08002537 return this;
2538 }
2539
Joe Onoratocb109a02011-01-18 17:57:41 -08002540 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002541 * Set the desired color for the indicator LED on the device, as well as the
2542 * blink duty cycle (specified in milliseconds).
2543 *
2544
2545 * Not all devices will honor all (or even any) of these values.
2546 *
2547
2548 * @see Notification#ledARGB
2549 * @see Notification#ledOnMS
2550 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002551 */
Tor Norbye80756e32015-03-02 09:39:27 -08002552 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002553 mN.ledARGB = argb;
2554 mN.ledOnMS = onMs;
2555 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002556 if (onMs != 0 || offMs != 0) {
2557 mN.flags |= FLAG_SHOW_LIGHTS;
2558 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002559 return this;
2560 }
2561
Joe Onoratocb109a02011-01-18 17:57:41 -08002562 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002563 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002564 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002565
2566 * Ongoing notifications cannot be dismissed by the user, so your application or service
2567 * must take care of canceling them.
2568 *
2569
2570 * They are typically used to indicate a background task that the user is actively engaged
2571 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2572 * (e.g., a file download, sync operation, active network connection).
2573 *
2574
2575 * @see Notification#FLAG_ONGOING_EVENT
2576 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002577 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002578 public Builder setOngoing(boolean ongoing) {
2579 setFlag(FLAG_ONGOING_EVENT, ongoing);
2580 return this;
2581 }
2582
Joe Onoratocb109a02011-01-18 17:57:41 -08002583 /**
2584 * Set this flag if you would only like the sound, vibrate
2585 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002586 *
2587 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002588 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002589 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2590 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2591 return this;
2592 }
2593
Joe Onoratocb109a02011-01-18 17:57:41 -08002594 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002595 * Make this notification automatically dismissed when the user touches it. The
2596 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2597 *
2598 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002599 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002600 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002601 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002602 return this;
2603 }
2604
Joe Onoratocb109a02011-01-18 17:57:41 -08002605 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002606 * Set whether or not this notification should not bridge to other devices.
2607 *
2608 * <p>Some notifications can be bridged to other devices for remote display.
2609 * This hint can be set to recommend this notification not be bridged.
2610 */
2611 public Builder setLocalOnly(boolean localOnly) {
2612 setFlag(FLAG_LOCAL_ONLY, localOnly);
2613 return this;
2614 }
2615
2616 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002617 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002618 * <p>
2619 * The value should be one or more of the following fields combined with
2620 * bitwise-or:
2621 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2622 * <p>
2623 * For all default values, use {@link #DEFAULT_ALL}.
2624 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002625 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002626 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08002627 return this;
2628 }
2629
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002630 /**
2631 * Set the priority of this notification.
2632 *
2633 * @see Notification#priority
2634 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002635 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002636 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002637 return this;
2638 }
Joe Malin8d40d042012-11-05 11:36:40 -08002639
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002640 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002641 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002642 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002643 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002644 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002645 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002646 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002647 return this;
2648 }
2649
2650 /**
Chris Wrendde75302014-03-26 17:24:15 -04002651 * Add a person that is relevant to this notification.
2652 *
Chris Wrene6c48932014-09-29 17:19:27 -04002653 * <P>
2654 * Depending on user preferences, this annotation may allow the notification to pass
2655 * through interruption filters, and to appear more prominently in the user interface.
2656 * </P>
2657 *
2658 * <P>
2659 * The person should be specified by the {@code String} representation of a
2660 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2661 * </P>
2662 *
2663 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2664 * URIs. The path part of these URIs must exist in the contacts database, in the
2665 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2666 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2667 * </P>
2668 *
2669 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002670 * @see Notification#EXTRA_PEOPLE
2671 */
Chris Wrene6c48932014-09-29 17:19:27 -04002672 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002673 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002674 return this;
2675 }
2676
2677 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002678 * Set this notification to be part of a group of notifications sharing the same key.
2679 * Grouped notifications may display in a cluster or stack on devices which
2680 * support such rendering.
2681 *
2682 * <p>To make this notification the summary for its group, also call
2683 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2684 * {@link #setSortKey}.
2685 * @param groupKey The group key of the group.
2686 * @return this object for method chaining
2687 */
2688 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002689 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002690 return this;
2691 }
2692
2693 /**
2694 * Set this notification to be the group summary for a group of notifications.
2695 * Grouped notifications may display in a cluster or stack on devices which
2696 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2697 * @param isGroupSummary Whether this notification should be a group summary.
2698 * @return this object for method chaining
2699 */
2700 public Builder setGroupSummary(boolean isGroupSummary) {
2701 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2702 return this;
2703 }
2704
2705 /**
2706 * Set a sort key that orders this notification among other notifications from the
2707 * same package. This can be useful if an external sort was already applied and an app
2708 * would like to preserve this. Notifications will be sorted lexicographically using this
2709 * value, although providing different priorities in addition to providing sort key may
2710 * cause this value to be ignored.
2711 *
2712 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002713 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002714 *
2715 * @see String#compareTo(String)
2716 */
2717 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002718 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002719 return this;
2720 }
2721
2722 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002723 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002724 *
Griff Hazen720042b2014-02-24 15:46:56 -08002725 * <p>Values within the Bundle will replace existing extras values in this Builder.
2726 *
2727 * @see Notification#extras
2728 */
Griff Hazen959591e2014-05-15 22:26:18 -07002729 public Builder addExtras(Bundle extras) {
2730 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002731 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08002732 }
2733 return this;
2734 }
2735
2736 /**
2737 * Set metadata for this notification.
2738 *
2739 * <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 -04002740 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002741 * called.
2742 *
Griff Hazen720042b2014-02-24 15:46:56 -08002743 * <p>Replaces any existing extras values with those from the provided Bundle.
2744 * Use {@link #addExtras} to merge in metadata instead.
2745 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002746 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002747 */
Griff Hazen959591e2014-05-15 22:26:18 -07002748 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002749 if (extras != null) {
2750 mUserExtras = extras;
2751 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002752 return this;
2753 }
2754
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002755 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002756 * Get the current metadata Bundle used by this notification Builder.
2757 *
2758 * <p>The returned Bundle is shared with this Builder.
2759 *
2760 * <p>The current contents of this Bundle are copied into the Notification each time
2761 * {@link #build()} is called.
2762 *
2763 * @see Notification#extras
2764 */
2765 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002766 return mUserExtras;
2767 }
2768
2769 private Bundle getAllExtras() {
2770 final Bundle saveExtras = (Bundle) mUserExtras.clone();
2771 saveExtras.putAll(mN.extras);
2772 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08002773 }
2774
2775 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002776 * Add an action to this notification. Actions are typically displayed by
2777 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002778 * <p>
2779 * Every action must have an icon (32dp square and matching the
2780 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2781 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2782 * <p>
2783 * A notification in its expanded form can display up to 3 actions, from left to right in
2784 * the order they were added. Actions will not be displayed when the notification is
2785 * collapsed, however, so be sure that any essential functions may be accessed by the user
2786 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002787 *
2788 * @param icon Resource ID of a drawable that represents the action.
2789 * @param title Text describing the action.
2790 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04002791 *
2792 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002793 */
Dan Sandler86647982015-05-13 23:41:13 -04002794 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002795 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002796 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002797 return this;
2798 }
2799
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002800 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002801 * Add an action to this notification. Actions are typically displayed by
2802 * the system as a button adjacent to the notification content.
2803 * <p>
2804 * Every action must have an icon (32dp square and matching the
2805 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2806 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2807 * <p>
2808 * A notification in its expanded form can display up to 3 actions, from left to right in
2809 * the order they were added. Actions will not be displayed when the notification is
2810 * collapsed, however, so be sure that any essential functions may be accessed by the user
2811 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2812 *
2813 * @param action The action to add.
2814 */
2815 public Builder addAction(Action action) {
2816 mActions.add(action);
2817 return this;
2818 }
2819
2820 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002821 * Alter the complete list of actions attached to this notification.
2822 * @see #addAction(Action).
2823 *
2824 * @param actions
2825 * @return
2826 */
2827 public Builder setActions(Action... actions) {
2828 mActions.clear();
2829 for (int i = 0; i < actions.length; i++) {
2830 mActions.add(actions[i]);
2831 }
2832 return this;
2833 }
2834
2835 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002836 * Add a rich notification style to be applied at build time.
2837 *
2838 * @param style Object responsible for modifying the notification style.
2839 */
2840 public Builder setStyle(Style style) {
2841 if (mStyle != style) {
2842 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002843 if (mStyle != null) {
2844 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002845 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
2846 } else {
2847 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002848 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002849 }
2850 return this;
2851 }
2852
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002853 /**
2854 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002855 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002856 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2857 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
2858 *
2859 * @return The same Builder.
2860 */
2861 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002862 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002863 return this;
2864 }
2865
2866 /**
2867 * Supply a replacement Notification whose contents should be shown in insecure contexts
2868 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
2869 * @param n A replacement notification, presumably with some or all info redacted.
2870 * @return The same Builder.
2871 */
2872 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002873 if (n != null) {
2874 mN.publicVersion = new Notification();
2875 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
2876 } else {
2877 mN.publicVersion = null;
2878 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002879 return this;
2880 }
2881
Griff Hazenb720abe2014-05-20 13:15:30 -07002882 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002883 * Apply an extender to this notification builder. Extenders may be used to add
2884 * metadata or change options on this builder.
2885 */
Griff Hazen61a9e862014-05-22 16:05:19 -07002886 public Builder extend(Extender extender) {
2887 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002888 return this;
2889 }
2890
Dan Sandler4e787062015-06-17 15:09:48 -04002891 /**
2892 * @hide
2893 */
2894 public void setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002895 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002896 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08002897 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002898 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08002899 }
2900 }
2901
Dan Sandler26e81cf2014-05-06 10:01:27 -04002902 /**
2903 * Sets {@link Notification#color}.
2904 *
2905 * @param argb The accent color to use
2906 *
2907 * @return The same Builder.
2908 */
Tor Norbye80756e32015-03-02 09:39:27 -08002909 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002910 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002911 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04002912 return this;
2913 }
2914
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002915 private Drawable getProfileBadgeDrawable() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02002916 // Note: This assumes that the current user can read the profile badge of the
2917 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08002918 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05002919 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002920 }
2921
2922 private Bitmap getProfileBadge() {
2923 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01002924 if (badge == null) {
2925 return null;
2926 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002927 final int size = mContext.getResources().getDimensionPixelSize(
2928 R.dimen.notification_badge_size);
2929 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01002930 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02002931 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01002932 badge.draw(canvas);
2933 return bitmap;
2934 }
2935
Selim Cinekc848c3a2016-01-13 15:27:30 -08002936 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01002937 Bitmap profileBadge = getProfileBadge();
2938
Kenny Guy98193ea2014-07-24 19:54:37 +01002939 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08002940 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
2941 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Kenny Guy98193ea2014-07-24 19:54:37 +01002942 }
Kenny Guy98193ea2014-07-24 19:54:37 +01002943 }
2944
Christoph Studerfe718432014-09-01 18:21:18 +02002945 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08002946 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07002947 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02002948 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08002949 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02002950 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08002951 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02002952 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08002953 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08002954 contentView.setTextViewText(R.id.text_line_1, null);
Christoph Studerfe718432014-09-01 18:21:18 +02002955 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07002956 }
2957
Selim Cinekeaa29ca2015-11-23 13:51:13 -08002958 /**
2959 * Resets the notification header to its original state
2960 */
2961 private void resetNotificationHeader(RemoteViews contentView) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07002962 contentView.setImageViewResource(R.id.icon, 0);
Selim Cinek7b836392015-12-04 20:02:59 -08002963 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07002964 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02002965 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07002966 contentView.setViewVisibility(R.id.header_sub_text, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08002967 contentView.setViewVisibility(R.id.header_content_info, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07002968 contentView.setViewVisibility(R.id.number_of_children, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08002969 contentView.setViewVisibility(R.id.sub_text_divider, View.GONE);
2970 contentView.setViewVisibility(R.id.content_info_divider, View.GONE);
2971 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08002972 contentView.setImageViewIcon(R.id.profile_badge, null);
2973 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07002974 }
2975
2976 private void resetContentMargins(RemoteViews contentView) {
2977 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08002978 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02002979 }
2980
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002981 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02002982 return applyStandardTemplate(resId, true /* hasProgress */);
2983 }
2984
2985 /**
2986 * @param hasProgress whether the progress bar should be shown and set
2987 */
2988 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Kenny Guy77320062014-08-27 21:37:15 +01002989 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04002990
Christoph Studerfe718432014-09-01 18:21:18 +02002991 resetStandardTemplate(contentView);
2992
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002993 final Bundle ex = mN.extras;
Dan Sandler190d58d2014-05-15 09:33:39 -04002994
Selim Cinek65b2e7c2015-10-26 14:11:31 -07002995 bindNotificationHeader(contentView);
2996 bindLargeIcon(contentView);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002997 if (ex.getCharSequence(EXTRA_TITLE) != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08002998 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002999 contentView.setTextViewText(R.id.title,
3000 processLegacyText(ex.getCharSequence(EXTRA_TITLE)));
Joe Onorato561d3852010-11-20 18:09:34 -08003001 }
Selim Cinek29603462015-11-17 19:04:39 -08003002 boolean showProgress = handleProgressBar(hasProgress, contentView, ex);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003003 if (ex.getCharSequence(EXTRA_TEXT) != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003004 int textId = showProgress ? com.android.internal.R.id.text_line_1
3005 : com.android.internal.R.id.text;
3006 contentView.setTextViewText(textId, processLegacyText(
3007 ex.getCharSequence(EXTRA_TEXT)));
3008 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003009 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003010
Selim Cinek860b6da2015-12-16 19:02:19 -08003011 setContentMinHeight(contentView, showProgress || mN.mLargeIcon != null);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003012
Selim Cinek29603462015-11-17 19:04:39 -08003013 return contentView;
3014 }
3015
Selim Cinek860b6da2015-12-16 19:02:19 -08003016 /**
3017 * @param remoteView the remote view to update the minheight in
3018 * @param hasMinHeight does it have a mimHeight
3019 * @hide
3020 */
3021 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3022 int minHeight = 0;
3023 if (hasMinHeight) {
3024 // we need to set the minHeight of the notification
3025 minHeight = mContext.getResources().getDimensionPixelSize(
3026 com.android.internal.R.dimen.notification_min_content_height);
3027 }
3028 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3029 }
3030
Selim Cinek29603462015-11-17 19:04:39 -08003031 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003032 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3033 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3034 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3035 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003036 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003037 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003038 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003039 contentView.setProgressBackgroundTintList(
3040 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3041 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003042 if (mN.color != COLOR_DEFAULT) {
3043 ColorStateList colorStateList = ColorStateList.valueOf(mN.color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003044 contentView.setProgressTintList(R.id.progress, colorStateList);
3045 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003046 }
Selim Cinek29603462015-11-17 19:04:39 -08003047 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003048 } else {
3049 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003050 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003051 }
Joe Onorato561d3852010-11-20 18:09:34 -08003052 }
3053
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003054 private void bindLargeIcon(RemoteViews contentView) {
3055 if (mN.mLargeIcon != null) {
3056 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3057 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3058 processLargeLegacyIcon(mN.mLargeIcon, contentView);
3059 int endMargin = mContext.getResources().getDimensionPixelSize(
3060 R.dimen.notification_content_picture_margin);
3061 contentView.setViewLayoutMarginEnd(R.id.line1, endMargin);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003062 contentView.setViewLayoutMarginEnd(R.id.text, endMargin);
Selim Cinek29603462015-11-17 19:04:39 -08003063 contentView.setViewLayoutMarginEnd(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003064 }
3065 }
3066
3067 private void bindNotificationHeader(RemoteViews contentView) {
3068 bindSmallIcon(contentView);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003069 bindChildCountColor(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003070 bindHeaderAppName(contentView);
Selim Cinek03d0d652015-11-13 13:18:09 -05003071 bindHeaderSubText(contentView);
Selim Cinek29603462015-11-17 19:04:39 -08003072 bindContentInfo(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003073 bindHeaderChronometerAndTime(contentView);
3074 bindExpandButton(contentView);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003075 bindProfileBadge(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003076 }
3077
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003078 private void bindChildCountColor(RemoteViews contentView) {
3079 contentView.setTextColor(R.id.number_of_children, resolveColor());
3080 }
3081
Selim Cinek29603462015-11-17 19:04:39 -08003082 private void bindContentInfo(RemoteViews contentView) {
3083 boolean visible = false;
3084 if (mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3085 contentView.setTextViewText(R.id.header_content_info,
3086 processLegacyText(mN.extras.getCharSequence(EXTRA_INFO_TEXT)));
3087 contentView.setViewVisibility(R.id.header_content_info, View.VISIBLE);
3088 visible = true;
3089 } else if (mN.number > 0) {
3090 final int tooBig = mContext.getResources().getInteger(
3091 R.integer.status_bar_notification_info_maxnum);
3092 if (mN.number > tooBig) {
3093 contentView.setTextViewText(R.id.header_content_info, processLegacyText(
3094 mContext.getResources().getString(
3095 R.string.status_bar_notification_info_overflow)));
3096 } else {
3097 contentView.setTextViewText(R.id.header_content_info,
3098 processLegacyText(String.valueOf(mN.number)));
3099 }
3100 contentView.setViewVisibility(R.id.header_content_info, View.VISIBLE);
3101 visible = true;
3102 }
3103 if (visible) {
3104 contentView.setViewVisibility(R.id.content_info_divider, View.VISIBLE);
3105 }
3106 }
3107
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003108 private void bindExpandButton(RemoteViews contentView) {
3109 contentView.setDrawableParameters(R.id.expand_button, false, -1, resolveColor(),
3110 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003111 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
3112 resolveColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003113 }
3114
3115 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3116 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003117 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003118 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3119 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3120 contentView.setLong(R.id.chronometer, "setBase",
3121 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3122 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Selim Cinek81c23aa2016-02-25 16:23:13 -08003123 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN);
3124 contentView.setChronometerCountsDown(R.id.chronometer, countsDown);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003125 } else {
3126 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3127 contentView.setLong(R.id.time, "setTime", mN.when);
3128 }
3129 }
3130 }
3131
Selim Cinek03d0d652015-11-13 13:18:09 -05003132 private void bindHeaderSubText(RemoteViews contentView) {
3133 CharSequence subText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3134 if (subText == null && mStyle != null && mStyle.mSummaryTextSet
3135 && mStyle.hasSummaryInHeader()) {
3136 subText = mStyle.mSummaryText;
3137 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003138 if (subText != null) {
3139 // TODO: Remove the span entirely to only have the string with propper formating.
3140 contentView.setTextViewText(R.id.header_sub_text, processLegacyText(subText));
3141 contentView.setViewVisibility(R.id.header_sub_text, View.VISIBLE);
Selim Cinek29603462015-11-17 19:04:39 -08003142 contentView.setViewVisibility(R.id.sub_text_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003143 }
3144 }
3145
3146 private void bindHeaderAppName(RemoteViews contentView) {
3147 PackageManager packageManager = mContext.getPackageManager();
3148 ApplicationInfo info = null;
3149 try {
3150 info = packageManager.getApplicationInfo(mContext.getApplicationInfo().packageName,
3151 0);
3152 } catch (final NameNotFoundException e) {
3153 return;
3154 }
3155 CharSequence appName = info != null ? packageManager.getApplicationLabel(info)
3156 : null;
3157 if (TextUtils.isEmpty(appName)) {
3158 return;
3159 }
3160 contentView.setTextViewText(R.id.app_name_text, appName);
Selim Cinekaef485a2016-02-05 10:31:20 -08003161 contentView.setTextColor(R.id.app_name_text, resolveColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003162 }
3163
3164 private void bindSmallIcon(RemoteViews contentView) {
3165 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
3166 processSmallIconColor(mN.mSmallIcon, contentView);
3167 }
3168
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003169 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003170 * @return true if the built notification will show the time or the chronometer; false
3171 * otherwise
3172 */
3173 private boolean showsTimeOrChronometer() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003174 return mN.when != 0 && mN.extras.getBoolean(EXTRA_SHOW_WHEN);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003175 }
3176
Christoph Studerfe718432014-09-01 18:21:18 +02003177 private void resetStandardTemplateWithActions(RemoteViews big) {
3178 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003179 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003180
3181 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3182 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3183
3184 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3185 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3186 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3187 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003188 }
3189
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003190 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003191 RemoteViews big = applyStandardTemplate(layoutId);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003192
Christoph Studerfe718432014-09-01 18:21:18 +02003193 resetStandardTemplateWithActions(big);
3194
Adrian Roose458aa82015-12-08 16:17:19 -08003195 boolean validRemoteInput = false;
3196
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003197 int N = mActions.size();
3198 if (N > 0) {
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003199 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003200 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003201 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003202 Action action = mActions.get(i);
3203 validRemoteInput |= hasValidRemoteInput(action);
3204
3205 final RemoteViews button = generateActionButton(action);
Adrian Roos9b123cf2016-02-04 14:55:57 -08003206 if (i == N - 1) {
3207 button.setViewLayoutWidth(com.android.internal.R.id.action0,
3208 ViewGroup.LayoutParams.MATCH_PARENT);
3209 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003210 big.addView(R.id.actions, button);
3211 }
3212 }
Adrian Roose458aa82015-12-08 16:17:19 -08003213
3214 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
3215 if (validRemoteInput && replyText != null
3216 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3217 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3218 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
3219
3220 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3221 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3222 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
3223
3224 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3225 big.setViewVisibility(
3226 R.id.notification_material_reply_text_3, View.VISIBLE);
3227 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
3228 }
3229 }
3230 }
3231
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003232 return big;
3233 }
3234
Adrian Roose458aa82015-12-08 16:17:19 -08003235 private boolean hasValidRemoteInput(Action action) {
3236 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
3237 // Weird actions
3238 return false;
3239 }
3240
3241 RemoteInput[] remoteInputs = action.getRemoteInputs();
3242 if (remoteInputs == null) {
3243 return false;
3244 }
3245
3246 for (RemoteInput r : remoteInputs) {
3247 CharSequence[] choices = r.getChoices();
3248 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
3249 return true;
3250 }
3251 }
3252 return false;
3253 }
3254
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003255 /**
3256 * Construct a RemoteViews for the final 1U notification layout. In order:
3257 * 1. Custom contentView from the caller
3258 * 2. Style's proposed content view
3259 * 3. Standard template view
3260 */
3261 public RemoteViews makeContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003262 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003263 return mN.contentView;
3264 } else if (mStyle != null) {
3265 final RemoteViews styleView = mStyle.makeContentView();
3266 if (styleView != null) {
3267 return styleView;
3268 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003269 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003270 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003271 }
3272
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003273 /**
3274 * Construct a RemoteViews for the final big notification layout.
3275 */
3276 public RemoteViews makeBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05003277 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08003278 if (mN.bigContentView != null
3279 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003280 return mN.bigContentView;
3281 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05003282 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08003283 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003284 } else if (mActions.size() != 0) {
3285 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05003286 }
3287 adaptNotificationHeaderForBigContentView(result);
3288 return result;
3289 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003290
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003291 /**
3292 * Construct a RemoteViews for the final notification header only
3293 *
3294 * @hide
3295 */
3296 public RemoteViews makeNotificationHeader() {
3297 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
3298 R.layout.notification_template_header);
3299 resetNotificationHeader(header);
3300 bindNotificationHeader(header);
3301 return header;
3302 }
3303
Selim Cinek29603462015-11-17 19:04:39 -08003304 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003305 if (result != null) {
3306 result.setViewVisibility(R.id.text_line_1, View.GONE);
3307 }
Selim Cinek29603462015-11-17 19:04:39 -08003308 }
3309
Selim Cinek850a8542015-11-11 11:48:36 -05003310 private void adaptNotificationHeaderForBigContentView(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003311 if (result != null) {
3312 result.setBoolean(R.id.notification_header, "setExpanded", true);
3313 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003314 }
3315
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003316 /**
3317 * Construct a RemoteViews for the final heads-up notification layout.
3318 */
3319 public RemoteViews makeHeadsUpContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003320 if (mN.headsUpContentView != null
3321 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003322 return mN.headsUpContentView;
3323 } else if (mStyle != null) {
3324 final RemoteViews styleView = mStyle.makeHeadsUpContentView();
3325 if (styleView != null) {
3326 return styleView;
3327 }
3328 } else if (mActions.size() == 0) {
3329 return null;
3330 }
3331
Chris Wren8fd39ec2014-02-27 17:43:26 -05003332
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003333 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003334 }
3335
Selim Cinek624c02db2015-12-14 21:00:02 -08003336 /**
3337 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
3338 *
3339 * @hide
3340 */
3341 public RemoteViews makePublicContentView() {
3342 if (mN.publicVersion != null) {
3343 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
3344 return builder.makeContentView();
3345 }
3346 Bundle savedBundle = mN.extras;
3347 Style style = mStyle;
3348 mStyle = null;
3349 Icon largeIcon = mN.mLargeIcon;
3350 mN.mLargeIcon = null;
3351 Bundle publicExtras = new Bundle();
3352 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
3353 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
3354 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
3355 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Selim Cinek81c23aa2016-02-25 16:23:13 -08003356 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN,
3357 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNTS_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08003358 publicExtras.putCharSequence(EXTRA_TITLE,
3359 mContext.getString(R.string.notification_hidden_text));
3360 mN.extras = publicExtras;
3361 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
3362 mN.extras = savedBundle;
3363 mN.mLargeIcon = largeIcon;
3364 mStyle = style;
3365 return publicView;
3366 }
3367
3368
Chris Wren8fd39ec2014-02-27 17:43:26 -05003369
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003370 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003371 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07003372 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003373 tombstone ? getActionTombstoneLayoutResource()
3374 : getActionLayoutResource());
Dan Sandler68079d52015-07-22 10:45:30 -04003375 final Icon ai = action.getIcon();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003376 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04003377 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003378 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003379 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003380 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003381 if (action.mRemoteInputs != null) {
3382 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
3383 }
3384 if (mN.color != COLOR_DEFAULT) {
3385 button.setTextColor(R.id.action0, mN.color);
3386 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003387 return button;
3388 }
3389
Joe Onoratocb109a02011-01-18 17:57:41 -08003390 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003391 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003392 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003393 */
3394 private boolean isLegacy() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003395 return getColorUtil() != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003396 }
3397
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003398 private CharSequence processLegacyText(CharSequence charSequence) {
3399 if (isLegacy()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003400 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003401 } else {
3402 return charSequence;
3403 }
3404 }
3405
Dan Sandler26e81cf2014-05-06 10:01:27 -04003406 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003407 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04003408 */
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003409 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView) {
Selim Cinekea4bef72015-12-02 15:51:10 -08003410 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
3411 if (colorable) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003412 contentView.setDrawableParameters(R.id.icon, false, -1, resolveColor(),
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003413 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003414
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003415 }
Selim Cinekea4bef72015-12-02 15:51:10 -08003416 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
3417 colorable ? resolveColor() : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003418 }
3419
Dan Sandler26e81cf2014-05-06 10:01:27 -04003420 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003421 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04003422 * if it's grayscale).
3423 */
3424 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003425 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3426 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003427 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003428 // resolve color will fall back to the default when legacy
3429 contentView.setDrawableParameters(R.id.icon, false, -1, resolveColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003430 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003431 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003432 }
3433
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003434 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003435 if (mN.color != COLOR_DEFAULT) {
3436 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02003437 }
Jorim Jaggi74419312014-06-10 20:57:21 +02003438 }
3439
Selim Cinek5bf069a2015-11-10 19:14:27 -05003440 int resolveColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003441 if (mN.color == COLOR_DEFAULT) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003442 return mContext.getColor(R.color.notification_icon_default_color);
Dan Sandler26e81cf2014-05-06 10:01:27 -04003443 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003444 return mN.color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003445 }
3446
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003447 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003448 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003449 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003450 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003451 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003452 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003453 mN.actions = new Action[mActions.size()];
3454 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003455 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003456 if (!mPersonList.isEmpty()) {
3457 mN.extras.putStringArray(EXTRA_PEOPLE,
3458 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003459 }
Selim Cinek247fa012016-02-18 09:50:48 -08003460 if (mN.bigContentView != null || mN.contentView != null
3461 || mN.headsUpContentView != null) {
3462 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
3463 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003464 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08003465 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003466
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003467 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003468 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003469 ApplicationInfo applicationInfo = n.extras.getParcelable(
3470 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003471 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05003472 if (applicationInfo != null) {
3473 try {
3474 builderContext = context.createApplicationContext(applicationInfo,
3475 Context.CONTEXT_RESTRICTED);
3476 } catch (NameNotFoundException e) {
3477 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
3478 builderContext = context; // try with our context
3479 }
3480 } else {
3481 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02003482 }
3483
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003484 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003485 }
3486
3487 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
Selim Cinek593610c2016-02-16 18:42:57 -08003488 Class<? extends Style>[] classes = new Class[] {
3489 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
Selim Cinek03eb3b72016-02-18 10:39:45 -08003490 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class };
Christoph Studer4600f9b2014-07-22 22:44:43 +02003491 for (Class<? extends Style> innerClass : classes) {
3492 if (templateClass.equals(innerClass.getName())) {
3493 return innerClass;
3494 }
3495 }
3496 return null;
3497 }
3498
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003499 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003500 * @deprecated Use {@link #build()} instead.
3501 */
3502 @Deprecated
3503 public Notification getNotification() {
3504 return build();
3505 }
3506
3507 /**
3508 * Combine all of the options that have been set and return a new {@link Notification}
3509 * object.
3510 */
3511 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003512 // first, add any extras from the calling code
3513 if (mUserExtras != null) {
3514 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003515 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003516
3517 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05003518 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02003519
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003520 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003521
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003522 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003523 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003524 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003525
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003526 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3527 if (mN.contentView == null) {
3528 mN.contentView = makeContentView();
3529 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
3530 mN.contentView.getSequenceNumber());
3531 }
3532 if (mN.bigContentView == null) {
3533 mN.bigContentView = makeBigContentView();
3534 if (mN.bigContentView != null) {
3535 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
3536 mN.bigContentView.getSequenceNumber());
3537 }
3538 }
3539 if (mN.headsUpContentView == null) {
3540 mN.headsUpContentView = makeHeadsUpContentView();
3541 if (mN.headsUpContentView != null) {
3542 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
3543 mN.headsUpContentView.getSequenceNumber());
3544 }
3545 }
3546 }
3547
Julia Reynolds4c0c2022016-02-02 15:11:59 -05003548 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
3549 mN.flags |= FLAG_SHOW_LIGHTS;
3550 }
3551
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003552 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003553 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003554
3555 /**
3556 * Apply this Builder to an existing {@link Notification} object.
3557 *
3558 * @hide
3559 */
3560 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003561 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003562 return n;
3563 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003564
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003565 /**
3566 * @hide
3567 */
3568 public static void stripForDelivery(Notification n) {
3569 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
3570 if (TextUtils.isEmpty(templateClass)) {
3571 return;
3572 }
3573 // Only strip views for known Styles because we won't know how to
3574 // re-create them otherwise.
3575 if (getNotificationStyleClass(templateClass) == null) {
3576 return;
3577 }
3578 // Get rid of unmodified BuilderRemoteViews.
3579 if (n.contentView instanceof BuilderRemoteViews &&
3580 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
3581 n.contentView.getSequenceNumber()) {
3582 n.contentView = null;
3583 n.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
3584 }
3585 if (n.bigContentView instanceof BuilderRemoteViews &&
3586 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
3587 n.bigContentView.getSequenceNumber()) {
3588 n.bigContentView = null;
3589 n.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
3590 }
3591 if (n.headsUpContentView instanceof BuilderRemoteViews &&
3592 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
3593 n.headsUpContentView.getSequenceNumber()) {
3594 n.headsUpContentView = null;
3595 n.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
3596 }
3597 }
3598
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003599 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003600 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003601 }
3602
3603 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003604 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003605 }
3606
3607 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003608 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003609 }
3610
3611 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003612 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003613 }
3614
3615 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003616 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003617 }
3618
3619 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003620 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003621 }
3622
3623 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003624 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003625 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003626 }
3627
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003628 /**
3629 * An object that can apply a rich notification style to a {@link Notification.Builder}
3630 * object.
3631 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003632 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003633 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003634
3635 /**
3636 * @hide
3637 */
3638 protected CharSequence mSummaryText = null;
3639
3640 /**
3641 * @hide
3642 */
3643 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003644
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003645 protected Builder mBuilder;
3646
Chris Wrend6297db2012-05-03 16:20:13 -04003647 /**
3648 * Overrides ContentTitle in the big form of the template.
3649 * This defaults to the value passed to setContentTitle().
3650 */
3651 protected void internalSetBigContentTitle(CharSequence title) {
3652 mBigContentTitle = title;
3653 }
3654
3655 /**
3656 * Set the first line of text after the detail section in the big form of the template.
3657 */
3658 protected void internalSetSummaryText(CharSequence cs) {
3659 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003660 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003661 }
3662
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003663 public void setBuilder(Builder builder) {
3664 if (mBuilder != builder) {
3665 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003666 if (mBuilder != null) {
3667 mBuilder.setStyle(this);
3668 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003669 }
3670 }
3671
Chris Wrend6297db2012-05-03 16:20:13 -04003672 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003673 if (mBuilder == null) {
3674 throw new IllegalArgumentException("Style requires a valid Builder object");
3675 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003676 }
Chris Wrend6297db2012-05-03 16:20:13 -04003677
3678 protected RemoteViews getStandardView(int layoutId) {
3679 checkBuilder();
3680
Christoph Studer4600f9b2014-07-22 22:44:43 +02003681 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003682 CharSequence oldBuilderContentTitle =
3683 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003684 if (mBigContentTitle != null) {
3685 mBuilder.setContentTitle(mBigContentTitle);
3686 }
3687
Chris Wrend6297db2012-05-03 16:20:13 -04003688 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3689
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003690 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003691
Chris Wrend6297db2012-05-03 16:20:13 -04003692 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3693 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003694 } else {
3695 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003696 }
3697
Chris Wrend6297db2012-05-03 16:20:13 -04003698 return contentView;
3699 }
3700
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003701 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003702 * Construct a Style-specific RemoteViews for the final 1U notification layout.
3703 * The default implementation has nothing additional to add.
3704 * @hide
3705 */
3706 public RemoteViews makeContentView() {
3707 return null;
3708 }
3709
3710 /**
3711 * Construct a Style-specific RemoteViews for the final big notification layout.
3712 * @hide
3713 */
3714 public RemoteViews makeBigContentView() {
3715 return null;
3716 }
3717
3718 /**
3719 * Construct a Style-specific RemoteViews for the final HUN layout.
3720 * @hide
3721 */
3722 public RemoteViews makeHeadsUpContentView() {
3723 return null;
3724 }
3725
3726 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003727 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003728 * @hide
3729 */
3730 public void addExtras(Bundle extras) {
3731 if (mSummaryTextSet) {
3732 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
3733 }
3734 if (mBigContentTitle != null) {
3735 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
3736 }
Chris Wren91ad5632013-06-05 15:05:57 -04003737 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003738 }
3739
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003740 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003741 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003742 * @hide
3743 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02003744 protected void restoreFromExtras(Bundle extras) {
3745 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
3746 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
3747 mSummaryTextSet = true;
3748 }
3749 if (extras.containsKey(EXTRA_TITLE_BIG)) {
3750 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
3751 }
3752 }
3753
3754
3755 /**
3756 * @hide
3757 */
3758 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003759 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003760 return wip;
3761 }
3762
Daniel Sandler0ec46202015-06-24 01:27:05 -04003763 /**
3764 * @hide
3765 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003766 public void purgeResources() {}
3767
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003768 /**
3769 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
3770 * attached to.
3771 *
3772 * @return the fully constructed Notification.
3773 */
3774 public Notification build() {
3775 checkBuilder();
3776 return mBuilder.build();
3777 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003778
3779 /**
3780 * @hide
3781 * @return true if the style positions the progress bar on the second line; false if the
3782 * style hides the progress bar
3783 */
3784 protected boolean hasProgress() {
3785 return true;
3786 }
Selim Cinek03d0d652015-11-13 13:18:09 -05003787
3788 /**
3789 * @hide
3790 * @return Whether we should put the summary be put into the notification header
3791 */
3792 public boolean hasSummaryInHeader() {
3793 return true;
3794 }
Selim Cinek593610c2016-02-16 18:42:57 -08003795
3796 /**
3797 * @hide
3798 * @return Whether custom content views are displayed inline in the style
3799 */
3800 public boolean displayCustomViewInline() {
3801 return false;
3802 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003803 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003804
3805 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003806 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08003807 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003808 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003809 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003810 * Notification notif = new Notification.Builder(mContext)
3811 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
3812 * .setContentText(subject)
3813 * .setSmallIcon(R.drawable.new_post)
3814 * .setLargeIcon(aBitmap)
3815 * .setStyle(new Notification.BigPictureStyle()
3816 * .bigPicture(aBigBitmap))
3817 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003818 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003819 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003820 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003821 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003822 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003823 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003824 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003825 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003826
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003827 public BigPictureStyle() {
3828 }
3829
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003830 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003831 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003832 }
3833
Chris Wrend6297db2012-05-03 16:20:13 -04003834 /**
3835 * Overrides ContentTitle in the big form of the template.
3836 * This defaults to the value passed to setContentTitle().
3837 */
3838 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003839 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003840 return this;
3841 }
3842
3843 /**
3844 * Set the first line of text after the detail section in the big form of the template.
3845 */
3846 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003847 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003848 return this;
3849 }
3850
Chris Wren0bd664d2012-08-01 13:56:56 -04003851 /**
3852 * Provide the bitmap to be used as the payload for the BigPicture notification.
3853 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003854 public BigPictureStyle bigPicture(Bitmap b) {
3855 mPicture = b;
3856 return this;
3857 }
3858
Chris Wren3745a3d2012-05-22 15:11:52 -04003859 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04003860 * Override the large icon when the big notification is shown.
3861 */
3862 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003863 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3864 }
3865
3866 /**
3867 * Override the large icon when the big notification is shown.
3868 */
3869 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04003870 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003871 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003872 return this;
3873 }
3874
Riley Andrews0394a0c2015-11-03 23:36:52 -08003875 /** @hide */
3876 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
3877
Daniel Sandler0ec46202015-06-24 01:27:05 -04003878 /**
3879 * @hide
3880 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003881 @Override
3882 public void purgeResources() {
3883 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08003884 if (mPicture != null &&
3885 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08003886 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003887 mPicture = mPicture.createAshmemBitmap();
3888 }
3889 if (mBigLargeIcon != null) {
3890 mBigLargeIcon.convertToAshmem();
3891 }
3892 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01003893
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003894 /**
3895 * @hide
3896 */
3897 public RemoteViews makeBigContentView() {
3898 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01003899 // This covers the following cases:
3900 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003901 // mN.mLargeIcon
3902 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04003903 Icon oldLargeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003904 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003905 oldLargeIcon = mBuilder.mN.mLargeIcon;
3906 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003907 }
3908
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003909 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05003910 if (mSummaryTextSet) {
3911 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinekc848c3a2016-01-13 15:27:30 -08003912 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05003913 }
Selim Cinek860b6da2015-12-16 19:02:19 -08003914 mBuilder.setContentMinHeight(contentView, mBuilder.mN.mLargeIcon != null);
Selim Cinek53e64a42015-11-16 10:40:56 -08003915
Christoph Studer5c510ee2014-12-15 16:32:27 +01003916 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003917 mBuilder.mN.mLargeIcon = oldLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003918 }
3919
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003920 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003921 return contentView;
3922 }
3923
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003924 /**
3925 * @hide
3926 */
3927 public void addExtras(Bundle extras) {
3928 super.addExtras(extras);
3929
3930 if (mBigLargeIconSet) {
3931 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
3932 }
3933 extras.putParcelable(EXTRA_PICTURE, mPicture);
3934 }
3935
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003936 /**
3937 * @hide
3938 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003939 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003940 protected void restoreFromExtras(Bundle extras) {
3941 super.restoreFromExtras(extras);
3942
3943 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01003944 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003945 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04003946 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003947 mPicture = extras.getParcelable(EXTRA_PICTURE);
3948 }
Selim Cinek03d0d652015-11-13 13:18:09 -05003949
3950 /**
3951 * @hide
3952 */
3953 @Override
3954 public boolean hasSummaryInHeader() {
3955 return false;
3956 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003957 }
3958
3959 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003960 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08003961 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003962 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003963 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003964 * Notification notif = new Notification.Builder(mContext)
3965 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3966 * .setContentText(subject)
3967 * .setSmallIcon(R.drawable.new_mail)
3968 * .setLargeIcon(aBitmap)
3969 * .setStyle(new Notification.BigTextStyle()
3970 * .bigText(aVeryLongString))
3971 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003972 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003973 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003974 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003975 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003976 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003977
3978 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08003979 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003980
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003981 private CharSequence mBigText;
3982
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003983 public BigTextStyle() {
3984 }
3985
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003986 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003987 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003988 }
3989
Chris Wrend6297db2012-05-03 16:20:13 -04003990 /**
3991 * Overrides ContentTitle in the big form of the template.
3992 * This defaults to the value passed to setContentTitle().
3993 */
3994 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003995 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003996 return this;
3997 }
3998
3999 /**
4000 * Set the first line of text after the detail section in the big form of the template.
4001 */
4002 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004003 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004004 return this;
4005 }
4006
Chris Wren0bd664d2012-08-01 13:56:56 -04004007 /**
4008 * Provide the longer text to be displayed in the big form of the
4009 * template in place of the content text.
4010 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004011 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004012 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004013 return this;
4014 }
4015
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004016 /**
4017 * @hide
4018 */
4019 public void addExtras(Bundle extras) {
4020 super.addExtras(extras);
4021
Christoph Studer4600f9b2014-07-22 22:44:43 +02004022 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4023 }
4024
4025 /**
4026 * @hide
4027 */
4028 @Override
4029 protected void restoreFromExtras(Bundle extras) {
4030 super.restoreFromExtras(extras);
4031
4032 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004033 }
4034
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004035 /**
4036 * @hide
4037 */
4038 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004039
4040 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004041 CharSequence oldBuilderContentText =
4042 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
4043 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004044
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004045 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004046
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004047 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004048
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004049 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
4050 contentView.setTextViewText(R.id.big_text, bigTextText);
4051 contentView.setViewVisibility(R.id.big_text,
4052 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004053 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines());
Selim Cinek4fb12d32015-11-19 18:10:48 -08004054 contentView.setBoolean(R.id.big_text, "setHasImage", mBuilder.mN.mLargeIcon != null);
4055
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004056 return contentView;
4057 }
4058
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004059 private int calculateMaxLines() {
4060 int lineCount = MAX_LINES;
4061 boolean hasActions = mBuilder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004062 if (hasActions) {
4063 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4064 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004065 return lineCount;
4066 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004067 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004068
4069 /**
4070 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08004071 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004072 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04004073 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004074 * Notification notif = new Notification.Builder(mContext)
4075 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
4076 * .setContentText(subject)
4077 * .setSmallIcon(R.drawable.new_mail)
4078 * .setLargeIcon(aBitmap)
4079 * .setStyle(new Notification.InboxStyle()
4080 * .addLine(str1)
4081 * .addLine(str2)
4082 * .setContentTitle(&quot;&quot;)
4083 * .setSummaryText(&quot;+3 more&quot;))
4084 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04004085 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004086 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04004087 * @see Notification#bigContentView
4088 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004089 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004090 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
4091
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004092 public InboxStyle() {
4093 }
4094
Daniel Sandler879c5e02012-04-17 16:46:51 -04004095 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004096 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004097 }
4098
Chris Wrend6297db2012-05-03 16:20:13 -04004099 /**
4100 * Overrides ContentTitle in the big form of the template.
4101 * This defaults to the value passed to setContentTitle().
4102 */
4103 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004104 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004105 return this;
4106 }
4107
4108 /**
4109 * Set the first line of text after the detail section in the big form of the template.
4110 */
4111 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004112 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004113 return this;
4114 }
4115
Chris Wren0bd664d2012-08-01 13:56:56 -04004116 /**
4117 * Append a line to the digest section of the Inbox notification.
4118 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04004119 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004120 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04004121 return this;
4122 }
4123
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004124 /**
4125 * @hide
4126 */
4127 public void addExtras(Bundle extras) {
4128 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004129
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004130 CharSequence[] a = new CharSequence[mTexts.size()];
4131 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
4132 }
4133
Christoph Studer4600f9b2014-07-22 22:44:43 +02004134 /**
4135 * @hide
4136 */
4137 @Override
4138 protected void restoreFromExtras(Bundle extras) {
4139 super.restoreFromExtras(extras);
4140
4141 mTexts.clear();
4142 if (extras.containsKey(EXTRA_TEXT_LINES)) {
4143 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
4144 }
4145 }
4146
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004147 /**
4148 * @hide
4149 */
4150 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004151 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02004152 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004153 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
4154 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004155
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004156 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04004157
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004158 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004159
Chris Wrend6297db2012-05-03 16:20:13 -04004160 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 -04004161 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04004162
Chris Wren4ed80d52012-05-17 09:30:03 -04004163 // Make sure all rows are gone in case we reuse a view.
4164 for (int rowId : rowIds) {
4165 contentView.setViewVisibility(rowId, View.GONE);
4166 }
4167
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004168 final boolean largeText =
4169 mBuilder.mContext.getResources().getConfiguration().fontScale > 1f;
4170 final float subTextSize = mBuilder.mContext.getResources().getDimensionPixelSize(
4171 R.dimen.notification_subtext_size);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004172 int i=0;
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004173 final float density = mBuilder.mContext.getResources().getDisplayMetrics().density;
4174 int topPadding = (int) (5 * density);
4175 int bottomPadding = (int) (13 * density);
Selim Cinek247fa012016-02-18 09:50:48 -08004176 boolean first = true;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004177 while (i < mTexts.size() && i < rowIds.length) {
4178 CharSequence str = mTexts.get(i);
4179 if (str != null && !str.equals("")) {
4180 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004181 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004182 if (largeText) {
4183 contentView.setTextViewTextSize(rowIds[i], TypedValue.COMPLEX_UNIT_PX,
4184 subTextSize);
4185 }
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004186 contentView.setViewPadding(rowIds[i], 0, topPadding, 0,
4187 i == rowIds.length - 1 || i == mTexts.size() - 1 ? bottomPadding : 0);
Selim Cinek247fa012016-02-18 09:50:48 -08004188 handleInboxImageMargin(contentView, rowIds[i], first);
4189 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004190 }
4191 i++;
4192 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004193
Selim Cinek1e0bf612015-11-20 15:57:26 -08004194
Daniel Sandler879c5e02012-04-17 16:46:51 -04004195 return contentView;
4196 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004197
Selim Cinek247fa012016-02-18 09:50:48 -08004198 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08004199 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08004200 if (first) {
4201 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
4202 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4203 boolean hasProgress = max != 0 || ind;
4204 if (mBuilder.mN.mLargeIcon != null && !hasProgress) {
4205 endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4206 R.dimen.notification_content_picture_margin);
4207 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004208 }
4209 contentView.setViewLayoutMarginEnd(id, endMargin);
4210 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004211 }
Dan Sandler842dd772014-05-15 09:36:47 -04004212
4213 /**
4214 * Notification style for media playback notifications.
4215 *
4216 * In the expanded form, {@link Notification#bigContentView}, up to 5
4217 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04004218 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04004219 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
4220 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
4221 * treated as album artwork.
4222 *
4223 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
4224 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01004225 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04004226 * in the standard view alongside the usual content.
4227 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004228 * Notifications created with MediaStyle will have their category set to
4229 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
4230 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
4231 *
Jeff Browndba34ba2014-06-24 20:46:03 -07004232 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
4233 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04004234 * the System UI can identify this as a notification representing an active media session
4235 * and respond accordingly (by showing album artwork in the lockscreen, for example).
4236 *
4237 * To use this style with your Notification, feed it to
4238 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4239 * <pre class="prettyprint">
4240 * Notification noti = new Notification.Builder()
4241 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01004242 * .setContentTitle(&quot;Track title&quot;)
4243 * .setContentText(&quot;Artist - Album&quot;)
4244 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004245 * .setStyle(<b>new Notification.MediaStyle()</b>
4246 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04004247 * .build();
4248 * </pre>
4249 *
4250 * @see Notification#bigContentView
4251 */
4252 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004253 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04004254 static final int MAX_MEDIA_BUTTONS = 5;
4255
4256 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07004257 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04004258
4259 public MediaStyle() {
4260 }
4261
4262 public MediaStyle(Builder builder) {
4263 setBuilder(builder);
4264 }
4265
4266 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004267 * 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 -04004268 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004269 *
4270 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04004271 */
4272 public MediaStyle setShowActionsInCompactView(int...actions) {
4273 mActionsToShowInCompact = actions;
4274 return this;
4275 }
4276
4277 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07004278 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
4279 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04004280 */
Jeff Browndba34ba2014-06-24 20:46:03 -07004281 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04004282 mToken = token;
4283 return this;
4284 }
4285
Christoph Studer4600f9b2014-07-22 22:44:43 +02004286 /**
4287 * @hide
4288 */
Dan Sandler842dd772014-05-15 09:36:47 -04004289 @Override
4290 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004291 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004292 if (wip.category == null) {
4293 wip.category = Notification.CATEGORY_TRANSPORT;
4294 }
Dan Sandler842dd772014-05-15 09:36:47 -04004295 return wip;
4296 }
4297
Christoph Studer4600f9b2014-07-22 22:44:43 +02004298 /**
4299 * @hide
4300 */
4301 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004302 public RemoteViews makeContentView() {
4303 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004304 }
4305
4306 /**
4307 * @hide
4308 */
4309 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004310 public RemoteViews makeBigContentView() {
4311 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004312 }
4313
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004314 /**
4315 * @hide
4316 */
4317 @Override
4318 public RemoteViews makeHeadsUpContentView() {
4319 RemoteViews expanded = makeMediaBigContentView();
4320 return expanded != null ? expanded : makeMediaContentView();
4321 }
4322
Dan Sandler842dd772014-05-15 09:36:47 -04004323 /** @hide */
4324 @Override
4325 public void addExtras(Bundle extras) {
4326 super.addExtras(extras);
4327
4328 if (mToken != null) {
4329 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
4330 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01004331 if (mActionsToShowInCompact != null) {
4332 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
4333 }
Dan Sandler842dd772014-05-15 09:36:47 -04004334 }
4335
Christoph Studer4600f9b2014-07-22 22:44:43 +02004336 /**
4337 * @hide
4338 */
4339 @Override
4340 protected void restoreFromExtras(Bundle extras) {
4341 super.restoreFromExtras(extras);
4342
4343 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
4344 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
4345 }
4346 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
4347 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
4348 }
4349 }
4350
Selim Cinek5bf069a2015-11-10 19:14:27 -05004351 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04004352 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004353 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07004354 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04004355 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05004356 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
4357 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04004358 if (!tombstone) {
4359 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
4360 }
4361 button.setContentDescription(R.id.action0, action.title);
4362 return button;
4363 }
4364
4365 private RemoteViews makeMediaContentView() {
4366 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004367 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04004368
4369 final int numActions = mBuilder.mActions.size();
4370 final int N = mActionsToShowInCompact == null
4371 ? 0
4372 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4373 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004374 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04004375 for (int i = 0; i < N; i++) {
4376 if (i >= numActions) {
4377 throw new IllegalArgumentException(String.format(
4378 "setShowActionsInCompactView: action %d out of bounds (max %d)",
4379 i, numActions - 1));
4380 }
4381
4382 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05004383 final RemoteViews button = generateMediaActionButton(action,
4384 mBuilder.resolveColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004385 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004386 }
4387 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08004388 handleImage(view);
4389 // handle the content margin
Selim Cinek247fa012016-02-18 09:50:48 -08004390 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4391 R.dimen.notification_content_margin_end);;
Selim Cinekfdc738f2016-01-27 20:04:27 -08004392 if (mBuilder.mN.mLargeIcon != null) {
Selim Cinek247fa012016-02-18 09:50:48 -08004393 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
4394 R.dimen.notification_content_picture_margin);
Selim Cinekfdc738f2016-01-27 20:04:27 -08004395 }
4396 view.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04004397 return view;
4398 }
4399
4400 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004401 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004402 // Dont add an expanded view if there is no more content to be revealed
4403 int actionsInCompact = mActionsToShowInCompact == null
4404 ? 0
4405 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4406 if (mBuilder.mN.mLargeIcon == null && actionCount <= actionsInCompact) {
4407 return null;
4408 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004409 RemoteViews big = mBuilder.applyStandardTemplate(
4410 R.layout.notification_template_material_big_media,
4411 false);
Dan Sandler842dd772014-05-15 09:36:47 -04004412
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004413 if (actionCount > 0) {
4414 big.removeAllViews(com.android.internal.R.id.media_actions);
4415 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05004416 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
4417 mBuilder.resolveColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004418 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004419 }
4420 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004421 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04004422 return big;
4423 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004424
Selim Cinek5bf069a2015-11-10 19:14:27 -05004425 private void handleImage(RemoteViews contentView) {
4426 if (mBuilder.mN.mLargeIcon != null) {
4427 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004428 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004429 }
4430 }
4431
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004432 /**
4433 * @hide
4434 */
4435 @Override
4436 protected boolean hasProgress() {
4437 return false;
4438 }
Dan Sandler842dd772014-05-15 09:36:47 -04004439 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004440
Selim Cinek593610c2016-02-16 18:42:57 -08004441 /**
4442 * Notification style for custom views that are decorated by the system
4443 *
4444 * <p>Instead of providing a notification that is completely custom, a developer can set this
4445 * style and still obtain system decorations like the notification header with the expand
4446 * affordance and actions.
4447 *
4448 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
4449 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
4450 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
4451 * corresponding custom views to display.
4452 *
4453 * To use this style with your Notification, feed it to
4454 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4455 * <pre class="prettyprint">
4456 * Notification noti = new Notification.Builder()
4457 * .setSmallIcon(R.drawable.ic_stat_player)
4458 * .setLargeIcon(albumArtBitmap))
4459 * .setCustomContentView(contentView);
4460 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
4461 * .build();
4462 * </pre>
4463 */
4464 public static class DecoratedCustomViewStyle extends Style {
4465
4466 public DecoratedCustomViewStyle() {
4467 }
4468
4469 public DecoratedCustomViewStyle(Builder builder) {
4470 setBuilder(builder);
4471 }
4472
4473 /**
4474 * @hide
4475 */
4476 public boolean displayCustomViewInline() {
4477 return true;
4478 }
4479
4480 /**
4481 * @hide
4482 */
4483 @Override
4484 public RemoteViews makeContentView() {
4485 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
4486 }
4487
4488 /**
4489 * @hide
4490 */
4491 @Override
4492 public RemoteViews makeBigContentView() {
4493 return makeDecoratedBigContentView();
4494 }
4495
4496 /**
4497 * @hide
4498 */
4499 @Override
4500 public RemoteViews makeHeadsUpContentView() {
4501 return makeDecoratedHeadsUpContentView();
4502 }
4503
Selim Cinek593610c2016-02-16 18:42:57 -08004504 private RemoteViews makeDecoratedHeadsUpContentView() {
4505 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
4506 ? mBuilder.mN.contentView
4507 : mBuilder.mN.headsUpContentView;
4508 if (mBuilder.mActions.size() == 0) {
4509 return makeStandardTemplateWithCustomContent(headsUpContentView);
4510 }
4511 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
4512 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004513 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08004514 return remoteViews;
4515 }
4516
Selim Cinek593610c2016-02-16 18:42:57 -08004517 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
4518 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
4519 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004520 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08004521 return remoteViews;
4522 }
4523
Selim Cinek593610c2016-02-16 18:42:57 -08004524 private RemoteViews makeDecoratedBigContentView() {
4525 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
4526 ? mBuilder.mN.contentView
4527 : mBuilder.mN.bigContentView;
4528 if (mBuilder.mActions.size() == 0) {
4529 return makeStandardTemplateWithCustomContent(bigContentView);
4530 }
4531 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
4532 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004533 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08004534 return remoteViews;
4535 }
Selim Cinek247fa012016-02-18 09:50:48 -08004536
4537 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
4538 RemoteViews customContent) {
4539 remoteViews.removeAllViews(R.id.notification_main_column);
4540 remoteViews.addView(R.id.notification_main_column, customContent);
4541 // also update the end margin if there is an image
4542 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4543 R.dimen.notification_content_margin_end);
4544 if (mBuilder.mN.mLargeIcon != null) {
4545 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
4546 R.dimen.notification_content_picture_margin);
4547 }
4548 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
4549 }
Selim Cinek593610c2016-02-16 18:42:57 -08004550 }
4551
Selim Cinek03eb3b72016-02-18 10:39:45 -08004552 /**
4553 * Notification style for media custom views that are decorated by the system
4554 *
4555 * <p>Instead of providing a media notification that is completely custom, a developer can set
4556 * this style and still obtain system decorations like the notification header with the expand
4557 * affordance and actions.
4558 *
4559 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
4560 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
4561 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
4562 * corresponding custom views to display.
4563 *
4564 * To use this style with your Notification, feed it to
4565 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4566 * <pre class="prettyprint">
4567 * Notification noti = new Notification.Builder()
4568 * .setSmallIcon(R.drawable.ic_stat_player)
4569 * .setLargeIcon(albumArtBitmap))
4570 * .setCustomContentView(contentView);
4571 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
4572 * .setMediaSession(mySession))
4573 * .build();
4574 * </pre>
4575 *
4576 * @see android.app.Notification.DecoratedCustomViewStyle
4577 * @see android.app.Notification.MediaStyle
4578 */
4579 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
4580
4581 public DecoratedMediaCustomViewStyle() {
4582 }
4583
4584 public DecoratedMediaCustomViewStyle(Builder builder) {
4585 setBuilder(builder);
4586 }
4587
4588 /**
4589 * @hide
4590 */
4591 public boolean displayCustomViewInline() {
4592 return true;
4593 }
4594
4595 /**
4596 * @hide
4597 */
4598 @Override
4599 public RemoteViews makeContentView() {
4600 RemoteViews remoteViews = super.makeContentView();
4601 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
4602 mBuilder.mN.contentView);
4603 }
4604
4605 /**
4606 * @hide
4607 */
4608 @Override
4609 public RemoteViews makeBigContentView() {
4610 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
4611 ? mBuilder.mN.bigContentView
4612 : mBuilder.mN.contentView;
4613 return makeBigContentViewWithCustomContent(customRemoteView);
4614 }
4615
4616 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
4617 RemoteViews remoteViews = super.makeBigContentView();
4618 if (remoteViews != null) {
4619 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
4620 customRemoteView);
4621 } else if (customRemoteView != mBuilder.mN.contentView){
4622 remoteViews = super.makeContentView();
4623 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
4624 customRemoteView);
4625 } else {
4626 return null;
4627 }
4628 }
4629
4630 /**
4631 * @hide
4632 */
4633 @Override
4634 public RemoteViews makeHeadsUpContentView() {
4635 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
4636 ? mBuilder.mN.headsUpContentView
4637 : mBuilder.mN.contentView;
4638 return makeBigContentViewWithCustomContent(customRemoteView);
4639 }
4640
4641 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
4642 RemoteViews customContent) {
4643 remoteViews.removeAllViews(id);
4644 remoteViews.addView(id, customContent);
4645 return remoteViews;
4646 }
4647 }
4648
Christoph Studer4600f9b2014-07-22 22:44:43 +02004649 // When adding a new Style subclass here, don't forget to update
4650 // Builder.getNotificationStyleClass.
4651
Griff Hazen61a9e862014-05-22 16:05:19 -07004652 /**
4653 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
4654 * metadata or change options on a notification builder.
4655 */
4656 public interface Extender {
4657 /**
4658 * Apply this extender to a notification builder.
4659 * @param builder the builder to be modified.
4660 * @return the build object for chaining.
4661 */
4662 public Builder extend(Builder builder);
4663 }
4664
4665 /**
4666 * Helper class to add wearable extensions to notifications.
4667 * <p class="note"> See
4668 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
4669 * for Android Wear</a> for more information on how to use this class.
4670 * <p>
4671 * To create a notification with wearable extensions:
4672 * <ol>
4673 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
4674 * properties.
4675 * <li>Create a {@link android.app.Notification.WearableExtender}.
4676 * <li>Set wearable-specific properties using the
4677 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
4678 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
4679 * notification.
4680 * <li>Post the notification to the notification system with the
4681 * {@code NotificationManager.notify(...)} methods.
4682 * </ol>
4683 *
4684 * <pre class="prettyprint">
4685 * Notification notif = new Notification.Builder(mContext)
4686 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4687 * .setContentText(subject)
4688 * .setSmallIcon(R.drawable.new_mail)
4689 * .extend(new Notification.WearableExtender()
4690 * .setContentIcon(R.drawable.new_mail))
4691 * .build();
4692 * NotificationManager notificationManger =
4693 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
4694 * notificationManger.notify(0, notif);</pre>
4695 *
4696 * <p>Wearable extensions can be accessed on an existing notification by using the
4697 * {@code WearableExtender(Notification)} constructor,
4698 * and then using the {@code get} methods to access values.
4699 *
4700 * <pre class="prettyprint">
4701 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
4702 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07004703 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004704 */
4705 public static final class WearableExtender implements Extender {
4706 /**
4707 * Sentinel value for an action index that is unset.
4708 */
4709 public static final int UNSET_ACTION_INDEX = -1;
4710
4711 /**
4712 * Size value for use with {@link #setCustomSizePreset} to show this notification with
4713 * default sizing.
4714 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07004715 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07004716 * on their content.
4717 */
4718 public static final int SIZE_DEFAULT = 0;
4719
4720 /**
4721 * Size value for use with {@link #setCustomSizePreset} to show this notification
4722 * with an extra small size.
4723 * <p>This value is only applicable for custom display notifications created using
4724 * {@link #setDisplayIntent}.
4725 */
4726 public static final int SIZE_XSMALL = 1;
4727
4728 /**
4729 * Size value for use with {@link #setCustomSizePreset} to show this notification
4730 * with a small size.
4731 * <p>This value is only applicable for custom display notifications created using
4732 * {@link #setDisplayIntent}.
4733 */
4734 public static final int SIZE_SMALL = 2;
4735
4736 /**
4737 * Size value for use with {@link #setCustomSizePreset} to show this notification
4738 * with a medium size.
4739 * <p>This value is only applicable for custom display notifications created using
4740 * {@link #setDisplayIntent}.
4741 */
4742 public static final int SIZE_MEDIUM = 3;
4743
4744 /**
4745 * Size value for use with {@link #setCustomSizePreset} to show this notification
4746 * with a large size.
4747 * <p>This value is only applicable for custom display notifications created using
4748 * {@link #setDisplayIntent}.
4749 */
4750 public static final int SIZE_LARGE = 4;
4751
Griff Hazend5f11f92014-05-27 15:40:09 -07004752 /**
4753 * Size value for use with {@link #setCustomSizePreset} to show this notification
4754 * full screen.
4755 * <p>This value is only applicable for custom display notifications created using
4756 * {@link #setDisplayIntent}.
4757 */
4758 public static final int SIZE_FULL_SCREEN = 5;
4759
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004760 /**
4761 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
4762 * short amount of time when this notification is displayed on the screen. This
4763 * is the default value.
4764 */
4765 public static final int SCREEN_TIMEOUT_SHORT = 0;
4766
4767 /**
4768 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
4769 * for a longer amount of time when this notification is displayed on the screen.
4770 */
4771 public static final int SCREEN_TIMEOUT_LONG = -1;
4772
Griff Hazen61a9e862014-05-22 16:05:19 -07004773 /** Notification extra which contains wearable extensions */
4774 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
4775
Pete Gastaf6781d2014-10-07 15:17:05 -04004776 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07004777 private static final String KEY_ACTIONS = "actions";
4778 private static final String KEY_FLAGS = "flags";
4779 private static final String KEY_DISPLAY_INTENT = "displayIntent";
4780 private static final String KEY_PAGES = "pages";
4781 private static final String KEY_BACKGROUND = "background";
4782 private static final String KEY_CONTENT_ICON = "contentIcon";
4783 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
4784 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
4785 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
4786 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
4787 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004788 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Griff Hazen61a9e862014-05-22 16:05:19 -07004789
4790 // Flags bitwise-ored to mFlags
4791 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
4792 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
4793 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
4794 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004795 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Griff Hazen61a9e862014-05-22 16:05:19 -07004796
4797 // Default value for flags integer
4798 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
4799
4800 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
4801 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
4802
4803 private ArrayList<Action> mActions = new ArrayList<Action>();
4804 private int mFlags = DEFAULT_FLAGS;
4805 private PendingIntent mDisplayIntent;
4806 private ArrayList<Notification> mPages = new ArrayList<Notification>();
4807 private Bitmap mBackground;
4808 private int mContentIcon;
4809 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
4810 private int mContentActionIndex = UNSET_ACTION_INDEX;
4811 private int mCustomSizePreset = SIZE_DEFAULT;
4812 private int mCustomContentHeight;
4813 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004814 private int mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004815
4816 /**
4817 * Create a {@link android.app.Notification.WearableExtender} with default
4818 * options.
4819 */
4820 public WearableExtender() {
4821 }
4822
4823 public WearableExtender(Notification notif) {
4824 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
4825 if (wearableBundle != null) {
4826 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
4827 if (actions != null) {
4828 mActions.addAll(actions);
4829 }
4830
4831 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
4832 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
4833
4834 Notification[] pages = getNotificationArrayFromBundle(
4835 wearableBundle, KEY_PAGES);
4836 if (pages != null) {
4837 Collections.addAll(mPages, pages);
4838 }
4839
4840 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
4841 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
4842 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
4843 DEFAULT_CONTENT_ICON_GRAVITY);
4844 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
4845 UNSET_ACTION_INDEX);
4846 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
4847 SIZE_DEFAULT);
4848 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
4849 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004850 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Griff Hazen61a9e862014-05-22 16:05:19 -07004851 }
4852 }
4853
4854 /**
4855 * Apply wearable extensions to a notification that is being built. This is typically
4856 * called by the {@link android.app.Notification.Builder#extend} method of
4857 * {@link android.app.Notification.Builder}.
4858 */
4859 @Override
4860 public Notification.Builder extend(Notification.Builder builder) {
4861 Bundle wearableBundle = new Bundle();
4862
4863 if (!mActions.isEmpty()) {
4864 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
4865 }
4866 if (mFlags != DEFAULT_FLAGS) {
4867 wearableBundle.putInt(KEY_FLAGS, mFlags);
4868 }
4869 if (mDisplayIntent != null) {
4870 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
4871 }
4872 if (!mPages.isEmpty()) {
4873 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
4874 new Notification[mPages.size()]));
4875 }
4876 if (mBackground != null) {
4877 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
4878 }
4879 if (mContentIcon != 0) {
4880 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
4881 }
4882 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
4883 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
4884 }
4885 if (mContentActionIndex != UNSET_ACTION_INDEX) {
4886 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
4887 mContentActionIndex);
4888 }
4889 if (mCustomSizePreset != SIZE_DEFAULT) {
4890 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
4891 }
4892 if (mCustomContentHeight != 0) {
4893 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
4894 }
4895 if (mGravity != DEFAULT_GRAVITY) {
4896 wearableBundle.putInt(KEY_GRAVITY, mGravity);
4897 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004898 if (mHintScreenTimeout != 0) {
4899 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
4900 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004901
4902 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
4903 return builder;
4904 }
4905
4906 @Override
4907 public WearableExtender clone() {
4908 WearableExtender that = new WearableExtender();
4909 that.mActions = new ArrayList<Action>(this.mActions);
4910 that.mFlags = this.mFlags;
4911 that.mDisplayIntent = this.mDisplayIntent;
4912 that.mPages = new ArrayList<Notification>(this.mPages);
4913 that.mBackground = this.mBackground;
4914 that.mContentIcon = this.mContentIcon;
4915 that.mContentIconGravity = this.mContentIconGravity;
4916 that.mContentActionIndex = this.mContentActionIndex;
4917 that.mCustomSizePreset = this.mCustomSizePreset;
4918 that.mCustomContentHeight = this.mCustomContentHeight;
4919 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004920 that.mHintScreenTimeout = this.mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004921 return that;
4922 }
4923
4924 /**
4925 * Add a wearable action to this notification.
4926 *
4927 * <p>When wearable actions are added using this method, the set of actions that
4928 * show on a wearable device splits from devices that only show actions added
4929 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4930 * of which actions display on different devices.
4931 *
4932 * @param action the action to add to this notification
4933 * @return this object for method chaining
4934 * @see android.app.Notification.Action
4935 */
4936 public WearableExtender addAction(Action action) {
4937 mActions.add(action);
4938 return this;
4939 }
4940
4941 /**
4942 * Adds wearable actions to this notification.
4943 *
4944 * <p>When wearable actions are added using this method, the set of actions that
4945 * show on a wearable device splits from devices that only show actions added
4946 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4947 * of which actions display on different devices.
4948 *
4949 * @param actions the actions to add to this notification
4950 * @return this object for method chaining
4951 * @see android.app.Notification.Action
4952 */
4953 public WearableExtender addActions(List<Action> actions) {
4954 mActions.addAll(actions);
4955 return this;
4956 }
4957
4958 /**
4959 * Clear all wearable actions present on this builder.
4960 * @return this object for method chaining.
4961 * @see #addAction
4962 */
4963 public WearableExtender clearActions() {
4964 mActions.clear();
4965 return this;
4966 }
4967
4968 /**
4969 * Get the wearable actions present on this notification.
4970 */
4971 public List<Action> getActions() {
4972 return mActions;
4973 }
4974
4975 /**
4976 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07004977 * this notification. The {@link PendingIntent} provided should be for an activity.
4978 *
4979 * <pre class="prettyprint">
4980 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
4981 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
4982 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
4983 * Notification notif = new Notification.Builder(context)
4984 * .extend(new Notification.WearableExtender()
4985 * .setDisplayIntent(displayPendingIntent)
4986 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
4987 * .build();</pre>
4988 *
4989 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07004990 * should have an empty task affinity. It is also recommended to use the device
4991 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07004992 *
4993 * <p>Example AndroidManifest.xml entry:
4994 * <pre class="prettyprint">
4995 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
4996 * android:exported=&quot;true&quot;
4997 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07004998 * android:taskAffinity=&quot;&quot;
4999 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07005000 *
5001 * @param intent the {@link PendingIntent} for an activity
5002 * @return this object for method chaining
5003 * @see android.app.Notification.WearableExtender#getDisplayIntent
5004 */
5005 public WearableExtender setDisplayIntent(PendingIntent intent) {
5006 mDisplayIntent = intent;
5007 return this;
5008 }
5009
5010 /**
5011 * Get the intent to launch inside of an activity view when displaying this
5012 * notification. This {@code PendingIntent} should be for an activity.
5013 */
5014 public PendingIntent getDisplayIntent() {
5015 return mDisplayIntent;
5016 }
5017
5018 /**
5019 * Add an additional page of content to display with this notification. The current
5020 * notification forms the first page, and pages added using this function form
5021 * subsequent pages. This field can be used to separate a notification into multiple
5022 * sections.
5023 *
5024 * @param page the notification to add as another page
5025 * @return this object for method chaining
5026 * @see android.app.Notification.WearableExtender#getPages
5027 */
5028 public WearableExtender addPage(Notification page) {
5029 mPages.add(page);
5030 return this;
5031 }
5032
5033 /**
5034 * Add additional pages of content to display with this notification. The current
5035 * notification forms the first page, and pages added using this function form
5036 * subsequent pages. This field can be used to separate a notification into multiple
5037 * sections.
5038 *
5039 * @param pages a list of notifications
5040 * @return this object for method chaining
5041 * @see android.app.Notification.WearableExtender#getPages
5042 */
5043 public WearableExtender addPages(List<Notification> pages) {
5044 mPages.addAll(pages);
5045 return this;
5046 }
5047
5048 /**
5049 * Clear all additional pages present on this builder.
5050 * @return this object for method chaining.
5051 * @see #addPage
5052 */
5053 public WearableExtender clearPages() {
5054 mPages.clear();
5055 return this;
5056 }
5057
5058 /**
5059 * Get the array of additional pages of content for displaying this notification. The
5060 * current notification forms the first page, and elements within this array form
5061 * subsequent pages. This field can be used to separate a notification into multiple
5062 * sections.
5063 * @return the pages for this notification
5064 */
5065 public List<Notification> getPages() {
5066 return mPages;
5067 }
5068
5069 /**
5070 * Set a background image to be displayed behind the notification content.
5071 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5072 * will work with any notification style.
5073 *
5074 * @param background the background bitmap
5075 * @return this object for method chaining
5076 * @see android.app.Notification.WearableExtender#getBackground
5077 */
5078 public WearableExtender setBackground(Bitmap background) {
5079 mBackground = background;
5080 return this;
5081 }
5082
5083 /**
5084 * Get a background image to be displayed behind the notification content.
5085 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5086 * will work with any notification style.
5087 *
5088 * @return the background image
5089 * @see android.app.Notification.WearableExtender#setBackground
5090 */
5091 public Bitmap getBackground() {
5092 return mBackground;
5093 }
5094
5095 /**
5096 * Set an icon that goes with the content of this notification.
5097 */
5098 public WearableExtender setContentIcon(int icon) {
5099 mContentIcon = icon;
5100 return this;
5101 }
5102
5103 /**
5104 * Get an icon that goes with the content of this notification.
5105 */
5106 public int getContentIcon() {
5107 return mContentIcon;
5108 }
5109
5110 /**
5111 * Set the gravity that the content icon should have within the notification display.
5112 * Supported values include {@link android.view.Gravity#START} and
5113 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5114 * @see #setContentIcon
5115 */
5116 public WearableExtender setContentIconGravity(int contentIconGravity) {
5117 mContentIconGravity = contentIconGravity;
5118 return this;
5119 }
5120
5121 /**
5122 * Get the gravity that the content icon should have within the notification display.
5123 * Supported values include {@link android.view.Gravity#START} and
5124 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5125 * @see #getContentIcon
5126 */
5127 public int getContentIconGravity() {
5128 return mContentIconGravity;
5129 }
5130
5131 /**
5132 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07005133 * this notification. This action will no longer display separately from the
5134 * notification's content.
5135 *
Griff Hazenca48d352014-05-28 22:37:13 -07005136 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005137 * set, although the list of available actions comes from the main notification and not
5138 * from the child page's notification.
5139 *
5140 * @param actionIndex The index of the action to hoist onto the current notification page.
5141 * If wearable actions were added to the main notification, this index
5142 * will apply to that list, otherwise it will apply to the regular
5143 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07005144 */
5145 public WearableExtender setContentAction(int actionIndex) {
5146 mContentActionIndex = actionIndex;
5147 return this;
5148 }
5149
5150 /**
Griff Hazenca48d352014-05-28 22:37:13 -07005151 * Get the index of the notification action, if any, that was specified as being clickable
5152 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07005153 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07005154 *
Griff Hazenca48d352014-05-28 22:37:13 -07005155 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005156 * set, although the list of available actions comes from the main notification and not
5157 * from the child page's notification.
5158 *
5159 * <p>If wearable specific actions were added to the main notification, this index will
5160 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07005161 *
5162 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07005163 */
5164 public int getContentAction() {
5165 return mContentActionIndex;
5166 }
5167
5168 /**
5169 * Set the gravity that this notification should have within the available viewport space.
5170 * Supported values include {@link android.view.Gravity#TOP},
5171 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5172 * The default value is {@link android.view.Gravity#BOTTOM}.
5173 */
5174 public WearableExtender setGravity(int gravity) {
5175 mGravity = gravity;
5176 return this;
5177 }
5178
5179 /**
5180 * Get the gravity that this notification should have within the available viewport space.
5181 * Supported values include {@link android.view.Gravity#TOP},
5182 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5183 * The default value is {@link android.view.Gravity#BOTTOM}.
5184 */
5185 public int getGravity() {
5186 return mGravity;
5187 }
5188
5189 /**
5190 * Set the custom size preset for the display of this notification out of the available
5191 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5192 * {@link #SIZE_LARGE}.
5193 * <p>Some custom size presets are only applicable for custom display notifications created
5194 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
5195 * documentation for the preset in question. See also
5196 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
5197 */
5198 public WearableExtender setCustomSizePreset(int sizePreset) {
5199 mCustomSizePreset = sizePreset;
5200 return this;
5201 }
5202
5203 /**
5204 * Get the custom size preset for the display of this notification out of the available
5205 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5206 * {@link #SIZE_LARGE}.
5207 * <p>Some custom size presets are only applicable for custom display notifications created
5208 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
5209 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
5210 */
5211 public int getCustomSizePreset() {
5212 return mCustomSizePreset;
5213 }
5214
5215 /**
5216 * Set the custom height in pixels for the display of this notification's content.
5217 * <p>This option is only available for custom display notifications created
5218 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
5219 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
5220 * {@link #getCustomContentHeight}.
5221 */
5222 public WearableExtender setCustomContentHeight(int height) {
5223 mCustomContentHeight = height;
5224 return this;
5225 }
5226
5227 /**
5228 * Get the custom height in pixels for the display of this notification's content.
5229 * <p>This option is only available for custom display notifications created
5230 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
5231 * {@link #setCustomContentHeight}.
5232 */
5233 public int getCustomContentHeight() {
5234 return mCustomContentHeight;
5235 }
5236
5237 /**
5238 * Set whether the scrolling position for the contents of this notification should start
5239 * at the bottom of the contents instead of the top when the contents are too long to
5240 * display within the screen. Default is false (start scroll at the top).
5241 */
5242 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
5243 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
5244 return this;
5245 }
5246
5247 /**
5248 * Get whether the scrolling position for the contents of this notification should start
5249 * at the bottom of the contents instead of the top when the contents are too long to
5250 * display within the screen. Default is false (start scroll at the top).
5251 */
5252 public boolean getStartScrollBottom() {
5253 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
5254 }
5255
5256 /**
5257 * Set whether the content intent is available when the wearable device is not connected
5258 * to a companion device. The user can still trigger this intent when the wearable device
5259 * is offline, but a visual hint will indicate that the content intent may not be available.
5260 * Defaults to true.
5261 */
5262 public WearableExtender setContentIntentAvailableOffline(
5263 boolean contentIntentAvailableOffline) {
5264 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
5265 return this;
5266 }
5267
5268 /**
5269 * Get whether the content intent is available when the wearable device is not connected
5270 * to a companion device. The user can still trigger this intent when the wearable device
5271 * is offline, but a visual hint will indicate that the content intent may not be available.
5272 * Defaults to true.
5273 */
5274 public boolean getContentIntentAvailableOffline() {
5275 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
5276 }
5277
5278 /**
5279 * Set a hint that this notification's icon should not be displayed.
5280 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
5281 * @return this object for method chaining
5282 */
5283 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
5284 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
5285 return this;
5286 }
5287
5288 /**
5289 * Get a hint that this notification's icon should not be displayed.
5290 * @return {@code true} if this icon should not be displayed, false otherwise.
5291 * The default value is {@code false} if this was never set.
5292 */
5293 public boolean getHintHideIcon() {
5294 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
5295 }
5296
5297 /**
5298 * Set a visual hint that only the background image of this notification should be
5299 * displayed, and other semantic content should be hidden. This hint is only applicable
5300 * to sub-pages added using {@link #addPage}.
5301 */
5302 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
5303 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
5304 return this;
5305 }
5306
5307 /**
5308 * Get a visual hint that only the background image of this notification should be
5309 * displayed, and other semantic content should be hidden. This hint is only applicable
5310 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
5311 */
5312 public boolean getHintShowBackgroundOnly() {
5313 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
5314 }
5315
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005316 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005317 * Set a hint that this notification's background should not be clipped if possible,
5318 * and should instead be resized to fully display on the screen, retaining the aspect
5319 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005320 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
5321 * @return this object for method chaining
5322 */
5323 public WearableExtender setHintAvoidBackgroundClipping(
5324 boolean hintAvoidBackgroundClipping) {
5325 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
5326 return this;
5327 }
5328
5329 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005330 * Get a hint that this notification's background should not be clipped if possible,
5331 * and should instead be resized to fully display on the screen, retaining the aspect
5332 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005333 * @return {@code true} if it's ok if the background is clipped on the screen, false
5334 * otherwise. The default value is {@code false} if this was never set.
5335 */
5336 public boolean getHintAvoidBackgroundClipping() {
5337 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
5338 }
5339
5340 /**
5341 * Set a hint that the screen should remain on for at least this duration when
5342 * this notification is displayed on the screen.
5343 * @param timeout The requested screen timeout in milliseconds. Can also be either
5344 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5345 * @return this object for method chaining
5346 */
5347 public WearableExtender setHintScreenTimeout(int timeout) {
5348 mHintScreenTimeout = timeout;
5349 return this;
5350 }
5351
5352 /**
5353 * Get the duration, in milliseconds, that the screen should remain on for
5354 * when this notification is displayed.
5355 * @return the duration in milliseconds if > 0, or either one of the sentinel values
5356 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5357 */
5358 public int getHintScreenTimeout() {
5359 return mHintScreenTimeout;
5360 }
5361
Griff Hazen61a9e862014-05-22 16:05:19 -07005362 private void setFlag(int mask, boolean value) {
5363 if (value) {
5364 mFlags |= mask;
5365 } else {
5366 mFlags &= ~mask;
5367 }
5368 }
5369 }
5370
5371 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005372 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
5373 * with car extensions:
5374 *
5375 * <ol>
5376 * <li>Create an {@link Notification.Builder}, setting any desired
5377 * properties.
5378 * <li>Create a {@link CarExtender}.
5379 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
5380 * {@link CarExtender}.
5381 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
5382 * to apply the extensions to a notification.
5383 * </ol>
5384 *
5385 * <pre class="prettyprint">
5386 * Notification notification = new Notification.Builder(context)
5387 * ...
5388 * .extend(new CarExtender()
5389 * .set*(...))
5390 * .build();
5391 * </pre>
5392 *
5393 * <p>Car extensions can be accessed on an existing notification by using the
5394 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
5395 * to access values.
5396 */
5397 public static final class CarExtender implements Extender {
5398 private static final String TAG = "CarExtender";
5399
5400 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
5401 private static final String EXTRA_LARGE_ICON = "large_icon";
5402 private static final String EXTRA_CONVERSATION = "car_conversation";
5403 private static final String EXTRA_COLOR = "app_color";
5404
5405 private Bitmap mLargeIcon;
5406 private UnreadConversation mUnreadConversation;
5407 private int mColor = Notification.COLOR_DEFAULT;
5408
5409 /**
5410 * Create a {@link CarExtender} with default options.
5411 */
5412 public CarExtender() {
5413 }
5414
5415 /**
5416 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
5417 *
5418 * @param notif The notification from which to copy options.
5419 */
5420 public CarExtender(Notification notif) {
5421 Bundle carBundle = notif.extras == null ?
5422 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
5423 if (carBundle != null) {
5424 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
5425 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
5426
5427 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
5428 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
5429 }
5430 }
5431
5432 /**
5433 * Apply car extensions to a notification that is being built. This is typically called by
5434 * the {@link Notification.Builder#extend(Notification.Extender)}
5435 * method of {@link Notification.Builder}.
5436 */
5437 @Override
5438 public Notification.Builder extend(Notification.Builder builder) {
5439 Bundle carExtensions = new Bundle();
5440
5441 if (mLargeIcon != null) {
5442 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
5443 }
5444 if (mColor != Notification.COLOR_DEFAULT) {
5445 carExtensions.putInt(EXTRA_COLOR, mColor);
5446 }
5447
5448 if (mUnreadConversation != null) {
5449 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
5450 carExtensions.putBundle(EXTRA_CONVERSATION, b);
5451 }
5452
5453 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
5454 return builder;
5455 }
5456
5457 /**
5458 * Sets the accent color to use when Android Auto presents the notification.
5459 *
5460 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
5461 * to accent the displayed notification. However, not all colors are acceptable in an
5462 * automotive setting. This method can be used to override the color provided in the
5463 * notification in such a situation.
5464 */
Tor Norbye80756e32015-03-02 09:39:27 -08005465 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005466 mColor = color;
5467 return this;
5468 }
5469
5470 /**
5471 * Gets the accent color.
5472 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005473 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005474 */
Tor Norbye80756e32015-03-02 09:39:27 -08005475 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005476 public int getColor() {
5477 return mColor;
5478 }
5479
5480 /**
5481 * Sets the large icon of the car notification.
5482 *
5483 * If no large icon is set in the extender, Android Auto will display the icon
5484 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
5485 *
5486 * @param largeIcon The large icon to use in the car notification.
5487 * @return This object for method chaining.
5488 */
5489 public CarExtender setLargeIcon(Bitmap largeIcon) {
5490 mLargeIcon = largeIcon;
5491 return this;
5492 }
5493
5494 /**
5495 * Gets the large icon used in this car notification, or null if no icon has been set.
5496 *
5497 * @return The large icon for the car notification.
5498 * @see CarExtender#setLargeIcon
5499 */
5500 public Bitmap getLargeIcon() {
5501 return mLargeIcon;
5502 }
5503
5504 /**
5505 * Sets the unread conversation in a message notification.
5506 *
5507 * @param unreadConversation The unread part of the conversation this notification conveys.
5508 * @return This object for method chaining.
5509 */
5510 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
5511 mUnreadConversation = unreadConversation;
5512 return this;
5513 }
5514
5515 /**
5516 * Returns the unread conversation conveyed by this notification.
5517 * @see #setUnreadConversation(UnreadConversation)
5518 */
5519 public UnreadConversation getUnreadConversation() {
5520 return mUnreadConversation;
5521 }
5522
5523 /**
5524 * A class which holds the unread messages from a conversation.
5525 */
5526 public static class UnreadConversation {
5527 private static final String KEY_AUTHOR = "author";
5528 private static final String KEY_TEXT = "text";
5529 private static final String KEY_MESSAGES = "messages";
5530 private static final String KEY_REMOTE_INPUT = "remote_input";
5531 private static final String KEY_ON_REPLY = "on_reply";
5532 private static final String KEY_ON_READ = "on_read";
5533 private static final String KEY_PARTICIPANTS = "participants";
5534 private static final String KEY_TIMESTAMP = "timestamp";
5535
5536 private final String[] mMessages;
5537 private final RemoteInput mRemoteInput;
5538 private final PendingIntent mReplyPendingIntent;
5539 private final PendingIntent mReadPendingIntent;
5540 private final String[] mParticipants;
5541 private final long mLatestTimestamp;
5542
5543 UnreadConversation(String[] messages, RemoteInput remoteInput,
5544 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
5545 String[] participants, long latestTimestamp) {
5546 mMessages = messages;
5547 mRemoteInput = remoteInput;
5548 mReadPendingIntent = readPendingIntent;
5549 mReplyPendingIntent = replyPendingIntent;
5550 mParticipants = participants;
5551 mLatestTimestamp = latestTimestamp;
5552 }
5553
5554 /**
5555 * Gets the list of messages conveyed by this notification.
5556 */
5557 public String[] getMessages() {
5558 return mMessages;
5559 }
5560
5561 /**
5562 * Gets the remote input that will be used to convey the response to a message list, or
5563 * null if no such remote input exists.
5564 */
5565 public RemoteInput getRemoteInput() {
5566 return mRemoteInput;
5567 }
5568
5569 /**
5570 * Gets the pending intent that will be triggered when the user replies to this
5571 * notification.
5572 */
5573 public PendingIntent getReplyPendingIntent() {
5574 return mReplyPendingIntent;
5575 }
5576
5577 /**
5578 * Gets the pending intent that Android Auto will send after it reads aloud all messages
5579 * in this object's message list.
5580 */
5581 public PendingIntent getReadPendingIntent() {
5582 return mReadPendingIntent;
5583 }
5584
5585 /**
5586 * Gets the participants in the conversation.
5587 */
5588 public String[] getParticipants() {
5589 return mParticipants;
5590 }
5591
5592 /**
5593 * Gets the firs participant in the conversation.
5594 */
5595 public String getParticipant() {
5596 return mParticipants.length > 0 ? mParticipants[0] : null;
5597 }
5598
5599 /**
5600 * Gets the timestamp of the conversation.
5601 */
5602 public long getLatestTimestamp() {
5603 return mLatestTimestamp;
5604 }
5605
5606 Bundle getBundleForUnreadConversation() {
5607 Bundle b = new Bundle();
5608 String author = null;
5609 if (mParticipants != null && mParticipants.length > 1) {
5610 author = mParticipants[0];
5611 }
5612 Parcelable[] messages = new Parcelable[mMessages.length];
5613 for (int i = 0; i < messages.length; i++) {
5614 Bundle m = new Bundle();
5615 m.putString(KEY_TEXT, mMessages[i]);
5616 m.putString(KEY_AUTHOR, author);
5617 messages[i] = m;
5618 }
5619 b.putParcelableArray(KEY_MESSAGES, messages);
5620 if (mRemoteInput != null) {
5621 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
5622 }
5623 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
5624 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
5625 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
5626 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
5627 return b;
5628 }
5629
5630 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
5631 if (b == null) {
5632 return null;
5633 }
5634 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
5635 String[] messages = null;
5636 if (parcelableMessages != null) {
5637 String[] tmp = new String[parcelableMessages.length];
5638 boolean success = true;
5639 for (int i = 0; i < tmp.length; i++) {
5640 if (!(parcelableMessages[i] instanceof Bundle)) {
5641 success = false;
5642 break;
5643 }
5644 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
5645 if (tmp[i] == null) {
5646 success = false;
5647 break;
5648 }
5649 }
5650 if (success) {
5651 messages = tmp;
5652 } else {
5653 return null;
5654 }
5655 }
5656
5657 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
5658 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
5659
5660 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
5661
5662 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
5663 if (participants == null || participants.length != 1) {
5664 return null;
5665 }
5666
5667 return new UnreadConversation(messages,
5668 remoteInput,
5669 onReply,
5670 onRead,
5671 participants, b.getLong(KEY_TIMESTAMP));
5672 }
5673 };
5674
5675 /**
5676 * Builder class for {@link CarExtender.UnreadConversation} objects.
5677 */
5678 public static class Builder {
5679 private final List<String> mMessages = new ArrayList<String>();
5680 private final String mParticipant;
5681 private RemoteInput mRemoteInput;
5682 private PendingIntent mReadPendingIntent;
5683 private PendingIntent mReplyPendingIntent;
5684 private long mLatestTimestamp;
5685
5686 /**
5687 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
5688 *
5689 * @param name The name of the other participant in the conversation.
5690 */
5691 public Builder(String name) {
5692 mParticipant = name;
5693 }
5694
5695 /**
5696 * Appends a new unread message to the list of messages for this conversation.
5697 *
5698 * The messages should be added from oldest to newest.
5699 *
5700 * @param message The text of the new unread message.
5701 * @return This object for method chaining.
5702 */
5703 public Builder addMessage(String message) {
5704 mMessages.add(message);
5705 return this;
5706 }
5707
5708 /**
5709 * Sets the pending intent and remote input which will convey the reply to this
5710 * notification.
5711 *
5712 * @param pendingIntent The pending intent which will be triggered on a reply.
5713 * @param remoteInput The remote input parcelable which will carry the reply.
5714 * @return This object for method chaining.
5715 *
5716 * @see CarExtender.UnreadConversation#getRemoteInput
5717 * @see CarExtender.UnreadConversation#getReplyPendingIntent
5718 */
5719 public Builder setReplyAction(
5720 PendingIntent pendingIntent, RemoteInput remoteInput) {
5721 mRemoteInput = remoteInput;
5722 mReplyPendingIntent = pendingIntent;
5723
5724 return this;
5725 }
5726
5727 /**
5728 * Sets the pending intent that will be sent once the messages in this notification
5729 * are read.
5730 *
5731 * @param pendingIntent The pending intent to use.
5732 * @return This object for method chaining.
5733 */
5734 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
5735 mReadPendingIntent = pendingIntent;
5736 return this;
5737 }
5738
5739 /**
5740 * Sets the timestamp of the most recent message in an unread conversation.
5741 *
5742 * If a messaging notification has been posted by your application and has not
5743 * yet been cancelled, posting a later notification with the same id and tag
5744 * but without a newer timestamp may result in Android Auto not displaying a
5745 * heads up notification for the later notification.
5746 *
5747 * @param timestamp The timestamp of the most recent message in the conversation.
5748 * @return This object for method chaining.
5749 */
5750 public Builder setLatestTimestamp(long timestamp) {
5751 mLatestTimestamp = timestamp;
5752 return this;
5753 }
5754
5755 /**
5756 * Builds a new unread conversation object.
5757 *
5758 * @return The new unread conversation object.
5759 */
5760 public UnreadConversation build() {
5761 String[] messages = mMessages.toArray(new String[mMessages.size()]);
5762 String[] participants = { mParticipant };
5763 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
5764 mReadPendingIntent, participants, mLatestTimestamp);
5765 }
5766 }
5767 }
5768
5769 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07005770 * Get an array of Notification objects from a parcelable array bundle field.
5771 * Update the bundle to have a typed array so fetches in the future don't need
5772 * to do an array copy.
5773 */
5774 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
5775 Parcelable[] array = bundle.getParcelableArray(key);
5776 if (array instanceof Notification[] || array == null) {
5777 return (Notification[]) array;
5778 }
5779 Notification[] typedArray = Arrays.copyOf(array, array.length,
5780 Notification[].class);
5781 bundle.putParcelableArray(key, typedArray);
5782 return typedArray;
5783 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005784
5785 private static class BuilderRemoteViews extends RemoteViews {
5786 public BuilderRemoteViews(Parcel parcel) {
5787 super(parcel);
5788 }
5789
Kenny Guy77320062014-08-27 21:37:15 +01005790 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
5791 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005792 }
5793
5794 @Override
5795 public BuilderRemoteViews clone() {
5796 Parcel p = Parcel.obtain();
5797 writeToParcel(p, 0);
5798 p.setDataPosition(0);
5799 BuilderRemoteViews brv = new BuilderRemoteViews(p);
5800 p.recycle();
5801 return brv;
5802 }
5803 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005804}