blob: e290d3e9475bf73d80bfcd3165855d2da151d40b [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.app;
18
Tor Norbye80756e32015-03-02 09:39:27 -080019import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070020import android.annotation.DrawableRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070021import android.annotation.IntDef;
Daniel Sandler01df1c62014-06-09 10:54:01 -040022import android.annotation.SdkConstant;
23import android.annotation.SdkConstant.SdkConstantType;
Julia Reynolds233a5f92015-10-19 13:51:23 -040024import android.annotation.SystemApi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080025import android.content.Context;
26import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010027import android.content.pm.ApplicationInfo;
Selim Cinek65b2e7c2015-10-26 14:11:31 -070028import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020029import android.content.pm.PackageManager.NameNotFoundException;
Jorim Jaggief72a192014-08-26 21:57:46 +020030import android.content.res.ColorStateList;
Joe Onoratoef1e7762010-09-17 18:38:38 -040031import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010032import android.graphics.Canvas;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010033import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010034import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040035import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040036import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070037import android.media.AudioManager;
Jeff Browndba34ba2014-06-24 20:46:03 -070038import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080039import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040040import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020041import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050042import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.Parcel;
44import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040045import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070046import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.text.TextUtils;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040048import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050049import android.util.SparseArray;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040050import android.util.TypedValue;
Griff Hazen61a9e862014-05-22 16:05:19 -070051import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080052import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080053import android.view.View;
Adrian Roos9b123cf2016-02-04 14:55:57 -080054import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070055import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056import android.widget.RemoteViews;
57
Griff Hazen959591e2014-05-15 22:26:18 -070058import com.android.internal.R;
Griff Hazenc091ba82014-05-16 10:13:26 -070059import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070060
Tor Norbyed9273d62013-05-30 15:59:53 -070061import java.lang.annotation.Retention;
62import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020063import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050064import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070065import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070066import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070067import java.util.List;
Julia Reynolds74303cf2015-10-16 11:37:55 -040068import java.util.Objects;
Dan Sandler50128532015-12-08 15:42:41 -050069import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080070
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071/**
72 * A class that represents how a persistent notification is to be presented to
73 * the user using the {@link android.app.NotificationManager}.
74 *
Joe Onoratocb109a02011-01-18 17:57:41 -080075 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
76 * easier to construct Notifications.</p>
77 *
Joe Fernandez558459f2011-10-13 16:47:36 -070078 * <div class="special reference">
79 * <h3>Developer Guides</h3>
80 * <p>For a guide to creating notifications, read the
81 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
82 * developer guide.</p>
83 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 */
85public class Notification implements Parcelable
86{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040087 private static final String TAG = "Notification";
88
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -040090 * An activity that provides a user interface for adjusting notification preferences for its
91 * containing application. Optional but recommended for apps that post
92 * {@link android.app.Notification Notifications}.
93 */
94 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
95 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
96 = "android.intent.category.NOTIFICATION_PREFERENCES";
97
98 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 * Use all default values (where applicable).
100 */
101 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500102
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 /**
104 * Use the default notification sound. This will ignore any given
105 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500106 *
Chris Wren47c20a12014-06-18 17:27:29 -0400107 * <p>
108 * A notification that is noisy is more likely to be presented as a heads-up notification.
109 * </p>
110 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500112 */
113
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800114 public static final int DEFAULT_SOUND = 1;
115
116 /**
117 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500118 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700119 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500120 *
Chris Wren47c20a12014-06-18 17:27:29 -0400121 * <p>
122 * A notification that vibrates is more likely to be presented as a heads-up notification.
123 * </p>
124 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500126 */
127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500129
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 /**
131 * Use the default notification lights. This will ignore the
132 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
133 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500134 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500136 */
137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500139
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800140 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200141 * Maximum length of CharSequences accepted by Builder and friends.
142 *
143 * <p>
144 * Avoids spamming the system with overly large strings such as full e-mails.
145 */
146 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
147
148 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800149 * Maximum entries of reply text that are accepted by Builder and friends.
150 */
151 private static final int MAX_REPLY_HISTORY = 5;
152
153 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500154 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800155 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500156 * Default value: {@link System#currentTimeMillis() Now}.
157 *
158 * Choose a timestamp that will be most relevant to the user. For most finite events, this
159 * corresponds to the time the event happened (or will happen, in the case of events that have
160 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800161 * timestamped according to when the activity began.
162 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500163 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800164 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500165 * <ul>
166 * <li>Notification of a new chat message should be stamped when the message was received.</li>
167 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
168 * <li>Notification of a completed file download should be stamped when the download finished.</li>
169 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
170 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
171 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800172 * </ul>
173 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 */
175 public long when;
176
177 /**
178 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400179 *
180 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 */
Dan Sandler86647982015-05-13 23:41:13 -0400182 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700183 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 public int icon;
185
186 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800187 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
188 * leave it at its default value of 0.
189 *
190 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700191 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800192 */
193 public int iconLevel;
194
195 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500196 * The number of events that this notification represents. For example, in a new mail
197 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800198 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500199 * The system may or may not use this field to modify the appearance of the notification. For
200 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
201 * superimposed over the icon in the status bar. Starting with
202 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
203 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800204 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500205 * If the number is 0 or negative, it is never shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 */
207 public int number;
208
209 /**
210 * The intent to execute when the expanded status entry is clicked. If
211 * this is an activity, it must include the
212 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800213 * that you take care of task management as described in the
214 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800215 * Stack</a> document. In particular, make sure to read the notification section
216 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
217 * Notifications</a> for the correct ways to launch an application from a
218 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 */
220 public PendingIntent contentIntent;
221
222 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500223 * The intent to execute when the notification is explicitly dismissed by the user, either with
224 * the "Clear All" button or by swiping it away individually.
225 *
226 * This probably shouldn't be launching an activity since several of those will be sent
227 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 */
229 public PendingIntent deleteIntent;
230
231 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700232 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800233 *
Chris Wren47c20a12014-06-18 17:27:29 -0400234 * <p>
235 * The system UI may choose to display a heads-up notification, instead of
236 * launching this intent, while the user is using the device.
237 * </p>
238 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800239 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400240 */
241 public PendingIntent fullScreenIntent;
242
243 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400244 * Text that summarizes this notification for accessibility services.
245 *
246 * As of the L release, this text is no longer shown on screen, but it is still useful to
247 * accessibility services (where it serves as an audible announcement of the notification's
248 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400249 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800250 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800251 */
252 public CharSequence tickerText;
253
254 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400255 * Formerly, a view showing the {@link #tickerText}.
256 *
257 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400258 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400259 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800260 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400261
262 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400263 * The view that will represent this notification in the notification list (which is pulled
264 * down from the status bar).
265 *
266 * As of N, this field is not used. The notification view is determined by the inputs to
267 * {@link Notification.Builder}; a custom RemoteViews can optionally be
268 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400270 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800271 public RemoteViews contentView;
272
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400273 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400274 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400275 * opportunity to show more detail. The system UI may choose to show this
276 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400277 *
278 * As of N, this field is not used. The expanded notification view is determined by the
279 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
280 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400281 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400282 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400283 public RemoteViews bigContentView;
284
Chris Wren8fd39ec2014-02-27 17:43:26 -0500285
286 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400287 * A medium-format version of {@link #contentView}, providing the Notification an
288 * opportunity to add action buttons to contentView. At its discretion, the system UI may
289 * choose to show this as a heads-up notification, which will pop up so the user can see
290 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400291 *
292 * As of N, this field is not used. The heads-up notification view is determined by the
293 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
294 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500295 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400296 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500297 public RemoteViews headsUpContentView;
298
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400299 /**
Dan Sandler86647982015-05-13 23:41:13 -0400300 * A large bitmap to be shown in the notification content area.
301 *
302 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 */
Dan Sandler86647982015-05-13 23:41:13 -0400304 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800305 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306
307 /**
308 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500309 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800310 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400311 * A notification that is noisy is more likely to be presented as a heads-up notification.
312 * </p>
313 *
314 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500315 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 * </p>
317 */
318 public Uri sound;
319
320 /**
321 * Use this constant as the value for audioStreamType to request that
322 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700323 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400324 *
325 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800326 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700327 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800328 public static final int STREAM_DEFAULT = -1;
329
330 /**
331 * The audio stream type to use when playing the sound.
332 * Should be one of the STREAM_ constants from
333 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400334 *
335 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800336 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700337 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800338 public int audioStreamType = STREAM_DEFAULT;
339
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800340 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400341 * The default value of {@link #audioAttributes}.
342 */
343 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
344 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
345 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
346 .build();
347
348 /**
349 * The {@link AudioAttributes audio attributes} to use when playing the sound.
350 */
351 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
352
353 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500354 * The pattern with which to vibrate.
355 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800356 * <p>
357 * To vibrate the default pattern, see {@link #defaults}.
358 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500359 *
Chris Wren47c20a12014-06-18 17:27:29 -0400360 * <p>
361 * A notification that vibrates is more likely to be presented as a heads-up notification.
362 * </p>
363 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 * @see android.os.Vibrator#vibrate(long[],int)
365 */
366 public long[] vibrate;
367
368 /**
369 * The color of the led. The hardware will do its best approximation.
370 *
371 * @see #FLAG_SHOW_LIGHTS
372 * @see #flags
373 */
Tor Norbye80756e32015-03-02 09:39:27 -0800374 @ColorInt
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800375 public int ledARGB;
376
377 /**
378 * The number of milliseconds for the LED to be on while it's flashing.
379 * The hardware will do its best approximation.
380 *
381 * @see #FLAG_SHOW_LIGHTS
382 * @see #flags
383 */
384 public int ledOnMS;
385
386 /**
387 * The number of milliseconds for the LED to be off while it's flashing.
388 * The hardware will do its best approximation.
389 *
390 * @see #FLAG_SHOW_LIGHTS
391 * @see #flags
392 */
393 public int ledOffMS;
394
395 /**
396 * Specifies which values should be taken from the defaults.
397 * <p>
398 * To set, OR the desired from {@link #DEFAULT_SOUND},
399 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
400 * values, use {@link #DEFAULT_ALL}.
401 * </p>
402 */
403 public int defaults;
404
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 /**
406 * Bit to be bitwise-ored into the {@link #flags} field that should be
407 * set if you want the LED on for this notification.
408 * <ul>
409 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
410 * or 0 for both ledOnMS and ledOffMS.</li>
411 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
412 * <li>To flash the LED, pass the number of milliseconds that it should
413 * be on and off to ledOnMS and ledOffMS.</li>
414 * </ul>
415 * <p>
416 * Since hardware varies, you are not guaranteed that any of the values
417 * you pass are honored exactly. Use the system defaults (TODO) if possible
418 * because they will be set to values that work on any given hardware.
419 * <p>
420 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500421 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800422 */
423 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
424
425 /**
426 * Bit to be bitwise-ored into the {@link #flags} field that should be
427 * set if this notification is in reference to something that is ongoing,
428 * like a phone call. It should not be set if this notification is in
429 * reference to something that happened at a particular point in time,
430 * like a missed phone call.
431 */
432 public static final int FLAG_ONGOING_EVENT = 0x00000002;
433
434 /**
435 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700436 * the audio will be repeated until the notification is
437 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800438 */
439 public static final int FLAG_INSISTENT = 0x00000004;
440
441 /**
442 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700443 * set if you would only like the sound, vibrate and ticker to be played
444 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 */
446 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
447
448 /**
449 * Bit to be bitwise-ored into the {@link #flags} field that should be
450 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500451 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800452 */
453 public static final int FLAG_AUTO_CANCEL = 0x00000010;
454
455 /**
456 * Bit to be bitwise-ored into the {@link #flags} field that should be
457 * set if the notification should not be canceled when the user clicks
458 * the Clear all button.
459 */
460 public static final int FLAG_NO_CLEAR = 0x00000020;
461
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700462 /**
463 * Bit to be bitwise-ored into the {@link #flags} field that should be
464 * set if this notification represents a currently running service. This
465 * will normally be set for you by {@link Service#startForeground}.
466 */
467 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
468
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400469 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500470 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800471 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500472 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400473 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500474 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400475
Griff Hazendfcb0802014-02-11 12:00:00 -0800476 /**
477 * Bit to be bitswise-ored into the {@link #flags} field that should be
478 * set if this notification is relevant to the current device only
479 * and it is not recommended that it bridge to other devices.
480 */
481 public static final int FLAG_LOCAL_ONLY = 0x00000100;
482
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700483 /**
484 * Bit to be bitswise-ored into the {@link #flags} field that should be
485 * set if this notification is the group summary for a group of notifications.
486 * Grouped notifications may display in a cluster or stack on devices which
487 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
488 */
489 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
490
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800491 public int flags;
492
Tor Norbyed9273d62013-05-30 15:59:53 -0700493 /** @hide */
494 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
495 @Retention(RetentionPolicy.SOURCE)
496 public @interface Priority {}
497
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800498 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500499 * Default notification {@link #priority}. If your application does not prioritize its own
500 * notifications, use this value for all notifications.
501 */
502 public static final int PRIORITY_DEFAULT = 0;
503
504 /**
505 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
506 * items smaller, or at a different position in the list, compared with your app's
507 * {@link #PRIORITY_DEFAULT} items.
508 */
509 public static final int PRIORITY_LOW = -1;
510
511 /**
512 * Lowest {@link #priority}; these items might not be shown to the user except under special
513 * circumstances, such as detailed notification logs.
514 */
515 public static final int PRIORITY_MIN = -2;
516
517 /**
518 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
519 * show these items larger, or at a different position in notification lists, compared with
520 * your app's {@link #PRIORITY_DEFAULT} items.
521 */
522 public static final int PRIORITY_HIGH = 1;
523
524 /**
525 * Highest {@link #priority}, for your application's most important items that require the
526 * user's prompt attention or input.
527 */
528 public static final int PRIORITY_MAX = 2;
529
530 /**
531 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800532 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500533 * Priority is an indication of how much of the user's valuable attention should be consumed by
534 * this notification. Low-priority notifications may be hidden from the user in certain
535 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500536 * system will make a determination about how to interpret this priority when presenting
537 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400538 *
539 * <p>
540 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
541 * as a heads-up notification.
542 * </p>
543 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500544 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700545 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500546 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800547
Dan Sandler26e81cf2014-05-06 10:01:27 -0400548 /**
549 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
550 * to be applied by the standard Style templates when presenting this notification.
551 *
552 * The current template design constructs a colorful header image by overlaying the
553 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
554 * ignored.
555 */
Tor Norbye80756e32015-03-02 09:39:27 -0800556 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400557 public int color = COLOR_DEFAULT;
558
559 /**
560 * Special value of {@link #color} telling the system not to decorate this notification with
561 * any special color but instead use default colors when presenting this notification.
562 */
Tor Norbye80756e32015-03-02 09:39:27 -0800563 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400564 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600565
566 /**
567 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
568 * the notification's presence and contents in untrusted situations (namely, on the secure
569 * lockscreen).
570 *
571 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
572 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
573 * shown in all situations, but the contents are only available if the device is unlocked for
574 * the appropriate user.
575 *
576 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
577 * can be read even in an "insecure" context (that is, above a secure lockscreen).
578 * To modify the public version of this notification—for example, to redact some portions—see
579 * {@link Builder#setPublicVersion(Notification)}.
580 *
581 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
582 * and ticker until the user has bypassed the lockscreen.
583 */
584 public int visibility;
585
Griff Hazenfc3922d2014-08-20 11:56:44 -0700586 /**
587 * Notification visibility: Show this notification in its entirety on all lockscreens.
588 *
589 * {@see #visibility}
590 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600591 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700592
593 /**
594 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
595 * private information on secure lockscreens.
596 *
597 * {@see #visibility}
598 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600599 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700600
601 /**
602 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
603 *
604 * {@see #visibility}
605 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600606 public static final int VISIBILITY_SECRET = -1;
607
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500608 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400609 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500610 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400611 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500612
613 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400614 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500615 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400616 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500617
618 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400619 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500620 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400621 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500622
623 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400624 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500625 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400626 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500627
628 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400629 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500630 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400631 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500632
633 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400634 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500635 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400636 public static final String CATEGORY_ALARM = "alarm";
637
638 /**
639 * Notification category: progress of a long-running background operation.
640 */
641 public static final String CATEGORY_PROGRESS = "progress";
642
643 /**
644 * Notification category: social network or sharing update.
645 */
646 public static final String CATEGORY_SOCIAL = "social";
647
648 /**
649 * Notification category: error in background operation or authentication status.
650 */
651 public static final String CATEGORY_ERROR = "err";
652
653 /**
654 * Notification category: media transport control for playback.
655 */
656 public static final String CATEGORY_TRANSPORT = "transport";
657
658 /**
659 * Notification category: system or device status update. Reserved for system use.
660 */
661 public static final String CATEGORY_SYSTEM = "sys";
662
663 /**
664 * Notification category: indication of running background service.
665 */
666 public static final String CATEGORY_SERVICE = "service";
667
668 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400669 * Notification category: a specific, timely recommendation for a single thing.
670 * For example, a news app might want to recommend a news story it believes the user will
671 * want to read next.
672 */
673 public static final String CATEGORY_RECOMMENDATION = "recommendation";
674
675 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400676 * Notification category: ongoing information about device or contextual status.
677 */
678 public static final String CATEGORY_STATUS = "status";
679
680 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400681 * Notification category: user-scheduled reminder.
682 */
683 public static final String CATEGORY_REMINDER = "reminder";
684
685 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400686 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
687 * that best describes this Notification. May be used by the system for ranking and filtering.
688 */
689 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500690
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700691 private String mGroupKey;
692
693 /**
694 * Get the key used to group this notification into a cluster or stack
695 * with other notifications on devices which support such rendering.
696 */
697 public String getGroup() {
698 return mGroupKey;
699 }
700
701 private String mSortKey;
702
703 /**
704 * Get a sort key that orders this notification among other notifications from the
705 * same package. This can be useful if an external sort was already applied and an app
706 * would like to preserve this. Notifications will be sorted lexicographically using this
707 * value, although providing different priorities in addition to providing sort key may
708 * cause this value to be ignored.
709 *
710 * <p>This sort key can also be used to order members of a notification group. See
711 * {@link Builder#setGroup}.
712 *
713 * @see String#compareTo(String)
714 */
715 public String getSortKey() {
716 return mSortKey;
717 }
718
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500719 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400720 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400721 * <p>
722 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
723 * APIs, and are intended to be used by
724 * {@link android.service.notification.NotificationListenerService} implementations to extract
725 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500726 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400727 public Bundle extras = new Bundle();
728
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400729 /**
730 * {@link #extras} key: this is the title of the notification,
731 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
732 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500733 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400734
735 /**
736 * {@link #extras} key: this is the title of the notification when shown in expanded form,
737 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
738 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400739 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400740
741 /**
742 * {@link #extras} key: this is the main text payload, as supplied to
743 * {@link Builder#setContentText(CharSequence)}.
744 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500745 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400746
747 /**
748 * {@link #extras} key: this is a third line of text, as supplied to
749 * {@link Builder#setSubText(CharSequence)}.
750 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400751 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400752
753 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800754 * {@link #extras} key: this is the remote input history, as supplied to
755 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
756 */
757 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
758
759 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400760 * {@link #extras} key: this is a small piece of additional text as supplied to
761 * {@link Builder#setContentInfo(CharSequence)}.
762 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400763 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400764
765 /**
766 * {@link #extras} key: this is a line of summary information intended to be shown
767 * alongside expanded notifications, as supplied to (e.g.)
768 * {@link BigTextStyle#setSummaryText(CharSequence)}.
769 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400770 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400771
772 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200773 * {@link #extras} key: this is the longer text shown in the big form of a
774 * {@link BigTextStyle} notification, as supplied to
775 * {@link BigTextStyle#bigText(CharSequence)}.
776 */
777 public static final String EXTRA_BIG_TEXT = "android.bigText";
778
779 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400780 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
781 * supplied to {@link Builder#setSmallIcon(int)}.
782 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500783 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400784
785 /**
786 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
787 * notification payload, as
788 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
789 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400790 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400791
792 /**
793 * {@link #extras} key: this is a bitmap to be used instead of the one from
794 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
795 * shown in its expanded form, as supplied to
796 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
797 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400798 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400799
800 /**
801 * {@link #extras} key: this is the progress value supplied to
802 * {@link Builder#setProgress(int, int, boolean)}.
803 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400804 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400805
806 /**
807 * {@link #extras} key: this is the maximum value supplied to
808 * {@link Builder#setProgress(int, int, boolean)}.
809 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400810 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400811
812 /**
813 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
814 * {@link Builder#setProgress(int, int, boolean)}.
815 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400816 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400817
818 /**
819 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
820 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
821 * {@link Builder#setUsesChronometer(boolean)}.
822 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400823 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400824
825 /**
826 * {@link #extras} key: whether {@link #when} should be shown,
827 * as supplied to {@link Builder#setShowWhen(boolean)}.
828 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400829 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400830
831 /**
832 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
833 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
834 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400835 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400836
837 /**
838 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
839 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
840 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400841 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400842
843 /**
844 * {@link #extras} key: A string representing the name of the specific
845 * {@link android.app.Notification.Style} used to create this notification.
846 */
Chris Wren91ad5632013-06-05 15:05:57 -0400847 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400848
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400849 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400850 * {@link #extras} key: A String array containing the people that this notification relates to,
851 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400852 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400853 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500854
855 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400856 * Allow certain system-generated notifications to appear before the device is provisioned.
857 * Only available to notifications coming from the android package.
858 * @hide
859 */
860 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
861
862 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700863 * {@link #extras} key: A
864 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
865 * in the background when the notification is selected. The URI must point to an image stream
866 * suitable for passing into
867 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
868 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
869 * URI used for this purpose must require no permissions to read the image data.
870 */
871 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
872
873 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400874 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -0700875 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -0400876 * {@link android.app.Notification.MediaStyle} notification.
877 */
878 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
879
880 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +0100881 * {@link #extras} key: the indices of actions to be shown in the compact view,
882 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
883 */
884 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
885
Christoph Studer943aa672014-08-03 20:31:16 +0200886 /**
Kenny Guy8942bcd2014-09-08 21:09:47 +0100887 * {@link #extras} key: the user that built the notification.
888 *
889 * @hide
890 */
891 public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId";
892
893 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400894 * @hide
895 */
896 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
897
Selim Cinek247fa012016-02-18 09:50:48 -0800898 /**
899 * @hide
900 */
901 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
902
Dan Sandlerd63f9322015-05-06 15:18:49 -0400903 private Icon mSmallIcon;
904 private Icon mLargeIcon;
905
Chris Wren51c75102013-07-16 20:49:17 -0400906 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400907 * Structure to encapsulate a named action that can be shown as part of this notification.
908 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
909 * selected by the user.
910 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700911 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
912 * or {@link Notification.Builder#addAction(Notification.Action)}
913 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400914 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500915 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700916 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -0400917 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -0700918 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700919
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400920 /**
921 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -0400922 *
923 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400924 */
Dan Sandler86647982015-05-13 23:41:13 -0400925 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400926 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700927
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400928 /**
929 * Title of the action.
930 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400931 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700932
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400933 /**
934 * Intent to send when the user invokes this action. May be null, in which case the action
935 * may be rendered in a disabled presentation by the system UI.
936 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400937 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -0700938
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400939 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -0400940 if (in.readInt() != 0) {
941 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -0400942 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
943 icon = mIcon.getResId();
944 }
Dan Sandler86647982015-05-13 23:41:13 -0400945 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400946 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
947 if (in.readInt() == 1) {
948 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
949 }
Griff Hazen959591e2014-05-15 22:26:18 -0700950 mExtras = in.readBundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700951 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400952 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700953
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400954 /**
Dan Sandler86647982015-05-13 23:41:13 -0400955 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400956 */
Dan Sandler86647982015-05-13 23:41:13 -0400957 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400958 public Action(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -0400959 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700960 }
961
Dan Sandler86647982015-05-13 23:41:13 -0400962 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700963 RemoteInput[] remoteInputs) {
Dan Sandler86647982015-05-13 23:41:13 -0400964 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -0400965 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
966 this.icon = icon.getResId();
967 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400968 this.title = title;
969 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -0700970 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700971 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700972 }
973
974 /**
Dan Sandler86647982015-05-13 23:41:13 -0400975 * Return an icon representing the action.
976 */
977 public Icon getIcon() {
978 if (mIcon == null && icon != 0) {
979 // you snuck an icon in here without using the builder; let's try to keep it
980 mIcon = Icon.createWithResource("", icon);
981 }
982 return mIcon;
983 }
984
985 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700986 * Get additional metadata carried around with this Action.
987 */
988 public Bundle getExtras() {
989 return mExtras;
990 }
991
992 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700993 * Get the list of inputs to be collected from the user when this action is sent.
994 * May return null if no remote inputs were added.
995 */
996 public RemoteInput[] getRemoteInputs() {
997 return mRemoteInputs;
998 }
999
1000 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001001 * Builder class for {@link Action} objects.
1002 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001003 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001004 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001005 private final CharSequence mTitle;
1006 private final PendingIntent mIntent;
1007 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001008 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001009
1010 /**
1011 * Construct a new builder for {@link Action} object.
1012 * @param icon icon to show for this action
1013 * @param title the title of the action
1014 * @param intent the {@link PendingIntent} to fire when users trigger this action
1015 */
Dan Sandler86647982015-05-13 23:41:13 -04001016 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001017 public Builder(int icon, CharSequence title, PendingIntent intent) {
Dan Sandler86647982015-05-13 23:41:13 -04001018 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null);
1019 }
1020
1021 /**
1022 * Construct a new builder for {@link Action} object.
1023 * @param icon icon to show for this action
1024 * @param title the title of the action
1025 * @param intent the {@link PendingIntent} to fire when users trigger this action
1026 */
1027 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001028 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -07001029 }
1030
1031 /**
1032 * Construct a new builder for {@link Action} object using the fields from an
1033 * {@link Action}.
1034 * @param action the action to read fields from.
1035 */
1036 public Builder(Action action) {
Dan Sandler86647982015-05-13 23:41:13 -04001037 this(action.getIcon(), action.title, action.actionIntent, new Bundle(action.mExtras),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001038 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -07001039 }
1040
Dan Sandler86647982015-05-13 23:41:13 -04001041 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001042 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -07001043 mIcon = icon;
1044 mTitle = title;
1045 mIntent = intent;
1046 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001047 if (remoteInputs != null) {
1048 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1049 Collections.addAll(mRemoteInputs, remoteInputs);
1050 }
Griff Hazen959591e2014-05-15 22:26:18 -07001051 }
1052
1053 /**
1054 * Merge additional metadata into this builder.
1055 *
1056 * <p>Values within the Bundle will replace existing extras values in this Builder.
1057 *
1058 * @see Notification.Action#extras
1059 */
1060 public Builder addExtras(Bundle extras) {
1061 if (extras != null) {
1062 mExtras.putAll(extras);
1063 }
1064 return this;
1065 }
1066
1067 /**
1068 * Get the metadata Bundle used by this Builder.
1069 *
1070 * <p>The returned Bundle is shared with this Builder.
1071 */
1072 public Bundle getExtras() {
1073 return mExtras;
1074 }
1075
1076 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001077 * Add an input to be collected from the user when this action is sent.
1078 * Response values can be retrieved from the fired intent by using the
1079 * {@link RemoteInput#getResultsFromIntent} function.
1080 * @param remoteInput a {@link RemoteInput} to add to the action
1081 * @return this object for method chaining
1082 */
1083 public Builder addRemoteInput(RemoteInput remoteInput) {
1084 if (mRemoteInputs == null) {
1085 mRemoteInputs = new ArrayList<RemoteInput>();
1086 }
1087 mRemoteInputs.add(remoteInput);
1088 return this;
1089 }
1090
1091 /**
1092 * Apply an extender to this action builder. Extenders may be used to add
1093 * metadata or change options on this builder.
1094 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001095 public Builder extend(Extender extender) {
1096 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001097 return this;
1098 }
1099
1100 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001101 * Combine all of the options that have been set and return a new {@link Action}
1102 * object.
1103 * @return the built action
1104 */
1105 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001106 RemoteInput[] remoteInputs = mRemoteInputs != null
1107 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
1108 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -07001109 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001110 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001111
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001112 @Override
1113 public Action clone() {
1114 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001115 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001116 title,
1117 actionIntent, // safe to alias
1118 new Bundle(mExtras),
1119 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001120 }
1121 @Override
1122 public int describeContents() {
1123 return 0;
1124 }
1125 @Override
1126 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001127 final Icon ic = getIcon();
1128 if (ic != null) {
1129 out.writeInt(1);
1130 ic.writeToParcel(out, 0);
1131 } else {
1132 out.writeInt(0);
1133 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001134 TextUtils.writeToParcel(title, out, flags);
1135 if (actionIntent != null) {
1136 out.writeInt(1);
1137 actionIntent.writeToParcel(out, flags);
1138 } else {
1139 out.writeInt(0);
1140 }
Griff Hazen959591e2014-05-15 22:26:18 -07001141 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001142 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001143 }
Griff Hazen959591e2014-05-15 22:26:18 -07001144 public static final Parcelable.Creator<Action> CREATOR =
1145 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001146 public Action createFromParcel(Parcel in) {
1147 return new Action(in);
1148 }
1149 public Action[] newArray(int size) {
1150 return new Action[size];
1151 }
1152 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001153
1154 /**
1155 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1156 * metadata or change options on an action builder.
1157 */
1158 public interface Extender {
1159 /**
1160 * Apply this extender to a notification action builder.
1161 * @param builder the builder to be modified.
1162 * @return the build object for chaining.
1163 */
1164 public Builder extend(Builder builder);
1165 }
1166
1167 /**
1168 * Wearable extender for notification actions. To add extensions to an action,
1169 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1170 * the {@code WearableExtender()} constructor and apply it to a
1171 * {@link android.app.Notification.Action.Builder} using
1172 * {@link android.app.Notification.Action.Builder#extend}.
1173 *
1174 * <pre class="prettyprint">
1175 * Notification.Action action = new Notification.Action.Builder(
1176 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001177 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001178 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001179 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001180 */
1181 public static final class WearableExtender implements Extender {
1182 /** Notification action extra which contains wearable extensions */
1183 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1184
Pete Gastaf6781d2014-10-07 15:17:05 -04001185 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001186 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001187 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1188 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1189 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001190
1191 // Flags bitwise-ored to mFlags
1192 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
1193
1194 // Default value for flags integer
1195 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1196
1197 private int mFlags = DEFAULT_FLAGS;
1198
Pete Gastaf6781d2014-10-07 15:17:05 -04001199 private CharSequence mInProgressLabel;
1200 private CharSequence mConfirmLabel;
1201 private CharSequence mCancelLabel;
1202
Griff Hazen61a9e862014-05-22 16:05:19 -07001203 /**
1204 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1205 * options.
1206 */
1207 public WearableExtender() {
1208 }
1209
1210 /**
1211 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1212 * wearable options present in an existing notification action.
1213 * @param action the notification action to inspect.
1214 */
1215 public WearableExtender(Action action) {
1216 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1217 if (wearableBundle != null) {
1218 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001219 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1220 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1221 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001222 }
1223 }
1224
1225 /**
1226 * Apply wearable extensions to a notification action that is being built. This is
1227 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1228 * method of {@link android.app.Notification.Action.Builder}.
1229 */
1230 @Override
1231 public Action.Builder extend(Action.Builder builder) {
1232 Bundle wearableBundle = new Bundle();
1233
1234 if (mFlags != DEFAULT_FLAGS) {
1235 wearableBundle.putInt(KEY_FLAGS, mFlags);
1236 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001237 if (mInProgressLabel != null) {
1238 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1239 }
1240 if (mConfirmLabel != null) {
1241 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1242 }
1243 if (mCancelLabel != null) {
1244 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1245 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001246
1247 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1248 return builder;
1249 }
1250
1251 @Override
1252 public WearableExtender clone() {
1253 WearableExtender that = new WearableExtender();
1254 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001255 that.mInProgressLabel = this.mInProgressLabel;
1256 that.mConfirmLabel = this.mConfirmLabel;
1257 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001258 return that;
1259 }
1260
1261 /**
1262 * Set whether this action is available when the wearable device is not connected to
1263 * a companion device. The user can still trigger this action when the wearable device is
1264 * offline, but a visual hint will indicate that the action may not be available.
1265 * Defaults to true.
1266 */
1267 public WearableExtender setAvailableOffline(boolean availableOffline) {
1268 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1269 return this;
1270 }
1271
1272 /**
1273 * Get whether this action is available when the wearable device is not connected to
1274 * a companion device. The user can still trigger this action when the wearable device is
1275 * offline, but a visual hint will indicate that the action may not be available.
1276 * Defaults to true.
1277 */
1278 public boolean isAvailableOffline() {
1279 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1280 }
1281
1282 private void setFlag(int mask, boolean value) {
1283 if (value) {
1284 mFlags |= mask;
1285 } else {
1286 mFlags &= ~mask;
1287 }
1288 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001289
1290 /**
1291 * Set a label to display while the wearable is preparing to automatically execute the
1292 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1293 *
1294 * @param label the label to display while the action is being prepared to execute
1295 * @return this object for method chaining
1296 */
1297 public WearableExtender setInProgressLabel(CharSequence label) {
1298 mInProgressLabel = label;
1299 return this;
1300 }
1301
1302 /**
1303 * Get the label to display while the wearable is preparing to automatically execute
1304 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1305 *
1306 * @return the label to display while the action is being prepared to execute
1307 */
1308 public CharSequence getInProgressLabel() {
1309 return mInProgressLabel;
1310 }
1311
1312 /**
1313 * Set a label to display to confirm that the action should be executed.
1314 * This is usually an imperative verb like "Send".
1315 *
1316 * @param label the label to confirm the action should be executed
1317 * @return this object for method chaining
1318 */
1319 public WearableExtender setConfirmLabel(CharSequence label) {
1320 mConfirmLabel = label;
1321 return this;
1322 }
1323
1324 /**
1325 * Get the label to display to confirm that the action should be executed.
1326 * This is usually an imperative verb like "Send".
1327 *
1328 * @return the label to confirm the action should be executed
1329 */
1330 public CharSequence getConfirmLabel() {
1331 return mConfirmLabel;
1332 }
1333
1334 /**
1335 * Set a label to display to cancel the action.
1336 * This is usually an imperative verb, like "Cancel".
1337 *
1338 * @param label the label to display to cancel the action
1339 * @return this object for method chaining
1340 */
1341 public WearableExtender setCancelLabel(CharSequence label) {
1342 mCancelLabel = label;
1343 return this;
1344 }
1345
1346 /**
1347 * Get the label to display to cancel the action.
1348 * This is usually an imperative verb like "Cancel".
1349 *
1350 * @return the label to display to cancel the action
1351 */
1352 public CharSequence getCancelLabel() {
1353 return mCancelLabel;
1354 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001355 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001356 }
1357
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001358 /**
1359 * Array of all {@link Action} structures attached to this notification by
1360 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1361 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1362 * interface for invoking actions.
1363 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001364 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001365
1366 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001367 * Replacement version of this notification whose content will be shown
1368 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1369 * and {@link #VISIBILITY_PUBLIC}.
1370 */
1371 public Notification publicVersion;
1372
1373 /**
Julia Reynolds74303cf2015-10-16 11:37:55 -04001374 * Structure to encapsulate a topic that is shown in Notification settings.
1375 * It must include an id and label.
1376 */
1377 public static class Topic implements Parcelable {
1378 private final String id;
1379 private final CharSequence label;
1380
1381 public Topic(String id, CharSequence label) {
1382 this.id = id;
1383 this.label = safeCharSequence(label);
1384 }
1385
1386 private Topic(Parcel in) {
1387 if (in.readInt() != 0) {
1388 id = in.readString();
1389 } else {
1390 id = null;
1391 }
1392 label = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1393 }
1394
1395 public String getId() {
1396 return id;
1397 }
1398
1399 public CharSequence getLabel() {
1400 return label;
1401 }
1402
1403 @Override
1404 public String toString() {
1405 return new StringBuilder(Topic.class.getSimpleName()).append('[')
1406 .append("id=").append(id)
1407 .append(",label=").append(label)
1408 .append(']').toString();
1409 }
1410
1411 @Override
1412 public boolean equals(Object o) {
1413 if (!(o instanceof Topic)) return false;
1414 if (o == this) return true;
1415 final Topic other = (Topic) o;
1416 return Objects.equals(other.id, id)
1417 && Objects.equals(other.label, label);
1418 }
1419
1420 @Override
1421 public int hashCode() {
1422 return Objects.hash(id, label);
1423 }
1424
1425 @Override
1426 public Topic clone() {
1427 return new Topic(id, label);
1428 }
1429
1430 @Override
1431 public int describeContents() {
1432 return 0;
1433 }
1434
1435 @Override
1436 public void writeToParcel(Parcel out, int flags) {
1437 if (id != null) {
1438 out.writeInt(1);
1439 out.writeString(id);
1440 } else {
1441 out.writeInt(0);
1442 }
1443 TextUtils.writeToParcel(label, out, flags);
1444 }
1445 public static final Parcelable.Creator<Topic> CREATOR =
1446 new Parcelable.Creator<Topic>() {
1447 public Topic createFromParcel(Parcel in) {
1448 return new Topic(in);
1449 }
1450 public Topic[] newArray(int size) {
1451 return new Topic[size];
1452 }
1453 };
1454 }
1455
Julia Reynolds233a5f92015-10-19 13:51:23 -04001456 @SystemApi
1457 public static final String TOPIC_DEFAULT = "system_default_topic";
1458
Julia Reynolds054c5dc2015-11-17 15:36:30 -05001459 private Topic topic;
Julia Reynolds74303cf2015-10-16 11:37:55 -04001460
Julia Reynolds054c5dc2015-11-17 15:36:30 -05001461 public Topic getTopic() {
1462 return topic;
Julia Reynolds74303cf2015-10-16 11:37:55 -04001463 }
1464
1465 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001466 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001467 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001468 */
1469 public Notification()
1470 {
1471 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001472 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001473 }
1474
1475 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001476 * @hide
1477 */
1478 public Notification(Context context, int icon, CharSequence tickerText, long when,
1479 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1480 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001481 new Builder(context)
1482 .setWhen(when)
1483 .setSmallIcon(icon)
1484 .setTicker(tickerText)
1485 .setContentTitle(contentTitle)
1486 .setContentText(contentText)
1487 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1488 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001489 }
1490
1491 /**
1492 * Constructs a Notification object with the information needed to
1493 * have a status bar icon without the standard expanded view.
1494 *
1495 * @param icon The resource id of the icon to put in the status bar.
1496 * @param tickerText The text that flows by in the status bar when the notification first
1497 * activates.
1498 * @param when The time to show in the time field. In the System.currentTimeMillis
1499 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001500 *
1501 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001502 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001503 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001504 public Notification(int icon, CharSequence tickerText, long when)
1505 {
1506 this.icon = icon;
1507 this.tickerText = tickerText;
1508 this.when = when;
1509 }
1510
1511 /**
1512 * Unflatten the notification from a parcel.
1513 */
1514 public Notification(Parcel parcel)
1515 {
1516 int version = parcel.readInt();
1517
1518 when = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001519 if (parcel.readInt() != 0) {
1520 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001521 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1522 icon = mSmallIcon.getResId();
1523 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001524 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001525 number = parcel.readInt();
1526 if (parcel.readInt() != 0) {
1527 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1528 }
1529 if (parcel.readInt() != 0) {
1530 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1531 }
1532 if (parcel.readInt() != 0) {
1533 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1534 }
1535 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001536 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001537 }
1538 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001539 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1540 }
Joe Onorato561d3852010-11-20 18:09:34 -08001541 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001542 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001544 defaults = parcel.readInt();
1545 flags = parcel.readInt();
1546 if (parcel.readInt() != 0) {
1547 sound = Uri.CREATOR.createFromParcel(parcel);
1548 }
1549
1550 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001551 if (parcel.readInt() != 0) {
1552 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1553 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001554 vibrate = parcel.createLongArray();
1555 ledARGB = parcel.readInt();
1556 ledOnMS = parcel.readInt();
1557 ledOffMS = parcel.readInt();
1558 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001559
1560 if (parcel.readInt() != 0) {
1561 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1562 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001563
1564 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001565
John Spurlockfd7f1e02014-03-18 16:41:57 -04001566 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001567
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001568 mGroupKey = parcel.readString();
1569
1570 mSortKey = parcel.readString();
1571
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001572 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001573
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001574 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1575
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001576 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001577 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1578 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001579
Chris Wren8fd39ec2014-02-27 17:43:26 -05001580 if (parcel.readInt() != 0) {
1581 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1582 }
1583
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001584 visibility = parcel.readInt();
1585
1586 if (parcel.readInt() != 0) {
1587 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1588 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001589
1590 color = parcel.readInt();
Julia Reynolds74303cf2015-10-16 11:37:55 -04001591
Julia Reynolds054c5dc2015-11-17 15:36:30 -05001592 if (parcel.readInt() != 0) {
1593 topic = Topic.CREATOR.createFromParcel(parcel);
1594 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001595 }
1596
Andy Stadler110988c2010-12-03 14:29:16 -08001597 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001598 public Notification clone() {
1599 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001600 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001601 return that;
1602 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001603
Daniel Sandler1a497d32013-04-18 14:52:45 -04001604 /**
1605 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1606 * of this into that.
1607 * @hide
1608 */
1609 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001610 that.when = this.when;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001611 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001612 that.number = this.number;
1613
1614 // PendingIntents are global, so there's no reason (or way) to clone them.
1615 that.contentIntent = this.contentIntent;
1616 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001617 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001618
1619 if (this.tickerText != null) {
1620 that.tickerText = this.tickerText.toString();
1621 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001622 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001623 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001624 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001625 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001626 that.contentView = this.contentView.clone();
1627 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001628 if (heavy && this.mLargeIcon != null) {
1629 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001630 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001631 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001632 that.sound = this.sound; // android.net.Uri is immutable
1633 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001634 if (this.audioAttributes != null) {
1635 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1636 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001637
1638 final long[] vibrate = this.vibrate;
1639 if (vibrate != null) {
1640 final int N = vibrate.length;
1641 final long[] vib = that.vibrate = new long[N];
1642 System.arraycopy(vibrate, 0, vib, 0, N);
1643 }
1644
1645 that.ledARGB = this.ledARGB;
1646 that.ledOnMS = this.ledOnMS;
1647 that.ledOffMS = this.ledOffMS;
1648 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001649
Joe Onorato18e69df2010-05-17 22:26:12 -07001650 that.flags = this.flags;
1651
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001652 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001653
John Spurlockfd7f1e02014-03-18 16:41:57 -04001654 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001655
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001656 that.mGroupKey = this.mGroupKey;
1657
1658 that.mSortKey = this.mSortKey;
1659
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001660 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001661 try {
1662 that.extras = new Bundle(this.extras);
1663 // will unparcel
1664 that.extras.size();
1665 } catch (BadParcelableException e) {
1666 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1667 that.extras = null;
1668 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001669 }
1670
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001671 if (this.actions != null) {
1672 that.actions = new Action[this.actions.length];
1673 for(int i=0; i<this.actions.length; i++) {
1674 that.actions[i] = this.actions[i].clone();
1675 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001676 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001677
Daniel Sandler1a497d32013-04-18 14:52:45 -04001678 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001679 that.bigContentView = this.bigContentView.clone();
1680 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001681
Chris Wren8fd39ec2014-02-27 17:43:26 -05001682 if (heavy && this.headsUpContentView != null) {
1683 that.headsUpContentView = this.headsUpContentView.clone();
1684 }
1685
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001686 that.visibility = this.visibility;
1687
1688 if (this.publicVersion != null) {
1689 that.publicVersion = new Notification();
1690 this.publicVersion.cloneInto(that.publicVersion, heavy);
1691 }
1692
Dan Sandler26e81cf2014-05-06 10:01:27 -04001693 that.color = this.color;
1694
Julia Reynolds054c5dc2015-11-17 15:36:30 -05001695 if (this.topic != null) {
1696 that.topic = this.topic.clone();
Julia Reynolds74303cf2015-10-16 11:37:55 -04001697 }
1698
Daniel Sandler1a497d32013-04-18 14:52:45 -04001699 if (!heavy) {
1700 that.lightenPayload(); // will clean out extras
1701 }
1702 }
1703
1704 /**
1705 * Removes heavyweight parts of the Notification object for archival or for sending to
1706 * listeners when the full contents are not necessary.
1707 * @hide
1708 */
1709 public final void lightenPayload() {
1710 tickerView = null;
1711 contentView = null;
1712 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001713 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001714 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05001715 if (extras != null && !extras.isEmpty()) {
1716 final Set<String> keyset = extras.keySet();
1717 final int N = keyset.size();
1718 final String[] keys = keyset.toArray(new String[N]);
1719 for (int i=0; i<N; i++) {
1720 final String key = keys[i];
1721 final Object obj = extras.get(key);
1722 if (obj != null &&
1723 ( obj instanceof Parcelable
1724 || obj instanceof Parcelable[]
1725 || obj instanceof SparseArray
1726 || obj instanceof ArrayList)) {
1727 extras.remove(key);
1728 }
1729 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001730 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001731 }
1732
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001733 /**
1734 * Make sure this CharSequence is safe to put into a bundle, which basically
1735 * means it had better not be some custom Parcelable implementation.
1736 * @hide
1737 */
1738 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02001739 if (cs == null) return cs;
1740 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
1741 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
1742 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001743 if (cs instanceof Parcelable) {
1744 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1745 + " instance is a custom Parcelable and not allowed in Notification");
1746 return cs.toString();
1747 }
1748
1749 return cs;
1750 }
1751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 public int describeContents() {
1753 return 0;
1754 }
1755
1756 /**
Dan Sandler4e787062015-06-17 15:09:48 -04001757 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001758 */
1759 public void writeToParcel(Parcel parcel, int flags)
1760 {
1761 parcel.writeInt(1);
1762
1763 parcel.writeLong(when);
Dan Sandler4e787062015-06-17 15:09:48 -04001764 if (mSmallIcon == null && icon != 0) {
1765 // you snuck an icon in here without using the builder; let's try to keep it
1766 mSmallIcon = Icon.createWithResource("", icon);
1767 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001768 if (mSmallIcon != null) {
1769 parcel.writeInt(1);
1770 mSmallIcon.writeToParcel(parcel, 0);
1771 } else {
1772 parcel.writeInt(0);
1773 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001774 parcel.writeInt(number);
1775 if (contentIntent != null) {
1776 parcel.writeInt(1);
1777 contentIntent.writeToParcel(parcel, 0);
1778 } else {
1779 parcel.writeInt(0);
1780 }
1781 if (deleteIntent != null) {
1782 parcel.writeInt(1);
1783 deleteIntent.writeToParcel(parcel, 0);
1784 } else {
1785 parcel.writeInt(0);
1786 }
1787 if (tickerText != null) {
1788 parcel.writeInt(1);
1789 TextUtils.writeToParcel(tickerText, parcel, flags);
1790 } else {
1791 parcel.writeInt(0);
1792 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001793 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001794 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001795 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001796 } else {
1797 parcel.writeInt(0);
1798 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001799 if (contentView != null) {
1800 parcel.writeInt(1);
1801 contentView.writeToParcel(parcel, 0);
1802 } else {
1803 parcel.writeInt(0);
1804 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001805 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001806 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04001807 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08001808 } else {
1809 parcel.writeInt(0);
1810 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001811
1812 parcel.writeInt(defaults);
1813 parcel.writeInt(this.flags);
1814
1815 if (sound != null) {
1816 parcel.writeInt(1);
1817 sound.writeToParcel(parcel, 0);
1818 } else {
1819 parcel.writeInt(0);
1820 }
1821 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04001822
1823 if (audioAttributes != null) {
1824 parcel.writeInt(1);
1825 audioAttributes.writeToParcel(parcel, 0);
1826 } else {
1827 parcel.writeInt(0);
1828 }
1829
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830 parcel.writeLongArray(vibrate);
1831 parcel.writeInt(ledARGB);
1832 parcel.writeInt(ledOnMS);
1833 parcel.writeInt(ledOffMS);
1834 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001835
1836 if (fullScreenIntent != null) {
1837 parcel.writeInt(1);
1838 fullScreenIntent.writeToParcel(parcel, 0);
1839 } else {
1840 parcel.writeInt(0);
1841 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001842
1843 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001844
John Spurlockfd7f1e02014-03-18 16:41:57 -04001845 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001846
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001847 parcel.writeString(mGroupKey);
1848
1849 parcel.writeString(mSortKey);
1850
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001851 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001852
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001853 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001854
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001855 if (bigContentView != null) {
1856 parcel.writeInt(1);
1857 bigContentView.writeToParcel(parcel, 0);
1858 } else {
1859 parcel.writeInt(0);
1860 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001861
Chris Wren8fd39ec2014-02-27 17:43:26 -05001862 if (headsUpContentView != null) {
1863 parcel.writeInt(1);
1864 headsUpContentView.writeToParcel(parcel, 0);
1865 } else {
1866 parcel.writeInt(0);
1867 }
1868
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001869 parcel.writeInt(visibility);
1870
1871 if (publicVersion != null) {
1872 parcel.writeInt(1);
1873 publicVersion.writeToParcel(parcel, 0);
1874 } else {
1875 parcel.writeInt(0);
1876 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001877
1878 parcel.writeInt(color);
Julia Reynolds74303cf2015-10-16 11:37:55 -04001879
Julia Reynolds054c5dc2015-11-17 15:36:30 -05001880 if (topic != null) {
1881 parcel.writeInt(1);
1882 topic.writeToParcel(parcel, 0);
1883 } else {
1884 parcel.writeInt(0);
1885 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 }
1887
1888 /**
1889 * Parcelable.Creator that instantiates Notification objects
1890 */
1891 public static final Parcelable.Creator<Notification> CREATOR
1892 = new Parcelable.Creator<Notification>()
1893 {
1894 public Notification createFromParcel(Parcel parcel)
1895 {
1896 return new Notification(parcel);
1897 }
1898
1899 public Notification[] newArray(int size)
1900 {
1901 return new Notification[size];
1902 }
1903 };
1904
1905 /**
1906 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1907 * layout.
1908 *
1909 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1910 * in the view.</p>
1911 * @param context The context for your application / activity.
1912 * @param contentTitle The title that goes in the expanded entry.
1913 * @param contentText The text that goes in the expanded entry.
1914 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1915 * If this is an activity, it must include the
1916 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001917 * that you take care of task management as described in the
1918 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1919 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001920 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001921 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04001922 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001923 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001924 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 public void setLatestEventInfo(Context context,
1926 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001927 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
1928 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
1929 new Throwable());
1930 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001931
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001932 // ensure that any information already set directly is preserved
1933 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001934
1935 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001936 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001937 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 }
1939 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001940 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001942 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001943
1944 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001945 }
1946
Julia Reynoldsda303542015-11-23 14:00:20 -05001947 /**
1948 * @hide
1949 */
1950 public static void addFieldsFromContext(Context context, Notification notification) {
1951 if (notification.extras.getParcelable(EXTRA_BUILDER_APPLICATION_INFO) == null) {
1952 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO,
1953 context.getApplicationInfo());
1954 }
1955 if (!notification.extras.containsKey(EXTRA_ORIGINATING_USERID)) {
1956 notification.extras.putInt(EXTRA_ORIGINATING_USERID, context.getUserId());
1957 }
1958 }
1959
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001960 @Override
1961 public String toString() {
1962 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001963 sb.append("Notification(pri=");
1964 sb.append(priority);
1965 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001966 if (contentView != null) {
1967 sb.append(contentView.getPackage());
1968 sb.append("/0x");
1969 sb.append(Integer.toHexString(contentView.getLayoutId()));
1970 } else {
1971 sb.append("null");
1972 }
1973 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001974 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1975 sb.append("default");
1976 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 int N = this.vibrate.length-1;
1978 sb.append("[");
1979 for (int i=0; i<N; i++) {
1980 sb.append(this.vibrate[i]);
1981 sb.append(',');
1982 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001983 if (N != -1) {
1984 sb.append(this.vibrate[N]);
1985 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 } else {
1988 sb.append("null");
1989 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001990 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001991 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001992 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001993 } else if (this.sound != null) {
1994 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001995 } else {
1996 sb.append("null");
1997 }
Chris Wren365b6d32015-07-16 10:39:26 -04001998 if (this.tickerText != null) {
1999 sb.append(" tick");
2000 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002001 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002002 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002003 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002004 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002005 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002006 if (this.category != null) {
2007 sb.append(" category=");
2008 sb.append(this.category);
2009 }
2010 if (this.mGroupKey != null) {
2011 sb.append(" groupKey=");
2012 sb.append(this.mGroupKey);
2013 }
2014 if (this.mSortKey != null) {
2015 sb.append(" sortKey=");
2016 sb.append(this.mSortKey);
2017 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002018 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002019 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002020 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002021 }
2022 sb.append(" vis=");
2023 sb.append(visibilityToString(this.visibility));
2024 if (this.publicVersion != null) {
2025 sb.append(" publicVersion=");
2026 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002027 }
Julia Reynolds054c5dc2015-11-17 15:36:30 -05002028 if (topic != null) {
2029 sb.append("topic=");
2030 sb.append(topic.toString());
Julia Reynolds74303cf2015-10-16 11:37:55 -04002031 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002032 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002033 return sb.toString();
2034 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002035
Dan Sandler1b718782014-07-18 12:43:45 -04002036 /**
2037 * {@hide}
2038 */
2039 public static String visibilityToString(int vis) {
2040 switch (vis) {
2041 case VISIBILITY_PRIVATE:
2042 return "PRIVATE";
2043 case VISIBILITY_PUBLIC:
2044 return "PUBLIC";
2045 case VISIBILITY_SECRET:
2046 return "SECRET";
2047 default:
2048 return "UNKNOWN(" + String.valueOf(vis) + ")";
2049 }
2050 }
2051
Joe Onoratocb109a02011-01-18 17:57:41 -08002052 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002053 * {@hide}
2054 */
2055 public static String priorityToString(@Priority int pri) {
2056 switch (pri) {
2057 case PRIORITY_MIN:
2058 return "MIN";
2059 case PRIORITY_LOW:
2060 return "LOW";
2061 case PRIORITY_DEFAULT:
2062 return "DEFAULT";
2063 case PRIORITY_HIGH:
2064 return "HIGH";
2065 case PRIORITY_MAX:
2066 return "MAX";
2067 default:
2068 return "UNKNOWN(" + String.valueOf(pri) + ")";
2069 }
2070 }
2071
2072 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002073 * The small icon representing this notification in the status bar and content view.
2074 *
2075 * @return the small icon representing this notification.
2076 *
2077 * @see Builder#getSmallIcon()
2078 * @see Builder#setSmallIcon(Icon)
2079 */
2080 public Icon getSmallIcon() {
2081 return mSmallIcon;
2082 }
2083
2084 /**
2085 * Used when notifying to clean up legacy small icons.
2086 * @hide
2087 */
2088 public void setSmallIcon(Icon icon) {
2089 mSmallIcon = icon;
2090 }
2091
2092 /**
2093 * The large icon shown in this notification's content view.
2094 * @see Builder#getLargeIcon()
2095 * @see Builder#setLargeIcon(Icon)
2096 */
2097 public Icon getLargeIcon() {
2098 return mLargeIcon;
2099 }
2100
2101 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002102 * @hide
2103 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002104 public boolean isGroupSummary() {
2105 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2106 }
2107
2108 /**
2109 * @hide
2110 */
2111 public boolean isGroupChild() {
2112 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2113 }
2114
2115 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002116 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002117 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002118 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002119 * content views using the platform's notification layout template. If your app supports
2120 * versions of Android as old as API level 4, you can instead use
2121 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2122 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2123 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002124 *
Scott Main183bf112012-08-13 19:12:13 -07002125 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002126 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002127 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002128 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002129 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2130 * .setContentText(subject)
2131 * .setSmallIcon(R.drawable.new_mail)
2132 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002133 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002134 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002135 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002136 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002137 private static final int MAX_ACTION_BUTTONS = 3;
Jorim Jaggi445d3c02014-08-19 22:33:42 +02002138 private static final float LARGE_TEXT_SCALE = 1.3f;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002139
Joe Onorato46439ce2010-11-19 13:56:21 -08002140 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002141 private Notification mN;
2142 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002143 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002144 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2145 private ArrayList<String> mPersonList = new ArrayList<String>();
2146 private NotificationColorUtil mColorUtil;
2147 private boolean mColorUtilInited = false;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002148
2149 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002150 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002151 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002152
2153 * <table>
2154 * <tr><th align=right>priority</th>
2155 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2156 * <tr><th align=right>when</th>
2157 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2158 * <tr><th align=right>audio stream</th>
2159 * <td>{@link #STREAM_DEFAULT}</td></tr>
2160 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002161 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002162
2163 * @param context
2164 * A {@link Context} that will be used by the Builder to construct the
2165 * RemoteViews. The Context will not be held past the lifetime of this Builder
2166 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08002167 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002168 public Builder(Context context) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002169 this(context, null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002170 }
2171
Joe Onoratocb109a02011-01-18 17:57:41 -08002172 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002173 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002174 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002175 public Builder(Context context, Notification toAdopt) {
2176 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002177
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002178 if (toAdopt == null) {
2179 mN = new Notification();
2180 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2181 mN.priority = PRIORITY_DEFAULT;
2182 mN.visibility = VISIBILITY_PRIVATE;
2183 } else {
2184 mN = toAdopt;
2185 if (mN.actions != null) {
2186 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002187 }
2188
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002189 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2190 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2191 }
2192
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002193 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2194 if (!TextUtils.isEmpty(templateClass)) {
2195 final Class<? extends Style> styleClass
2196 = getNotificationStyleClass(templateClass);
2197 if (styleClass == null) {
2198 Log.d(TAG, "Unknown style class: " + templateClass);
2199 } else {
2200 try {
2201 final Constructor<? extends Style> ctor = styleClass.getConstructor();
2202 ctor.setAccessible(true);
2203 final Style style = ctor.newInstance();
2204 style.restoreFromExtras(mN.extras);
2205
2206 if (style != null) {
2207 setStyle(style);
2208 }
2209 } catch (Throwable t) {
2210 Log.e(TAG, "Could not create Style", t);
2211 }
2212 }
2213 }
2214
2215 }
2216 }
2217
2218 private NotificationColorUtil getColorUtil() {
2219 if (!mColorUtilInited) {
2220 mColorUtilInited = true;
2221 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.LOLLIPOP) {
2222 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002223 }
2224 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002225 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002226 }
2227
2228 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002229 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07002230 * It will be shown in the notification content view by default; use
Griff Hazen50c11652014-05-16 09:46:31 -07002231 * {@link #setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002232 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002233 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002234 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002235 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002236 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002237 return this;
2238 }
2239
Joe Onoratocb109a02011-01-18 17:57:41 -08002240 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002241 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002242 * in the content view.
2243 */
2244 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002245 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002246 return this;
2247 }
2248
2249 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002250 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002251 *
2252 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002253 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002254 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002255 * Useful when showing an elapsed time (like an ongoing phone call).
2256 *
2257 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002258 * @see Notification#when
2259 */
2260 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002261 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002262 return this;
2263 }
2264
2265 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002266 * Set the small icon resource, which will be used to represent the notification in the
2267 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002268 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002269
2270 * The platform template for the expanded view will draw this icon in the left, unless a
2271 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2272 * icon will be moved to the right-hand side.
2273 *
2274
2275 * @param icon
2276 * A resource ID in the application's package of the drawable to use.
2277 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002278 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002279 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002280 return setSmallIcon(icon != 0
2281 ? Icon.createWithResource(mContext, icon)
2282 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002283 }
2284
Joe Onoratocb109a02011-01-18 17:57:41 -08002285 /**
2286 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2287 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2288 * LevelListDrawable}.
2289 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002290 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002291 * @param level The level to use for the icon.
2292 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002293 * @see Notification#icon
2294 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002295 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002296 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002297 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002298 return setSmallIcon(icon);
2299 }
2300
2301 /**
2302 * Set the small icon, which will be used to represent the notification in the
2303 * status bar and content view (unless overriden there by a
2304 * {@link #setLargeIcon(Bitmap) large icon}).
2305 *
2306 * @param icon An Icon object to use.
2307 * @see Notification#icon
2308 */
2309 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002310 mN.setSmallIcon(icon);
2311 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2312 mN.icon = icon.getResId();
2313 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002314 return this;
2315 }
2316
Joe Onoratocb109a02011-01-18 17:57:41 -08002317 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002318 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002319 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002320 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002321 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002322 return this;
2323 }
2324
Joe Onoratocb109a02011-01-18 17:57:41 -08002325 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002326 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002327 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002328 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002329 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002330 return this;
2331 }
2332
Joe Onoratocb109a02011-01-18 17:57:41 -08002333 /**
Joe Malin8d40d042012-11-05 11:36:40 -08002334 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002335 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
2336 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002337 */
2338 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002339 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002340 return this;
2341 }
2342
2343 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002344 * Set the remote input history.
2345 *
2346 * This should be set to the most recent inputs that have been sent
2347 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2348 * longer relevant (e.g. for chat notifications once the other party has responded).
2349 *
2350 * The most recent input must be stored at the 0 index, the second most recent at the
2351 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2352 * and how much of each individual input is shown.
2353 *
2354 * <p>Note: The reply text will only be shown on notifications that have least one action
2355 * with a {@code RemoteInput}.</p>
2356 */
2357 public Builder setRemoteInputHistory(CharSequence[] text) {
2358 if (text == null) {
2359 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2360 } else {
2361 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2362 CharSequence[] safe = new CharSequence[N];
2363 for (int i = 0; i < N; i++) {
2364 safe[i] = safeCharSequence(text[i]);
2365 }
2366 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2367 }
2368 return this;
2369 }
2370
2371 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002372 * Set the large number at the right-hand side of the notification. This is
2373 * equivalent to setContentInfo, although it might show the number in a different
2374 * font size for readability.
2375 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002376 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002377 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002378 return this;
2379 }
2380
Joe Onoratocb109a02011-01-18 17:57:41 -08002381 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002382 * A small piece of additional information pertaining to this notification.
2383 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002384 * The platform template will draw this on the last line of the notification, at the far
2385 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08002386 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002387 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002388 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002389 return this;
2390 }
2391
Joe Onoratocb109a02011-01-18 17:57:41 -08002392 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002393 * Set the progress this notification represents.
2394 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002395 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07002396 */
2397 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002398 mN.extras.putInt(EXTRA_PROGRESS, progress);
2399 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
2400 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07002401 return this;
2402 }
2403
2404 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002405 * Supply a custom RemoteViews to use instead of the platform template.
2406 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002407 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08002408 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002409 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002410 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002411 return setCustomContentView(views);
2412 }
2413
2414 /**
2415 * Supply custom RemoteViews to use instead of the platform template.
2416 *
2417 * This will override the layout that would otherwise be constructed by this Builder
2418 * object.
2419 */
2420 public Builder setCustomContentView(RemoteViews contentView) {
2421 mN.contentView = contentView;
2422 return this;
2423 }
2424
2425 /**
2426 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
2427 *
2428 * This will override the expanded layout that would otherwise be constructed by this
2429 * Builder object.
2430 */
2431 public Builder setCustomBigContentView(RemoteViews contentView) {
2432 mN.bigContentView = contentView;
2433 return this;
2434 }
2435
2436 /**
2437 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
2438 *
2439 * This will override the heads-up layout that would otherwise be constructed by this
2440 * Builder object.
2441 */
2442 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
2443 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08002444 return this;
2445 }
2446
Joe Onoratocb109a02011-01-18 17:57:41 -08002447 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002448 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
2449 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002450 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
2451 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
2452 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002453 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002454 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002455 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002456 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002457 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002458 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002459 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002460 return this;
2461 }
2462
Joe Onoratocb109a02011-01-18 17:57:41 -08002463 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002464 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
2465 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002466 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002467 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002468 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002469 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002470 return this;
2471 }
2472
Joe Onoratocb109a02011-01-18 17:57:41 -08002473 /**
2474 * An intent to launch instead of posting the notification to the status bar.
2475 * Only for use with extremely high-priority notifications demanding the user's
2476 * <strong>immediate</strong> attention, such as an incoming phone call or
2477 * alarm clock that the user has explicitly set to a particular time.
2478 * If this facility is used for something else, please give the user an option
2479 * to turn it off and use a normal notification, as this can be extremely
2480 * disruptive.
2481 *
Chris Wren47c20a12014-06-18 17:27:29 -04002482 * <p>
2483 * The system UI may choose to display a heads-up notification, instead of
2484 * launching this intent, while the user is using the device.
2485 * </p>
2486 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002487 * @param intent The pending intent to launch.
2488 * @param highPriority Passing true will cause this notification to be sent
2489 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002490 *
2491 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08002492 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002493 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002494 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08002495 setFlag(FLAG_HIGH_PRIORITY, highPriority);
2496 return this;
2497 }
2498
Joe Onoratocb109a02011-01-18 17:57:41 -08002499 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002500 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002501 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002502 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08002503 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002504 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002505 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08002506 return this;
2507 }
2508
Joe Onoratocb109a02011-01-18 17:57:41 -08002509 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002510 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002511 *
Joe Onoratocb109a02011-01-18 17:57:41 -08002512 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04002513 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002514 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002515 setTicker(tickerText);
2516 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08002517 return this;
2518 }
2519
Joe Onoratocb109a02011-01-18 17:57:41 -08002520 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002521 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002522 *
2523 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04002524 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2525 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04002526 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04002527 public Builder setLargeIcon(Bitmap b) {
2528 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
2529 }
2530
2531 /**
2532 * Add a large icon to the notification content view.
2533 *
2534 * In the platform template, this image will be shown on the left of the notification view
2535 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
2536 * badge atop the large icon).
2537 */
2538 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002539 mN.mLargeIcon = icon;
2540 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08002541 return this;
2542 }
2543
Joe Onoratocb109a02011-01-18 17:57:41 -08002544 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002545 * Set the sound to play.
2546 *
John Spurlockc0650f022014-07-19 13:22:39 -04002547 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
2548 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002549 *
Chris Wren47c20a12014-06-18 17:27:29 -04002550 * <p>
2551 * A notification that is noisy is more likely to be presented as a heads-up notification.
2552 * </p>
2553 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002554 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002555 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08002556 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002557 mN.sound = sound;
2558 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08002559 return this;
2560 }
2561
Joe Onoratocb109a02011-01-18 17:57:41 -08002562 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002563 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08002564 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002565 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
2566 *
Chris Wren47c20a12014-06-18 17:27:29 -04002567 * <p>
2568 * A notification that is noisy is more likely to be presented as a heads-up notification.
2569 * </p>
John Spurlockc0650f022014-07-19 13:22:39 -04002570 * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002571 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08002572 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07002573 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002574 public Builder setSound(Uri sound, int streamType) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002575 mN.sound = sound;
2576 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08002577 return this;
2578 }
2579
Joe Onoratocb109a02011-01-18 17:57:41 -08002580 /**
John Spurlockc0650f022014-07-19 13:22:39 -04002581 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
2582 * use during playback.
2583 *
2584 * <p>
2585 * A notification that is noisy is more likely to be presented as a heads-up notification.
2586 * </p>
2587 *
2588 * @see Notification#sound
2589 */
2590 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002591 mN.sound = sound;
2592 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04002593 return this;
2594 }
2595
2596 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08002597 * Set the vibration pattern to use.
2598 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002599 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
2600 * <code>pattern</code> parameter.
2601 *
Chris Wren47c20a12014-06-18 17:27:29 -04002602 * <p>
2603 * A notification that vibrates is more likely to be presented as a heads-up notification.
2604 * </p>
2605 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002606 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08002607 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002608 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002609 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08002610 return this;
2611 }
2612
Joe Onoratocb109a02011-01-18 17:57:41 -08002613 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002614 * Set the desired color for the indicator LED on the device, as well as the
2615 * blink duty cycle (specified in milliseconds).
2616 *
2617
2618 * Not all devices will honor all (or even any) of these values.
2619 *
2620
2621 * @see Notification#ledARGB
2622 * @see Notification#ledOnMS
2623 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08002624 */
Tor Norbye80756e32015-03-02 09:39:27 -08002625 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002626 mN.ledARGB = argb;
2627 mN.ledOnMS = onMs;
2628 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002629 if (onMs != 0 || offMs != 0) {
2630 mN.flags |= FLAG_SHOW_LIGHTS;
2631 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002632 return this;
2633 }
2634
Joe Onoratocb109a02011-01-18 17:57:41 -08002635 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002636 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08002637 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002638
2639 * Ongoing notifications cannot be dismissed by the user, so your application or service
2640 * must take care of canceling them.
2641 *
2642
2643 * They are typically used to indicate a background task that the user is actively engaged
2644 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2645 * (e.g., a file download, sync operation, active network connection).
2646 *
2647
2648 * @see Notification#FLAG_ONGOING_EVENT
2649 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002650 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002651 public Builder setOngoing(boolean ongoing) {
2652 setFlag(FLAG_ONGOING_EVENT, ongoing);
2653 return this;
2654 }
2655
Joe Onoratocb109a02011-01-18 17:57:41 -08002656 /**
2657 * Set this flag if you would only like the sound, vibrate
2658 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002659 *
2660 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002661 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002662 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2663 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2664 return this;
2665 }
2666
Joe Onoratocb109a02011-01-18 17:57:41 -08002667 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002668 * Make this notification automatically dismissed when the user touches it. The
2669 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2670 *
2671 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002672 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002673 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002674 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002675 return this;
2676 }
2677
Joe Onoratocb109a02011-01-18 17:57:41 -08002678 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002679 * Set whether or not this notification should not bridge to other devices.
2680 *
2681 * <p>Some notifications can be bridged to other devices for remote display.
2682 * This hint can be set to recommend this notification not be bridged.
2683 */
2684 public Builder setLocalOnly(boolean localOnly) {
2685 setFlag(FLAG_LOCAL_ONLY, localOnly);
2686 return this;
2687 }
2688
2689 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002690 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002691 * <p>
2692 * The value should be one or more of the following fields combined with
2693 * bitwise-or:
2694 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2695 * <p>
2696 * For all default values, use {@link #DEFAULT_ALL}.
2697 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002698 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002699 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08002700 return this;
2701 }
2702
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002703 /**
2704 * Set the priority of this notification.
2705 *
2706 * @see Notification#priority
2707 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002708 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002709 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002710 return this;
2711 }
Joe Malin8d40d042012-11-05 11:36:40 -08002712
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002713 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002714 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002715 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002716 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002717 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002718 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002719 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002720 return this;
2721 }
2722
2723 /**
Chris Wrendde75302014-03-26 17:24:15 -04002724 * Add a person that is relevant to this notification.
2725 *
Chris Wrene6c48932014-09-29 17:19:27 -04002726 * <P>
2727 * Depending on user preferences, this annotation may allow the notification to pass
2728 * through interruption filters, and to appear more prominently in the user interface.
2729 * </P>
2730 *
2731 * <P>
2732 * The person should be specified by the {@code String} representation of a
2733 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
2734 * </P>
2735 *
2736 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
2737 * URIs. The path part of these URIs must exist in the contacts database, in the
2738 * appropriate column, or the reference will be discarded as invalid. Telephone schema
2739 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
2740 * </P>
2741 *
2742 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04002743 * @see Notification#EXTRA_PEOPLE
2744 */
Chris Wrene6c48932014-09-29 17:19:27 -04002745 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002746 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04002747 return this;
2748 }
2749
2750 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002751 * Set this notification to be part of a group of notifications sharing the same key.
2752 * Grouped notifications may display in a cluster or stack on devices which
2753 * support such rendering.
2754 *
2755 * <p>To make this notification the summary for its group, also call
2756 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2757 * {@link #setSortKey}.
2758 * @param groupKey The group key of the group.
2759 * @return this object for method chaining
2760 */
2761 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002762 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002763 return this;
2764 }
2765
2766 /**
2767 * Set this notification to be the group summary for a group of notifications.
2768 * Grouped notifications may display in a cluster or stack on devices which
2769 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2770 * @param isGroupSummary Whether this notification should be a group summary.
2771 * @return this object for method chaining
2772 */
2773 public Builder setGroupSummary(boolean isGroupSummary) {
2774 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2775 return this;
2776 }
2777
2778 /**
2779 * Set a sort key that orders this notification among other notifications from the
2780 * same package. This can be useful if an external sort was already applied and an app
2781 * would like to preserve this. Notifications will be sorted lexicographically using this
2782 * value, although providing different priorities in addition to providing sort key may
2783 * cause this value to be ignored.
2784 *
2785 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002786 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002787 *
2788 * @see String#compareTo(String)
2789 */
2790 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002791 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002792 return this;
2793 }
2794
2795 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002796 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002797 *
Griff Hazen720042b2014-02-24 15:46:56 -08002798 * <p>Values within the Bundle will replace existing extras values in this Builder.
2799 *
2800 * @see Notification#extras
2801 */
Griff Hazen959591e2014-05-15 22:26:18 -07002802 public Builder addExtras(Bundle extras) {
2803 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002804 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08002805 }
2806 return this;
2807 }
2808
2809 /**
2810 * Set metadata for this notification.
2811 *
2812 * <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 -04002813 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002814 * called.
2815 *
Griff Hazen720042b2014-02-24 15:46:56 -08002816 * <p>Replaces any existing extras values with those from the provided Bundle.
2817 * Use {@link #addExtras} to merge in metadata instead.
2818 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002819 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002820 */
Griff Hazen959591e2014-05-15 22:26:18 -07002821 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002822 if (extras != null) {
2823 mUserExtras = extras;
2824 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002825 return this;
2826 }
2827
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002828 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002829 * Get the current metadata Bundle used by this notification Builder.
2830 *
2831 * <p>The returned Bundle is shared with this Builder.
2832 *
2833 * <p>The current contents of this Bundle are copied into the Notification each time
2834 * {@link #build()} is called.
2835 *
2836 * @see Notification#extras
2837 */
2838 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002839 return mUserExtras;
2840 }
2841
2842 private Bundle getAllExtras() {
2843 final Bundle saveExtras = (Bundle) mUserExtras.clone();
2844 saveExtras.putAll(mN.extras);
2845 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08002846 }
2847
2848 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002849 * Add an action to this notification. Actions are typically displayed by
2850 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002851 * <p>
2852 * Every action must have an icon (32dp square and matching the
2853 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2854 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2855 * <p>
2856 * A notification in its expanded form can display up to 3 actions, from left to right in
2857 * the order they were added. Actions will not be displayed when the notification is
2858 * collapsed, however, so be sure that any essential functions may be accessed by the user
2859 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002860 *
2861 * @param icon Resource ID of a drawable that represents the action.
2862 * @param title Text describing the action.
2863 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04002864 *
2865 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002866 */
Dan Sandler86647982015-05-13 23:41:13 -04002867 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002868 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002869 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002870 return this;
2871 }
2872
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002873 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002874 * Add an action to this notification. Actions are typically displayed by
2875 * the system as a button adjacent to the notification content.
2876 * <p>
2877 * Every action must have an icon (32dp square and matching the
2878 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2879 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2880 * <p>
2881 * A notification in its expanded form can display up to 3 actions, from left to right in
2882 * the order they were added. Actions will not be displayed when the notification is
2883 * collapsed, however, so be sure that any essential functions may be accessed by the user
2884 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2885 *
2886 * @param action The action to add.
2887 */
2888 public Builder addAction(Action action) {
2889 mActions.add(action);
2890 return this;
2891 }
2892
2893 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002894 * Alter the complete list of actions attached to this notification.
2895 * @see #addAction(Action).
2896 *
2897 * @param actions
2898 * @return
2899 */
2900 public Builder setActions(Action... actions) {
2901 mActions.clear();
2902 for (int i = 0; i < actions.length; i++) {
2903 mActions.add(actions[i]);
2904 }
2905 return this;
2906 }
2907
2908 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002909 * Add a rich notification style to be applied at build time.
2910 *
2911 * @param style Object responsible for modifying the notification style.
2912 */
2913 public Builder setStyle(Style style) {
2914 if (mStyle != style) {
2915 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002916 if (mStyle != null) {
2917 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002918 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
2919 } else {
2920 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002921 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002922 }
2923 return this;
2924 }
2925
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002926 /**
2927 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002928 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002929 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2930 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
2931 *
2932 * @return The same Builder.
2933 */
2934 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002935 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002936 return this;
2937 }
2938
2939 /**
2940 * Supply a replacement Notification whose contents should be shown in insecure contexts
2941 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
2942 * @param n A replacement notification, presumably with some or all info redacted.
2943 * @return The same Builder.
2944 */
2945 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002946 if (n != null) {
2947 mN.publicVersion = new Notification();
2948 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
2949 } else {
2950 mN.publicVersion = null;
2951 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002952 return this;
2953 }
2954
Griff Hazenb720abe2014-05-20 13:15:30 -07002955 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002956 * Apply an extender to this notification builder. Extenders may be used to add
2957 * metadata or change options on this builder.
2958 */
Griff Hazen61a9e862014-05-22 16:05:19 -07002959 public Builder extend(Extender extender) {
2960 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002961 return this;
2962 }
2963
Dan Sandler4e787062015-06-17 15:09:48 -04002964 /**
2965 * @hide
2966 */
2967 public void setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08002968 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002969 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08002970 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002971 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08002972 }
2973 }
2974
Dan Sandler26e81cf2014-05-06 10:01:27 -04002975 /**
2976 * Sets {@link Notification#color}.
2977 *
2978 * @param argb The accent color to use
2979 *
2980 * @return The same Builder.
2981 */
Tor Norbye80756e32015-03-02 09:39:27 -08002982 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002983 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05002984 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04002985 return this;
2986 }
2987
Julia Reynolds74303cf2015-10-16 11:37:55 -04002988 /**
Julia Reynolds054c5dc2015-11-17 15:36:30 -05002989 * Sets the topic of this notification. Topics are typically displayed in Notification
Julia Reynolds74303cf2015-10-16 11:37:55 -04002990 * settings.
2991 * <p>
2992 * Every topic must have an id and a textual label.
2993 *
2994 * @param topic The topic to add.
2995 */
Julia Reynolds054c5dc2015-11-17 15:36:30 -05002996 public Builder setTopic(Topic topic) {
2997 mN.topic = topic;
Julia Reynolds74303cf2015-10-16 11:37:55 -04002998 return this;
2999 }
3000
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003001 private Drawable getProfileBadgeDrawable() {
Christoph Studer7ac80e62014-08-04 16:01:57 +02003002 // Note: This assumes that the current user can read the profile badge of the
3003 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003004 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003005 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003006 }
3007
3008 private Bitmap getProfileBadge() {
3009 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003010 if (badge == null) {
3011 return null;
3012 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003013 final int size = mContext.getResources().getDimensionPixelSize(
3014 R.dimen.notification_badge_size);
3015 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003016 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003017 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003018 badge.draw(canvas);
3019 return bitmap;
3020 }
3021
Selim Cinekc848c3a2016-01-13 15:27:30 -08003022 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003023 Bitmap profileBadge = getProfileBadge();
3024
Kenny Guy98193ea2014-07-24 19:54:37 +01003025 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003026 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3027 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Kenny Guy98193ea2014-07-24 19:54:37 +01003028 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003029 }
3030
Christoph Studerfe718432014-09-01 18:21:18 +02003031 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003032 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003033 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003034 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003035 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003036 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003037 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003038 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003039 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003040 contentView.setTextViewText(R.id.text_line_1, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003041 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003042 }
3043
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003044 /**
3045 * Resets the notification header to its original state
3046 */
3047 private void resetNotificationHeader(RemoteViews contentView) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003048 contentView.setImageViewResource(R.id.icon, 0);
Selim Cinek7b836392015-12-04 20:02:59 -08003049 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003050 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003051 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003052 contentView.setViewVisibility(R.id.header_sub_text, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003053 contentView.setViewVisibility(R.id.header_content_info, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003054 contentView.setViewVisibility(R.id.number_of_children, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003055 contentView.setViewVisibility(R.id.sub_text_divider, View.GONE);
3056 contentView.setViewVisibility(R.id.content_info_divider, View.GONE);
3057 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003058 contentView.setImageViewIcon(R.id.profile_badge, null);
3059 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003060 }
3061
3062 private void resetContentMargins(RemoteViews contentView) {
3063 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003064 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003065 }
3066
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003067 private RemoteViews applyStandardTemplate(int resId) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003068 return applyStandardTemplate(resId, true /* hasProgress */);
3069 }
3070
3071 /**
3072 * @param hasProgress whether the progress bar should be shown and set
3073 */
3074 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Kenny Guy77320062014-08-27 21:37:15 +01003075 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003076
Christoph Studerfe718432014-09-01 18:21:18 +02003077 resetStandardTemplate(contentView);
3078
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003079 final Bundle ex = mN.extras;
Dan Sandler190d58d2014-05-15 09:33:39 -04003080
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003081 bindNotificationHeader(contentView);
3082 bindLargeIcon(contentView);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003083 if (ex.getCharSequence(EXTRA_TITLE) != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003084 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003085 contentView.setTextViewText(R.id.title,
3086 processLegacyText(ex.getCharSequence(EXTRA_TITLE)));
Joe Onorato561d3852010-11-20 18:09:34 -08003087 }
Selim Cinek29603462015-11-17 19:04:39 -08003088 boolean showProgress = handleProgressBar(hasProgress, contentView, ex);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003089 if (ex.getCharSequence(EXTRA_TEXT) != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003090 int textId = showProgress ? com.android.internal.R.id.text_line_1
3091 : com.android.internal.R.id.text;
3092 contentView.setTextViewText(textId, processLegacyText(
3093 ex.getCharSequence(EXTRA_TEXT)));
3094 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003095 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003096
Selim Cinek860b6da2015-12-16 19:02:19 -08003097 setContentMinHeight(contentView, showProgress || mN.mLargeIcon != null);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003098
Selim Cinek29603462015-11-17 19:04:39 -08003099 return contentView;
3100 }
3101
Selim Cinek860b6da2015-12-16 19:02:19 -08003102 /**
3103 * @param remoteView the remote view to update the minheight in
3104 * @param hasMinHeight does it have a mimHeight
3105 * @hide
3106 */
3107 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3108 int minHeight = 0;
3109 if (hasMinHeight) {
3110 // we need to set the minHeight of the notification
3111 minHeight = mContext.getResources().getDimensionPixelSize(
3112 com.android.internal.R.dimen.notification_min_content_height);
3113 }
3114 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3115 }
3116
Selim Cinek29603462015-11-17 19:04:39 -08003117 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003118 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3119 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3120 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3121 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003122 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003123 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003124 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003125 contentView.setProgressBackgroundTintList(
3126 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3127 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003128 if (mN.color != COLOR_DEFAULT) {
3129 ColorStateList colorStateList = ColorStateList.valueOf(mN.color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003130 contentView.setProgressTintList(R.id.progress, colorStateList);
3131 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003132 }
Selim Cinek29603462015-11-17 19:04:39 -08003133 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003134 } else {
3135 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003136 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003137 }
Joe Onorato561d3852010-11-20 18:09:34 -08003138 }
3139
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003140 private void bindLargeIcon(RemoteViews contentView) {
3141 if (mN.mLargeIcon != null) {
3142 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3143 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3144 processLargeLegacyIcon(mN.mLargeIcon, contentView);
3145 int endMargin = mContext.getResources().getDimensionPixelSize(
3146 R.dimen.notification_content_picture_margin);
3147 contentView.setViewLayoutMarginEnd(R.id.line1, endMargin);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003148 contentView.setViewLayoutMarginEnd(R.id.text, endMargin);
Selim Cinek29603462015-11-17 19:04:39 -08003149 contentView.setViewLayoutMarginEnd(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003150 }
3151 }
3152
3153 private void bindNotificationHeader(RemoteViews contentView) {
3154 bindSmallIcon(contentView);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003155 bindChildCountColor(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003156 bindHeaderAppName(contentView);
Selim Cinek03d0d652015-11-13 13:18:09 -05003157 bindHeaderSubText(contentView);
Selim Cinek29603462015-11-17 19:04:39 -08003158 bindContentInfo(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003159 bindHeaderChronometerAndTime(contentView);
3160 bindExpandButton(contentView);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003161 bindProfileBadge(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003162 }
3163
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003164 private void bindChildCountColor(RemoteViews contentView) {
3165 contentView.setTextColor(R.id.number_of_children, resolveColor());
3166 }
3167
Selim Cinek29603462015-11-17 19:04:39 -08003168 private void bindContentInfo(RemoteViews contentView) {
3169 boolean visible = false;
3170 if (mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3171 contentView.setTextViewText(R.id.header_content_info,
3172 processLegacyText(mN.extras.getCharSequence(EXTRA_INFO_TEXT)));
3173 contentView.setViewVisibility(R.id.header_content_info, View.VISIBLE);
3174 visible = true;
3175 } else if (mN.number > 0) {
3176 final int tooBig = mContext.getResources().getInteger(
3177 R.integer.status_bar_notification_info_maxnum);
3178 if (mN.number > tooBig) {
3179 contentView.setTextViewText(R.id.header_content_info, processLegacyText(
3180 mContext.getResources().getString(
3181 R.string.status_bar_notification_info_overflow)));
3182 } else {
3183 contentView.setTextViewText(R.id.header_content_info,
3184 processLegacyText(String.valueOf(mN.number)));
3185 }
3186 contentView.setViewVisibility(R.id.header_content_info, View.VISIBLE);
3187 visible = true;
3188 }
3189 if (visible) {
3190 contentView.setViewVisibility(R.id.content_info_divider, View.VISIBLE);
3191 }
3192 }
3193
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003194 private void bindExpandButton(RemoteViews contentView) {
3195 contentView.setDrawableParameters(R.id.expand_button, false, -1, resolveColor(),
3196 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003197 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
3198 resolveColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003199 }
3200
3201 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3202 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003203 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003204 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3205 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3206 contentView.setLong(R.id.chronometer, "setBase",
3207 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3208 contentView.setBoolean(R.id.chronometer, "setStarted", true);
3209 } else {
3210 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3211 contentView.setLong(R.id.time, "setTime", mN.when);
3212 }
3213 }
3214 }
3215
Selim Cinek03d0d652015-11-13 13:18:09 -05003216 private void bindHeaderSubText(RemoteViews contentView) {
3217 CharSequence subText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3218 if (subText == null && mStyle != null && mStyle.mSummaryTextSet
3219 && mStyle.hasSummaryInHeader()) {
3220 subText = mStyle.mSummaryText;
3221 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003222 if (subText != null) {
3223 // TODO: Remove the span entirely to only have the string with propper formating.
3224 contentView.setTextViewText(R.id.header_sub_text, processLegacyText(subText));
3225 contentView.setViewVisibility(R.id.header_sub_text, View.VISIBLE);
Selim Cinek29603462015-11-17 19:04:39 -08003226 contentView.setViewVisibility(R.id.sub_text_divider, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003227 }
3228 }
3229
3230 private void bindHeaderAppName(RemoteViews contentView) {
3231 PackageManager packageManager = mContext.getPackageManager();
3232 ApplicationInfo info = null;
3233 try {
3234 info = packageManager.getApplicationInfo(mContext.getApplicationInfo().packageName,
3235 0);
3236 } catch (final NameNotFoundException e) {
3237 return;
3238 }
3239 CharSequence appName = info != null ? packageManager.getApplicationLabel(info)
3240 : null;
3241 if (TextUtils.isEmpty(appName)) {
3242 return;
3243 }
3244 contentView.setTextViewText(R.id.app_name_text, appName);
Selim Cinekaef485a2016-02-05 10:31:20 -08003245 contentView.setTextColor(R.id.app_name_text, resolveColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003246 }
3247
3248 private void bindSmallIcon(RemoteViews contentView) {
3249 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
3250 processSmallIconColor(mN.mSmallIcon, contentView);
3251 }
3252
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003253 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003254 * @return true if the built notification will show the time or the chronometer; false
3255 * otherwise
3256 */
3257 private boolean showsTimeOrChronometer() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003258 return mN.when != 0 && mN.extras.getBoolean(EXTRA_SHOW_WHEN);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003259 }
3260
Christoph Studerfe718432014-09-01 18:21:18 +02003261 private void resetStandardTemplateWithActions(RemoteViews big) {
3262 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003263 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003264
3265 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3266 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3267
3268 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3269 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3270 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3271 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003272 }
3273
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003274 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003275 RemoteViews big = applyStandardTemplate(layoutId);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003276
Christoph Studerfe718432014-09-01 18:21:18 +02003277 resetStandardTemplateWithActions(big);
3278
Adrian Roose458aa82015-12-08 16:17:19 -08003279 boolean validRemoteInput = false;
3280
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003281 int N = mActions.size();
3282 if (N > 0) {
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003283 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04003284 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003285 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08003286 Action action = mActions.get(i);
3287 validRemoteInput |= hasValidRemoteInput(action);
3288
3289 final RemoteViews button = generateActionButton(action);
Adrian Roos9b123cf2016-02-04 14:55:57 -08003290 if (i == N - 1) {
3291 button.setViewLayoutWidth(com.android.internal.R.id.action0,
3292 ViewGroup.LayoutParams.MATCH_PARENT);
3293 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003294 big.addView(R.id.actions, button);
3295 }
3296 }
Adrian Roose458aa82015-12-08 16:17:19 -08003297
3298 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
3299 if (validRemoteInput && replyText != null
3300 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
3301 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
3302 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
3303
3304 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
3305 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
3306 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
3307
3308 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
3309 big.setViewVisibility(
3310 R.id.notification_material_reply_text_3, View.VISIBLE);
3311 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
3312 }
3313 }
3314 }
3315
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003316 return big;
3317 }
3318
Adrian Roose458aa82015-12-08 16:17:19 -08003319 private boolean hasValidRemoteInput(Action action) {
3320 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
3321 // Weird actions
3322 return false;
3323 }
3324
3325 RemoteInput[] remoteInputs = action.getRemoteInputs();
3326 if (remoteInputs == null) {
3327 return false;
3328 }
3329
3330 for (RemoteInput r : remoteInputs) {
3331 CharSequence[] choices = r.getChoices();
3332 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
3333 return true;
3334 }
3335 }
3336 return false;
3337 }
3338
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003339 /**
3340 * Construct a RemoteViews for the final 1U notification layout. In order:
3341 * 1. Custom contentView from the caller
3342 * 2. Style's proposed content view
3343 * 3. Standard template view
3344 */
3345 public RemoteViews makeContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003346 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003347 return mN.contentView;
3348 } else if (mStyle != null) {
3349 final RemoteViews styleView = mStyle.makeContentView();
3350 if (styleView != null) {
3351 return styleView;
3352 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003353 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003354 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08003355 }
3356
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003357 /**
3358 * Construct a RemoteViews for the final big notification layout.
3359 */
3360 public RemoteViews makeBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05003361 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08003362 if (mN.bigContentView != null
3363 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003364 return mN.bigContentView;
3365 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05003366 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08003367 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003368 } else if (mActions.size() != 0) {
3369 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05003370 }
3371 adaptNotificationHeaderForBigContentView(result);
3372 return result;
3373 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003374
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003375 /**
3376 * Construct a RemoteViews for the final notification header only
3377 *
3378 * @hide
3379 */
3380 public RemoteViews makeNotificationHeader() {
3381 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
3382 R.layout.notification_template_header);
3383 resetNotificationHeader(header);
3384 bindNotificationHeader(header);
3385 return header;
3386 }
3387
Selim Cinek29603462015-11-17 19:04:39 -08003388 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003389 if (result != null) {
3390 result.setViewVisibility(R.id.text_line_1, View.GONE);
3391 }
Selim Cinek29603462015-11-17 19:04:39 -08003392 }
3393
Selim Cinek850a8542015-11-11 11:48:36 -05003394 private void adaptNotificationHeaderForBigContentView(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08003395 if (result != null) {
3396 result.setBoolean(R.id.notification_header, "setExpanded", true);
3397 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003398 }
3399
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003400 /**
3401 * Construct a RemoteViews for the final heads-up notification layout.
3402 */
3403 public RemoteViews makeHeadsUpContentView() {
Selim Cinek593610c2016-02-16 18:42:57 -08003404 if (mN.headsUpContentView != null
3405 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05003406 return mN.headsUpContentView;
3407 } else if (mStyle != null) {
3408 final RemoteViews styleView = mStyle.makeHeadsUpContentView();
3409 if (styleView != null) {
3410 return styleView;
3411 }
3412 } else if (mActions.size() == 0) {
3413 return null;
3414 }
3415
Chris Wren8fd39ec2014-02-27 17:43:26 -05003416
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003417 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05003418 }
3419
Selim Cinek624c02db2015-12-14 21:00:02 -08003420 /**
3421 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
3422 *
3423 * @hide
3424 */
3425 public RemoteViews makePublicContentView() {
3426 if (mN.publicVersion != null) {
3427 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
3428 return builder.makeContentView();
3429 }
3430 Bundle savedBundle = mN.extras;
3431 Style style = mStyle;
3432 mStyle = null;
3433 Icon largeIcon = mN.mLargeIcon;
3434 mN.mLargeIcon = null;
3435 Bundle publicExtras = new Bundle();
3436 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
3437 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
3438 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
3439 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
3440 publicExtras.putCharSequence(EXTRA_TITLE,
3441 mContext.getString(R.string.notification_hidden_text));
3442 mN.extras = publicExtras;
3443 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
3444 mN.extras = savedBundle;
3445 mN.mLargeIcon = largeIcon;
3446 mStyle = style;
3447 return publicView;
3448 }
3449
3450
Chris Wren8fd39ec2014-02-27 17:43:26 -05003451
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003452 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04003453 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07003454 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003455 tombstone ? getActionTombstoneLayoutResource()
3456 : getActionLayoutResource());
Dan Sandler68079d52015-07-22 10:45:30 -04003457 final Icon ai = action.getIcon();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003458 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04003459 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04003460 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04003461 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003462 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08003463 if (action.mRemoteInputs != null) {
3464 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
3465 }
3466 if (mN.color != COLOR_DEFAULT) {
3467 button.setTextColor(R.id.action0, mN.color);
3468 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003469 return button;
3470 }
3471
Joe Onoratocb109a02011-01-18 17:57:41 -08003472 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003473 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07003474 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003475 */
3476 private boolean isLegacy() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003477 return getColorUtil() != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003478 }
3479
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003480 private CharSequence processLegacyText(CharSequence charSequence) {
3481 if (isLegacy()) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003482 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003483 } else {
3484 return charSequence;
3485 }
3486 }
3487
Dan Sandler26e81cf2014-05-06 10:01:27 -04003488 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003489 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04003490 */
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003491 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView) {
Selim Cinekea4bef72015-12-02 15:51:10 -08003492 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
3493 if (colorable) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003494 contentView.setDrawableParameters(R.id.icon, false, -1, resolveColor(),
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003495 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003496
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003497 }
Selim Cinekea4bef72015-12-02 15:51:10 -08003498 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
3499 colorable ? resolveColor() : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003500 }
3501
Dan Sandler26e81cf2014-05-06 10:01:27 -04003502 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003503 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04003504 * if it's grayscale).
3505 */
3506 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04003507 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
3508 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003509 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003510 // resolve color will fall back to the default when legacy
3511 contentView.setDrawableParameters(R.id.icon, false, -1, resolveColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04003512 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01003513 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003514 }
3515
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003516 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003517 if (mN.color != COLOR_DEFAULT) {
3518 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02003519 }
Jorim Jaggi74419312014-06-10 20:57:21 +02003520 }
3521
Selim Cinek5bf069a2015-11-10 19:14:27 -05003522 int resolveColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003523 if (mN.color == COLOR_DEFAULT) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003524 return mContext.getColor(R.color.notification_icon_default_color);
Dan Sandler26e81cf2014-05-06 10:01:27 -04003525 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003526 return mN.color;
Dan Sandler26e81cf2014-05-06 10:01:27 -04003527 }
3528
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003529 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003530 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003531 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08003532 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003533 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003534 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003535 mN.actions = new Action[mActions.size()];
3536 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003537 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003538 if (!mPersonList.isEmpty()) {
3539 mN.extras.putStringArray(EXTRA_PEOPLE,
3540 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003541 }
Selim Cinek247fa012016-02-18 09:50:48 -08003542 if (mN.bigContentView != null || mN.contentView != null
3543 || mN.headsUpContentView != null) {
3544 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
3545 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003546 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08003547 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003548
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003549 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02003550 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003551 ApplicationInfo applicationInfo = n.extras.getParcelable(
3552 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003553 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05003554 if (applicationInfo != null) {
3555 try {
3556 builderContext = context.createApplicationContext(applicationInfo,
3557 Context.CONTEXT_RESTRICTED);
3558 } catch (NameNotFoundException e) {
3559 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
3560 builderContext = context; // try with our context
3561 }
3562 } else {
3563 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02003564 }
3565
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003566 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003567 }
3568
3569 private static Class<? extends Style> getNotificationStyleClass(String templateClass) {
Selim Cinek593610c2016-02-16 18:42:57 -08003570 Class<? extends Style>[] classes = new Class[] {
3571 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
3572 DecoratedCustomViewStyle.class };
Christoph Studer4600f9b2014-07-22 22:44:43 +02003573 for (Class<? extends Style> innerClass : classes) {
3574 if (templateClass.equals(innerClass.getName())) {
3575 return innerClass;
3576 }
3577 }
3578 return null;
3579 }
3580
3581 private void setBuilderContentView(Notification n, RemoteViews contentView) {
3582 n.contentView = contentView;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003583 }
3584
3585 private void setBuilderBigContentView(Notification n, RemoteViews bigContentView) {
3586 n.bigContentView = bigContentView;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003587 }
3588
3589 private void setBuilderHeadsUpContentView(Notification n,
3590 RemoteViews headsUpContentView) {
3591 n.headsUpContentView = headsUpContentView;
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003592 }
3593
3594 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003595 * @deprecated Use {@link #build()} instead.
3596 */
3597 @Deprecated
3598 public Notification getNotification() {
3599 return build();
3600 }
3601
3602 /**
3603 * Combine all of the options that have been set and return a new {@link Notification}
3604 * object.
3605 */
3606 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003607 // first, add any extras from the calling code
3608 if (mUserExtras != null) {
3609 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003610 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003611
3612 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05003613 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02003614
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003615 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003616
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003617 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003618 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003619 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003620
Julia Reynolds4c0c2022016-02-02 15:11:59 -05003621 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
3622 mN.flags |= FLAG_SHOW_LIGHTS;
3623 }
3624
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003625 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003626 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003627
3628 /**
3629 * Apply this Builder to an existing {@link Notification} object.
3630 *
3631 * @hide
3632 */
3633 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04003634 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003635 return n;
3636 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003637
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003638 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003639 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003640 }
3641
3642 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003643 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003644 }
3645
3646 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003647 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003648 }
3649
3650 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003651 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003652 }
3653
3654 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003655 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003656 }
3657
3658 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003659 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003660 }
3661
3662 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07003663 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003664 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003665 }
3666
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003667 /**
3668 * An object that can apply a rich notification style to a {@link Notification.Builder}
3669 * object.
3670 */
Griff Hazendfcb0802014-02-11 12:00:00 -08003671 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04003672 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02003673
3674 /**
3675 * @hide
3676 */
3677 protected CharSequence mSummaryText = null;
3678
3679 /**
3680 * @hide
3681 */
3682 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04003683
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003684 protected Builder mBuilder;
3685
Chris Wrend6297db2012-05-03 16:20:13 -04003686 /**
3687 * Overrides ContentTitle in the big form of the template.
3688 * This defaults to the value passed to setContentTitle().
3689 */
3690 protected void internalSetBigContentTitle(CharSequence title) {
3691 mBigContentTitle = title;
3692 }
3693
3694 /**
3695 * Set the first line of text after the detail section in the big form of the template.
3696 */
3697 protected void internalSetSummaryText(CharSequence cs) {
3698 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04003699 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04003700 }
3701
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003702 public void setBuilder(Builder builder) {
3703 if (mBuilder != builder) {
3704 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003705 if (mBuilder != null) {
3706 mBuilder.setStyle(this);
3707 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003708 }
3709 }
3710
Chris Wrend6297db2012-05-03 16:20:13 -04003711 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003712 if (mBuilder == null) {
3713 throw new IllegalArgumentException("Style requires a valid Builder object");
3714 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003715 }
Chris Wrend6297db2012-05-03 16:20:13 -04003716
3717 protected RemoteViews getStandardView(int layoutId) {
3718 checkBuilder();
3719
Christoph Studer4600f9b2014-07-22 22:44:43 +02003720 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003721 CharSequence oldBuilderContentTitle =
3722 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003723 if (mBigContentTitle != null) {
3724 mBuilder.setContentTitle(mBigContentTitle);
3725 }
3726
Chris Wrend6297db2012-05-03 16:20:13 -04003727 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
3728
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003729 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003730
Chris Wrend6297db2012-05-03 16:20:13 -04003731 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
3732 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04003733 } else {
3734 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04003735 }
3736
Chris Wrend6297db2012-05-03 16:20:13 -04003737 return contentView;
3738 }
3739
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003740 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003741 * Construct a Style-specific RemoteViews for the final 1U notification layout.
3742 * The default implementation has nothing additional to add.
3743 * @hide
3744 */
3745 public RemoteViews makeContentView() {
3746 return null;
3747 }
3748
3749 /**
3750 * Construct a Style-specific RemoteViews for the final big notification layout.
3751 * @hide
3752 */
3753 public RemoteViews makeBigContentView() {
3754 return null;
3755 }
3756
3757 /**
3758 * Construct a Style-specific RemoteViews for the final HUN layout.
3759 * @hide
3760 */
3761 public RemoteViews makeHeadsUpContentView() {
3762 return null;
3763 }
3764
3765 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003766 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003767 * @hide
3768 */
3769 public void addExtras(Bundle extras) {
3770 if (mSummaryTextSet) {
3771 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
3772 }
3773 if (mBigContentTitle != null) {
3774 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
3775 }
Chris Wren91ad5632013-06-05 15:05:57 -04003776 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003777 }
3778
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003779 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003780 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003781 * @hide
3782 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02003783 protected void restoreFromExtras(Bundle extras) {
3784 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
3785 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
3786 mSummaryTextSet = true;
3787 }
3788 if (extras.containsKey(EXTRA_TITLE_BIG)) {
3789 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
3790 }
3791 }
3792
3793
3794 /**
3795 * @hide
3796 */
3797 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003798 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02003799 return wip;
3800 }
3801
Daniel Sandler0ec46202015-06-24 01:27:05 -04003802 /**
3803 * @hide
3804 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003805 public void purgeResources() {}
3806
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003807 /**
3808 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
3809 * attached to.
3810 *
3811 * @return the fully constructed Notification.
3812 */
3813 public Notification build() {
3814 checkBuilder();
3815 return mBuilder.build();
3816 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003817
3818 /**
3819 * @hide
3820 * @return true if the style positions the progress bar on the second line; false if the
3821 * style hides the progress bar
3822 */
3823 protected boolean hasProgress() {
3824 return true;
3825 }
Selim Cinek03d0d652015-11-13 13:18:09 -05003826
3827 /**
3828 * @hide
3829 * @return Whether we should put the summary be put into the notification header
3830 */
3831 public boolean hasSummaryInHeader() {
3832 return true;
3833 }
Selim Cinek593610c2016-02-16 18:42:57 -08003834
3835 /**
3836 * @hide
3837 * @return Whether custom content views are displayed inline in the style
3838 */
3839 public boolean displayCustomViewInline() {
3840 return false;
3841 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003842 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003843
3844 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003845 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08003846 *
Robert Ly91c5ce32014-06-08 15:37:00 -07003847 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003848 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07003849 * Notification notif = new Notification.Builder(mContext)
3850 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
3851 * .setContentText(subject)
3852 * .setSmallIcon(R.drawable.new_post)
3853 * .setLargeIcon(aBitmap)
3854 * .setStyle(new Notification.BigPictureStyle()
3855 * .bigPicture(aBigBitmap))
3856 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003857 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003858 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003859 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003860 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003861 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003862 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003863 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003864 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003865
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003866 public BigPictureStyle() {
3867 }
3868
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003869 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003870 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003871 }
3872
Chris Wrend6297db2012-05-03 16:20:13 -04003873 /**
3874 * Overrides ContentTitle in the big form of the template.
3875 * This defaults to the value passed to setContentTitle().
3876 */
3877 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003878 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003879 return this;
3880 }
3881
3882 /**
3883 * Set the first line of text after the detail section in the big form of the template.
3884 */
3885 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003886 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003887 return this;
3888 }
3889
Chris Wren0bd664d2012-08-01 13:56:56 -04003890 /**
3891 * Provide the bitmap to be used as the payload for the BigPicture notification.
3892 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003893 public BigPictureStyle bigPicture(Bitmap b) {
3894 mPicture = b;
3895 return this;
3896 }
3897
Chris Wren3745a3d2012-05-22 15:11:52 -04003898 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04003899 * Override the large icon when the big notification is shown.
3900 */
3901 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04003902 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3903 }
3904
3905 /**
3906 * Override the large icon when the big notification is shown.
3907 */
3908 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04003909 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04003910 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04003911 return this;
3912 }
3913
Riley Andrews0394a0c2015-11-03 23:36:52 -08003914 /** @hide */
3915 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
3916
Daniel Sandler0ec46202015-06-24 01:27:05 -04003917 /**
3918 * @hide
3919 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003920 @Override
3921 public void purgeResources() {
3922 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08003923 if (mPicture != null &&
3924 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08003925 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07003926 mPicture = mPicture.createAshmemBitmap();
3927 }
3928 if (mBigLargeIcon != null) {
3929 mBigLargeIcon.convertToAshmem();
3930 }
3931 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01003932
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003933 /**
3934 * @hide
3935 */
3936 public RemoteViews makeBigContentView() {
3937 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01003938 // This covers the following cases:
3939 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003940 // mN.mLargeIcon
3941 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04003942 Icon oldLargeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003943 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003944 oldLargeIcon = mBuilder.mN.mLargeIcon;
3945 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003946 }
3947
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003948 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05003949 if (mSummaryTextSet) {
3950 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinekc848c3a2016-01-13 15:27:30 -08003951 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05003952 }
Selim Cinek860b6da2015-12-16 19:02:19 -08003953 mBuilder.setContentMinHeight(contentView, mBuilder.mN.mLargeIcon != null);
Selim Cinek53e64a42015-11-16 10:40:56 -08003954
Christoph Studer5c510ee2014-12-15 16:32:27 +01003955 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003956 mBuilder.mN.mLargeIcon = oldLargeIcon;
Christoph Studer5c510ee2014-12-15 16:32:27 +01003957 }
3958
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003959 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003960 return contentView;
3961 }
3962
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003963 /**
3964 * @hide
3965 */
3966 public void addExtras(Bundle extras) {
3967 super.addExtras(extras);
3968
3969 if (mBigLargeIconSet) {
3970 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
3971 }
3972 extras.putParcelable(EXTRA_PICTURE, mPicture);
3973 }
3974
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003975 /**
3976 * @hide
3977 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003978 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02003979 protected void restoreFromExtras(Bundle extras) {
3980 super.restoreFromExtras(extras);
3981
3982 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01003983 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02003984 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04003985 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02003986 mPicture = extras.getParcelable(EXTRA_PICTURE);
3987 }
Selim Cinek03d0d652015-11-13 13:18:09 -05003988
3989 /**
3990 * @hide
3991 */
3992 @Override
3993 public boolean hasSummaryInHeader() {
3994 return false;
3995 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003996 }
3997
3998 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04003999 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08004000 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004001 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004002 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004003 * Notification notif = new Notification.Builder(mContext)
4004 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4005 * .setContentText(subject)
4006 * .setSmallIcon(R.drawable.new_mail)
4007 * .setLargeIcon(aBitmap)
4008 * .setStyle(new Notification.BigTextStyle()
4009 * .bigText(aVeryLongString))
4010 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004011 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004012 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04004013 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004014 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004015 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004016
4017 private static final int MAX_LINES = 13;
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004018 private static final int LINES_CONSUMED_BY_ACTIONS = 4;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004019
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004020 private CharSequence mBigText;
4021
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004022 public BigTextStyle() {
4023 }
4024
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004025 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004026 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004027 }
4028
Chris Wrend6297db2012-05-03 16:20:13 -04004029 /**
4030 * Overrides ContentTitle in the big form of the template.
4031 * This defaults to the value passed to setContentTitle().
4032 */
4033 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004034 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004035 return this;
4036 }
4037
4038 /**
4039 * Set the first line of text after the detail section in the big form of the template.
4040 */
4041 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004042 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004043 return this;
4044 }
4045
Chris Wren0bd664d2012-08-01 13:56:56 -04004046 /**
4047 * Provide the longer text to be displayed in the big form of the
4048 * template in place of the content text.
4049 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004050 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004051 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004052 return this;
4053 }
4054
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004055 /**
4056 * @hide
4057 */
4058 public void addExtras(Bundle extras) {
4059 super.addExtras(extras);
4060
Christoph Studer4600f9b2014-07-22 22:44:43 +02004061 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
4062 }
4063
4064 /**
4065 * @hide
4066 */
4067 @Override
4068 protected void restoreFromExtras(Bundle extras) {
4069 super.restoreFromExtras(extras);
4070
4071 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004072 }
4073
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004074 /**
4075 * @hide
4076 */
4077 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004078
4079 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004080 CharSequence oldBuilderContentText =
4081 mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
4082 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04004083
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004084 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08004085
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004086 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004087
Selim Cinek3a2c4b92015-12-17 17:01:17 -08004088 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
4089 contentView.setTextViewText(R.id.big_text, bigTextText);
4090 contentView.setViewVisibility(R.id.big_text,
4091 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004092 contentView.setInt(R.id.big_text, "setMaxLines", calculateMaxLines());
Selim Cinek4fb12d32015-11-19 18:10:48 -08004093 contentView.setBoolean(R.id.big_text, "setHasImage", mBuilder.mN.mLargeIcon != null);
4094
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004095 return contentView;
4096 }
4097
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004098 private int calculateMaxLines() {
4099 int lineCount = MAX_LINES;
4100 boolean hasActions = mBuilder.mActions.size() > 0;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004101 if (hasActions) {
4102 lineCount -= LINES_CONSUMED_BY_ACTIONS;
4103 }
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004104 return lineCount;
4105 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04004106 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004107
4108 /**
4109 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08004110 *
Robert Ly91c5ce32014-06-08 15:37:00 -07004111 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04004112 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07004113 * Notification notif = new Notification.Builder(mContext)
4114 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
4115 * .setContentText(subject)
4116 * .setSmallIcon(R.drawable.new_mail)
4117 * .setLargeIcon(aBitmap)
4118 * .setStyle(new Notification.InboxStyle()
4119 * .addLine(str1)
4120 * .addLine(str2)
4121 * .setContentTitle(&quot;&quot;)
4122 * .setSummaryText(&quot;+3 more&quot;))
4123 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04004124 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08004125 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04004126 * @see Notification#bigContentView
4127 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004128 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04004129 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
4130
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004131 public InboxStyle() {
4132 }
4133
Daniel Sandler879c5e02012-04-17 16:46:51 -04004134 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004135 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004136 }
4137
Chris Wrend6297db2012-05-03 16:20:13 -04004138 /**
4139 * Overrides ContentTitle in the big form of the template.
4140 * This defaults to the value passed to setContentTitle().
4141 */
4142 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004143 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04004144 return this;
4145 }
4146
4147 /**
4148 * Set the first line of text after the detail section in the big form of the template.
4149 */
4150 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004151 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04004152 return this;
4153 }
4154
Chris Wren0bd664d2012-08-01 13:56:56 -04004155 /**
4156 * Append a line to the digest section of the Inbox notification.
4157 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04004158 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04004159 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04004160 return this;
4161 }
4162
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004163 /**
4164 * @hide
4165 */
4166 public void addExtras(Bundle extras) {
4167 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004168
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004169 CharSequence[] a = new CharSequence[mTexts.size()];
4170 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
4171 }
4172
Christoph Studer4600f9b2014-07-22 22:44:43 +02004173 /**
4174 * @hide
4175 */
4176 @Override
4177 protected void restoreFromExtras(Bundle extras) {
4178 super.restoreFromExtras(extras);
4179
4180 mTexts.clear();
4181 if (extras.containsKey(EXTRA_TEXT_LINES)) {
4182 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
4183 }
4184 }
4185
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004186 /**
4187 * @hide
4188 */
4189 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08004190 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02004191 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004192 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
4193 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004194
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004195 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04004196
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004197 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004198
Chris Wrend6297db2012-05-03 16:20:13 -04004199 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 -04004200 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04004201
Chris Wren4ed80d52012-05-17 09:30:03 -04004202 // Make sure all rows are gone in case we reuse a view.
4203 for (int rowId : rowIds) {
4204 contentView.setViewVisibility(rowId, View.GONE);
4205 }
4206
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004207 final boolean largeText =
4208 mBuilder.mContext.getResources().getConfiguration().fontScale > 1f;
4209 final float subTextSize = mBuilder.mContext.getResources().getDimensionPixelSize(
4210 R.dimen.notification_subtext_size);
Daniel Sandler879c5e02012-04-17 16:46:51 -04004211 int i=0;
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004212 final float density = mBuilder.mContext.getResources().getDisplayMetrics().density;
4213 int topPadding = (int) (5 * density);
4214 int bottomPadding = (int) (13 * density);
Selim Cinek247fa012016-02-18 09:50:48 -08004215 boolean first = true;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004216 while (i < mTexts.size() && i < rowIds.length) {
4217 CharSequence str = mTexts.get(i);
4218 if (str != null && !str.equals("")) {
4219 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004220 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004221 if (largeText) {
4222 contentView.setTextViewTextSize(rowIds[i], TypedValue.COMPLEX_UNIT_PX,
4223 subTextSize);
4224 }
Selim Cinek9d9fc6e2015-11-12 15:49:14 -05004225 contentView.setViewPadding(rowIds[i], 0, topPadding, 0,
4226 i == rowIds.length - 1 || i == mTexts.size() - 1 ? bottomPadding : 0);
Selim Cinek247fa012016-02-18 09:50:48 -08004227 handleInboxImageMargin(contentView, rowIds[i], first);
4228 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04004229 }
4230 i++;
4231 }
Kenny Guy98193ea2014-07-24 19:54:37 +01004232
Selim Cinek1e0bf612015-11-20 15:57:26 -08004233
Daniel Sandler879c5e02012-04-17 16:46:51 -04004234 return contentView;
4235 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004236
Selim Cinek247fa012016-02-18 09:50:48 -08004237 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08004238 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08004239 if (first) {
4240 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
4241 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
4242 boolean hasProgress = max != 0 || ind;
4243 if (mBuilder.mN.mLargeIcon != null && !hasProgress) {
4244 endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4245 R.dimen.notification_content_picture_margin);
4246 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08004247 }
4248 contentView.setViewLayoutMarginEnd(id, endMargin);
4249 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04004250 }
Dan Sandler842dd772014-05-15 09:36:47 -04004251
4252 /**
4253 * Notification style for media playback notifications.
4254 *
4255 * In the expanded form, {@link Notification#bigContentView}, up to 5
4256 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04004257 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04004258 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
4259 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
4260 * treated as album artwork.
4261 *
4262 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
4263 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01004264 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04004265 * in the standard view alongside the usual content.
4266 *
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004267 * Notifications created with MediaStyle will have their category set to
4268 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
4269 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
4270 *
Jeff Browndba34ba2014-06-24 20:46:03 -07004271 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
4272 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04004273 * the System UI can identify this as a notification representing an active media session
4274 * and respond accordingly (by showing album artwork in the lockscreen, for example).
4275 *
4276 * To use this style with your Notification, feed it to
4277 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4278 * <pre class="prettyprint">
4279 * Notification noti = new Notification.Builder()
4280 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01004281 * .setContentTitle(&quot;Track title&quot;)
4282 * .setContentText(&quot;Artist - Album&quot;)
4283 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004284 * .setStyle(<b>new Notification.MediaStyle()</b>
4285 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04004286 * .build();
4287 * </pre>
4288 *
4289 * @see Notification#bigContentView
4290 */
4291 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004292 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04004293 static final int MAX_MEDIA_BUTTONS = 5;
4294
4295 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07004296 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04004297
4298 public MediaStyle() {
4299 }
4300
4301 public MediaStyle(Builder builder) {
4302 setBuilder(builder);
4303 }
4304
4305 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004306 * 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 -04004307 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004308 *
4309 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04004310 */
4311 public MediaStyle setShowActionsInCompactView(int...actions) {
4312 mActionsToShowInCompact = actions;
4313 return this;
4314 }
4315
4316 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07004317 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
4318 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04004319 */
Jeff Browndba34ba2014-06-24 20:46:03 -07004320 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04004321 mToken = token;
4322 return this;
4323 }
4324
Christoph Studer4600f9b2014-07-22 22:44:43 +02004325 /**
4326 * @hide
4327 */
Dan Sandler842dd772014-05-15 09:36:47 -04004328 @Override
4329 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004330 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01004331 if (wip.category == null) {
4332 wip.category = Notification.CATEGORY_TRANSPORT;
4333 }
Dan Sandler842dd772014-05-15 09:36:47 -04004334 return wip;
4335 }
4336
Christoph Studer4600f9b2014-07-22 22:44:43 +02004337 /**
4338 * @hide
4339 */
4340 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004341 public RemoteViews makeContentView() {
4342 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004343 }
4344
4345 /**
4346 * @hide
4347 */
4348 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004349 public RemoteViews makeBigContentView() {
4350 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02004351 }
4352
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004353 /**
4354 * @hide
4355 */
4356 @Override
4357 public RemoteViews makeHeadsUpContentView() {
4358 RemoteViews expanded = makeMediaBigContentView();
4359 return expanded != null ? expanded : makeMediaContentView();
4360 }
4361
Dan Sandler842dd772014-05-15 09:36:47 -04004362 /** @hide */
4363 @Override
4364 public void addExtras(Bundle extras) {
4365 super.addExtras(extras);
4366
4367 if (mToken != null) {
4368 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
4369 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01004370 if (mActionsToShowInCompact != null) {
4371 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
4372 }
Dan Sandler842dd772014-05-15 09:36:47 -04004373 }
4374
Christoph Studer4600f9b2014-07-22 22:44:43 +02004375 /**
4376 * @hide
4377 */
4378 @Override
4379 protected void restoreFromExtras(Bundle extras) {
4380 super.restoreFromExtras(extras);
4381
4382 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
4383 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
4384 }
4385 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
4386 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
4387 }
4388 }
4389
Selim Cinek5bf069a2015-11-10 19:14:27 -05004390 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04004391 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004392 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07004393 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04004394 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05004395 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
4396 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04004397 if (!tombstone) {
4398 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
4399 }
4400 button.setContentDescription(R.id.action0, action.title);
4401 return button;
4402 }
4403
4404 private RemoteViews makeMediaContentView() {
4405 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004406 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04004407
4408 final int numActions = mBuilder.mActions.size();
4409 final int N = mActionsToShowInCompact == null
4410 ? 0
4411 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4412 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004413 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04004414 for (int i = 0; i < N; i++) {
4415 if (i >= numActions) {
4416 throw new IllegalArgumentException(String.format(
4417 "setShowActionsInCompactView: action %d out of bounds (max %d)",
4418 i, numActions - 1));
4419 }
4420
4421 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05004422 final RemoteViews button = generateMediaActionButton(action,
4423 mBuilder.resolveColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004424 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004425 }
4426 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08004427 handleImage(view);
4428 // handle the content margin
Selim Cinek247fa012016-02-18 09:50:48 -08004429 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4430 R.dimen.notification_content_margin_end);;
Selim Cinekfdc738f2016-01-27 20:04:27 -08004431 if (mBuilder.mN.mLargeIcon != null) {
Selim Cinek247fa012016-02-18 09:50:48 -08004432 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
4433 R.dimen.notification_content_picture_margin);
Selim Cinekfdc738f2016-01-27 20:04:27 -08004434 }
4435 view.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04004436 return view;
4437 }
4438
4439 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004440 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004441 // Dont add an expanded view if there is no more content to be revealed
4442 int actionsInCompact = mActionsToShowInCompact == null
4443 ? 0
4444 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
4445 if (mBuilder.mN.mLargeIcon == null && actionCount <= actionsInCompact) {
4446 return null;
4447 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004448 RemoteViews big = mBuilder.applyStandardTemplate(
4449 R.layout.notification_template_material_big_media,
4450 false);
Dan Sandler842dd772014-05-15 09:36:47 -04004451
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004452 if (actionCount > 0) {
4453 big.removeAllViews(com.android.internal.R.id.media_actions);
4454 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05004455 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
4456 mBuilder.resolveColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004457 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04004458 }
4459 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05004460 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04004461 return big;
4462 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004463
Selim Cinek5bf069a2015-11-10 19:14:27 -05004464 private void handleImage(RemoteViews contentView) {
4465 if (mBuilder.mN.mLargeIcon != null) {
4466 contentView.setViewLayoutMarginEnd(R.id.line1, 0);
Selim Cinekc848c3a2016-01-13 15:27:30 -08004467 contentView.setViewLayoutMarginEnd(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004468 }
4469 }
4470
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02004471 /**
4472 * @hide
4473 */
4474 @Override
4475 protected boolean hasProgress() {
4476 return false;
4477 }
Dan Sandler842dd772014-05-15 09:36:47 -04004478 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004479
Selim Cinek593610c2016-02-16 18:42:57 -08004480 /**
4481 * Notification style for custom views that are decorated by the system
4482 *
4483 * <p>Instead of providing a notification that is completely custom, a developer can set this
4484 * style and still obtain system decorations like the notification header with the expand
4485 * affordance and actions.
4486 *
4487 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
4488 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
4489 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
4490 * corresponding custom views to display.
4491 *
4492 * To use this style with your Notification, feed it to
4493 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
4494 * <pre class="prettyprint">
4495 * Notification noti = new Notification.Builder()
4496 * .setSmallIcon(R.drawable.ic_stat_player)
4497 * .setLargeIcon(albumArtBitmap))
4498 * .setCustomContentView(contentView);
4499 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
4500 * .build();
4501 * </pre>
4502 */
4503 public static class DecoratedCustomViewStyle extends Style {
4504
4505 public DecoratedCustomViewStyle() {
4506 }
4507
4508 public DecoratedCustomViewStyle(Builder builder) {
4509 setBuilder(builder);
4510 }
4511
4512 /**
4513 * @hide
4514 */
4515 public boolean displayCustomViewInline() {
4516 return true;
4517 }
4518
4519 /**
4520 * @hide
4521 */
4522 @Override
4523 public RemoteViews makeContentView() {
4524 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
4525 }
4526
4527 /**
4528 * @hide
4529 */
4530 @Override
4531 public RemoteViews makeBigContentView() {
4532 return makeDecoratedBigContentView();
4533 }
4534
4535 /**
4536 * @hide
4537 */
4538 @Override
4539 public RemoteViews makeHeadsUpContentView() {
4540 return makeDecoratedHeadsUpContentView();
4541 }
4542
Selim Cinek593610c2016-02-16 18:42:57 -08004543 private RemoteViews makeDecoratedHeadsUpContentView() {
4544 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
4545 ? mBuilder.mN.contentView
4546 : mBuilder.mN.headsUpContentView;
4547 if (mBuilder.mActions.size() == 0) {
4548 return makeStandardTemplateWithCustomContent(headsUpContentView);
4549 }
4550 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
4551 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004552 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08004553 return remoteViews;
4554 }
4555
Selim Cinek593610c2016-02-16 18:42:57 -08004556 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
4557 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
4558 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004559 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08004560 return remoteViews;
4561 }
4562
Selim Cinek593610c2016-02-16 18:42:57 -08004563 private RemoteViews makeDecoratedBigContentView() {
4564 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
4565 ? mBuilder.mN.contentView
4566 : mBuilder.mN.bigContentView;
4567 if (mBuilder.mActions.size() == 0) {
4568 return makeStandardTemplateWithCustomContent(bigContentView);
4569 }
4570 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
4571 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08004572 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08004573 return remoteViews;
4574 }
Selim Cinek247fa012016-02-18 09:50:48 -08004575
4576 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
4577 RemoteViews customContent) {
4578 remoteViews.removeAllViews(R.id.notification_main_column);
4579 remoteViews.addView(R.id.notification_main_column, customContent);
4580 // also update the end margin if there is an image
4581 int endMargin = mBuilder.mContext.getResources().getDimensionPixelSize(
4582 R.dimen.notification_content_margin_end);
4583 if (mBuilder.mN.mLargeIcon != null) {
4584 endMargin += mBuilder.mContext.getResources().getDimensionPixelSize(
4585 R.dimen.notification_content_picture_margin);
4586 }
4587 remoteViews.setViewLayoutMarginEnd(R.id.notification_main_column, endMargin);
4588 }
Selim Cinek593610c2016-02-16 18:42:57 -08004589 }
4590
Christoph Studer4600f9b2014-07-22 22:44:43 +02004591 // When adding a new Style subclass here, don't forget to update
4592 // Builder.getNotificationStyleClass.
4593
Griff Hazen61a9e862014-05-22 16:05:19 -07004594 /**
4595 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
4596 * metadata or change options on a notification builder.
4597 */
4598 public interface Extender {
4599 /**
4600 * Apply this extender to a notification builder.
4601 * @param builder the builder to be modified.
4602 * @return the build object for chaining.
4603 */
4604 public Builder extend(Builder builder);
4605 }
4606
4607 /**
4608 * Helper class to add wearable extensions to notifications.
4609 * <p class="note"> See
4610 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
4611 * for Android Wear</a> for more information on how to use this class.
4612 * <p>
4613 * To create a notification with wearable extensions:
4614 * <ol>
4615 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
4616 * properties.
4617 * <li>Create a {@link android.app.Notification.WearableExtender}.
4618 * <li>Set wearable-specific properties using the
4619 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
4620 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
4621 * notification.
4622 * <li>Post the notification to the notification system with the
4623 * {@code NotificationManager.notify(...)} methods.
4624 * </ol>
4625 *
4626 * <pre class="prettyprint">
4627 * Notification notif = new Notification.Builder(mContext)
4628 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
4629 * .setContentText(subject)
4630 * .setSmallIcon(R.drawable.new_mail)
4631 * .extend(new Notification.WearableExtender()
4632 * .setContentIcon(R.drawable.new_mail))
4633 * .build();
4634 * NotificationManager notificationManger =
4635 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
4636 * notificationManger.notify(0, notif);</pre>
4637 *
4638 * <p>Wearable extensions can be accessed on an existing notification by using the
4639 * {@code WearableExtender(Notification)} constructor,
4640 * and then using the {@code get} methods to access values.
4641 *
4642 * <pre class="prettyprint">
4643 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
4644 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07004645 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004646 */
4647 public static final class WearableExtender implements Extender {
4648 /**
4649 * Sentinel value for an action index that is unset.
4650 */
4651 public static final int UNSET_ACTION_INDEX = -1;
4652
4653 /**
4654 * Size value for use with {@link #setCustomSizePreset} to show this notification with
4655 * default sizing.
4656 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07004657 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07004658 * on their content.
4659 */
4660 public static final int SIZE_DEFAULT = 0;
4661
4662 /**
4663 * Size value for use with {@link #setCustomSizePreset} to show this notification
4664 * with an extra small size.
4665 * <p>This value is only applicable for custom display notifications created using
4666 * {@link #setDisplayIntent}.
4667 */
4668 public static final int SIZE_XSMALL = 1;
4669
4670 /**
4671 * Size value for use with {@link #setCustomSizePreset} to show this notification
4672 * with a small size.
4673 * <p>This value is only applicable for custom display notifications created using
4674 * {@link #setDisplayIntent}.
4675 */
4676 public static final int SIZE_SMALL = 2;
4677
4678 /**
4679 * Size value for use with {@link #setCustomSizePreset} to show this notification
4680 * with a medium size.
4681 * <p>This value is only applicable for custom display notifications created using
4682 * {@link #setDisplayIntent}.
4683 */
4684 public static final int SIZE_MEDIUM = 3;
4685
4686 /**
4687 * Size value for use with {@link #setCustomSizePreset} to show this notification
4688 * with a large size.
4689 * <p>This value is only applicable for custom display notifications created using
4690 * {@link #setDisplayIntent}.
4691 */
4692 public static final int SIZE_LARGE = 4;
4693
Griff Hazend5f11f92014-05-27 15:40:09 -07004694 /**
4695 * Size value for use with {@link #setCustomSizePreset} to show this notification
4696 * full screen.
4697 * <p>This value is only applicable for custom display notifications created using
4698 * {@link #setDisplayIntent}.
4699 */
4700 public static final int SIZE_FULL_SCREEN = 5;
4701
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004702 /**
4703 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
4704 * short amount of time when this notification is displayed on the screen. This
4705 * is the default value.
4706 */
4707 public static final int SCREEN_TIMEOUT_SHORT = 0;
4708
4709 /**
4710 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
4711 * for a longer amount of time when this notification is displayed on the screen.
4712 */
4713 public static final int SCREEN_TIMEOUT_LONG = -1;
4714
Griff Hazen61a9e862014-05-22 16:05:19 -07004715 /** Notification extra which contains wearable extensions */
4716 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
4717
Pete Gastaf6781d2014-10-07 15:17:05 -04004718 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07004719 private static final String KEY_ACTIONS = "actions";
4720 private static final String KEY_FLAGS = "flags";
4721 private static final String KEY_DISPLAY_INTENT = "displayIntent";
4722 private static final String KEY_PAGES = "pages";
4723 private static final String KEY_BACKGROUND = "background";
4724 private static final String KEY_CONTENT_ICON = "contentIcon";
4725 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
4726 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
4727 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
4728 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
4729 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004730 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Griff Hazen61a9e862014-05-22 16:05:19 -07004731
4732 // Flags bitwise-ored to mFlags
4733 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
4734 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
4735 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
4736 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004737 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Griff Hazen61a9e862014-05-22 16:05:19 -07004738
4739 // Default value for flags integer
4740 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
4741
4742 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
4743 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
4744
4745 private ArrayList<Action> mActions = new ArrayList<Action>();
4746 private int mFlags = DEFAULT_FLAGS;
4747 private PendingIntent mDisplayIntent;
4748 private ArrayList<Notification> mPages = new ArrayList<Notification>();
4749 private Bitmap mBackground;
4750 private int mContentIcon;
4751 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
4752 private int mContentActionIndex = UNSET_ACTION_INDEX;
4753 private int mCustomSizePreset = SIZE_DEFAULT;
4754 private int mCustomContentHeight;
4755 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004756 private int mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004757
4758 /**
4759 * Create a {@link android.app.Notification.WearableExtender} with default
4760 * options.
4761 */
4762 public WearableExtender() {
4763 }
4764
4765 public WearableExtender(Notification notif) {
4766 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
4767 if (wearableBundle != null) {
4768 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
4769 if (actions != null) {
4770 mActions.addAll(actions);
4771 }
4772
4773 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
4774 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
4775
4776 Notification[] pages = getNotificationArrayFromBundle(
4777 wearableBundle, KEY_PAGES);
4778 if (pages != null) {
4779 Collections.addAll(mPages, pages);
4780 }
4781
4782 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
4783 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
4784 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
4785 DEFAULT_CONTENT_ICON_GRAVITY);
4786 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
4787 UNSET_ACTION_INDEX);
4788 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
4789 SIZE_DEFAULT);
4790 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
4791 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004792 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Griff Hazen61a9e862014-05-22 16:05:19 -07004793 }
4794 }
4795
4796 /**
4797 * Apply wearable extensions to a notification that is being built. This is typically
4798 * called by the {@link android.app.Notification.Builder#extend} method of
4799 * {@link android.app.Notification.Builder}.
4800 */
4801 @Override
4802 public Notification.Builder extend(Notification.Builder builder) {
4803 Bundle wearableBundle = new Bundle();
4804
4805 if (!mActions.isEmpty()) {
4806 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
4807 }
4808 if (mFlags != DEFAULT_FLAGS) {
4809 wearableBundle.putInt(KEY_FLAGS, mFlags);
4810 }
4811 if (mDisplayIntent != null) {
4812 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
4813 }
4814 if (!mPages.isEmpty()) {
4815 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
4816 new Notification[mPages.size()]));
4817 }
4818 if (mBackground != null) {
4819 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
4820 }
4821 if (mContentIcon != 0) {
4822 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
4823 }
4824 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
4825 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
4826 }
4827 if (mContentActionIndex != UNSET_ACTION_INDEX) {
4828 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
4829 mContentActionIndex);
4830 }
4831 if (mCustomSizePreset != SIZE_DEFAULT) {
4832 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
4833 }
4834 if (mCustomContentHeight != 0) {
4835 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
4836 }
4837 if (mGravity != DEFAULT_GRAVITY) {
4838 wearableBundle.putInt(KEY_GRAVITY, mGravity);
4839 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004840 if (mHintScreenTimeout != 0) {
4841 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
4842 }
Griff Hazen61a9e862014-05-22 16:05:19 -07004843
4844 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
4845 return builder;
4846 }
4847
4848 @Override
4849 public WearableExtender clone() {
4850 WearableExtender that = new WearableExtender();
4851 that.mActions = new ArrayList<Action>(this.mActions);
4852 that.mFlags = this.mFlags;
4853 that.mDisplayIntent = this.mDisplayIntent;
4854 that.mPages = new ArrayList<Notification>(this.mPages);
4855 that.mBackground = this.mBackground;
4856 that.mContentIcon = this.mContentIcon;
4857 that.mContentIconGravity = this.mContentIconGravity;
4858 that.mContentActionIndex = this.mContentActionIndex;
4859 that.mCustomSizePreset = this.mCustomSizePreset;
4860 that.mCustomContentHeight = this.mCustomContentHeight;
4861 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07004862 that.mHintScreenTimeout = this.mHintScreenTimeout;
Griff Hazen61a9e862014-05-22 16:05:19 -07004863 return that;
4864 }
4865
4866 /**
4867 * Add a wearable action to this notification.
4868 *
4869 * <p>When wearable actions are added using this method, the set of actions that
4870 * show on a wearable device splits from devices that only show actions added
4871 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4872 * of which actions display on different devices.
4873 *
4874 * @param action the action to add to this notification
4875 * @return this object for method chaining
4876 * @see android.app.Notification.Action
4877 */
4878 public WearableExtender addAction(Action action) {
4879 mActions.add(action);
4880 return this;
4881 }
4882
4883 /**
4884 * Adds wearable actions to this notification.
4885 *
4886 * <p>When wearable actions are added using this method, the set of actions that
4887 * show on a wearable device splits from devices that only show actions added
4888 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
4889 * of which actions display on different devices.
4890 *
4891 * @param actions the actions to add to this notification
4892 * @return this object for method chaining
4893 * @see android.app.Notification.Action
4894 */
4895 public WearableExtender addActions(List<Action> actions) {
4896 mActions.addAll(actions);
4897 return this;
4898 }
4899
4900 /**
4901 * Clear all wearable actions present on this builder.
4902 * @return this object for method chaining.
4903 * @see #addAction
4904 */
4905 public WearableExtender clearActions() {
4906 mActions.clear();
4907 return this;
4908 }
4909
4910 /**
4911 * Get the wearable actions present on this notification.
4912 */
4913 public List<Action> getActions() {
4914 return mActions;
4915 }
4916
4917 /**
4918 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07004919 * this notification. The {@link PendingIntent} provided should be for an activity.
4920 *
4921 * <pre class="prettyprint">
4922 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
4923 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
4924 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
4925 * Notification notif = new Notification.Builder(context)
4926 * .extend(new Notification.WearableExtender()
4927 * .setDisplayIntent(displayPendingIntent)
4928 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
4929 * .build();</pre>
4930 *
4931 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07004932 * should have an empty task affinity. It is also recommended to use the device
4933 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07004934 *
4935 * <p>Example AndroidManifest.xml entry:
4936 * <pre class="prettyprint">
4937 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
4938 * android:exported=&quot;true&quot;
4939 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07004940 * android:taskAffinity=&quot;&quot;
4941 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07004942 *
4943 * @param intent the {@link PendingIntent} for an activity
4944 * @return this object for method chaining
4945 * @see android.app.Notification.WearableExtender#getDisplayIntent
4946 */
4947 public WearableExtender setDisplayIntent(PendingIntent intent) {
4948 mDisplayIntent = intent;
4949 return this;
4950 }
4951
4952 /**
4953 * Get the intent to launch inside of an activity view when displaying this
4954 * notification. This {@code PendingIntent} should be for an activity.
4955 */
4956 public PendingIntent getDisplayIntent() {
4957 return mDisplayIntent;
4958 }
4959
4960 /**
4961 * Add an additional page of content to display with this notification. The current
4962 * notification forms the first page, and pages added using this function form
4963 * subsequent pages. This field can be used to separate a notification into multiple
4964 * sections.
4965 *
4966 * @param page the notification to add as another page
4967 * @return this object for method chaining
4968 * @see android.app.Notification.WearableExtender#getPages
4969 */
4970 public WearableExtender addPage(Notification page) {
4971 mPages.add(page);
4972 return this;
4973 }
4974
4975 /**
4976 * Add additional pages of content to display with this notification. The current
4977 * notification forms the first page, and pages added using this function form
4978 * subsequent pages. This field can be used to separate a notification into multiple
4979 * sections.
4980 *
4981 * @param pages a list of notifications
4982 * @return this object for method chaining
4983 * @see android.app.Notification.WearableExtender#getPages
4984 */
4985 public WearableExtender addPages(List<Notification> pages) {
4986 mPages.addAll(pages);
4987 return this;
4988 }
4989
4990 /**
4991 * Clear all additional pages present on this builder.
4992 * @return this object for method chaining.
4993 * @see #addPage
4994 */
4995 public WearableExtender clearPages() {
4996 mPages.clear();
4997 return this;
4998 }
4999
5000 /**
5001 * Get the array of additional pages of content for displaying this notification. The
5002 * current notification forms the first page, and elements within this array form
5003 * subsequent pages. This field can be used to separate a notification into multiple
5004 * sections.
5005 * @return the pages for this notification
5006 */
5007 public List<Notification> getPages() {
5008 return mPages;
5009 }
5010
5011 /**
5012 * Set a background image to be displayed behind the notification content.
5013 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5014 * will work with any notification style.
5015 *
5016 * @param background the background bitmap
5017 * @return this object for method chaining
5018 * @see android.app.Notification.WearableExtender#getBackground
5019 */
5020 public WearableExtender setBackground(Bitmap background) {
5021 mBackground = background;
5022 return this;
5023 }
5024
5025 /**
5026 * Get a background image to be displayed behind the notification content.
5027 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
5028 * will work with any notification style.
5029 *
5030 * @return the background image
5031 * @see android.app.Notification.WearableExtender#setBackground
5032 */
5033 public Bitmap getBackground() {
5034 return mBackground;
5035 }
5036
5037 /**
5038 * Set an icon that goes with the content of this notification.
5039 */
5040 public WearableExtender setContentIcon(int icon) {
5041 mContentIcon = icon;
5042 return this;
5043 }
5044
5045 /**
5046 * Get an icon that goes with the content of this notification.
5047 */
5048 public int getContentIcon() {
5049 return mContentIcon;
5050 }
5051
5052 /**
5053 * Set the gravity that the content icon should have within the notification display.
5054 * Supported values include {@link android.view.Gravity#START} and
5055 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5056 * @see #setContentIcon
5057 */
5058 public WearableExtender setContentIconGravity(int contentIconGravity) {
5059 mContentIconGravity = contentIconGravity;
5060 return this;
5061 }
5062
5063 /**
5064 * Get the gravity that the content icon should have within the notification display.
5065 * Supported values include {@link android.view.Gravity#START} and
5066 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
5067 * @see #getContentIcon
5068 */
5069 public int getContentIconGravity() {
5070 return mContentIconGravity;
5071 }
5072
5073 /**
5074 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07005075 * this notification. This action will no longer display separately from the
5076 * notification's content.
5077 *
Griff Hazenca48d352014-05-28 22:37:13 -07005078 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005079 * set, although the list of available actions comes from the main notification and not
5080 * from the child page's notification.
5081 *
5082 * @param actionIndex The index of the action to hoist onto the current notification page.
5083 * If wearable actions were added to the main notification, this index
5084 * will apply to that list, otherwise it will apply to the regular
5085 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07005086 */
5087 public WearableExtender setContentAction(int actionIndex) {
5088 mContentActionIndex = actionIndex;
5089 return this;
5090 }
5091
5092 /**
Griff Hazenca48d352014-05-28 22:37:13 -07005093 * Get the index of the notification action, if any, that was specified as being clickable
5094 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07005095 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07005096 *
Griff Hazenca48d352014-05-28 22:37:13 -07005097 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07005098 * set, although the list of available actions comes from the main notification and not
5099 * from the child page's notification.
5100 *
5101 * <p>If wearable specific actions were added to the main notification, this index will
5102 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07005103 *
5104 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07005105 */
5106 public int getContentAction() {
5107 return mContentActionIndex;
5108 }
5109
5110 /**
5111 * Set the gravity that this notification should have within the available viewport space.
5112 * Supported values include {@link android.view.Gravity#TOP},
5113 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5114 * The default value is {@link android.view.Gravity#BOTTOM}.
5115 */
5116 public WearableExtender setGravity(int gravity) {
5117 mGravity = gravity;
5118 return this;
5119 }
5120
5121 /**
5122 * Get the gravity that this notification should have within the available viewport space.
5123 * Supported values include {@link android.view.Gravity#TOP},
5124 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
5125 * The default value is {@link android.view.Gravity#BOTTOM}.
5126 */
5127 public int getGravity() {
5128 return mGravity;
5129 }
5130
5131 /**
5132 * Set the custom size preset for the display of this notification out of the available
5133 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5134 * {@link #SIZE_LARGE}.
5135 * <p>Some custom size presets are only applicable for custom display notifications created
5136 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
5137 * documentation for the preset in question. See also
5138 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
5139 */
5140 public WearableExtender setCustomSizePreset(int sizePreset) {
5141 mCustomSizePreset = sizePreset;
5142 return this;
5143 }
5144
5145 /**
5146 * Get the custom size preset for the display of this notification out of the available
5147 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
5148 * {@link #SIZE_LARGE}.
5149 * <p>Some custom size presets are only applicable for custom display notifications created
5150 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
5151 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
5152 */
5153 public int getCustomSizePreset() {
5154 return mCustomSizePreset;
5155 }
5156
5157 /**
5158 * Set the custom height in pixels for the display of this notification's content.
5159 * <p>This option is only available for custom display notifications created
5160 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
5161 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
5162 * {@link #getCustomContentHeight}.
5163 */
5164 public WearableExtender setCustomContentHeight(int height) {
5165 mCustomContentHeight = height;
5166 return this;
5167 }
5168
5169 /**
5170 * Get the custom height in pixels for the display of this notification's content.
5171 * <p>This option is only available for custom display notifications created
5172 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
5173 * {@link #setCustomContentHeight}.
5174 */
5175 public int getCustomContentHeight() {
5176 return mCustomContentHeight;
5177 }
5178
5179 /**
5180 * Set whether the scrolling position for the contents of this notification should start
5181 * at the bottom of the contents instead of the top when the contents are too long to
5182 * display within the screen. Default is false (start scroll at the top).
5183 */
5184 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
5185 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
5186 return this;
5187 }
5188
5189 /**
5190 * Get whether the scrolling position for the contents of this notification should start
5191 * at the bottom of the contents instead of the top when the contents are too long to
5192 * display within the screen. Default is false (start scroll at the top).
5193 */
5194 public boolean getStartScrollBottom() {
5195 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
5196 }
5197
5198 /**
5199 * Set whether the content intent is available when the wearable device is not connected
5200 * to a companion device. The user can still trigger this intent when the wearable device
5201 * is offline, but a visual hint will indicate that the content intent may not be available.
5202 * Defaults to true.
5203 */
5204 public WearableExtender setContentIntentAvailableOffline(
5205 boolean contentIntentAvailableOffline) {
5206 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
5207 return this;
5208 }
5209
5210 /**
5211 * Get whether the content intent is available when the wearable device is not connected
5212 * to a companion device. The user can still trigger this intent when the wearable device
5213 * is offline, but a visual hint will indicate that the content intent may not be available.
5214 * Defaults to true.
5215 */
5216 public boolean getContentIntentAvailableOffline() {
5217 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
5218 }
5219
5220 /**
5221 * Set a hint that this notification's icon should not be displayed.
5222 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
5223 * @return this object for method chaining
5224 */
5225 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
5226 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
5227 return this;
5228 }
5229
5230 /**
5231 * Get a hint that this notification's icon should not be displayed.
5232 * @return {@code true} if this icon should not be displayed, false otherwise.
5233 * The default value is {@code false} if this was never set.
5234 */
5235 public boolean getHintHideIcon() {
5236 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
5237 }
5238
5239 /**
5240 * Set a visual hint that only the background image of this notification should be
5241 * displayed, and other semantic content should be hidden. This hint is only applicable
5242 * to sub-pages added using {@link #addPage}.
5243 */
5244 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
5245 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
5246 return this;
5247 }
5248
5249 /**
5250 * Get a visual hint that only the background image of this notification should be
5251 * displayed, and other semantic content should be hidden. This hint is only applicable
5252 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
5253 */
5254 public boolean getHintShowBackgroundOnly() {
5255 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
5256 }
5257
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005258 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005259 * Set a hint that this notification's background should not be clipped if possible,
5260 * and should instead be resized to fully display on the screen, retaining the aspect
5261 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005262 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
5263 * @return this object for method chaining
5264 */
5265 public WearableExtender setHintAvoidBackgroundClipping(
5266 boolean hintAvoidBackgroundClipping) {
5267 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
5268 return this;
5269 }
5270
5271 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08005272 * Get a hint that this notification's background should not be clipped if possible,
5273 * and should instead be resized to fully display on the screen, retaining the aspect
5274 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07005275 * @return {@code true} if it's ok if the background is clipped on the screen, false
5276 * otherwise. The default value is {@code false} if this was never set.
5277 */
5278 public boolean getHintAvoidBackgroundClipping() {
5279 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
5280 }
5281
5282 /**
5283 * Set a hint that the screen should remain on for at least this duration when
5284 * this notification is displayed on the screen.
5285 * @param timeout The requested screen timeout in milliseconds. Can also be either
5286 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5287 * @return this object for method chaining
5288 */
5289 public WearableExtender setHintScreenTimeout(int timeout) {
5290 mHintScreenTimeout = timeout;
5291 return this;
5292 }
5293
5294 /**
5295 * Get the duration, in milliseconds, that the screen should remain on for
5296 * when this notification is displayed.
5297 * @return the duration in milliseconds if > 0, or either one of the sentinel values
5298 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
5299 */
5300 public int getHintScreenTimeout() {
5301 return mHintScreenTimeout;
5302 }
5303
Griff Hazen61a9e862014-05-22 16:05:19 -07005304 private void setFlag(int mask, boolean value) {
5305 if (value) {
5306 mFlags |= mask;
5307 } else {
5308 mFlags &= ~mask;
5309 }
5310 }
5311 }
5312
5313 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005314 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
5315 * with car extensions:
5316 *
5317 * <ol>
5318 * <li>Create an {@link Notification.Builder}, setting any desired
5319 * properties.
5320 * <li>Create a {@link CarExtender}.
5321 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
5322 * {@link CarExtender}.
5323 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
5324 * to apply the extensions to a notification.
5325 * </ol>
5326 *
5327 * <pre class="prettyprint">
5328 * Notification notification = new Notification.Builder(context)
5329 * ...
5330 * .extend(new CarExtender()
5331 * .set*(...))
5332 * .build();
5333 * </pre>
5334 *
5335 * <p>Car extensions can be accessed on an existing notification by using the
5336 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
5337 * to access values.
5338 */
5339 public static final class CarExtender implements Extender {
5340 private static final String TAG = "CarExtender";
5341
5342 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
5343 private static final String EXTRA_LARGE_ICON = "large_icon";
5344 private static final String EXTRA_CONVERSATION = "car_conversation";
5345 private static final String EXTRA_COLOR = "app_color";
5346
5347 private Bitmap mLargeIcon;
5348 private UnreadConversation mUnreadConversation;
5349 private int mColor = Notification.COLOR_DEFAULT;
5350
5351 /**
5352 * Create a {@link CarExtender} with default options.
5353 */
5354 public CarExtender() {
5355 }
5356
5357 /**
5358 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
5359 *
5360 * @param notif The notification from which to copy options.
5361 */
5362 public CarExtender(Notification notif) {
5363 Bundle carBundle = notif.extras == null ?
5364 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
5365 if (carBundle != null) {
5366 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
5367 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
5368
5369 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
5370 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
5371 }
5372 }
5373
5374 /**
5375 * Apply car extensions to a notification that is being built. This is typically called by
5376 * the {@link Notification.Builder#extend(Notification.Extender)}
5377 * method of {@link Notification.Builder}.
5378 */
5379 @Override
5380 public Notification.Builder extend(Notification.Builder builder) {
5381 Bundle carExtensions = new Bundle();
5382
5383 if (mLargeIcon != null) {
5384 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
5385 }
5386 if (mColor != Notification.COLOR_DEFAULT) {
5387 carExtensions.putInt(EXTRA_COLOR, mColor);
5388 }
5389
5390 if (mUnreadConversation != null) {
5391 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
5392 carExtensions.putBundle(EXTRA_CONVERSATION, b);
5393 }
5394
5395 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
5396 return builder;
5397 }
5398
5399 /**
5400 * Sets the accent color to use when Android Auto presents the notification.
5401 *
5402 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
5403 * to accent the displayed notification. However, not all colors are acceptable in an
5404 * automotive setting. This method can be used to override the color provided in the
5405 * notification in such a situation.
5406 */
Tor Norbye80756e32015-03-02 09:39:27 -08005407 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005408 mColor = color;
5409 return this;
5410 }
5411
5412 /**
5413 * Gets the accent color.
5414 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005415 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005416 */
Tor Norbye80756e32015-03-02 09:39:27 -08005417 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08005418 public int getColor() {
5419 return mColor;
5420 }
5421
5422 /**
5423 * Sets the large icon of the car notification.
5424 *
5425 * If no large icon is set in the extender, Android Auto will display the icon
5426 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
5427 *
5428 * @param largeIcon The large icon to use in the car notification.
5429 * @return This object for method chaining.
5430 */
5431 public CarExtender setLargeIcon(Bitmap largeIcon) {
5432 mLargeIcon = largeIcon;
5433 return this;
5434 }
5435
5436 /**
5437 * Gets the large icon used in this car notification, or null if no icon has been set.
5438 *
5439 * @return The large icon for the car notification.
5440 * @see CarExtender#setLargeIcon
5441 */
5442 public Bitmap getLargeIcon() {
5443 return mLargeIcon;
5444 }
5445
5446 /**
5447 * Sets the unread conversation in a message notification.
5448 *
5449 * @param unreadConversation The unread part of the conversation this notification conveys.
5450 * @return This object for method chaining.
5451 */
5452 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
5453 mUnreadConversation = unreadConversation;
5454 return this;
5455 }
5456
5457 /**
5458 * Returns the unread conversation conveyed by this notification.
5459 * @see #setUnreadConversation(UnreadConversation)
5460 */
5461 public UnreadConversation getUnreadConversation() {
5462 return mUnreadConversation;
5463 }
5464
5465 /**
5466 * A class which holds the unread messages from a conversation.
5467 */
5468 public static class UnreadConversation {
5469 private static final String KEY_AUTHOR = "author";
5470 private static final String KEY_TEXT = "text";
5471 private static final String KEY_MESSAGES = "messages";
5472 private static final String KEY_REMOTE_INPUT = "remote_input";
5473 private static final String KEY_ON_REPLY = "on_reply";
5474 private static final String KEY_ON_READ = "on_read";
5475 private static final String KEY_PARTICIPANTS = "participants";
5476 private static final String KEY_TIMESTAMP = "timestamp";
5477
5478 private final String[] mMessages;
5479 private final RemoteInput mRemoteInput;
5480 private final PendingIntent mReplyPendingIntent;
5481 private final PendingIntent mReadPendingIntent;
5482 private final String[] mParticipants;
5483 private final long mLatestTimestamp;
5484
5485 UnreadConversation(String[] messages, RemoteInput remoteInput,
5486 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
5487 String[] participants, long latestTimestamp) {
5488 mMessages = messages;
5489 mRemoteInput = remoteInput;
5490 mReadPendingIntent = readPendingIntent;
5491 mReplyPendingIntent = replyPendingIntent;
5492 mParticipants = participants;
5493 mLatestTimestamp = latestTimestamp;
5494 }
5495
5496 /**
5497 * Gets the list of messages conveyed by this notification.
5498 */
5499 public String[] getMessages() {
5500 return mMessages;
5501 }
5502
5503 /**
5504 * Gets the remote input that will be used to convey the response to a message list, or
5505 * null if no such remote input exists.
5506 */
5507 public RemoteInput getRemoteInput() {
5508 return mRemoteInput;
5509 }
5510
5511 /**
5512 * Gets the pending intent that will be triggered when the user replies to this
5513 * notification.
5514 */
5515 public PendingIntent getReplyPendingIntent() {
5516 return mReplyPendingIntent;
5517 }
5518
5519 /**
5520 * Gets the pending intent that Android Auto will send after it reads aloud all messages
5521 * in this object's message list.
5522 */
5523 public PendingIntent getReadPendingIntent() {
5524 return mReadPendingIntent;
5525 }
5526
5527 /**
5528 * Gets the participants in the conversation.
5529 */
5530 public String[] getParticipants() {
5531 return mParticipants;
5532 }
5533
5534 /**
5535 * Gets the firs participant in the conversation.
5536 */
5537 public String getParticipant() {
5538 return mParticipants.length > 0 ? mParticipants[0] : null;
5539 }
5540
5541 /**
5542 * Gets the timestamp of the conversation.
5543 */
5544 public long getLatestTimestamp() {
5545 return mLatestTimestamp;
5546 }
5547
5548 Bundle getBundleForUnreadConversation() {
5549 Bundle b = new Bundle();
5550 String author = null;
5551 if (mParticipants != null && mParticipants.length > 1) {
5552 author = mParticipants[0];
5553 }
5554 Parcelable[] messages = new Parcelable[mMessages.length];
5555 for (int i = 0; i < messages.length; i++) {
5556 Bundle m = new Bundle();
5557 m.putString(KEY_TEXT, mMessages[i]);
5558 m.putString(KEY_AUTHOR, author);
5559 messages[i] = m;
5560 }
5561 b.putParcelableArray(KEY_MESSAGES, messages);
5562 if (mRemoteInput != null) {
5563 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
5564 }
5565 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
5566 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
5567 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
5568 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
5569 return b;
5570 }
5571
5572 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
5573 if (b == null) {
5574 return null;
5575 }
5576 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
5577 String[] messages = null;
5578 if (parcelableMessages != null) {
5579 String[] tmp = new String[parcelableMessages.length];
5580 boolean success = true;
5581 for (int i = 0; i < tmp.length; i++) {
5582 if (!(parcelableMessages[i] instanceof Bundle)) {
5583 success = false;
5584 break;
5585 }
5586 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
5587 if (tmp[i] == null) {
5588 success = false;
5589 break;
5590 }
5591 }
5592 if (success) {
5593 messages = tmp;
5594 } else {
5595 return null;
5596 }
5597 }
5598
5599 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
5600 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
5601
5602 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
5603
5604 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
5605 if (participants == null || participants.length != 1) {
5606 return null;
5607 }
5608
5609 return new UnreadConversation(messages,
5610 remoteInput,
5611 onReply,
5612 onRead,
5613 participants, b.getLong(KEY_TIMESTAMP));
5614 }
5615 };
5616
5617 /**
5618 * Builder class for {@link CarExtender.UnreadConversation} objects.
5619 */
5620 public static class Builder {
5621 private final List<String> mMessages = new ArrayList<String>();
5622 private final String mParticipant;
5623 private RemoteInput mRemoteInput;
5624 private PendingIntent mReadPendingIntent;
5625 private PendingIntent mReplyPendingIntent;
5626 private long mLatestTimestamp;
5627
5628 /**
5629 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
5630 *
5631 * @param name The name of the other participant in the conversation.
5632 */
5633 public Builder(String name) {
5634 mParticipant = name;
5635 }
5636
5637 /**
5638 * Appends a new unread message to the list of messages for this conversation.
5639 *
5640 * The messages should be added from oldest to newest.
5641 *
5642 * @param message The text of the new unread message.
5643 * @return This object for method chaining.
5644 */
5645 public Builder addMessage(String message) {
5646 mMessages.add(message);
5647 return this;
5648 }
5649
5650 /**
5651 * Sets the pending intent and remote input which will convey the reply to this
5652 * notification.
5653 *
5654 * @param pendingIntent The pending intent which will be triggered on a reply.
5655 * @param remoteInput The remote input parcelable which will carry the reply.
5656 * @return This object for method chaining.
5657 *
5658 * @see CarExtender.UnreadConversation#getRemoteInput
5659 * @see CarExtender.UnreadConversation#getReplyPendingIntent
5660 */
5661 public Builder setReplyAction(
5662 PendingIntent pendingIntent, RemoteInput remoteInput) {
5663 mRemoteInput = remoteInput;
5664 mReplyPendingIntent = pendingIntent;
5665
5666 return this;
5667 }
5668
5669 /**
5670 * Sets the pending intent that will be sent once the messages in this notification
5671 * are read.
5672 *
5673 * @param pendingIntent The pending intent to use.
5674 * @return This object for method chaining.
5675 */
5676 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
5677 mReadPendingIntent = pendingIntent;
5678 return this;
5679 }
5680
5681 /**
5682 * Sets the timestamp of the most recent message in an unread conversation.
5683 *
5684 * If a messaging notification has been posted by your application and has not
5685 * yet been cancelled, posting a later notification with the same id and tag
5686 * but without a newer timestamp may result in Android Auto not displaying a
5687 * heads up notification for the later notification.
5688 *
5689 * @param timestamp The timestamp of the most recent message in the conversation.
5690 * @return This object for method chaining.
5691 */
5692 public Builder setLatestTimestamp(long timestamp) {
5693 mLatestTimestamp = timestamp;
5694 return this;
5695 }
5696
5697 /**
5698 * Builds a new unread conversation object.
5699 *
5700 * @return The new unread conversation object.
5701 */
5702 public UnreadConversation build() {
5703 String[] messages = mMessages.toArray(new String[mMessages.size()]);
5704 String[] participants = { mParticipant };
5705 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
5706 mReadPendingIntent, participants, mLatestTimestamp);
5707 }
5708 }
5709 }
5710
5711 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07005712 * Get an array of Notification objects from a parcelable array bundle field.
5713 * Update the bundle to have a typed array so fetches in the future don't need
5714 * to do an array copy.
5715 */
5716 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
5717 Parcelable[] array = bundle.getParcelableArray(key);
5718 if (array instanceof Notification[] || array == null) {
5719 return (Notification[]) array;
5720 }
5721 Notification[] typedArray = Arrays.copyOf(array, array.length,
5722 Notification[].class);
5723 bundle.putParcelableArray(key, typedArray);
5724 return typedArray;
5725 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005726
5727 private static class BuilderRemoteViews extends RemoteViews {
5728 public BuilderRemoteViews(Parcel parcel) {
5729 super(parcel);
5730 }
5731
Kenny Guy77320062014-08-27 21:37:15 +01005732 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
5733 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005734 }
5735
5736 @Override
5737 public BuilderRemoteViews clone() {
5738 Parcel p = Parcel.obtain();
5739 writeToParcel(p, 0);
5740 p.setDataPosition(0);
5741 BuilderRemoteViews brv = new BuilderRemoteViews(p);
5742 p.recycle();
5743 return brv;
5744 }
5745 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08005746}