blob: 52631d130e5cb698afe4e073edb45eec934aee70 [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 /**
826 * {@link #extras} key: whether {@link #when} should be shown,
827 * as supplied to {@link Builder#setShowWhen(boolean)}.
828 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400829 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400830
831 /**
832 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
833 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
834 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400835 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400836
837 /**
838 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
839 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
840 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400841 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400842
843 /**
844 * {@link #extras} key: A string representing the name of the specific
845 * {@link android.app.Notification.Style} used to create this notification.
846 */
Chris Wren91ad5632013-06-05 15:05:57 -0400847 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400848
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400849 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400850 * {@link #extras} key: A String array containing the people that this notification relates to,
851 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400852 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400853 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500854
855 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400856 * Allow certain system-generated notifications to appear before the device is provisioned.
857 * Only available to notifications coming from the android package.
858 * @hide
859 */
860 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
861
862 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700863 * {@link #extras} key: A
864 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
865 * in the background when the notification is selected. The URI must point to an image stream
866 * suitable for passing into
867 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
868 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
869 * URI used for this purpose must require no permissions to read the image data.
870 */
871 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
872
873 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400874 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700875 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400876 * {@link android.app.Notification.MediaStyle} notification.
877 */
878 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
879
880 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100881 * {@link #extras} key: the indices of actions to be shown in the compact view,
882 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
883 */
884 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
885
Christoph Studer943aa672014-08-03 20:31:16 +0200886 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100887 * {@link #extras} key: the user that built the notification.
888 *
889 * @hide
890 */
891 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
892
893 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400894 * @hide
895 */
896 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
897
Dan Sandlerd63f9322015-05-06 15:18:49 -0400898 private Icon mSmallIcon;
899 private Icon mLargeIcon;
900
Chris Wren51c75102013-07-16 20:49:17 -0400901 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400902 * Structure to encapsulate a named action that can be shown as part of this notification.
903 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
904 * selected by the user.
905 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700906 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
907 * or {@link Notification.Builder#addAction(Notification.Action)}
908 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400909 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500910 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700911 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -0400912 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -0700913 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700914
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400915 /**
916 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -0400917 *
918 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400919 */
Dan Sandler86647982015-05-13 23:41:13 -0400920 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400921 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700922
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400923 /**
924 * Title of the action.
925 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400926 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700927
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400928 /**
929 * Intent to send when the user invokes this action. May be null, in which case the action
930 * may be rendered in a disabled presentation by the system UI.
931 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400932 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -0700933
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400934 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -0400935 if (in.readInt() != 0) {
936 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -0400937 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
938 icon = mIcon.getResId();
939 }
Dan Sandler86647982015-05-13 23:41:13 -0400940 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400941 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
942 if (in.readInt() == 1) {
943 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
944 }
Griff Hazen959591e2014-05-15 22:26:18 -0700945 mExtras = in.readBundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700946 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400947 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700948
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400949 /**
Dan Sandler86647982015-05-13 23:41:13 -0400950 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400951 */
Dan Sandler86647982015-05-13 23:41:13 -0400952 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400953 public Action(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -0400954 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700955 }
956
Dan Sandler86647982015-05-13 23:41:13 -0400957 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700958 RemoteInput[] remoteInputs) {
Dan Sandler86647982015-05-13 23:41:13 -0400959 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -0400960 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
961 this.icon = icon.getResId();
962 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400963 this.title = title;
964 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -0700965 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700966 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700967 }
968
969 /**
Dan Sandler86647982015-05-13 23:41:13 -0400970 * Return an icon representing the action.
971 */
972 public Icon getIcon() {
973 if (mIcon == null && icon != 0) {
974 // you snuck an icon in here without using the builder; let's try to keep it
975 mIcon = Icon.createWithResource("", icon);
976 }
977 return mIcon;
978 }
979
980 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700981 * Get additional metadata carried around with this Action.
982 */
983 public Bundle getExtras() {
984 return mExtras;
985 }
986
987 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700988 * Get the list of inputs to be collected from the user when this action is sent.
989 * May return null if no remote inputs were added.
990 */
991 public RemoteInput[] getRemoteInputs() {
992 return mRemoteInputs;
993 }
994
995 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700996 * Builder class for {@link Action} objects.
997 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700998 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -0400999 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001000 private final CharSequence mTitle;
1001 private final PendingIntent mIntent;
1002 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001003 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001004
1005 /**
1006 * Construct a new builder for {@link Action} object.
1007 * @param icon icon to show for this action
1008 * @param title the title of the action
1009 * @param intent the {@link PendingIntent} to fire when users trigger this action
1010 */
Dan Sandler86647982015-05-13 23:41:13 -04001011 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001012 public Builder(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -04001013 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
1014 }
1015
1016 /**
1017 * Construct a new builder for {@link Action} object.
1018 * @param icon icon to show for this action
1019 * @param title the title of the action
1020 * @param intent the {@link PendingIntent} to fire when users trigger this action
1021 */
1022 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001023 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -07001024 }
1025
1026 /**
1027 * Construct a new builder for {@link Action} object using the fields from an
1028 * {@link Action}.
1029 * @param action the action to read fields from.
1030 */
1031 public Builder(Action action) {
Dan Sandler86647982015-05-13 23:41:13 -04001032 this(action.getIcon(), action.title, action.actionIntent, new Bundle(action.mExtras),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001033 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -07001034 }
1035
Dan Sandler86647982015-05-13 23:41:13 -04001036 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001037 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -07001038 mIcon = icon;
1039 mTitle = title;
1040 mIntent = intent;
1041 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001042 if (remoteInputs != null) {
1043 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1044 Collections.addAll(mRemoteInputs, remoteInputs);
1045 }
Griff Hazen959591e2014-05-15 22:26:18 -07001046 }
1047
1048 /**
1049 * Merge additional metadata into this builder.
1050 *
1051 * <p>Values within the Bundle will replace existing extras values in this Builder.
1052 *
1053 * @see Notification.Action#extras
1054 */
1055 public Builder addExtras(Bundle extras) {
1056 if (extras != null) {
1057 mExtras.putAll(extras);
1058 }
1059 return this;
1060 }
1061
1062 /**
1063 * Get the metadata Bundle used by this Builder.
1064 *
1065 * <p>The returned Bundle is shared with this Builder.
1066 */
1067 public Bundle getExtras() {
1068 return mExtras;
1069 }
1070
1071 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001072 * Add an input to be collected from the user when this action is sent.
1073 * Response values can be retrieved from the fired intent by using the
1074 * {@link RemoteInput#getResultsFromIntent} function.
1075 * @param remoteInput a {@link RemoteInput} to add to the action
1076 * @return this object for method chaining
1077 */
1078 public Builder addRemoteInput(RemoteInput remoteInput) {
1079 if (mRemoteInputs == null) {
1080 mRemoteInputs = new ArrayList<RemoteInput>();
1081 }
1082 mRemoteInputs.add(remoteInput);
1083 return this;
1084 }
1085
1086 /**
1087 * Apply an extender to this action builder. Extenders may be used to add
1088 * metadata or change options on this builder.
1089 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001090 public Builder extend(Extender extender) {
1091 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001092 return this;
1093 }
1094
1095 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001096 * Combine all of the options that have been set and return a new {@link Action}
1097 * object.
1098 * @return the built action
1099 */
1100 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001101 RemoteInput[] remoteInputs = mRemoteInputs != null
1102 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
1103 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -07001104 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001105 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001106
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001107 @Override
1108 public Action clone() {
1109 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001110 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001111 title,
1112 actionIntent, // safe to alias
1113 new Bundle(mExtras),
1114 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001115 }
1116 @Override
1117 public int describeContents() {
1118 return 0;
1119 }
1120 @Override
1121 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001122 final Icon ic = getIcon();
1123 if (ic != null) {
1124 out.writeInt(1);
1125 ic.writeToParcel(out, 0);
1126 } else {
1127 out.writeInt(0);
1128 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001129 TextUtils.writeToParcel(title, out, flags);
1130 if (actionIntent != null) {
1131 out.writeInt(1);
1132 actionIntent.writeToParcel(out, flags);
1133 } else {
1134 out.writeInt(0);
1135 }
Griff Hazen959591e2014-05-15 22:26:18 -07001136 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001137 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001138 }
Griff Hazen959591e2014-05-15 22:26:18 -07001139 public static final Parcelable.Creator<Action> CREATOR =
1140 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001141 public Action createFromParcel(Parcel in) {
1142 return new Action(in);
1143 }
1144 public Action[] newArray(int size) {
1145 return new Action[size];
1146 }
1147 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001148
1149 /**
1150 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1151 * metadata or change options on an action builder.
1152 */
1153 public interface Extender {
1154 /**
1155 * Apply this extender to a notification action builder.
1156 * @param builder the builder to be modified.
1157 * @return the build object for chaining.
1158 */
1159 public Builder extend(Builder builder);
1160 }
1161
1162 /**
1163 * Wearable extender for notification actions. To add extensions to an action,
1164 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1165 * the {@code WearableExtender()} constructor and apply it to a
1166 * {@link android.app.Notification.Action.Builder} using
1167 * {@link android.app.Notification.Action.Builder#extend}.
1168 *
1169 * <pre class="prettyprint">
1170 * Notification.Action action = new Notification.Action.Builder(
1171 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001172 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001173 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001174 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001175 */
1176 public static final class WearableExtender implements Extender {
1177 /** Notification action extra which contains wearable extensions */
1178 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1179
Pete Gastaf6781d2014-10-07 15:17:05 -04001180 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001181 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001182 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1183 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1184 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001185
1186 // Flags bitwise-ored to mFlags
1187 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
1188
1189 // Default value for flags integer
1190 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1191
1192 private int mFlags = DEFAULT_FLAGS;
1193
Pete Gastaf6781d2014-10-07 15:17:05 -04001194 private CharSequence mInProgressLabel;
1195 private CharSequence mConfirmLabel;
1196 private CharSequence mCancelLabel;
1197
Griff Hazen61a9e862014-05-22 16:05:19 -07001198 /**
1199 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1200 * options.
1201 */
1202 public WearableExtender() {
1203 }
1204
1205 /**
1206 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1207 * wearable options present in an existing notification action.
1208 * @param action the notification action to inspect.
1209 */
1210 public WearableExtender(Action action) {
1211 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1212 if (wearableBundle != null) {
1213 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001214 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1215 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1216 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001217 }
1218 }
1219
1220 /**
1221 * Apply wearable extensions to a notification action that is being built. This is
1222 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1223 * method of {@link android.app.Notification.Action.Builder}.
1224 */
1225 @Override
1226 public Action.Builder extend(Action.Builder builder) {
1227 Bundle wearableBundle = new Bundle();
1228
1229 if (mFlags != DEFAULT_FLAGS) {
1230 wearableBundle.putInt(KEY_FLAGS, mFlags);
1231 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001232 if (mInProgressLabel != null) {
1233 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1234 }
1235 if (mConfirmLabel != null) {
1236 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1237 }
1238 if (mCancelLabel != null) {
1239 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1240 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001241
1242 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1243 return builder;
1244 }
1245
1246 @Override
1247 public WearableExtender clone() {
1248 WearableExtender that = new WearableExtender();
1249 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001250 that.mInProgressLabel = this.mInProgressLabel;
1251 that.mConfirmLabel = this.mConfirmLabel;
1252 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001253 return that;
1254 }
1255
1256 /**
1257 * Set whether this action is available when the wearable device is not connected to
1258 * a companion device. The user can still trigger this action when the wearable device is
1259 * offline, but a visual hint will indicate that the action may not be available.
1260 * Defaults to true.
1261 */
1262 public WearableExtender setAvailableOffline(boolean availableOffline) {
1263 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1264 return this;
1265 }
1266
1267 /**
1268 * Get whether this action is available when the wearable device is not connected to
1269 * a companion device. The user can still trigger this action when the wearable device is
1270 * offline, but a visual hint will indicate that the action may not be available.
1271 * Defaults to true.
1272 */
1273 public boolean isAvailableOffline() {
1274 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1275 }
1276
1277 private void setFlag(int mask, boolean value) {
1278 if (value) {
1279 mFlags |= mask;
1280 } else {
1281 mFlags &= ~mask;
1282 }
1283 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001284
1285 /**
1286 * Set a label to display while the wearable is preparing to automatically execute the
1287 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1288 *
1289 * @param label the label to display while the action is being prepared to execute
1290 * @return this object for method chaining
1291 */
1292 public WearableExtender setInProgressLabel(CharSequence label) {
1293 mInProgressLabel = label;
1294 return this;
1295 }
1296
1297 /**
1298 * Get the label to display while the wearable is preparing to automatically execute
1299 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1300 *
1301 * @return the label to display while the action is being prepared to execute
1302 */
1303 public CharSequence getInProgressLabel() {
1304 return mInProgressLabel;
1305 }
1306
1307 /**
1308 * Set a label to display to confirm that the action should be executed.
1309 * This is usually an imperative verb like "Send".
1310 *
1311 * @param label the label to confirm the action should be executed
1312 * @return this object for method chaining
1313 */
1314 public WearableExtender setConfirmLabel(CharSequence label) {
1315 mConfirmLabel = label;
1316 return this;
1317 }
1318
1319 /**
1320 * Get the label to display to confirm that the action should be executed.
1321 * This is usually an imperative verb like "Send".
1322 *
1323 * @return the label to confirm the action should be executed
1324 */
1325 public CharSequence getConfirmLabel() {
1326 return mConfirmLabel;
1327 }
1328
1329 /**
1330 * Set a label to display to cancel the action.
1331 * This is usually an imperative verb, like "Cancel".
1332 *
1333 * @param label the label to display to cancel the action
1334 * @return this object for method chaining
1335 */
1336 public WearableExtender setCancelLabel(CharSequence label) {
1337 mCancelLabel = label;
1338 return this;
1339 }
1340
1341 /**
1342 * Get the label to display to cancel the action.
1343 * This is usually an imperative verb like "Cancel".
1344 *
1345 * @return the label to display to cancel the action
1346 */
1347 public CharSequence getCancelLabel() {
1348 return mCancelLabel;
1349 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001350 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001351 }
1352
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001353 /**
1354 * Array of all {@link Action} structures attached to this notification by
1355 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1356 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1357 * interface for invoking actions.
1358 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001359 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001360
1361 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001362 * Replacement version of this notification whose content will be shown
1363 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1364 * and {@link #VISIBILITY_PUBLIC}.
1365 */
1366 public Notification publicVersion;
1367
1368 /**
Julia Reynolds74303cf2015-10-16 11:37:55 -04001369 * Structure to encapsulate a topic that is shown in Notification settings.
1370 * It must include an id and label.
1371 */
1372 public static class Topic implements Parcelable {
1373 private final String id;
1374 private final CharSequence label;
1375
1376 public Topic(String id, CharSequence label) {
1377 this.id = id;
1378 this.label = safeCharSequence(label);
1379 }
1380
1381 private Topic(Parcel in) {
1382 if (in.readInt() != 0) {
1383 id = in.readString();
1384 } else {
1385 id = null;
1386 }
1387 label = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1388 }
1389
1390 public String getId() {
1391 return id;
1392 }
1393
1394 public CharSequence getLabel() {
1395 return label;
1396 }
1397
1398 @Override
1399 public String toString() {
1400 return new StringBuilder(Topic.class.getSimpleName()).append('[')
1401 .append("id=").append(id)
1402 .append(",label=").append(label)
1403 .append(']').toString();
1404 }
1405
1406 @Override
1407 public boolean equals(Object o) {
1408 if (!(o instanceof Topic)) return false;
1409 if (o == this) return true;
1410 final Topic other = (Topic) o;
1411 return Objects.equals(other.id, id)
1412 && Objects.equals(other.label, label);
1413 }
1414
1415 @Override
1416 public int hashCode() {
1417 return Objects.hash(id, label);
1418 }
1419
1420 @Override
1421 public Topic clone() {
1422 return new Topic(id, label);
1423 }
1424
1425 @Override
1426 public int describeContents() {
1427 return 0;
1428 }
1429
1430 @Override
1431 public void writeToParcel(Parcel out, int flags) {
1432 if (id != null) {
1433 out.writeInt(1);
1434 out.writeString(id);
1435 } else {
1436 out.writeInt(0);
1437 }
1438 TextUtils.writeToParcel(label, out, flags);
1439 }
1440 public static final Parcelable.Creator<Topic> CREATOR =
1441 new Parcelable.Creator<Topic>() {
1442 public Topic createFromParcel(Parcel in) {
1443 return new Topic(in);
1444 }
1445 public Topic[] newArray(int size) {
1446 return new Topic[size];
1447 }
1448 };
1449 }
1450
Julia Reynolds233a5f92015-10-19 13:51:23 -04001451 @SystemApi
1452 public static final String TOPIC_DEFAULT = "system_default_topic";
1453
Julia Reynolds054c5dc2015-11-17 15:36:30 -05001454 private Topic topic;
Julia Reynolds74303cf2015-10-16 11:37:55 -04001455
Julia Reynolds054c5dc2015-11-17 15:36:30 -05001456 public Topic getTopic() {
1457 return topic;
Julia Reynolds74303cf2015-10-16 11:37:55 -04001458 }
1459
1460 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001461 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001462 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001463 */
1464 public Notification()
1465 {
1466 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001467 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 }
1469
1470 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001471 * @hide
1472 */
1473 public Notification(Context context, int icon, CharSequence tickerText, long when,
1474 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1475 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001476 new Builder(context)
1477 .setWhen(when)
1478 .setSmallIcon(icon)
1479 .setTicker(tickerText)
1480 .setContentTitle(contentTitle)
1481 .setContentText(contentText)
1482 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1483 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001484 }
1485
1486 /**
1487 * Constructs a Notification object with the information needed to
1488 * have a status bar icon without the standard expanded view.
1489 *
1490 * @param icon The resource id of the icon to put in the status bar.
1491 * @param tickerText The text that flows by in the status bar when the notification first
1492 * activates.
1493 * @param when The time to show in the time field. In the System.currentTimeMillis
1494 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001495 *
1496 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001498 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001499 public Notification(int icon, CharSequence tickerText, long when)
1500 {
1501 this.icon = icon;
1502 this.tickerText = tickerText;
1503 this.when = when;
1504 }
1505
1506 /**
1507 * Unflatten the notification from a parcel.
1508 */
1509 public Notification(Parcel parcel)
1510 {
1511 int version = parcel.readInt();
1512
1513 when = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001514 if (parcel.readInt() != 0) {
1515 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001516 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1517 icon = mSmallIcon.getResId();
1518 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001519 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001520 number = parcel.readInt();
1521 if (parcel.readInt() != 0) {
1522 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1523 }
1524 if (parcel.readInt() != 0) {
1525 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1526 }
1527 if (parcel.readInt() != 0) {
1528 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1529 }
1530 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001531 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001532 }
1533 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001534 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1535 }
Joe Onorato561d3852010-11-20 18:09:34 -08001536 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001537 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001538 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 defaults = parcel.readInt();
1540 flags = parcel.readInt();
1541 if (parcel.readInt() != 0) {
1542 sound = Uri.CREATOR.createFromParcel(parcel);
1543 }
1544
1545 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001546 if (parcel.readInt() != 0) {
1547 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1548 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001549 vibrate = parcel.createLongArray();
1550 ledARGB = parcel.readInt();
1551 ledOnMS = parcel.readInt();
1552 ledOffMS = parcel.readInt();
1553 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001554
1555 if (parcel.readInt() != 0) {
1556 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1557 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001558
1559 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001560
John Spurlockfd7f1e02014-03-18 16:41:57 -04001561 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001562
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001563 mGroupKey = parcel.readString();
1564
1565 mSortKey = parcel.readString();
1566
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001567 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001568
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001569 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1570
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001571 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001572 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1573 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001574
Chris Wren8fd39ec2014-02-27 17:43:26 -05001575 if (parcel.readInt() != 0) {
1576 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1577 }
1578
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001579 visibility = parcel.readInt();
1580
1581 if (parcel.readInt() != 0) {
1582 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1583 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001584
1585 color = parcel.readInt();
Julia Reynolds74303cf2015-10-16 11:37:55 -04001586
Julia Reynolds054c5dc2015-11-17 15:36:30 -05001587 if (parcel.readInt() != 0) {
1588 topic = Topic.CREATOR.createFromParcel(parcel);
1589 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 }
1591
Andy Stadler110988c2010-12-03 14:29:16 -08001592 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001593 public Notification clone() {
1594 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001595 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001596 return that;
1597 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001598
Daniel Sandler1a497d32013-04-18 14:52:45 -04001599 /**
1600 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1601 * of this into that.
1602 * @hide
1603 */
1604 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001605 that.when = this.when;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001606 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001607 that.number = this.number;
1608
1609 // PendingIntents are global, so there's no reason (or way) to clone them.
1610 that.contentIntent = this.contentIntent;
1611 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001612 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001613
1614 if (this.tickerText != null) {
1615 that.tickerText = this.tickerText.toString();
1616 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001617 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001618 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001619 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001620 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001621 that.contentView = this.contentView.clone();
1622 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001623 if (heavy && this.mLargeIcon != null) {
1624 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001625 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001626 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001627 that.sound = this.sound; // android.net.Uri is immutable
1628 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001629 if (this.audioAttributes != null) {
1630 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1631 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001632
1633 final long[] vibrate = this.vibrate;
1634 if (vibrate != null) {
1635 final int N = vibrate.length;
1636 final long[] vib = that.vibrate = new long[N];
1637 System.arraycopy(vibrate, 0, vib, 0, N);
1638 }
1639
1640 that.ledARGB = this.ledARGB;
1641 that.ledOnMS = this.ledOnMS;
1642 that.ledOffMS = this.ledOffMS;
1643 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001644
Joe Onorato18e69df2010-05-17 22:26:12 -07001645 that.flags = this.flags;
1646
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001647 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001648
John Spurlockfd7f1e02014-03-18 16:41:57 -04001649 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001650
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001651 that.mGroupKey = this.mGroupKey;
1652
1653 that.mSortKey = this.mSortKey;
1654
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001655 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001656 try {
1657 that.extras = new Bundle(this.extras);
1658 // will unparcel
1659 that.extras.size();
1660 } catch (BadParcelableException e) {
1661 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1662 that.extras = null;
1663 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001664 }
1665
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001666 if (this.actions != null) {
1667 that.actions = new Action[this.actions.length];
1668 for(int i=0; i<this.actions.length; i++) {
1669 that.actions[i] = this.actions[i].clone();
1670 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001671 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001672
Daniel Sandler1a497d32013-04-18 14:52:45 -04001673 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001674 that.bigContentView = this.bigContentView.clone();
1675 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001676
Chris Wren8fd39ec2014-02-27 17:43:26 -05001677 if (heavy && this.headsUpContentView != null) {
1678 that.headsUpContentView = this.headsUpContentView.clone();
1679 }
1680
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001681 that.visibility = this.visibility;
1682
1683 if (this.publicVersion != null) {
1684 that.publicVersion = new Notification();
1685 this.publicVersion.cloneInto(that.publicVersion, heavy);
1686 }
1687
Dan Sandler26e81cf2014-05-06 10:01:27 -04001688 that.color = this.color;
1689
Julia Reynolds054c5dc2015-11-17 15:36:30 -05001690 if (this.topic != null) {
1691 that.topic = this.topic.clone();
Julia Reynolds74303cf2015-10-16 11:37:55 -04001692 }
1693
Daniel Sandler1a497d32013-04-18 14:52:45 -04001694 if (!heavy) {
1695 that.lightenPayload(); // will clean out extras
1696 }
1697 }
1698
1699 /**
1700 * Removes heavyweight parts of the Notification object for archival or for sending to
1701 * listeners when the full contents are not necessary.
1702 * @hide
1703 */
1704 public final void lightenPayload() {
1705 tickerView = null;
1706 contentView = null;
1707 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001708 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001709 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05001710 if (extras != null && !extras.isEmpty()) {
1711 final Set<String> keyset = extras.keySet();
1712 final int N = keyset.size();
1713 final String[] keys = keyset.toArray(new String[N]);
1714 for (int i=0; i<N; i++) {
1715 final String key = keys[i];
1716 final Object obj = extras.get(key);
1717 if (obj != null &&
1718 ( obj instanceof Parcelable
1719 || obj instanceof Parcelable[]
1720 || obj instanceof SparseArray
1721 || obj instanceof ArrayList)) {
1722 extras.remove(key);
1723 }
1724 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001725 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001726 }
1727
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001728 /**
1729 * Make sure this CharSequence is safe to put into a bundle, which basically
1730 * means it had better not be some custom Parcelable implementation.
1731 * @hide
1732 */
1733 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001734 if (cs == null) return cs;
1735 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1736 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1737 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001738 if (cs instanceof Parcelable) {
1739 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1740 + " instance is a custom Parcelable and not allowed in Notification");
1741 return cs.toString();
1742 }
1743
1744 return cs;
1745 }
1746
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001747 public int describeContents() {
1748 return 0;
1749 }
1750
1751 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001752 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001753 */
1754 public void writeToParcel(Parcel parcel, int flags)
1755 {
1756 parcel.writeInt(1);
1757
1758 parcel.writeLong(when);
Dan Sandler4e787062015-06-17 15:09:48 -04001759 if (mSmallIcon == null && icon != 0) {
1760 // you snuck an icon in here without using the builder; let's try to keep it
1761 mSmallIcon = Icon.createWithResource("", icon);
1762 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001763 if (mSmallIcon != null) {
1764 parcel.writeInt(1);
1765 mSmallIcon.writeToParcel(parcel, 0);
1766 } else {
1767 parcel.writeInt(0);
1768 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001769 parcel.writeInt(number);
1770 if (contentIntent != null) {
1771 parcel.writeInt(1);
1772 contentIntent.writeToParcel(parcel, 0);
1773 } else {
1774 parcel.writeInt(0);
1775 }
1776 if (deleteIntent != null) {
1777 parcel.writeInt(1);
1778 deleteIntent.writeToParcel(parcel, 0);
1779 } else {
1780 parcel.writeInt(0);
1781 }
1782 if (tickerText != null) {
1783 parcel.writeInt(1);
1784 TextUtils.writeToParcel(tickerText, parcel, flags);
1785 } else {
1786 parcel.writeInt(0);
1787 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001788 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001789 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001790 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001791 } else {
1792 parcel.writeInt(0);
1793 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 if (contentView != null) {
1795 parcel.writeInt(1);
1796 contentView.writeToParcel(parcel, 0);
1797 } else {
1798 parcel.writeInt(0);
1799 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001800 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001801 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001802 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001803 } else {
1804 parcel.writeInt(0);
1805 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001806
1807 parcel.writeInt(defaults);
1808 parcel.writeInt(this.flags);
1809
1810 if (sound != null) {
1811 parcel.writeInt(1);
1812 sound.writeToParcel(parcel, 0);
1813 } else {
1814 parcel.writeInt(0);
1815 }
1816 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001817
1818 if (audioAttributes != null) {
1819 parcel.writeInt(1);
1820 audioAttributes.writeToParcel(parcel, 0);
1821 } else {
1822 parcel.writeInt(0);
1823 }
1824
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001825 parcel.writeLongArray(vibrate);
1826 parcel.writeInt(ledARGB);
1827 parcel.writeInt(ledOnMS);
1828 parcel.writeInt(ledOffMS);
1829 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001830
1831 if (fullScreenIntent != null) {
1832 parcel.writeInt(1);
1833 fullScreenIntent.writeToParcel(parcel, 0);
1834 } else {
1835 parcel.writeInt(0);
1836 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001837
1838 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001839
John Spurlockfd7f1e02014-03-18 16:41:57 -04001840 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001841
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001842 parcel.writeString(mGroupKey);
1843
1844 parcel.writeString(mSortKey);
1845
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001846 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001847
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001848 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001849
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001850 if (bigContentView != null) {
1851 parcel.writeInt(1);
1852 bigContentView.writeToParcel(parcel, 0);
1853 } else {
1854 parcel.writeInt(0);
1855 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001856
Chris Wren8fd39ec2014-02-27 17:43:26 -05001857 if (headsUpContentView != null) {
1858 parcel.writeInt(1);
1859 headsUpContentView.writeToParcel(parcel, 0);
1860 } else {
1861 parcel.writeInt(0);
1862 }
1863
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001864 parcel.writeInt(visibility);
1865
1866 if (publicVersion != null) {
1867 parcel.writeInt(1);
1868 publicVersion.writeToParcel(parcel, 0);
1869 } else {
1870 parcel.writeInt(0);
1871 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001872
1873 parcel.writeInt(color);
Julia Reynolds74303cf2015-10-16 11:37:55 -04001874
Julia Reynolds054c5dc2015-11-17 15:36:30 -05001875 if (topic != null) {
1876 parcel.writeInt(1);
1877 topic.writeToParcel(parcel, 0);
1878 } else {
1879 parcel.writeInt(0);
1880 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001881 }
1882
1883 /**
1884 * Parcelable.Creator that instantiates Notification objects
1885 */
1886 public static final Parcelable.Creator<Notification> CREATOR
1887 = new Parcelable.Creator<Notification>()
1888 {
1889 public Notification createFromParcel(Parcel parcel)
1890 {
1891 return new Notification(parcel);
1892 }
1893
1894 public Notification[] newArray(int size)
1895 {
1896 return new Notification[size];
1897 }
1898 };
1899
1900 /**
1901 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1902 * layout.
1903 *
1904 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1905 * in the view.</p>
1906 * @param context The context for your application / activity.
1907 * @param contentTitle The title that goes in the expanded entry.
1908 * @param contentText The text that goes in the expanded entry.
1909 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1910 * If this is an activity, it must include the
1911 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001912 * that you take care of task management as described in the
1913 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1914 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001915 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001916 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04001917 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001918 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001919 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001920 public void setLatestEventInfo(Context context,
1921 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001922 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
1923 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
1924 new Throwable());
1925 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001926
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001927 // ensure that any information already set directly is preserved
1928 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001929
1930 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001931 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001932 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 }
1934 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001935 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001937 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001938
1939 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001940 }
1941
Julia Reynoldsda303542015-11-23 14:00:20 -05001942 /**
1943 * @hide
1944 */
1945 public static void addFieldsFromContext(Context context, Notification notification) {
1946 if (notification.extras.getParcelable(EXTRA_BUILDER_APPLICATION_INFO) == null) {
1947 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO,
1948 context.getApplicationInfo());
1949 }
1950 if (!notification.extras.containsKey(EXTRA_ORIGINATING_USERID)) {
1951 notification.extras.putInt(EXTRA_ORIGINATING_USERID, context.getUserId());
1952 }
1953 }
1954
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001955 @Override
1956 public String toString() {
1957 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001958 sb.append("Notification(pri=");
1959 sb.append(priority);
1960 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001961 if (contentView != null) {
1962 sb.append(contentView.getPackage());
1963 sb.append("/0x");
1964 sb.append(Integer.toHexString(contentView.getLayoutId()));
1965 } else {
1966 sb.append("null");
1967 }
1968 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001969 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1970 sb.append("default");
1971 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001972 int N = this.vibrate.length-1;
1973 sb.append("[");
1974 for (int i=0; i<N; i++) {
1975 sb.append(this.vibrate[i]);
1976 sb.append(',');
1977 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001978 if (N != -1) {
1979 sb.append(this.vibrate[N]);
1980 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001981 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001982 } else {
1983 sb.append("null");
1984 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001985 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001986 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001988 } else if (this.sound != null) {
1989 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001990 } else {
1991 sb.append("null");
1992 }
Chris Wren365b6d32015-07-16 10:39:26 -04001993 if (this.tickerText != null) {
1994 sb.append(" tick");
1995 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001996 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001997 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001998 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001999 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002000 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002001 if (this.category != null) {
2002 sb.append(" category=");
2003 sb.append(this.category);
2004 }
2005 if (this.mGroupKey != null) {
2006 sb.append(" groupKey=");
2007 sb.append(this.mGroupKey);
2008 }
2009 if (this.mSortKey != null) {
2010 sb.append(" sortKey=");
2011 sb.append(this.mSortKey);
2012 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002013 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002014 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002015 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002016 }
2017 sb.append(" vis=");
2018 sb.append(visibilityToString(this.visibility));
2019 if (this.publicVersion != null) {
2020 sb.append(" publicVersion=");
2021 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002022 }
Julia Reynolds054c5dc2015-11-17 15:36:30 -05002023 if (topic != null) {
2024 sb.append("topic=");
2025 sb.append(topic.toString());
Julia Reynolds74303cf2015-10-16 11:37:55 -04002026 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002027 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002028 return sb.toString();
2029 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002030
Dan Sandler1b718782014-07-18 12:43:45 -04002031 /**
2032 * {@hide}
2033 */
2034 public static String visibilityToString(int vis) {
2035 switch (vis) {
2036 case VISIBILITY_PRIVATE:
2037 return "PRIVATE";
2038 case VISIBILITY_PUBLIC:
2039 return "PUBLIC";
2040 case VISIBILITY_SECRET:
2041 return "SECRET";
2042 default:
2043 return "UNKNOWN(" + String.valueOf(vis) + ")";
2044 }
2045 }
2046
Joe Onoratocb109a02011-01-18 17:57:41 -08002047 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002048 * {@hide}
2049 */
2050 public static String priorityToString(@Priority int pri) {
2051 switch (pri) {
2052 case PRIORITY_MIN:
2053 return "MIN";
2054 case PRIORITY_LOW:
2055 return "LOW";
2056 case PRIORITY_DEFAULT:
2057 return "DEFAULT";
2058 case PRIORITY_HIGH:
2059 return "HIGH";
2060 case PRIORITY_MAX:
2061 return "MAX";
2062 default:
2063 return "UNKNOWN(" + String.valueOf(pri) + ")";
2064 }
2065 }
2066
2067 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002068 * The small icon representing this notification in the status bar and content view.
2069 *
2070 * @return the small icon representing this notification.
2071 *
2072 * @see Builder#getSmallIcon()
2073 * @see Builder#setSmallIcon(Icon)
2074 */
2075 public Icon getSmallIcon() {
2076 return mSmallIcon;
2077 }
2078
2079 /**
2080 * Used when notifying to clean up legacy small icons.
2081 * @hide
2082 */
2083 public void setSmallIcon(Icon icon) {
2084 mSmallIcon = icon;
2085 }
2086
2087 /**
2088 * The large icon shown in this notification's content view.
2089 * @see Builder#getLargeIcon()
2090 * @see Builder#setLargeIcon(Icon)
2091 */
2092 public Icon getLargeIcon() {
2093 return mLargeIcon;
2094 }
2095
2096 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002097 * @hide
2098 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002099 public boolean isGroupSummary() {
2100 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2101 }
2102
2103 /**
2104 * @hide
2105 */
2106 public boolean isGroupChild() {
2107 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2108 }
2109
2110 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002111 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002112 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002113 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002114 * content views using the platform's notification layout template. If your app supports
2115 * versions of Android as old as API level 4, you can instead use
2116 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2117 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2118 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002119 *
Scott Main183bf112012-08-13 19:12:13 -07002120 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002121 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002122 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002123 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002124 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2125 * .setContentText(subject)
2126 * .setSmallIcon(R.drawable.new_mail)
2127 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002128 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002129 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002130 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002131 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002132 /**
2133 * @hide
2134 */
2135 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2136 "android.rebuild.contentViewActionCount";
2137 /**
2138 * @hide
2139 */
2140 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2141 = "android.rebuild.bigViewActionCount";
2142 /**
2143 * @hide
2144 */
2145 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2146 = "android.rebuild.hudViewActionCount";
2147
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002148 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002149
Joe Onorato46439ce2010-11-19 13:56:21 -08002150 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002151 private Notification mN;
2152 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002153 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002154 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2155 private ArrayList<String> mPersonList = new ArrayList<String>();
2156 private NotificationColorUtil mColorUtil;
2157 private boolean mColorUtilInited = false;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002158
2159 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002160 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002161 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002162
2163 * <table>
2164 * <tr><th align=right>priority</th>
2165 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2166 * <tr><th align=right>when</th>
2167 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2168 * <tr><th align=right>audio stream</th>
2169 * <td>{@link #STREAM_DEFAULT}</td></tr>
2170 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002171 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002172
2173 * @param context
2174 * A {@link Context} that will be used by the Builder to construct the
2175 * RemoteViews. The Context will not be held past the lifetime of this Builder
2176 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002177 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002178 public Builder(Context context) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002179 this(context, null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002180 }
2181
Joe Onoratocb109a02011-01-18 17:57:41 -08002182 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002183 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002184 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002185 public Builder(Context context, Notification toAdopt) {
2186 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002187
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002188 if (toAdopt == null) {
2189 mN = new Notification();
2190 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2191 mN.priority = PRIORITY_DEFAULT;
2192 mN.visibility = VISIBILITY_PRIVATE;
2193 } else {
2194 mN = toAdopt;
2195 if (mN.actions != null) {
2196 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002197 }
2198
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002199 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2200 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2201 }
2202
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002203 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2204 if (!TextUtils.isEmpty(templateClass)) {
2205 final Class<? extends Style> styleClass
2206 = getNotificationStyleClass(templateClass);
2207 if (styleClass == null) {
2208 Log.d(TAG, "Unknown style class: " + templateClass);
2209 } else {
2210 try {
2211 final Constructor<? extends Style> ctor = styleClass.getConstructor();
2212 ctor.setAccessible(true);
2213 final Style style = ctor.newInstance();
2214 style.restoreFromExtras(mN.extras);
2215
2216 if (style != null) {
2217 setStyle(style);
2218 }
2219 } catch (Throwable t) {
2220 Log.e(TAG, "Could not create Style", t);
2221 }
2222 }
2223 }
2224
2225 }
2226 }
2227
2228 private NotificationColorUtil getColorUtil() {
2229 if (!mColorUtilInited) {
2230 mColorUtilInited = true;
2231 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
2232 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002233 }
2234 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002235 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002236 }
2237
2238 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002239 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07002240 * It will be shown in the notification content view by default; use
Griff Hazen50c11652014-05-16 09:46:31 -07002241 * {@link #setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002242 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002243 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002244 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002245 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002246 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002247 return this;
2248 }
2249
Joe Onoratocb109a02011-01-18 17:57:41 -08002250 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002251 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002252 * in the content view.
2253 */
2254 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002255 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002256 return this;
2257 }
2258
2259 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002260 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002261 *
2262 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002263 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002264 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002265 * Useful when showing an elapsed time (like an ongoing phone call).
2266 *
2267 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002268 * @see Notification#when
2269 */
2270 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002271 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002272 return this;
2273 }
2274
2275 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002276 * Set the small icon resource, which will be used to represent the notification in the
2277 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002278 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002279
2280 * The platform template for the expanded view will draw this icon in the left, unless a
2281 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2282 * icon will be moved to the right-hand side.
2283 *
2284
2285 * @param icon
2286 * A resource ID in the application's package of the drawable to use.
2287 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002288 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002289 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002290 return setSmallIcon(icon != 0
2291 ? Icon.createWithResource(mContext, icon)
2292 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002293 }
2294
Joe Onoratocb109a02011-01-18 17:57:41 -08002295 /**
2296 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2297 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2298 * LevelListDrawable}.
2299 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002300 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002301 * @param level The level to use for the icon.
2302 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002303 * @see Notification#icon
2304 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002305 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002306 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002307 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002308 return setSmallIcon(icon);
2309 }
2310
2311 /**
2312 * Set the small icon, which will be used to represent the notification in the
2313 * status bar and content view (unless overriden there by a
2314 * {@link #setLargeIcon(Bitmap) large icon}).
2315 *
2316 * @param icon An Icon object to use.
2317 * @see Notification#icon
2318 */
2319 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002320 mN.setSmallIcon(icon);
2321 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2322 mN.icon = icon.getResId();
2323 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002324 return this;
2325 }
2326
Joe Onoratocb109a02011-01-18 17:57:41 -08002327 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002328 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002329 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002330 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002331 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002332 return this;
2333 }
2334
Joe Onoratocb109a02011-01-18 17:57:41 -08002335 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002336 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002337 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002338 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002339 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002340 return this;
2341 }
2342
Joe Onoratocb109a02011-01-18 17:57:41 -08002343 /**
Joe Malin8d40d042012-11-05 11:36:40 -08002344 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002345 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
2346 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002347 */
2348 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002349 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002350 return this;
2351 }
2352
2353 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002354 * Set the remote input history.
2355 *
2356 * This should be set to the most recent inputs that have been sent
2357 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2358 * longer relevant (e.g. for chat notifications once the other party has responded).
2359 *
2360 * The most recent input must be stored at the 0 index, the second most recent at the
2361 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2362 * and how much of each individual input is shown.
2363 *
2364 * <p>Note: The reply text will only be shown on notifications that have least one action
2365 * with a {@code RemoteInput}.</p>
2366 */
2367 public Builder setRemoteInputHistory(CharSequence[] text) {
2368 if (text == null) {
2369 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2370 } else {
2371 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2372 CharSequence[] safe = new CharSequence[N];
2373 for (int i = 0; i < N; i++) {
2374 safe[i] = safeCharSequence(text[i]);
2375 }
2376 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2377 }
2378 return this;
2379 }
2380
2381 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002382 * Set the large number at the right-hand side of the notification. This is
2383 * equivalent to setContentInfo, although it might show the number in a different
2384 * font size for readability.
2385 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002386 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002387 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002388 return this;
2389 }
2390
Joe Onoratocb109a02011-01-18 17:57:41 -08002391 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002392 * A small piece of additional information pertaining to this notification.
2393 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002394 * The platform template will draw this on the last line of the notification, at the far
2395 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08002396 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002397 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002398 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002399 return this;
2400 }
2401
Joe Onoratocb109a02011-01-18 17:57:41 -08002402 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002403 * Set the progress this notification represents.
2404 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002405 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002406 */
2407 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002408 mN.extras.putInt(EXTRA_PROGRESS, progress);
2409 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
2410 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002411 return this;
2412 }
2413
2414 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002415 * Supply a custom RemoteViews to use instead of the platform template.
2416 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002417 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08002418 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002419 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002420 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002421 return setCustomContentView(views);
2422 }
2423
2424 /**
2425 * Supply custom RemoteViews to use instead of the platform template.
2426 *
2427 * This will override the layout that would otherwise be constructed by this Builder
2428 * object.
2429 */
2430 public Builder setCustomContentView(RemoteViews contentView) {
2431 mN.contentView = contentView;
2432 return this;
2433 }
2434
2435 /**
2436 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
2437 *
2438 * This will override the expanded layout that would otherwise be constructed by this
2439 * Builder object.
2440 */
2441 public Builder setCustomBigContentView(RemoteViews contentView) {
2442 mN.bigContentView = contentView;
2443 return this;
2444 }
2445
2446 /**
2447 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
2448 *
2449 * This will override the heads-up layout that would otherwise be constructed by this
2450 * Builder object.
2451 */
2452 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
2453 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002454 return this;
2455 }
2456
Joe Onoratocb109a02011-01-18 17:57:41 -08002457 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002458 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2459 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002460 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2461 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2462 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002463 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002464 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002465 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002466 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002467 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002468 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002469 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002470 return this;
2471 }
2472
Joe Onoratocb109a02011-01-18 17:57:41 -08002473 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002474 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2475 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002476 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002477 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002478 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002479 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002480 return this;
2481 }
2482
Joe Onoratocb109a02011-01-18 17:57:41 -08002483 /**
2484 * An intent to launch instead of posting the notification to the status bar.
2485 * Only for use with extremely high-priority notifications demanding the user's
2486 * <strong>immediate</strong> attention, such as an incoming phone call or
2487 * alarm clock that the user has explicitly set to a particular time.
2488 * If this facility is used for something else, please give the user an option
2489 * to turn it off and use a normal notification, as this can be extremely
2490 * disruptive.
2491 *
Chris Wren47c20a12014-06-18 17:27:29 -04002492 * <p>
2493 * The system UI may choose to display a heads-up notification, instead of
2494 * launching this intent, while the user is using the device.
2495 * </p>
2496 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002497 * @param intent The pending intent to launch.
2498 * @param highPriority Passing true will cause this notification to be sent
2499 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002500 *
2501 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002502 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002503 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002504 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002505 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2506 return this;
2507 }
2508
Joe Onoratocb109a02011-01-18 17:57:41 -08002509 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002510 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002511 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002512 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002513 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002514 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002515 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002516 return this;
2517 }
2518
Joe Onoratocb109a02011-01-18 17:57:41 -08002519 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002520 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002521 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002522 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002523 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002524 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002525 setTicker(tickerText);
2526 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08002527 return this;
2528 }
2529
Joe Onoratocb109a02011-01-18 17:57:41 -08002530 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002531 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002532 *
2533 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002534 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2535 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002536 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002537 public Builder setLargeIcon(Bitmap b) {
2538 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2539 }
2540
2541 /**
2542 * Add a large icon to the notification content view.
2543 *
2544 * In the platform template, this image will be shown on the left of the notification view
2545 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2546 * badge atop the large icon).
2547 */
2548 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002549 mN.mLargeIcon = icon;
2550 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08002551 return this;
2552 }
2553
Joe Onoratocb109a02011-01-18 17:57:41 -08002554 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002555 * Set the sound to play.
2556 *
John Spurlockc0650f022014-07-19 13:22:39 -04002557 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2558 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002559 *
Chris Wren47c20a12014-06-18 17:27:29 -04002560 * <p>
2561 * A notification that is noisy is more likely to be presented as a heads-up notification.
2562 * </p>
2563 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002564 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002565 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002566 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002567 mN.sound = sound;
2568 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002569 return this;
2570 }
2571
Joe Onoratocb109a02011-01-18 17:57:41 -08002572 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002573 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002574 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002575 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2576 *
Chris Wren47c20a12014-06-18 17:27:29 -04002577 * <p>
2578 * A notification that is noisy is more likely to be presented as a heads-up notification.
2579 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002580 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002581 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002582 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002583 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002584 public Builder setSound(Uri sound, int streamType) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002585 mN.sound = sound;
2586 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08002587 return this;
2588 }
2589
Joe Onoratocb109a02011-01-18 17:57:41 -08002590 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002591 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2592 * use during playback.
2593 *
2594 * <p>
2595 * A notification that is noisy is more likely to be presented as a heads-up notification.
2596 * </p>
2597 *
2598 * @see Notification#sound
2599 */
2600 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002601 mN.sound = sound;
2602 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04002603 return this;
2604 }
2605
2606 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002607 * Set the vibration pattern to use.
2608 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002609 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2610 * <code>pattern</code> parameter.
2611 *
Chris Wren47c20a12014-06-18 17:27:29 -04002612 * <p>
2613 * A notification that vibrates is more likely to be presented as a heads-up notification.
2614 * </p>
2615 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002616 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002617 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002618 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002619 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08002620 return this;
2621 }
2622
Joe Onoratocb109a02011-01-18 17:57:41 -08002623 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002624 * Set the desired color for the indicator LED on the device, as well as the
2625 * blink duty cycle (specified in milliseconds).
2626 *
2627
2628 * Not all devices will honor all (or even any) of these values.
2629 *
2630
2631 * @see Notification#ledARGB
2632 * @see Notification#ledOnMS
2633 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002634 */
Tor Norbye80756e32015-03-02 09:39:27 -08002635 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002636 mN.ledARGB = argb;
2637 mN.ledOnMS = onMs;
2638 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002639 if (onMs != 0 || offMs != 0) {
2640 mN.flags |= FLAG_SHOW_LIGHTS;
2641 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002642 return this;
2643 }
2644
Joe Onoratocb109a02011-01-18 17:57:41 -08002645 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002646 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002647 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002648
2649 * Ongoing notifications cannot be dismissed by the user, so your application or service
2650 * must take care of canceling them.
2651 *
2652
2653 * They are typically used to indicate a background task that the user is actively engaged
2654 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2655 * (e.g., a file download, sync operation, active network connection).
2656 *
2657
2658 * @see Notification#FLAG_ONGOING_EVENT
2659 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002660 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002661 public Builder setOngoing(boolean ongoing) {
2662 setFlag(FLAG_ONGOING_EVENT, ongoing);
2663 return this;
2664 }
2665
Joe Onoratocb109a02011-01-18 17:57:41 -08002666 /**
2667 * Set this flag if you would only like the sound, vibrate
2668 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002669 *
2670 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002671 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002672 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2673 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2674 return this;
2675 }
2676
Joe Onoratocb109a02011-01-18 17:57:41 -08002677 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002678 * Make this notification automatically dismissed when the user touches it. The
2679 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2680 *
2681 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002682 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002683 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002684 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002685 return this;
2686 }
2687
Joe Onoratocb109a02011-01-18 17:57:41 -08002688 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002689 * Set whether or not this notification should not bridge to other devices.
2690 *
2691 * <p>Some notifications can be bridged to other devices for remote display.
2692 * This hint can be set to recommend this notification not be bridged.
2693 */
2694 public Builder setLocalOnly(boolean localOnly) {
2695 setFlag(FLAG_LOCAL_ONLY, localOnly);
2696 return this;
2697 }
2698
2699 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002700 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002701 * <p>
2702 * The value should be one or more of the following fields combined with
2703 * bitwise-or:
2704 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2705 * <p>
2706 * For all default values, use {@link #DEFAULT_ALL}.
2707 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002708 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002709 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08002710 return this;
2711 }
2712
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002713 /**
2714 * Set the priority of this notification.
2715 *
2716 * @see Notification#priority
2717 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002718 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002719 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002720 return this;
2721 }
Joe Malin8d40d042012-11-05 11:36:40 -08002722
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002723 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002724 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002725 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002726 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002727 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002728 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002729 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002730 return this;
2731 }
2732
2733 /**
Chris Wrendde75302014-03-26 17:24:15 -04002734 * Add a person that is relevant to this notification.
2735 *
Chris Wrene6c48932014-09-29 17:19:27 -04002736 * <P>
2737 * Depending on user preferences, this annotation may allow the notification to pass
2738 * through interruption filters, and to appear more prominently in the user interface.
2739 * </P>
2740 *
2741 * <P>
2742 * The person should be specified by the {@code String} representation of a
2743 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2744 * </P>
2745 *
2746 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2747 * URIs. The path part of these URIs must exist in the contacts database, in the
2748 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2749 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2750 * </P>
2751 *
2752 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002753 * @see Notification#EXTRA_PEOPLE
2754 */
Chris Wrene6c48932014-09-29 17:19:27 -04002755 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002756 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002757 return this;
2758 }
2759
2760 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002761 * Set this notification to be part of a group of notifications sharing the same key.
2762 * Grouped notifications may display in a cluster or stack on devices which
2763 * support such rendering.
2764 *
2765 * <p>To make this notification the summary for its group, also call
2766 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2767 * {@link #setSortKey}.
2768 * @param groupKey The group key of the group.
2769 * @return this object for method chaining
2770 */
2771 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002772 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002773 return this;
2774 }
2775
2776 /**
2777 * Set this notification to be the group summary for a group of notifications.
2778 * Grouped notifications may display in a cluster or stack on devices which
2779 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2780 * @param isGroupSummary Whether this notification should be a group summary.
2781 * @return this object for method chaining
2782 */
2783 public Builder setGroupSummary(boolean isGroupSummary) {
2784 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2785 return this;
2786 }
2787
2788 /**
2789 * Set a sort key that orders this notification among other notifications from the
2790 * same package. This can be useful if an external sort was already applied and an app
2791 * would like to preserve this. Notifications will be sorted lexicographically using this
2792 * value, although providing different priorities in addition to providing sort key may
2793 * cause this value to be ignored.
2794 *
2795 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002796 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002797 *
2798 * @see String#compareTo(String)
2799 */
2800 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002801 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002802 return this;
2803 }
2804
2805 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002806 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002807 *
Griff Hazen720042b2014-02-24 15:46:56 -08002808 * <p>Values within the Bundle will replace existing extras values in this Builder.
2809 *
2810 * @see Notification#extras
2811 */
Griff Hazen959591e2014-05-15 22:26:18 -07002812 public Builder addExtras(Bundle extras) {
2813 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002814 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08002815 }
2816 return this;
2817 }
2818
2819 /**
2820 * Set metadata for this notification.
2821 *
2822 * <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 -04002823 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002824 * called.
2825 *
Griff Hazen720042b2014-02-24 15:46:56 -08002826 * <p>Replaces any existing extras values with those from the provided Bundle.
2827 * Use {@link #addExtras} to merge in metadata instead.
2828 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002829 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002830 */
Griff Hazen959591e2014-05-15 22:26:18 -07002831 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002832 if (extras != null) {
2833 mUserExtras = extras;
2834 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002835 return this;
2836 }
2837
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002838 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002839 * Get the current metadata Bundle used by this notification Builder.
2840 *
2841 * <p>The returned Bundle is shared with this Builder.
2842 *
2843 * <p>The current contents of this Bundle are copied into the Notification each time
2844 * {@link #build()} is called.
2845 *
2846 * @see Notification#extras
2847 */
2848 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002849 return mUserExtras;
2850 }
2851
2852 private Bundle getAllExtras() {
2853 final Bundle saveExtras = (Bundle) mUserExtras.clone();
2854 saveExtras.putAll(mN.extras);
2855 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08002856 }
2857
2858 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002859 * Add an action to this notification. Actions are typically displayed by
2860 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002861 * <p>
2862 * Every action must have an icon (32dp square and matching the
2863 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2864 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2865 * <p>
2866 * A notification in its expanded form can display up to 3 actions, from left to right in
2867 * the order they were added. Actions will not be displayed when the notification is
2868 * collapsed, however, so be sure that any essential functions may be accessed by the user
2869 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002870 *
2871 * @param icon Resource ID of a drawable that represents the action.
2872 * @param title Text describing the action.
2873 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04002874 *
2875 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002876 */
Dan Sandler86647982015-05-13 23:41:13 -04002877 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002878 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002879 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002880 return this;
2881 }
2882
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002883 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002884 * Add an action to this notification. Actions are typically displayed by
2885 * the system as a button adjacent to the notification content.
2886 * <p>
2887 * Every action must have an icon (32dp square and matching the
2888 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2889 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2890 * <p>
2891 * A notification in its expanded form can display up to 3 actions, from left to right in
2892 * the order they were added. Actions will not be displayed when the notification is
2893 * collapsed, however, so be sure that any essential functions may be accessed by the user
2894 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2895 *
2896 * @param action The action to add.
2897 */
2898 public Builder addAction(Action action) {
2899 mActions.add(action);
2900 return this;
2901 }
2902
2903 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002904 * Alter the complete list of actions attached to this notification.
2905 * @see #addAction(Action).
2906 *
2907 * @param actions
2908 * @return
2909 */
2910 public Builder setActions(Action... actions) {
2911 mActions.clear();
2912 for (int i = 0; i < actions.length; i++) {
2913 mActions.add(actions[i]);
2914 }
2915 return this;
2916 }
2917
2918 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002919 * Add a rich notification style to be applied at build time.
2920 *
2921 * @param style Object responsible for modifying the notification style.
2922 */
2923 public Builder setStyle(Style style) {
2924 if (mStyle != style) {
2925 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002926 if (mStyle != null) {
2927 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002928 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
2929 } else {
2930 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002931 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002932 }
2933 return this;
2934 }
2935
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002936 /**
2937 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002938 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002939 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2940 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
2941 *
2942 * @return The same Builder.
2943 */
2944 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002945 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002946 return this;
2947 }
2948
2949 /**
2950 * Supply a replacement Notification whose contents should be shown in insecure contexts
2951 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
2952 * @param n A replacement notification, presumably with some or all info redacted.
2953 * @return The same Builder.
2954 */
2955 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002956 if (n != null) {
2957 mN.publicVersion = new Notification();
2958 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
2959 } else {
2960 mN.publicVersion = null;
2961 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002962 return this;
2963 }
2964
Griff Hazenb720abe2014-05-20 13:15:30 -07002965 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002966 * Apply an extender to this notification builder. Extenders may be used to add
2967 * metadata or change options on this builder.
2968 */
Griff Hazen61a9e862014-05-22 16:05:19 -07002969 public Builder extend(Extender extender) {
2970 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002971 return this;
2972 }
2973
Dan Sandler4e787062015-06-17 15:09:48 -04002974 /**
2975 * @hide
2976 */
2977 public void setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002978 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002979 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08002980 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002981 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08002982 }
2983 }
2984
Dan Sandler26e81cf2014-05-06 10:01:27 -04002985 /**
2986 * Sets {@link Notification#color}.
2987 *
2988 * @param argb The accent color to use
2989 *
2990 * @return The same Builder.
2991 */
Tor Norbye80756e32015-03-02 09:39:27 -08002992 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002993 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002994 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04002995 return this;
2996 }
2997
Julia Reynolds74303cf2015-10-16 11:37:55 -04002998 /**
Julia Reynolds054c5dc2015-11-17 15:36:30 -05002999 * Sets the topic of this notification. Topics are typically displayed in Notification
Julia Reynolds74303cf2015-10-16 11:37:55 -04003000 * settings.
3001 * <p>
3002 * Every topic must have an id and a textual label.
3003 *
3004 * @param topic The topic to add.
3005 */
Julia Reynolds054c5dc2015-11-17 15:36:30 -05003006 public Builder setTopic(Topic topic) {
3007 mN.topic = topic;
Julia Reynolds74303cf2015-10-16 11:37:55 -04003008 return this;
3009 }
3010
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003011 private Drawable getProfileBadgeDrawable() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02003012 // Note: This assumes that the current user can read the profile badge of the
3013 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003014 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003015 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003016 }
3017
3018 private Bitmap getProfileBadge() {
3019 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003020 if (badge == null) {
3021 return null;
3022 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003023 final int size = mContext.getResources().getDimensionPixelSize(
3024 R.dimen.notification_badge_size);
3025 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003026 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003027 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003028 badge.draw(canvas);
3029 return bitmap;
3030 }
3031
Selim Cinekc848c3a2016-01-13 15:27:30 -08003032 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003033 Bitmap profileBadge = getProfileBadge();
3034
Kenny Guy98193ea2014-07-24 19:54:37 +01003035 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003036 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3037 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Kenny Guy98193ea2014-07-24 19:54:37 +01003038 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003039 }
3040
Christoph Studerfe718432014-09-01 18:21:18 +02003041 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003042 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003043 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003044 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003045 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003046 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003047 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003048 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003049 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003050 contentView.setTextViewText(R.id.text_line_1, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003051 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003052 }
3053
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003054 /**
3055 * Resets the notification header to its original state
3056 */
3057 private void resetNotificationHeader(RemoteViews contentView) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003058 contentView.setImageViewResource(R.id.icon, 0);
Selim Cinek7b836392015-12-04 20:02:59 -08003059 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003060 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003061 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003062 contentView.setViewVisibility(R.id.header_sub_text, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003063 contentView.setViewVisibility(R.id.header_content_info, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003064 contentView.setViewVisibility(R.id.number_of_children, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003065 contentView.setViewVisibility(R.id.sub_text_divider, View.GONE);
3066 contentView.setViewVisibility(R.id.content_info_divider, View.GONE);
3067 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003068 contentView.setImageViewIcon(R.id.profile_badge, null);
3069 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003070 }
3071
3072 private void resetContentMargins(RemoteViews contentView) {
3073 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003074 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003075 }
3076
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003077 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003078 return applyStandardTemplate(resId, true /* hasProgress */);
3079 }
3080
3081 /**
3082 * @param hasProgress whether the progress bar should be shown and set
3083 */
3084 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Kenny Guy77320062014-08-27 21:37:15 +01003085 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003086
Christoph Studerfe718432014-09-01 18:21:18 +02003087 resetStandardTemplate(contentView);
3088
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003089 final Bundle ex = mN.extras;
Dan Sandler190d58d2014-05-15 09:33:39 -04003090
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003091 bindNotificationHeader(contentView);
3092 bindLargeIcon(contentView);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003093 if (ex.getCharSequence(EXTRA_TITLE) != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003094 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003095 contentView.setTextViewText(R.id.title,
3096 processLegacyText(ex.getCharSequence(EXTRA_TITLE)));
Joe Onorato561d3852010-11-20 18:09:34 -08003097 }
Selim Cinek29603462015-11-17 19:04:39 -08003098 boolean showProgress = handleProgressBar(hasProgress, contentView, ex);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003099 if (ex.getCharSequence(EXTRA_TEXT) != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003100 int textId = showProgress ? com.android.internal.R.id.text_line_1
3101 : com.android.internal.R.id.text;
3102 contentView.setTextViewText(textId, processLegacyText(
3103 ex.getCharSequence(EXTRA_TEXT)));
3104 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003105 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003106
Selim Cinek860b6da2015-12-16 19:02:19 -08003107 setContentMinHeight(contentView, showProgress || mN.mLargeIcon != null);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003108
Selim Cinek29603462015-11-17 19:04:39 -08003109 return contentView;
3110 }
3111
Selim Cinek860b6da2015-12-16 19:02:19 -08003112 /**
3113 * @param remoteView the remote view to update the minheight in
3114 * @param hasMinHeight does it have a mimHeight
3115 * @hide
3116 */
3117 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3118 int minHeight = 0;
3119 if (hasMinHeight) {
3120 // we need to set the minHeight of the notification
3121 minHeight = mContext.getResources().getDimensionPixelSize(
3122 com.android.internal.R.dimen.notification_min_content_height);
3123 }
3124 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3125 }
3126
Selim Cinek29603462015-11-17 19:04:39 -08003127 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003128 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3129 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3130 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3131 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003132 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003133 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003134 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003135 contentView.setProgressBackgroundTintList(
3136 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3137 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003138 if (mN.color != COLOR_DEFAULT) {
3139 ColorStateList colorStateList = ColorStateList.valueOf(mN.color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003140 contentView.setProgressTintList(R.id.progress, colorStateList);
3141 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003142 }
Selim Cinek29603462015-11-17 19:04:39 -08003143 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003144 } else {
3145 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003146 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003147 }
Joe Onorato561d3852010-11-20 18:09:34 -08003148 }
3149
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003150 private void bindLargeIcon(RemoteViews contentView) {
3151 if (mN.mLargeIcon != null) {
3152 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3153 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3154 processLargeLegacyIcon(mN.mLargeIcon, contentView);
3155 int endMargin = mContext.getResources().getDimensionPixelSize(
3156 R.dimen.notification_content_picture_margin);
3157 contentView.setViewLayoutMarginEnd(R.id.line1, endMargin);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003158 contentView.setViewLayoutMarginEnd(R.id.text, endMargin);
Selim Cinek29603462015-11-17 19:04:39 -08003159 contentView.setViewLayoutMarginEnd(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003160 }
3161 }
3162
3163 private void bindNotificationHeader(RemoteViews contentView) {
3164 bindSmallIcon(contentView);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003165 bindChildCountColor(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003166 bindHeaderAppName(contentView);
Selim Cinek03d0d652015-11-13 13:18:09 -05003167 bindHeaderSubText(contentView);
Selim Cinek29603462015-11-17 19:04:39 -08003168 bindContentInfo(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003169 bindHeaderChronometerAndTime(contentView);
3170 bindExpandButton(contentView);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003171 bindProfileBadge(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003172 }
3173
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003174 private void bindChildCountColor(RemoteViews contentView) {
3175 contentView.setTextColor(R.id.number_of_children, resolveColor());
3176 }
3177
Selim Cinek29603462015-11-17 19:04:39 -08003178 private void bindContentInfo(RemoteViews contentView) {
3179 boolean visible = false;
3180 if (mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3181 contentView.setTextViewText(R.id.header_content_info,
3182 processLegacyText(mN.extras.getCharSequence(EXTRA_INFO_TEXT)));
3183 contentView.setViewVisibility(R.id.header_content_info, View.VISIBLE);
3184 visible = true;
3185 } else if (mN.number > 0) {
3186 final int tooBig = mContext.getResources().getInteger(
3187 R.integer.status_bar_notification_info_maxnum);
3188 if (mN.number > tooBig) {
3189 contentView.setTextViewText(R.id.header_content_info, processLegacyText(
3190 mContext.getResources().getString(
3191 R.string.status_bar_notification_info_overflow)));
3192 } else {
3193 contentView.setTextViewText(R.id.header_content_info,
3194 processLegacyText(String.valueOf(mN.number)));
3195 }
3196 contentView.setViewVisibility(R.id.header_content_info, View.VISIBLE);
3197 visible = true;
3198 }
3199 if (visible) {
3200 contentView.setViewVisibility(R.id.content_info_divider, View.VISIBLE);
3201 }
3202 }
3203
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003204 private void bindExpandButton(RemoteViews contentView) {
3205 contentView.setDrawableParameters(R.id.expand_button, false, -1, resolveColor(),
3206 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003207 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
3208 resolveColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003209 }
3210
3211 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3212 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003213 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003214 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3215 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3216 contentView.setLong(R.id.chronometer, "setBase",
3217 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3218 contentView.setBoolean(R.id.chronometer, "setStarted", true);
3219 } else {
3220 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3221 contentView.setLong(R.id.time, "setTime", mN.when);
3222 }
3223 }
3224 }
3225
Selim Cinek03d0d652015-11-13 13:18:09 -05003226 private void bindHeaderSubText(RemoteViews contentView) {
3227 CharSequence subText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3228 if (subText == null && mStyle != null && mStyle.mSummaryTextSet
3229 && mStyle.hasSummaryInHeader()) {
3230 subText = mStyle.mSummaryText;
3231 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003232 if (subText != null) {
3233 // TODO: Remove the span entirely to only have the string with propper formating.
3234 contentView.setTextViewText(R.id.header_sub_text, processLegacyText(subText));
3235 contentView.setViewVisibility(R.id.header_sub_text, View.VISIBLE);
Selim Cinek29603462015-11-17 19:04:39 -08003236 contentView.setViewVisibility(R.id.sub_text_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003237 }
3238 }
3239
3240 private void bindHeaderAppName(RemoteViews contentView) {
3241 PackageManager packageManager = mContext.getPackageManager();
3242 ApplicationInfo info = null;
3243 try {
3244 info = packageManager.getApplicationInfo(mContext.getApplicationInfo().packageName,
3245 0);
3246 } catch (final NameNotFoundException e) {
3247 return;
3248 }
3249 CharSequence appName = info != null ? packageManager.getApplicationLabel(info)
3250 : null;
3251 if (TextUtils.isEmpty(appName)) {
3252 return;
3253 }
3254 contentView.setTextViewText(R.id.app_name_text, appName);
Selim Cinekaef485a2016-02-05 10:31:20 -08003255 contentView.setTextColor(R.id.app_name_text, resolveColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003256 }
3257
3258 private void bindSmallIcon(RemoteViews contentView) {
3259 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
3260 processSmallIconColor(mN.mSmallIcon, contentView);
3261 }
3262
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003263 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003264 * @return true if the built notification will show the time or the chronometer; false
3265 * otherwise
3266 */
3267 private boolean showsTimeOrChronometer() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003268 return mN.when != 0 && mN.extras.getBoolean(EXTRA_SHOW_WHEN);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003269 }
3270
Christoph Studerfe718432014-09-01 18:21:18 +02003271 private void resetStandardTemplateWithActions(RemoteViews big) {
3272 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003273 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003274
3275 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3276 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3277
3278 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3279 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3280 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3281 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003282 }
3283
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003284 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003285 RemoteViews big = applyStandardTemplate(layoutId);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003286
Christoph Studerfe718432014-09-01 18:21:18 +02003287 resetStandardTemplateWithActions(big);
3288
Adrian Roose458aa82015-12-08 16:17:19 -08003289 boolean validRemoteInput = false;
3290
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003291 int N = mActions.size();
3292 if (N > 0) {
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003293 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003294 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003295 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003296 Action action = mActions.get(i);
3297 validRemoteInput |= hasValidRemoteInput(action);
3298
3299 final RemoteViews button = generateActionButton(action);
Adrian Roos9b123cf2016-02-04 14:55:57 -08003300 if (i == N - 1) {
3301 button.setViewLayoutWidth(com.android.internal.R.id.action0,
3302 ViewGroup.LayoutParams.MATCH_PARENT);
3303 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003304 big.addView(R.id.actions, button);
3305 }
3306 }
Adrian Roose458aa82015-12-08 16:17:19 -08003307
3308 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
3309 if (validRemoteInput && replyText != null
3310 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3311 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3312 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
3313
3314 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3315 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3316 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
3317
3318 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3319 big.setViewVisibility(
3320 R.id.notification_material_reply_text_3, View.VISIBLE);
3321 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
3322 }
3323 }
3324 }
3325
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003326 return big;
3327 }
3328
Adrian Roose458aa82015-12-08 16:17:19 -08003329 private boolean hasValidRemoteInput(Action action) {
3330 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
3331 // Weird actions
3332 return false;
3333 }
3334
3335 RemoteInput[] remoteInputs = action.getRemoteInputs();
3336 if (remoteInputs == null) {
3337 return false;
3338 }
3339
3340 for (RemoteInput r : remoteInputs) {
3341 CharSequence[] choices = r.getChoices();
3342 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
3343 return true;
3344 }
3345 }
3346 return false;
3347 }
3348
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003349 /**
3350 * Construct a RemoteViews for the final 1U notification layout. In order:
3351 * 1. Custom contentView from the caller
3352 * 2. Style's proposed content view
3353 * 3. Standard template view
3354 */
3355 public RemoteViews makeContentView() {
3356 if (mN.contentView != null) {
3357 return mN.contentView;
3358 } else if (mStyle != null) {
3359 final RemoteViews styleView = mStyle.makeContentView();
3360 if (styleView != null) {
3361 return styleView;
3362 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003363 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003364 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003365 }
3366
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003367 /**
3368 * Construct a RemoteViews for the final big notification layout.
3369 */
3370 public RemoteViews makeBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05003371 RemoteViews result = null;
Julia Reynolds089e3e42015-11-18 09:59:57 -05003372 if (mN.bigContentView != null) {
3373 return mN.bigContentView;
3374 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05003375 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08003376 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003377 } else if (mActions.size() != 0) {
3378 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05003379 }
3380 adaptNotificationHeaderForBigContentView(result);
3381 return result;
3382 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003383
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003384 /**
3385 * Construct a RemoteViews for the final notification header only
3386 *
3387 * @hide
3388 */
3389 public RemoteViews makeNotificationHeader() {
3390 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
3391 R.layout.notification_template_header);
3392 resetNotificationHeader(header);
3393 bindNotificationHeader(header);
3394 return header;
3395 }
3396
Selim Cinek29603462015-11-17 19:04:39 -08003397 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003398 if (result != null) {
3399 result.setViewVisibility(R.id.text_line_1, View.GONE);
3400 }
Selim Cinek29603462015-11-17 19:04:39 -08003401 }
3402
Selim Cinek850a8542015-11-11 11:48:36 -05003403 private void adaptNotificationHeaderForBigContentView(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003404 if (result != null) {
3405 result.setBoolean(R.id.notification_header, "setExpanded", true);
3406 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003407 }
3408
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003409 /**
3410 * Construct a RemoteViews for the final heads-up notification layout.
3411 */
3412 public RemoteViews makeHeadsUpContentView() {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003413 if (mN.headsUpContentView != null) {
3414 return mN.headsUpContentView;
3415 } else if (mStyle != null) {
3416 final RemoteViews styleView = mStyle.makeHeadsUpContentView();
3417 if (styleView != null) {
3418 return styleView;
3419 }
3420 } else if (mActions.size() == 0) {
3421 return null;
3422 }
3423
Chris Wren8fd39ec2014-02-27 17:43:26 -05003424
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003425 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003426 }
3427
Selim Cinek624c02db2015-12-14 21:00:02 -08003428 /**
3429 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
3430 *
3431 * @hide
3432 */
3433 public RemoteViews makePublicContentView() {
3434 if (mN.publicVersion != null) {
3435 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
3436 return builder.makeContentView();
3437 }
3438 Bundle savedBundle = mN.extras;
3439 Style style = mStyle;
3440 mStyle = null;
3441 Icon largeIcon = mN.mLargeIcon;
3442 mN.mLargeIcon = null;
3443 Bundle publicExtras = new Bundle();
3444 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
3445 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
3446 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
3447 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
3448 publicExtras.putCharSequence(EXTRA_TITLE,
3449 mContext.getString(R.string.notification_hidden_text));
3450 mN.extras = publicExtras;
3451 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
3452 mN.extras = savedBundle;
3453 mN.mLargeIcon = largeIcon;
3454 mStyle = style;
3455 return publicView;
3456 }
3457
3458
Chris Wren8fd39ec2014-02-27 17:43:26 -05003459
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003460 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003461 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07003462 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003463 tombstone ? getActionTombstoneLayoutResource()
3464 : getActionLayoutResource());
Dan Sandler68079d52015-07-22 10:45:30 -04003465 final Icon ai = action.getIcon();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003466 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04003467 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003468 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003469 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003470 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003471 if (action.mRemoteInputs != null) {
3472 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
3473 }
3474 if (mN.color != COLOR_DEFAULT) {
3475 button.setTextColor(R.id.action0, mN.color);
3476 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003477 return button;
3478 }
3479
Joe Onoratocb109a02011-01-18 17:57:41 -08003480 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003481 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003482 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003483 */
3484 private boolean isLegacy() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003485 return getColorUtil() != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003486 }
3487
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003488 private CharSequence processLegacyText(CharSequence charSequence) {
3489 if (isLegacy()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003490 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003491 } else {
3492 return charSequence;
3493 }
3494 }
3495
Dan Sandler26e81cf2014-05-06 10:01:27 -04003496 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003497 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04003498 */
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003499 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView) {
Selim Cinekea4bef72015-12-02 15:51:10 -08003500 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
3501 if (colorable) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003502 contentView.setDrawableParameters(R.id.icon, false, -1, resolveColor(),
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003503 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003504
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003505 }
Selim Cinekea4bef72015-12-02 15:51:10 -08003506 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
3507 colorable ? resolveColor() : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003508 }
3509
Dan Sandler26e81cf2014-05-06 10:01:27 -04003510 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003511 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04003512 * if it's grayscale).
3513 */
3514 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003515 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3516 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003517 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003518 // resolve color will fall back to the default when legacy
3519 contentView.setDrawableParameters(R.id.icon, false, -1, resolveColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003520 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003521 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003522 }
3523
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003524 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003525 if (mN.color != COLOR_DEFAULT) {
3526 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02003527 }
Jorim Jaggi74419312014-06-10 20:57:21 +02003528 }
3529
Selim Cinek5bf069a2015-11-10 19:14:27 -05003530 int resolveColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003531 if (mN.color == COLOR_DEFAULT) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003532 return mContext.getColor(R.color.notification_icon_default_color);
Dan Sandler26e81cf2014-05-06 10:01:27 -04003533 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003534 return mN.color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003535 }
3536
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003537 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003538 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003539 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003540 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003541 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003542 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003543 mN.actions = new Action[mActions.size()];
3544 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003545 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003546 if (!mPersonList.isEmpty()) {
3547 mN.extras.putStringArray(EXTRA_PEOPLE,
3548 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003549 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003550 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08003551 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003552
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003553 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003554 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003555 ApplicationInfo applicationInfo = n.extras.getParcelable(
3556 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003557 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05003558 if (applicationInfo != null) {
3559 try {
3560 builderContext = context.createApplicationContext(applicationInfo,
3561 Context.CONTEXT_RESTRICTED);
3562 } catch (NameNotFoundException e) {
3563 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
3564 builderContext = context; // try with our context
3565 }
3566 } else {
3567 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02003568 }
3569
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003570 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003571 }
3572
3573 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
3574 Class<? extends Style>[] classes = new Class[]{
3575 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class};
3576 for (Class<? extends Style> innerClass : classes) {
3577 if (templateClass.equals(innerClass.getName())) {
3578 return innerClass;
3579 }
3580 }
3581 return null;
3582 }
3583
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003584 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003585 * @deprecated Use {@link #build()} instead.
3586 */
3587 @Deprecated
3588 public Notification getNotification() {
3589 return build();
3590 }
3591
3592 /**
3593 * Combine all of the options that have been set and return a new {@link Notification}
3594 * object.
3595 */
3596 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003597 // first, add any extras from the calling code
3598 if (mUserExtras != null) {
3599 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003600 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003601
3602 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05003603 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02003604
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003605 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003606
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003607 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003608 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003609 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003610
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003611 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
3612 if (mN.contentView == null) {
3613 mN.contentView = makeContentView();
3614 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
3615 mN.contentView.getSequenceNumber());
3616 }
3617 if (mN.bigContentView == null) {
3618 mN.bigContentView = makeBigContentView();
3619 if (mN.bigContentView != null) {
3620 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
3621 mN.bigContentView.getSequenceNumber());
3622 }
3623 }
3624 if (mN.headsUpContentView == null) {
3625 mN.headsUpContentView = makeHeadsUpContentView();
3626 if (mN.headsUpContentView != null) {
3627 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
3628 mN.headsUpContentView.getSequenceNumber());
3629 }
3630 }
3631 }
3632
Julia Reynolds4c0c2022016-02-02 15:11:59 -05003633 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
3634 mN.flags |= FLAG_SHOW_LIGHTS;
3635 }
3636
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003637 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003638 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003639
3640 /**
3641 * Apply this Builder to an existing {@link Notification} object.
3642 *
3643 * @hide
3644 */
3645 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003646 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003647 return n;
3648 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003649
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05003650 /**
3651 * @hide
3652 */
3653 public static void stripForDelivery(Notification n) {
3654 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
3655 if (TextUtils.isEmpty(templateClass)) {
3656 return;
3657 }
3658 // Only strip views for known Styles because we won't know how to
3659 // re-create them otherwise.
3660 if (getNotificationStyleClass(templateClass) == null) {
3661 return;
3662 }
3663 // Get rid of unmodified BuilderRemoteViews.
3664 if (n.contentView instanceof BuilderRemoteViews &&
3665 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
3666 n.contentView.getSequenceNumber()) {
3667 n.contentView = null;
3668 n.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
3669 }
3670 if (n.bigContentView instanceof BuilderRemoteViews &&
3671 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
3672 n.bigContentView.getSequenceNumber()) {
3673 n.bigContentView = null;
3674 n.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
3675 }
3676 if (n.headsUpContentView instanceof BuilderRemoteViews &&
3677 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
3678 n.headsUpContentView.getSequenceNumber()) {
3679 n.headsUpContentView = null;
3680 n.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
3681 }
3682 }
3683
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003684 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003685 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003686 }
3687
3688 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003689 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003690 }
3691
3692 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003693 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003694 }
3695
3696 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003697 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003698 }
3699
3700 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003701 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003702 }
3703
3704 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003705 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003706 }
3707
3708 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003709 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003710 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003711 }
3712
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003713 /**
3714 * An object that can apply a rich notification style to a {@link Notification.Builder}
3715 * object.
3716 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003717 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003718 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003719
3720 /**
3721 * @hide
3722 */
3723 protected CharSequence mSummaryText = null;
3724
3725 /**
3726 * @hide
3727 */
3728 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003729
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003730 protected Builder mBuilder;
3731
Chris Wrend6297db2012-05-03 16:20:13 -04003732 /**
3733 * Overrides ContentTitle in the big form of the template.
3734 * This defaults to the value passed to setContentTitle().
3735 */
3736 protected void internalSetBigContentTitle(CharSequence title) {
3737 mBigContentTitle = title;
3738 }
3739
3740 /**
3741 * Set the first line of text after the detail section in the big form of the template.
3742 */
3743 protected void internalSetSummaryText(CharSequence cs) {
3744 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003745 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003746 }
3747
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003748 public void setBuilder(Builder builder) {
3749 if (mBuilder != builder) {
3750 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003751 if (mBuilder != null) {
3752 mBuilder.setStyle(this);
3753 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003754 }
3755 }
3756
Chris Wrend6297db2012-05-03 16:20:13 -04003757 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003758 if (mBuilder == null) {
3759 throw new IllegalArgumentException("Style requires a valid Builder object");
3760 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003761 }
Chris Wrend6297db2012-05-03 16:20:13 -04003762
3763 protected RemoteViews getStandardView(int layoutId) {
3764 checkBuilder();
3765
Christoph Studer4600f9b2014-07-22 22:44:43 +02003766 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003767 CharSequence oldBuilderContentTitle =
3768 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003769 if (mBigContentTitle != null) {
3770 mBuilder.setContentTitle(mBigContentTitle);
3771 }
3772
Chris Wrend6297db2012-05-03 16:20:13 -04003773 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3774
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003775 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003776
Chris Wrend6297db2012-05-03 16:20:13 -04003777 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3778 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003779 } else {
3780 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003781 }
3782
Chris Wrend6297db2012-05-03 16:20:13 -04003783 return contentView;
3784 }
3785
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003786 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003787 * Construct a Style-specific RemoteViews for the final 1U notification layout.
3788 * The default implementation has nothing additional to add.
3789 * @hide
3790 */
3791 public RemoteViews makeContentView() {
3792 return null;
3793 }
3794
3795 /**
3796 * Construct a Style-specific RemoteViews for the final big notification layout.
3797 * @hide
3798 */
3799 public RemoteViews makeBigContentView() {
3800 return null;
3801 }
3802
3803 /**
3804 * Construct a Style-specific RemoteViews for the final HUN layout.
3805 * @hide
3806 */
3807 public RemoteViews makeHeadsUpContentView() {
3808 return null;
3809 }
3810
3811 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003812 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003813 * @hide
3814 */
3815 public void addExtras(Bundle extras) {
3816 if (mSummaryTextSet) {
3817 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
3818 }
3819 if (mBigContentTitle != null) {
3820 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
3821 }
Chris Wren91ad5632013-06-05 15:05:57 -04003822 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003823 }
3824
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003825 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003826 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003827 * @hide
3828 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02003829 protected void restoreFromExtras(Bundle extras) {
3830 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
3831 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
3832 mSummaryTextSet = true;
3833 }
3834 if (extras.containsKey(EXTRA_TITLE_BIG)) {
3835 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
3836 }
3837 }
3838
3839
3840 /**
3841 * @hide
3842 */
3843 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003844 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003845 return wip;
3846 }
3847
Daniel Sandler0ec46202015-06-24 01:27:05 -04003848 /**
3849 * @hide
3850 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003851 public void purgeResources() {}
3852
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003853 /**
3854 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
3855 * attached to.
3856 *
3857 * @return the fully constructed Notification.
3858 */
3859 public Notification build() {
3860 checkBuilder();
3861 return mBuilder.build();
3862 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003863
3864 /**
3865 * @hide
3866 * @return true if the style positions the progress bar on the second line; false if the
3867 * style hides the progress bar
3868 */
3869 protected boolean hasProgress() {
3870 return true;
3871 }
Selim Cinek03d0d652015-11-13 13:18:09 -05003872
3873 /**
3874 * @hide
3875 * @return Whether we should put the summary be put into the notification header
3876 */
3877 public boolean hasSummaryInHeader() {
3878 return true;
3879 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003880 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003881
3882 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003883 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08003884 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003885 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003886 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003887 * Notification notif = new Notification.Builder(mContext)
3888 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
3889 * .setContentText(subject)
3890 * .setSmallIcon(R.drawable.new_post)
3891 * .setLargeIcon(aBitmap)
3892 * .setStyle(new Notification.BigPictureStyle()
3893 * .bigPicture(aBigBitmap))
3894 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003895 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003896 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003897 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003898 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003899 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003900 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003901 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003902 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003903
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003904 public BigPictureStyle() {
3905 }
3906
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003907 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003908 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003909 }
3910
Chris Wrend6297db2012-05-03 16:20:13 -04003911 /**
3912 * Overrides ContentTitle in the big form of the template.
3913 * This defaults to the value passed to setContentTitle().
3914 */
3915 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003916 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003917 return this;
3918 }
3919
3920 /**
3921 * Set the first line of text after the detail section in the big form of the template.
3922 */
3923 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003924 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003925 return this;
3926 }
3927
Chris Wren0bd664d2012-08-01 13:56:56 -04003928 /**
3929 * Provide the bitmap to be used as the payload for the BigPicture notification.
3930 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003931 public BigPictureStyle bigPicture(Bitmap b) {
3932 mPicture = b;
3933 return this;
3934 }
3935
Chris Wren3745a3d2012-05-22 15:11:52 -04003936 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04003937 * Override the large icon when the big notification is shown.
3938 */
3939 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003940 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3941 }
3942
3943 /**
3944 * Override the large icon when the big notification is shown.
3945 */
3946 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04003947 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003948 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003949 return this;
3950 }
3951
Riley Andrews0394a0c2015-11-03 23:36:52 -08003952 /** @hide */
3953 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
3954
Daniel Sandler0ec46202015-06-24 01:27:05 -04003955 /**
3956 * @hide
3957 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003958 @Override
3959 public void purgeResources() {
3960 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08003961 if (mPicture != null &&
3962 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08003963 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003964 mPicture = mPicture.createAshmemBitmap();
3965 }
3966 if (mBigLargeIcon != null) {
3967 mBigLargeIcon.convertToAshmem();
3968 }
3969 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01003970
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003971 /**
3972 * @hide
3973 */
3974 public RemoteViews makeBigContentView() {
3975 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01003976 // This covers the following cases:
3977 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003978 // mN.mLargeIcon
3979 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04003980 Icon oldLargeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003981 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003982 oldLargeIcon = mBuilder.mN.mLargeIcon;
3983 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003984 }
3985
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003986 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05003987 if (mSummaryTextSet) {
3988 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinekc848c3a2016-01-13 15:27:30 -08003989 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05003990 }
Selim Cinek860b6da2015-12-16 19:02:19 -08003991 mBuilder.setContentMinHeight(contentView, mBuilder.mN.mLargeIcon != null);
Selim Cinek53e64a42015-11-16 10:40:56 -08003992
Christoph Studer5c510ee2014-12-15 16:32:27 +01003993 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003994 mBuilder.mN.mLargeIcon = oldLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003995 }
3996
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003997 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003998 return contentView;
3999 }
4000
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004001 /**
4002 * @hide
4003 */
4004 public void addExtras(Bundle extras) {
4005 super.addExtras(extras);
4006
4007 if (mBigLargeIconSet) {
4008 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
4009 }
4010 extras.putParcelable(EXTRA_PICTURE, mPicture);
4011 }
4012
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004013 /**
4014 * @hide
4015 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004016 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02004017 protected void restoreFromExtras(Bundle extras) {
4018 super.restoreFromExtras(extras);
4019
4020 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01004021 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02004022 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04004023 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02004024 mPicture = extras.getParcelable(EXTRA_PICTURE);
4025 }
Selim Cinek03d0d652015-11-13 13:18:09 -05004026
4027 /**
4028 * @hide
4029 */
4030 @Override
4031 public boolean hasSummaryInHeader() {
4032 return false;
4033 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004034 }
4035
4036 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004037 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08004038 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004039 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004040 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004041 * Notification notif = new Notification.Builder(mContext)
4042 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4043 * .setContentText(subject)
4044 * .setSmallIcon(R.drawable.new_mail)
4045 * .setLargeIcon(aBitmap)
4046 * .setStyle(new Notification.BigTextStyle()
4047 * .bigText(aVeryLongString))
4048 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004049 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004050 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004051 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004052 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004053 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004054
4055 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004056 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004057
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004058 private CharSequence mBigText;
4059
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004060 public BigTextStyle() {
4061 }
4062
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004063 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004064 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004065 }
4066
Chris Wrend6297db2012-05-03 16:20:13 -04004067 /**
4068 * Overrides ContentTitle in the big form of the template.
4069 * This defaults to the value passed to setContentTitle().
4070 */
4071 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004072 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004073 return this;
4074 }
4075
4076 /**
4077 * Set the first line of text after the detail section in the big form of the template.
4078 */
4079 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004080 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004081 return this;
4082 }
4083
Chris Wren0bd664d2012-08-01 13:56:56 -04004084 /**
4085 * Provide the longer text to be displayed in the big form of the
4086 * template in place of the content text.
4087 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004088 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004089 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004090 return this;
4091 }
4092
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004093 /**
4094 * @hide
4095 */
4096 public void addExtras(Bundle extras) {
4097 super.addExtras(extras);
4098
Christoph Studer4600f9b2014-07-22 22:44:43 +02004099 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4100 }
4101
4102 /**
4103 * @hide
4104 */
4105 @Override
4106 protected void restoreFromExtras(Bundle extras) {
4107 super.restoreFromExtras(extras);
4108
4109 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004110 }
4111
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004112 /**
4113 * @hide
4114 */
4115 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004116
4117 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004118 CharSequence oldBuilderContentText =
4119 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
4120 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004121
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004122 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004123
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004124 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004125
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004126 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
4127 contentView.setTextViewText(R.id.big_text, bigTextText);
4128 contentView.setViewVisibility(R.id.big_text,
4129 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004130 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines());
Selim Cinek4fb12d32015-11-19 18:10:48 -08004131 contentView.setBoolean(R.id.big_text, "setHasImage", mBuilder.mN.mLargeIcon != null);
4132
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004133 return contentView;
4134 }
4135
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004136 private int calculateMaxLines() {
4137 int lineCount = MAX_LINES;
4138 boolean hasActions = mBuilder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004139 if (hasActions) {
4140 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4141 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004142 return lineCount;
4143 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004144 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004145
4146 /**
4147 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08004148 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004149 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04004150 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004151 * Notification notif = new Notification.Builder(mContext)
4152 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
4153 * .setContentText(subject)
4154 * .setSmallIcon(R.drawable.new_mail)
4155 * .setLargeIcon(aBitmap)
4156 * .setStyle(new Notification.InboxStyle()
4157 * .addLine(str1)
4158 * .addLine(str2)
4159 * .setContentTitle(&quot;&quot;)
4160 * .setSummaryText(&quot;+3 more&quot;))
4161 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04004162 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004163 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04004164 * @see Notification#bigContentView
4165 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004166 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004167 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
4168
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004169 public InboxStyle() {
4170 }
4171
Daniel Sandler879c5e02012-04-17 16:46:51 -04004172 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004173 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004174 }
4175
Chris Wrend6297db2012-05-03 16:20:13 -04004176 /**
4177 * Overrides ContentTitle in the big form of the template.
4178 * This defaults to the value passed to setContentTitle().
4179 */
4180 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004181 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004182 return this;
4183 }
4184
4185 /**
4186 * Set the first line of text after the detail section in the big form of the template.
4187 */
4188 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004189 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004190 return this;
4191 }
4192
Chris Wren0bd664d2012-08-01 13:56:56 -04004193 /**
4194 * Append a line to the digest section of the Inbox notification.
4195 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04004196 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004197 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04004198 return this;
4199 }
4200
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004201 /**
4202 * @hide
4203 */
4204 public void addExtras(Bundle extras) {
4205 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004206
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004207 CharSequence[] a = new CharSequence[mTexts.size()];
4208 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
4209 }
4210
Christoph Studer4600f9b2014-07-22 22:44:43 +02004211 /**
4212 * @hide
4213 */
4214 @Override
4215 protected void restoreFromExtras(Bundle extras) {
4216 super.restoreFromExtras(extras);
4217
4218 mTexts.clear();
4219 if (extras.containsKey(EXTRA_TEXT_LINES)) {
4220 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
4221 }
4222 }
4223
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004224 /**
4225 * @hide
4226 */
4227 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004228 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02004229 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004230 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
4231 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004232
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004233 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04004234
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004235 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004236
Chris Wrend6297db2012-05-03 16:20:13 -04004237 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 -04004238 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04004239
Chris Wren4ed80d52012-05-17 09:30:03 -04004240 // Make sure all rows are gone in case we reuse a view.
4241 for (int rowId : rowIds) {
4242 contentView.setViewVisibility(rowId, View.GONE);
4243 }
4244
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004245 final boolean largeText =
4246 mBuilder.mContext.getResources().getConfiguration().fontScale > 1f;
4247 final float subTextSize = mBuilder.mContext.getResources().getDimensionPixelSize(
4248 R.dimen.notification_subtext_size);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004249 int i=0;
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004250 final float density = mBuilder.mContext.getResources().getDisplayMetrics().density;
4251 int topPadding = (int) (5 * density);
4252 int bottomPadding = (int) (13 * density);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004253 while (i < mTexts.size() && i < rowIds.length) {
4254 CharSequence str = mTexts.get(i);
4255 if (str != null && !str.equals("")) {
4256 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004257 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004258 if (largeText) {
4259 contentView.setTextViewTextSize(rowIds[i], TypedValue.COMPLEX_UNIT_PX,
4260 subTextSize);
4261 }
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004262 contentView.setViewPadding(rowIds[i], 0, topPadding, 0,
4263 i == rowIds.length - 1 || i == mTexts.size() - 1 ? bottomPadding : 0);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004264 }
4265 i++;
4266 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004267
Selim Cinek1e0bf612015-11-20 15:57:26 -08004268 handleInboxImageMargin(contentView, rowIds[0]);
4269
Daniel Sandler879c5e02012-04-17 16:46:51 -04004270 return contentView;
4271 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004272
4273 private void handleInboxImageMargin(RemoteViews contentView, int id) {
4274 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
4275 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4276 boolean hasProgress = max != 0 || ind;
4277 int endMargin = 0;
4278 if (mTexts.size() > 0 && mBuilder.mN.mLargeIcon != null && !hasProgress) {
4279 endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4280 R.dimen.notification_content_picture_margin);
4281 }
4282 contentView.setViewLayoutMarginEnd(id, endMargin);
4283 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004284 }
Dan Sandler842dd772014-05-15 09:36:47 -04004285
4286 /**
4287 * Notification style for media playback notifications.
4288 *
4289 * In the expanded form, {@link Notification#bigContentView}, up to 5
4290 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04004291 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04004292 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
4293 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
4294 * treated as album artwork.
4295 *
4296 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
4297 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01004298 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04004299 * in the standard view alongside the usual content.
4300 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004301 * Notifications created with MediaStyle will have their category set to
4302 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
4303 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
4304 *
Jeff Browndba34ba2014-06-24 20:46:03 -07004305 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
4306 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04004307 * the System UI can identify this as a notification representing an active media session
4308 * and respond accordingly (by showing album artwork in the lockscreen, for example).
4309 *
4310 * To use this style with your Notification, feed it to
4311 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4312 * <pre class="prettyprint">
4313 * Notification noti = new Notification.Builder()
4314 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01004315 * .setContentTitle(&quot;Track title&quot;)
4316 * .setContentText(&quot;Artist - Album&quot;)
4317 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004318 * .setStyle(<b>new Notification.MediaStyle()</b>
4319 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04004320 * .build();
4321 * </pre>
4322 *
4323 * @see Notification#bigContentView
4324 */
4325 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004326 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04004327 static final int MAX_MEDIA_BUTTONS = 5;
4328
4329 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07004330 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04004331
4332 public MediaStyle() {
4333 }
4334
4335 public MediaStyle(Builder builder) {
4336 setBuilder(builder);
4337 }
4338
4339 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004340 * 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 -04004341 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004342 *
4343 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04004344 */
4345 public MediaStyle setShowActionsInCompactView(int...actions) {
4346 mActionsToShowInCompact = actions;
4347 return this;
4348 }
4349
4350 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07004351 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
4352 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04004353 */
Jeff Browndba34ba2014-06-24 20:46:03 -07004354 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04004355 mToken = token;
4356 return this;
4357 }
4358
Christoph Studer4600f9b2014-07-22 22:44:43 +02004359 /**
4360 * @hide
4361 */
Dan Sandler842dd772014-05-15 09:36:47 -04004362 @Override
4363 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004364 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004365 if (wip.category == null) {
4366 wip.category = Notification.CATEGORY_TRANSPORT;
4367 }
Dan Sandler842dd772014-05-15 09:36:47 -04004368 return wip;
4369 }
4370
Christoph Studer4600f9b2014-07-22 22:44:43 +02004371 /**
4372 * @hide
4373 */
4374 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004375 public RemoteViews makeContentView() {
4376 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004377 }
4378
4379 /**
4380 * @hide
4381 */
4382 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004383 public RemoteViews makeBigContentView() {
4384 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004385 }
4386
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004387 /**
4388 * @hide
4389 */
4390 @Override
4391 public RemoteViews makeHeadsUpContentView() {
4392 RemoteViews expanded = makeMediaBigContentView();
4393 return expanded != null ? expanded : makeMediaContentView();
4394 }
4395
Dan Sandler842dd772014-05-15 09:36:47 -04004396 /** @hide */
4397 @Override
4398 public void addExtras(Bundle extras) {
4399 super.addExtras(extras);
4400
4401 if (mToken != null) {
4402 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
4403 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01004404 if (mActionsToShowInCompact != null) {
4405 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
4406 }
Dan Sandler842dd772014-05-15 09:36:47 -04004407 }
4408
Christoph Studer4600f9b2014-07-22 22:44:43 +02004409 /**
4410 * @hide
4411 */
4412 @Override
4413 protected void restoreFromExtras(Bundle extras) {
4414 super.restoreFromExtras(extras);
4415
4416 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
4417 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
4418 }
4419 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
4420 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
4421 }
4422 }
4423
Selim Cinek5bf069a2015-11-10 19:14:27 -05004424 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04004425 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004426 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07004427 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04004428 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05004429 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
4430 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04004431 if (!tombstone) {
4432 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
4433 }
4434 button.setContentDescription(R.id.action0, action.title);
4435 return button;
4436 }
4437
4438 private RemoteViews makeMediaContentView() {
4439 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004440 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04004441
4442 final int numActions = mBuilder.mActions.size();
4443 final int N = mActionsToShowInCompact == null
4444 ? 0
4445 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4446 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004447 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04004448 for (int i = 0; i < N; i++) {
4449 if (i >= numActions) {
4450 throw new IllegalArgumentException(String.format(
4451 "setShowActionsInCompactView: action %d out of bounds (max %d)",
4452 i, numActions - 1));
4453 }
4454
4455 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05004456 final RemoteViews button = generateMediaActionButton(action,
4457 mBuilder.resolveColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004458 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004459 }
4460 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08004461 handleImage(view);
4462 // handle the content margin
4463 int endMargin;
4464 if (mBuilder.mN.mLargeIcon != null) {
4465 endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4466 R.dimen.notification_content_picture_margin_media);
4467 } else {
4468 endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4469 R.dimen.notification_content_margin_end);
4470 }
4471 view.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04004472 return view;
4473 }
4474
4475 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004476 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004477 // Dont add an expanded view if there is no more content to be revealed
4478 int actionsInCompact = mActionsToShowInCompact == null
4479 ? 0
4480 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4481 if (mBuilder.mN.mLargeIcon == null && actionCount <= actionsInCompact) {
4482 return null;
4483 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004484 RemoteViews big = mBuilder.applyStandardTemplate(
4485 R.layout.notification_template_material_big_media,
4486 false);
Dan Sandler842dd772014-05-15 09:36:47 -04004487
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004488 if (actionCount > 0) {
4489 big.removeAllViews(com.android.internal.R.id.media_actions);
4490 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05004491 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
4492 mBuilder.resolveColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004493 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004494 }
4495 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004496 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04004497 return big;
4498 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004499
Selim Cinek5bf069a2015-11-10 19:14:27 -05004500 private void handleImage(RemoteViews contentView) {
4501 if (mBuilder.mN.mLargeIcon != null) {
4502 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004503 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004504 }
4505 }
4506
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004507 /**
4508 * @hide
4509 */
4510 @Override
4511 protected boolean hasProgress() {
4512 return false;
4513 }
Dan Sandler842dd772014-05-15 09:36:47 -04004514 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004515
Christoph Studer4600f9b2014-07-22 22:44:43 +02004516 // When adding a new Style subclass here, don't forget to update
4517 // Builder.getNotificationStyleClass.
4518
Griff Hazen61a9e862014-05-22 16:05:19 -07004519 /**
4520 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
4521 * metadata or change options on a notification builder.
4522 */
4523 public interface Extender {
4524 /**
4525 * Apply this extender to a notification builder.
4526 * @param builder the builder to be modified.
4527 * @return the build object for chaining.
4528 */
4529 public Builder extend(Builder builder);
4530 }
4531
4532 /**
4533 * Helper class to add wearable extensions to notifications.
4534 * <p class="note"> See
4535 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
4536 * for Android Wear</a> for more information on how to use this class.
4537 * <p>
4538 * To create a notification with wearable extensions:
4539 * <ol>
4540 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
4541 * properties.
4542 * <li>Create a {@link android.app.Notification.WearableExtender}.
4543 * <li>Set wearable-specific properties using the
4544 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
4545 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
4546 * notification.
4547 * <li>Post the notification to the notification system with the
4548 * {@code NotificationManager.notify(...)} methods.
4549 * </ol>
4550 *
4551 * <pre class="prettyprint">
4552 * Notification notif = new Notification.Builder(mContext)
4553 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4554 * .setContentText(subject)
4555 * .setSmallIcon(R.drawable.new_mail)
4556 * .extend(new Notification.WearableExtender()
4557 * .setContentIcon(R.drawable.new_mail))
4558 * .build();
4559 * NotificationManager notificationManger =
4560 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
4561 * notificationManger.notify(0, notif);</pre>
4562 *
4563 * <p>Wearable extensions can be accessed on an existing notification by using the
4564 * {@code WearableExtender(Notification)} constructor,
4565 * and then using the {@code get} methods to access values.
4566 *
4567 * <pre class="prettyprint">
4568 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
4569 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07004570 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004571 */
4572 public static final class WearableExtender implements Extender {
4573 /**
4574 * Sentinel value for an action index that is unset.
4575 */
4576 public static final int UNSET_ACTION_INDEX = -1;
4577
4578 /**
4579 * Size value for use with {@link #setCustomSizePreset} to show this notification with
4580 * default sizing.
4581 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07004582 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07004583 * on their content.
4584 */
4585 public static final int SIZE_DEFAULT = 0;
4586
4587 /**
4588 * Size value for use with {@link #setCustomSizePreset} to show this notification
4589 * with an extra small size.
4590 * <p>This value is only applicable for custom display notifications created using
4591 * {@link #setDisplayIntent}.
4592 */
4593 public static final int SIZE_XSMALL = 1;
4594
4595 /**
4596 * Size value for use with {@link #setCustomSizePreset} to show this notification
4597 * with a small size.
4598 * <p>This value is only applicable for custom display notifications created using
4599 * {@link #setDisplayIntent}.
4600 */
4601 public static final int SIZE_SMALL = 2;
4602
4603 /**
4604 * Size value for use with {@link #setCustomSizePreset} to show this notification
4605 * with a medium size.
4606 * <p>This value is only applicable for custom display notifications created using
4607 * {@link #setDisplayIntent}.
4608 */
4609 public static final int SIZE_MEDIUM = 3;
4610
4611 /**
4612 * Size value for use with {@link #setCustomSizePreset} to show this notification
4613 * with a large size.
4614 * <p>This value is only applicable for custom display notifications created using
4615 * {@link #setDisplayIntent}.
4616 */
4617 public static final int SIZE_LARGE = 4;
4618
Griff Hazend5f11f92014-05-27 15:40:09 -07004619 /**
4620 * Size value for use with {@link #setCustomSizePreset} to show this notification
4621 * full screen.
4622 * <p>This value is only applicable for custom display notifications created using
4623 * {@link #setDisplayIntent}.
4624 */
4625 public static final int SIZE_FULL_SCREEN = 5;
4626
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004627 /**
4628 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
4629 * short amount of time when this notification is displayed on the screen. This
4630 * is the default value.
4631 */
4632 public static final int SCREEN_TIMEOUT_SHORT = 0;
4633
4634 /**
4635 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
4636 * for a longer amount of time when this notification is displayed on the screen.
4637 */
4638 public static final int SCREEN_TIMEOUT_LONG = -1;
4639
Griff Hazen61a9e862014-05-22 16:05:19 -07004640 /** Notification extra which contains wearable extensions */
4641 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
4642
Pete Gastaf6781d2014-10-07 15:17:05 -04004643 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07004644 private static final String KEY_ACTIONS = "actions";
4645 private static final String KEY_FLAGS = "flags";
4646 private static final String KEY_DISPLAY_INTENT = "displayIntent";
4647 private static final String KEY_PAGES = "pages";
4648 private static final String KEY_BACKGROUND = "background";
4649 private static final String KEY_CONTENT_ICON = "contentIcon";
4650 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
4651 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
4652 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
4653 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
4654 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004655 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Griff Hazen61a9e862014-05-22 16:05:19 -07004656
4657 // Flags bitwise-ored to mFlags
4658 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
4659 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
4660 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
4661 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004662 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Griff Hazen61a9e862014-05-22 16:05:19 -07004663
4664 // Default value for flags integer
4665 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
4666
4667 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
4668 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
4669
4670 private ArrayList<Action> mActions = new ArrayList<Action>();
4671 private int mFlags = DEFAULT_FLAGS;
4672 private PendingIntent mDisplayIntent;
4673 private ArrayList<Notification> mPages = new ArrayList<Notification>();
4674 private Bitmap mBackground;
4675 private int mContentIcon;
4676 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
4677 private int mContentActionIndex = UNSET_ACTION_INDEX;
4678 private int mCustomSizePreset = SIZE_DEFAULT;
4679 private int mCustomContentHeight;
4680 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004681 private int mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004682
4683 /**
4684 * Create a {@link android.app.Notification.WearableExtender} with default
4685 * options.
4686 */
4687 public WearableExtender() {
4688 }
4689
4690 public WearableExtender(Notification notif) {
4691 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
4692 if (wearableBundle != null) {
4693 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
4694 if (actions != null) {
4695 mActions.addAll(actions);
4696 }
4697
4698 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
4699 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
4700
4701 Notification[] pages = getNotificationArrayFromBundle(
4702 wearableBundle, KEY_PAGES);
4703 if (pages != null) {
4704 Collections.addAll(mPages, pages);
4705 }
4706
4707 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
4708 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
4709 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
4710 DEFAULT_CONTENT_ICON_GRAVITY);
4711 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
4712 UNSET_ACTION_INDEX);
4713 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
4714 SIZE_DEFAULT);
4715 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
4716 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004717 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Griff Hazen61a9e862014-05-22 16:05:19 -07004718 }
4719 }
4720
4721 /**
4722 * Apply wearable extensions to a notification that is being built. This is typically
4723 * called by the {@link android.app.Notification.Builder#extend} method of
4724 * {@link android.app.Notification.Builder}.
4725 */
4726 @Override
4727 public Notification.Builder extend(Notification.Builder builder) {
4728 Bundle wearableBundle = new Bundle();
4729
4730 if (!mActions.isEmpty()) {
4731 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
4732 }
4733 if (mFlags != DEFAULT_FLAGS) {
4734 wearableBundle.putInt(KEY_FLAGS, mFlags);
4735 }
4736 if (mDisplayIntent != null) {
4737 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
4738 }
4739 if (!mPages.isEmpty()) {
4740 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
4741 new Notification[mPages.size()]));
4742 }
4743 if (mBackground != null) {
4744 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
4745 }
4746 if (mContentIcon != 0) {
4747 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
4748 }
4749 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
4750 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
4751 }
4752 if (mContentActionIndex != UNSET_ACTION_INDEX) {
4753 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
4754 mContentActionIndex);
4755 }
4756 if (mCustomSizePreset != SIZE_DEFAULT) {
4757 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
4758 }
4759 if (mCustomContentHeight != 0) {
4760 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
4761 }
4762 if (mGravity != DEFAULT_GRAVITY) {
4763 wearableBundle.putInt(KEY_GRAVITY, mGravity);
4764 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004765 if (mHintScreenTimeout != 0) {
4766 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
4767 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004768
4769 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
4770 return builder;
4771 }
4772
4773 @Override
4774 public WearableExtender clone() {
4775 WearableExtender that = new WearableExtender();
4776 that.mActions = new ArrayList<Action>(this.mActions);
4777 that.mFlags = this.mFlags;
4778 that.mDisplayIntent = this.mDisplayIntent;
4779 that.mPages = new ArrayList<Notification>(this.mPages);
4780 that.mBackground = this.mBackground;
4781 that.mContentIcon = this.mContentIcon;
4782 that.mContentIconGravity = this.mContentIconGravity;
4783 that.mContentActionIndex = this.mContentActionIndex;
4784 that.mCustomSizePreset = this.mCustomSizePreset;
4785 that.mCustomContentHeight = this.mCustomContentHeight;
4786 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004787 that.mHintScreenTimeout = this.mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004788 return that;
4789 }
4790
4791 /**
4792 * Add a wearable action to this notification.
4793 *
4794 * <p>When wearable actions are added using this method, the set of actions that
4795 * show on a wearable device splits from devices that only show actions added
4796 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4797 * of which actions display on different devices.
4798 *
4799 * @param action the action to add to this notification
4800 * @return this object for method chaining
4801 * @see android.app.Notification.Action
4802 */
4803 public WearableExtender addAction(Action action) {
4804 mActions.add(action);
4805 return this;
4806 }
4807
4808 /**
4809 * Adds wearable actions to this notification.
4810 *
4811 * <p>When wearable actions are added using this method, the set of actions that
4812 * show on a wearable device splits from devices that only show actions added
4813 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4814 * of which actions display on different devices.
4815 *
4816 * @param actions the actions to add to this notification
4817 * @return this object for method chaining
4818 * @see android.app.Notification.Action
4819 */
4820 public WearableExtender addActions(List<Action> actions) {
4821 mActions.addAll(actions);
4822 return this;
4823 }
4824
4825 /**
4826 * Clear all wearable actions present on this builder.
4827 * @return this object for method chaining.
4828 * @see #addAction
4829 */
4830 public WearableExtender clearActions() {
4831 mActions.clear();
4832 return this;
4833 }
4834
4835 /**
4836 * Get the wearable actions present on this notification.
4837 */
4838 public List<Action> getActions() {
4839 return mActions;
4840 }
4841
4842 /**
4843 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07004844 * this notification. The {@link PendingIntent} provided should be for an activity.
4845 *
4846 * <pre class="prettyprint">
4847 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
4848 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
4849 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
4850 * Notification notif = new Notification.Builder(context)
4851 * .extend(new Notification.WearableExtender()
4852 * .setDisplayIntent(displayPendingIntent)
4853 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
4854 * .build();</pre>
4855 *
4856 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07004857 * should have an empty task affinity. It is also recommended to use the device
4858 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07004859 *
4860 * <p>Example AndroidManifest.xml entry:
4861 * <pre class="prettyprint">
4862 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
4863 * android:exported=&quot;true&quot;
4864 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07004865 * android:taskAffinity=&quot;&quot;
4866 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004867 *
4868 * @param intent the {@link PendingIntent} for an activity
4869 * @return this object for method chaining
4870 * @see android.app.Notification.WearableExtender#getDisplayIntent
4871 */
4872 public WearableExtender setDisplayIntent(PendingIntent intent) {
4873 mDisplayIntent = intent;
4874 return this;
4875 }
4876
4877 /**
4878 * Get the intent to launch inside of an activity view when displaying this
4879 * notification. This {@code PendingIntent} should be for an activity.
4880 */
4881 public PendingIntent getDisplayIntent() {
4882 return mDisplayIntent;
4883 }
4884
4885 /**
4886 * Add an additional page of content to display with this notification. The current
4887 * notification forms the first page, and pages added using this function form
4888 * subsequent pages. This field can be used to separate a notification into multiple
4889 * sections.
4890 *
4891 * @param page the notification to add as another page
4892 * @return this object for method chaining
4893 * @see android.app.Notification.WearableExtender#getPages
4894 */
4895 public WearableExtender addPage(Notification page) {
4896 mPages.add(page);
4897 return this;
4898 }
4899
4900 /**
4901 * Add additional pages of content to display with this notification. The current
4902 * notification forms the first page, and pages added using this function form
4903 * subsequent pages. This field can be used to separate a notification into multiple
4904 * sections.
4905 *
4906 * @param pages a list of notifications
4907 * @return this object for method chaining
4908 * @see android.app.Notification.WearableExtender#getPages
4909 */
4910 public WearableExtender addPages(List<Notification> pages) {
4911 mPages.addAll(pages);
4912 return this;
4913 }
4914
4915 /**
4916 * Clear all additional pages present on this builder.
4917 * @return this object for method chaining.
4918 * @see #addPage
4919 */
4920 public WearableExtender clearPages() {
4921 mPages.clear();
4922 return this;
4923 }
4924
4925 /**
4926 * Get the array of additional pages of content for displaying this notification. The
4927 * current notification forms the first page, and elements within this array form
4928 * subsequent pages. This field can be used to separate a notification into multiple
4929 * sections.
4930 * @return the pages for this notification
4931 */
4932 public List<Notification> getPages() {
4933 return mPages;
4934 }
4935
4936 /**
4937 * Set a background image to be displayed behind the notification content.
4938 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
4939 * will work with any notification style.
4940 *
4941 * @param background the background bitmap
4942 * @return this object for method chaining
4943 * @see android.app.Notification.WearableExtender#getBackground
4944 */
4945 public WearableExtender setBackground(Bitmap background) {
4946 mBackground = background;
4947 return this;
4948 }
4949
4950 /**
4951 * Get a background image to be displayed behind the notification content.
4952 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
4953 * will work with any notification style.
4954 *
4955 * @return the background image
4956 * @see android.app.Notification.WearableExtender#setBackground
4957 */
4958 public Bitmap getBackground() {
4959 return mBackground;
4960 }
4961
4962 /**
4963 * Set an icon that goes with the content of this notification.
4964 */
4965 public WearableExtender setContentIcon(int icon) {
4966 mContentIcon = icon;
4967 return this;
4968 }
4969
4970 /**
4971 * Get an icon that goes with the content of this notification.
4972 */
4973 public int getContentIcon() {
4974 return mContentIcon;
4975 }
4976
4977 /**
4978 * Set the gravity that the content icon should have within the notification display.
4979 * Supported values include {@link android.view.Gravity#START} and
4980 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
4981 * @see #setContentIcon
4982 */
4983 public WearableExtender setContentIconGravity(int contentIconGravity) {
4984 mContentIconGravity = contentIconGravity;
4985 return this;
4986 }
4987
4988 /**
4989 * Get the gravity that the content icon should have within the notification display.
4990 * Supported values include {@link android.view.Gravity#START} and
4991 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
4992 * @see #getContentIcon
4993 */
4994 public int getContentIconGravity() {
4995 return mContentIconGravity;
4996 }
4997
4998 /**
4999 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07005000 * this notification. This action will no longer display separately from the
5001 * notification's content.
5002 *
Griff Hazenca48d352014-05-28 22:37:13 -07005003 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005004 * set, although the list of available actions comes from the main notification and not
5005 * from the child page's notification.
5006 *
5007 * @param actionIndex The index of the action to hoist onto the current notification page.
5008 * If wearable actions were added to the main notification, this index
5009 * will apply to that list, otherwise it will apply to the regular
5010 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07005011 */
5012 public WearableExtender setContentAction(int actionIndex) {
5013 mContentActionIndex = actionIndex;
5014 return this;
5015 }
5016
5017 /**
Griff Hazenca48d352014-05-28 22:37:13 -07005018 * Get the index of the notification action, if any, that was specified as being clickable
5019 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07005020 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07005021 *
Griff Hazenca48d352014-05-28 22:37:13 -07005022 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005023 * set, although the list of available actions comes from the main notification and not
5024 * from the child page's notification.
5025 *
5026 * <p>If wearable specific actions were added to the main notification, this index will
5027 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07005028 *
5029 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07005030 */
5031 public int getContentAction() {
5032 return mContentActionIndex;
5033 }
5034
5035 /**
5036 * Set the gravity that this notification should have within the available viewport space.
5037 * Supported values include {@link android.view.Gravity#TOP},
5038 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5039 * The default value is {@link android.view.Gravity#BOTTOM}.
5040 */
5041 public WearableExtender setGravity(int gravity) {
5042 mGravity = gravity;
5043 return this;
5044 }
5045
5046 /**
5047 * Get the gravity that this notification should have within the available viewport space.
5048 * Supported values include {@link android.view.Gravity#TOP},
5049 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5050 * The default value is {@link android.view.Gravity#BOTTOM}.
5051 */
5052 public int getGravity() {
5053 return mGravity;
5054 }
5055
5056 /**
5057 * Set the custom size preset for the display of this notification out of the available
5058 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5059 * {@link #SIZE_LARGE}.
5060 * <p>Some custom size presets are only applicable for custom display notifications created
5061 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
5062 * documentation for the preset in question. See also
5063 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
5064 */
5065 public WearableExtender setCustomSizePreset(int sizePreset) {
5066 mCustomSizePreset = sizePreset;
5067 return this;
5068 }
5069
5070 /**
5071 * Get the custom size preset for the display of this notification out of the available
5072 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5073 * {@link #SIZE_LARGE}.
5074 * <p>Some custom size presets are only applicable for custom display notifications created
5075 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
5076 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
5077 */
5078 public int getCustomSizePreset() {
5079 return mCustomSizePreset;
5080 }
5081
5082 /**
5083 * Set the custom height in pixels for the display of this notification's content.
5084 * <p>This option is only available for custom display notifications created
5085 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
5086 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
5087 * {@link #getCustomContentHeight}.
5088 */
5089 public WearableExtender setCustomContentHeight(int height) {
5090 mCustomContentHeight = height;
5091 return this;
5092 }
5093
5094 /**
5095 * Get the custom height in pixels for the display of this notification's content.
5096 * <p>This option is only available for custom display notifications created
5097 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
5098 * {@link #setCustomContentHeight}.
5099 */
5100 public int getCustomContentHeight() {
5101 return mCustomContentHeight;
5102 }
5103
5104 /**
5105 * Set whether the scrolling position for the contents of this notification should start
5106 * at the bottom of the contents instead of the top when the contents are too long to
5107 * display within the screen. Default is false (start scroll at the top).
5108 */
5109 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
5110 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
5111 return this;
5112 }
5113
5114 /**
5115 * Get whether the scrolling position for the contents of this notification should start
5116 * at the bottom of the contents instead of the top when the contents are too long to
5117 * display within the screen. Default is false (start scroll at the top).
5118 */
5119 public boolean getStartScrollBottom() {
5120 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
5121 }
5122
5123 /**
5124 * Set whether the content intent is available when the wearable device is not connected
5125 * to a companion device. The user can still trigger this intent when the wearable device
5126 * is offline, but a visual hint will indicate that the content intent may not be available.
5127 * Defaults to true.
5128 */
5129 public WearableExtender setContentIntentAvailableOffline(
5130 boolean contentIntentAvailableOffline) {
5131 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
5132 return this;
5133 }
5134
5135 /**
5136 * Get whether the content intent is available when the wearable device is not connected
5137 * to a companion device. The user can still trigger this intent when the wearable device
5138 * is offline, but a visual hint will indicate that the content intent may not be available.
5139 * Defaults to true.
5140 */
5141 public boolean getContentIntentAvailableOffline() {
5142 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
5143 }
5144
5145 /**
5146 * Set a hint that this notification's icon should not be displayed.
5147 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
5148 * @return this object for method chaining
5149 */
5150 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
5151 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
5152 return this;
5153 }
5154
5155 /**
5156 * Get a hint that this notification's icon should not be displayed.
5157 * @return {@code true} if this icon should not be displayed, false otherwise.
5158 * The default value is {@code false} if this was never set.
5159 */
5160 public boolean getHintHideIcon() {
5161 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
5162 }
5163
5164 /**
5165 * Set a visual hint that only the background image of this notification should be
5166 * displayed, and other semantic content should be hidden. This hint is only applicable
5167 * to sub-pages added using {@link #addPage}.
5168 */
5169 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
5170 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
5171 return this;
5172 }
5173
5174 /**
5175 * Get a visual hint that only the background image of this notification should be
5176 * displayed, and other semantic content should be hidden. This hint is only applicable
5177 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
5178 */
5179 public boolean getHintShowBackgroundOnly() {
5180 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
5181 }
5182
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005183 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005184 * Set a hint that this notification's background should not be clipped if possible,
5185 * and should instead be resized to fully display on the screen, retaining the aspect
5186 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005187 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
5188 * @return this object for method chaining
5189 */
5190 public WearableExtender setHintAvoidBackgroundClipping(
5191 boolean hintAvoidBackgroundClipping) {
5192 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
5193 return this;
5194 }
5195
5196 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005197 * Get a hint that this notification's background should not be clipped if possible,
5198 * and should instead be resized to fully display on the screen, retaining the aspect
5199 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005200 * @return {@code true} if it's ok if the background is clipped on the screen, false
5201 * otherwise. The default value is {@code false} if this was never set.
5202 */
5203 public boolean getHintAvoidBackgroundClipping() {
5204 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
5205 }
5206
5207 /**
5208 * Set a hint that the screen should remain on for at least this duration when
5209 * this notification is displayed on the screen.
5210 * @param timeout The requested screen timeout in milliseconds. Can also be either
5211 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5212 * @return this object for method chaining
5213 */
5214 public WearableExtender setHintScreenTimeout(int timeout) {
5215 mHintScreenTimeout = timeout;
5216 return this;
5217 }
5218
5219 /**
5220 * Get the duration, in milliseconds, that the screen should remain on for
5221 * when this notification is displayed.
5222 * @return the duration in milliseconds if > 0, or either one of the sentinel values
5223 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5224 */
5225 public int getHintScreenTimeout() {
5226 return mHintScreenTimeout;
5227 }
5228
Griff Hazen61a9e862014-05-22 16:05:19 -07005229 private void setFlag(int mask, boolean value) {
5230 if (value) {
5231 mFlags |= mask;
5232 } else {
5233 mFlags &= ~mask;
5234 }
5235 }
5236 }
5237
5238 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005239 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
5240 * with car extensions:
5241 *
5242 * <ol>
5243 * <li>Create an {@link Notification.Builder}, setting any desired
5244 * properties.
5245 * <li>Create a {@link CarExtender}.
5246 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
5247 * {@link CarExtender}.
5248 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
5249 * to apply the extensions to a notification.
5250 * </ol>
5251 *
5252 * <pre class="prettyprint">
5253 * Notification notification = new Notification.Builder(context)
5254 * ...
5255 * .extend(new CarExtender()
5256 * .set*(...))
5257 * .build();
5258 * </pre>
5259 *
5260 * <p>Car extensions can be accessed on an existing notification by using the
5261 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
5262 * to access values.
5263 */
5264 public static final class CarExtender implements Extender {
5265 private static final String TAG = "CarExtender";
5266
5267 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
5268 private static final String EXTRA_LARGE_ICON = "large_icon";
5269 private static final String EXTRA_CONVERSATION = "car_conversation";
5270 private static final String EXTRA_COLOR = "app_color";
5271
5272 private Bitmap mLargeIcon;
5273 private UnreadConversation mUnreadConversation;
5274 private int mColor = Notification.COLOR_DEFAULT;
5275
5276 /**
5277 * Create a {@link CarExtender} with default options.
5278 */
5279 public CarExtender() {
5280 }
5281
5282 /**
5283 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
5284 *
5285 * @param notif The notification from which to copy options.
5286 */
5287 public CarExtender(Notification notif) {
5288 Bundle carBundle = notif.extras == null ?
5289 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
5290 if (carBundle != null) {
5291 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
5292 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
5293
5294 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
5295 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
5296 }
5297 }
5298
5299 /**
5300 * Apply car extensions to a notification that is being built. This is typically called by
5301 * the {@link Notification.Builder#extend(Notification.Extender)}
5302 * method of {@link Notification.Builder}.
5303 */
5304 @Override
5305 public Notification.Builder extend(Notification.Builder builder) {
5306 Bundle carExtensions = new Bundle();
5307
5308 if (mLargeIcon != null) {
5309 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
5310 }
5311 if (mColor != Notification.COLOR_DEFAULT) {
5312 carExtensions.putInt(EXTRA_COLOR, mColor);
5313 }
5314
5315 if (mUnreadConversation != null) {
5316 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
5317 carExtensions.putBundle(EXTRA_CONVERSATION, b);
5318 }
5319
5320 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
5321 return builder;
5322 }
5323
5324 /**
5325 * Sets the accent color to use when Android Auto presents the notification.
5326 *
5327 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
5328 * to accent the displayed notification. However, not all colors are acceptable in an
5329 * automotive setting. This method can be used to override the color provided in the
5330 * notification in such a situation.
5331 */
Tor Norbye80756e32015-03-02 09:39:27 -08005332 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005333 mColor = color;
5334 return this;
5335 }
5336
5337 /**
5338 * Gets the accent color.
5339 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005340 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005341 */
Tor Norbye80756e32015-03-02 09:39:27 -08005342 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005343 public int getColor() {
5344 return mColor;
5345 }
5346
5347 /**
5348 * Sets the large icon of the car notification.
5349 *
5350 * If no large icon is set in the extender, Android Auto will display the icon
5351 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
5352 *
5353 * @param largeIcon The large icon to use in the car notification.
5354 * @return This object for method chaining.
5355 */
5356 public CarExtender setLargeIcon(Bitmap largeIcon) {
5357 mLargeIcon = largeIcon;
5358 return this;
5359 }
5360
5361 /**
5362 * Gets the large icon used in this car notification, or null if no icon has been set.
5363 *
5364 * @return The large icon for the car notification.
5365 * @see CarExtender#setLargeIcon
5366 */
5367 public Bitmap getLargeIcon() {
5368 return mLargeIcon;
5369 }
5370
5371 /**
5372 * Sets the unread conversation in a message notification.
5373 *
5374 * @param unreadConversation The unread part of the conversation this notification conveys.
5375 * @return This object for method chaining.
5376 */
5377 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
5378 mUnreadConversation = unreadConversation;
5379 return this;
5380 }
5381
5382 /**
5383 * Returns the unread conversation conveyed by this notification.
5384 * @see #setUnreadConversation(UnreadConversation)
5385 */
5386 public UnreadConversation getUnreadConversation() {
5387 return mUnreadConversation;
5388 }
5389
5390 /**
5391 * A class which holds the unread messages from a conversation.
5392 */
5393 public static class UnreadConversation {
5394 private static final String KEY_AUTHOR = "author";
5395 private static final String KEY_TEXT = "text";
5396 private static final String KEY_MESSAGES = "messages";
5397 private static final String KEY_REMOTE_INPUT = "remote_input";
5398 private static final String KEY_ON_REPLY = "on_reply";
5399 private static final String KEY_ON_READ = "on_read";
5400 private static final String KEY_PARTICIPANTS = "participants";
5401 private static final String KEY_TIMESTAMP = "timestamp";
5402
5403 private final String[] mMessages;
5404 private final RemoteInput mRemoteInput;
5405 private final PendingIntent mReplyPendingIntent;
5406 private final PendingIntent mReadPendingIntent;
5407 private final String[] mParticipants;
5408 private final long mLatestTimestamp;
5409
5410 UnreadConversation(String[] messages, RemoteInput remoteInput,
5411 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
5412 String[] participants, long latestTimestamp) {
5413 mMessages = messages;
5414 mRemoteInput = remoteInput;
5415 mReadPendingIntent = readPendingIntent;
5416 mReplyPendingIntent = replyPendingIntent;
5417 mParticipants = participants;
5418 mLatestTimestamp = latestTimestamp;
5419 }
5420
5421 /**
5422 * Gets the list of messages conveyed by this notification.
5423 */
5424 public String[] getMessages() {
5425 return mMessages;
5426 }
5427
5428 /**
5429 * Gets the remote input that will be used to convey the response to a message list, or
5430 * null if no such remote input exists.
5431 */
5432 public RemoteInput getRemoteInput() {
5433 return mRemoteInput;
5434 }
5435
5436 /**
5437 * Gets the pending intent that will be triggered when the user replies to this
5438 * notification.
5439 */
5440 public PendingIntent getReplyPendingIntent() {
5441 return mReplyPendingIntent;
5442 }
5443
5444 /**
5445 * Gets the pending intent that Android Auto will send after it reads aloud all messages
5446 * in this object's message list.
5447 */
5448 public PendingIntent getReadPendingIntent() {
5449 return mReadPendingIntent;
5450 }
5451
5452 /**
5453 * Gets the participants in the conversation.
5454 */
5455 public String[] getParticipants() {
5456 return mParticipants;
5457 }
5458
5459 /**
5460 * Gets the firs participant in the conversation.
5461 */
5462 public String getParticipant() {
5463 return mParticipants.length > 0 ? mParticipants[0] : null;
5464 }
5465
5466 /**
5467 * Gets the timestamp of the conversation.
5468 */
5469 public long getLatestTimestamp() {
5470 return mLatestTimestamp;
5471 }
5472
5473 Bundle getBundleForUnreadConversation() {
5474 Bundle b = new Bundle();
5475 String author = null;
5476 if (mParticipants != null && mParticipants.length > 1) {
5477 author = mParticipants[0];
5478 }
5479 Parcelable[] messages = new Parcelable[mMessages.length];
5480 for (int i = 0; i < messages.length; i++) {
5481 Bundle m = new Bundle();
5482 m.putString(KEY_TEXT, mMessages[i]);
5483 m.putString(KEY_AUTHOR, author);
5484 messages[i] = m;
5485 }
5486 b.putParcelableArray(KEY_MESSAGES, messages);
5487 if (mRemoteInput != null) {
5488 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
5489 }
5490 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
5491 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
5492 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
5493 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
5494 return b;
5495 }
5496
5497 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
5498 if (b == null) {
5499 return null;
5500 }
5501 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
5502 String[] messages = null;
5503 if (parcelableMessages != null) {
5504 String[] tmp = new String[parcelableMessages.length];
5505 boolean success = true;
5506 for (int i = 0; i < tmp.length; i++) {
5507 if (!(parcelableMessages[i] instanceof Bundle)) {
5508 success = false;
5509 break;
5510 }
5511 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
5512 if (tmp[i] == null) {
5513 success = false;
5514 break;
5515 }
5516 }
5517 if (success) {
5518 messages = tmp;
5519 } else {
5520 return null;
5521 }
5522 }
5523
5524 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
5525 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
5526
5527 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
5528
5529 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
5530 if (participants == null || participants.length != 1) {
5531 return null;
5532 }
5533
5534 return new UnreadConversation(messages,
5535 remoteInput,
5536 onReply,
5537 onRead,
5538 participants, b.getLong(KEY_TIMESTAMP));
5539 }
5540 };
5541
5542 /**
5543 * Builder class for {@link CarExtender.UnreadConversation} objects.
5544 */
5545 public static class Builder {
5546 private final List<String> mMessages = new ArrayList<String>();
5547 private final String mParticipant;
5548 private RemoteInput mRemoteInput;
5549 private PendingIntent mReadPendingIntent;
5550 private PendingIntent mReplyPendingIntent;
5551 private long mLatestTimestamp;
5552
5553 /**
5554 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
5555 *
5556 * @param name The name of the other participant in the conversation.
5557 */
5558 public Builder(String name) {
5559 mParticipant = name;
5560 }
5561
5562 /**
5563 * Appends a new unread message to the list of messages for this conversation.
5564 *
5565 * The messages should be added from oldest to newest.
5566 *
5567 * @param message The text of the new unread message.
5568 * @return This object for method chaining.
5569 */
5570 public Builder addMessage(String message) {
5571 mMessages.add(message);
5572 return this;
5573 }
5574
5575 /**
5576 * Sets the pending intent and remote input which will convey the reply to this
5577 * notification.
5578 *
5579 * @param pendingIntent The pending intent which will be triggered on a reply.
5580 * @param remoteInput The remote input parcelable which will carry the reply.
5581 * @return This object for method chaining.
5582 *
5583 * @see CarExtender.UnreadConversation#getRemoteInput
5584 * @see CarExtender.UnreadConversation#getReplyPendingIntent
5585 */
5586 public Builder setReplyAction(
5587 PendingIntent pendingIntent, RemoteInput remoteInput) {
5588 mRemoteInput = remoteInput;
5589 mReplyPendingIntent = pendingIntent;
5590
5591 return this;
5592 }
5593
5594 /**
5595 * Sets the pending intent that will be sent once the messages in this notification
5596 * are read.
5597 *
5598 * @param pendingIntent The pending intent to use.
5599 * @return This object for method chaining.
5600 */
5601 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
5602 mReadPendingIntent = pendingIntent;
5603 return this;
5604 }
5605
5606 /**
5607 * Sets the timestamp of the most recent message in an unread conversation.
5608 *
5609 * If a messaging notification has been posted by your application and has not
5610 * yet been cancelled, posting a later notification with the same id and tag
5611 * but without a newer timestamp may result in Android Auto not displaying a
5612 * heads up notification for the later notification.
5613 *
5614 * @param timestamp The timestamp of the most recent message in the conversation.
5615 * @return This object for method chaining.
5616 */
5617 public Builder setLatestTimestamp(long timestamp) {
5618 mLatestTimestamp = timestamp;
5619 return this;
5620 }
5621
5622 /**
5623 * Builds a new unread conversation object.
5624 *
5625 * @return The new unread conversation object.
5626 */
5627 public UnreadConversation build() {
5628 String[] messages = mMessages.toArray(new String[mMessages.size()]);
5629 String[] participants = { mParticipant };
5630 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
5631 mReadPendingIntent, participants, mLatestTimestamp);
5632 }
5633 }
5634 }
5635
5636 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07005637 * Get an array of Notification objects from a parcelable array bundle field.
5638 * Update the bundle to have a typed array so fetches in the future don't need
5639 * to do an array copy.
5640 */
5641 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
5642 Parcelable[] array = bundle.getParcelableArray(key);
5643 if (array instanceof Notification[] || array == null) {
5644 return (Notification[]) array;
5645 }
5646 Notification[] typedArray = Arrays.copyOf(array, array.length,
5647 Notification[].class);
5648 bundle.putParcelableArray(key, typedArray);
5649 return typedArray;
5650 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005651
5652 private static class BuilderRemoteViews extends RemoteViews {
5653 public BuilderRemoteViews(Parcel parcel) {
5654 super(parcel);
5655 }
5656
Kenny Guy77320062014-08-27 21:37:15 +01005657 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
5658 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005659 }
5660
5661 @Override
5662 public BuilderRemoteViews clone() {
5663 Parcel p = Parcel.obtain();
5664 writeToParcel(p, 0);
5665 p.setDataPosition(0);
5666 BuilderRemoteViews brv = new BuilderRemoteViews(p);
5667 p.recycle();
5668 return brv;
5669 }
5670 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005671}