blob: 70f270fdf72dfade05288289bd759497fbd6aacc [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 Norbyed9273d62013-05-30 15:59:53 -070019import android.annotation.IntDef;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.content.Context;
21import android.content.Intent;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040022import android.content.res.Resources;
Joe Onoratoef1e7762010-09-17 18:38:38 -040023import android.graphics.Bitmap;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010024import android.graphics.PorterDuff;
Jeff Sharkey098d5802012-04-26 17:30:34 -070025import android.media.AudioManager;
Dan Sandler842dd772014-05-15 09:36:47 -040026import android.media.session.MediaSessionToken;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080027import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040028import android.os.BadParcelableException;
Dan Sandler26e81cf2014-05-06 10:01:27 -040029import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050030import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080031import android.os.Parcel;
32import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040033import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070034import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.text.TextUtils;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040036import android.util.Log;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040037import android.util.TypedValue;
Griff Hazen61a9e862014-05-22 16:05:19 -070038import android.view.Gravity;
Joe Onorato8595a3d2010-11-19 18:12:07 -080039import android.view.View;
Jeff Sharkey1c400132011-08-05 14:50:13 -070040import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.widget.RemoteViews;
42
Griff Hazen959591e2014-05-15 22:26:18 -070043import com.android.internal.R;
Griff Hazenc091ba82014-05-16 10:13:26 -070044import com.android.internal.util.NotificationColorUtil;
Griff Hazen959591e2014-05-15 22:26:18 -070045
Tor Norbyed9273d62013-05-30 15:59:53 -070046import java.lang.annotation.Retention;
47import java.lang.annotation.RetentionPolicy;
Andy Stadler110988c2010-12-03 14:29:16 -080048import java.text.NumberFormat;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050049import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070050import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070051import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070052import java.util.List;
Joe Onorato561d3852010-11-20 18:09:34 -080053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054/**
55 * A class that represents how a persistent notification is to be presented to
56 * the user using the {@link android.app.NotificationManager}.
57 *
Joe Onoratocb109a02011-01-18 17:57:41 -080058 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
59 * easier to construct Notifications.</p>
60 *
Joe Fernandez558459f2011-10-13 16:47:36 -070061 * <div class="special reference">
62 * <h3>Developer Guides</h3>
63 * <p>For a guide to creating notifications, read the
64 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
65 * developer guide.</p>
66 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 */
68public class Notification implements Parcelable
69{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040070 private static final String TAG = "Notification";
71
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 /**
73 * Use all default values (where applicable).
74 */
75 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050076
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 /**
78 * Use the default notification sound. This will ignore any given
79 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050080 *
81
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050083 */
84
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080085 public static final int DEFAULT_SOUND = 1;
86
87 /**
88 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -050089 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -070090 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050091 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050093 */
94
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 /**
98 * Use the default notification lights. This will ignore the
99 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
100 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500101 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500103 */
104
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500106
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500108 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800109 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500110 * Default value: {@link System#currentTimeMillis() Now}.
111 *
112 * Choose a timestamp that will be most relevant to the user. For most finite events, this
113 * corresponds to the time the event happened (or will happen, in the case of events that have
114 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800115 * timestamped according to when the activity began.
116 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500117 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800118 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500119 * <ul>
120 * <li>Notification of a new chat message should be stamped when the message was received.</li>
121 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
122 * <li>Notification of a completed file download should be stamped when the download finished.</li>
123 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
124 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
125 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800126 * </ul>
127 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 */
129 public long when;
130
131 /**
132 * The resource id of a drawable to use as the icon in the status bar.
Daniel Sandlerd952dae2011-02-07 16:33:36 -0500133 * This is required; notifications with an invalid icon resource will not be shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 */
135 public int icon;
136
137 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800138 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
139 * leave it at its default value of 0.
140 *
141 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700142 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800143 */
144 public int iconLevel;
145
146 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500147 * The number of events that this notification represents. For example, in a new mail
148 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800149 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500150 * The system may or may not use this field to modify the appearance of the notification. For
151 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
152 * superimposed over the icon in the status bar. Starting with
153 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
154 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800155 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500156 * If the number is 0 or negative, it is never shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 */
158 public int number;
159
160 /**
161 * The intent to execute when the expanded status entry is clicked. If
162 * this is an activity, it must include the
163 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800164 * that you take care of task management as described in the
165 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800166 * Stack</a> document. In particular, make sure to read the notification section
167 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
168 * Notifications</a> for the correct ways to launch an application from a
169 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 */
171 public PendingIntent contentIntent;
172
173 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500174 * The intent to execute when the notification is explicitly dismissed by the user, either with
175 * the "Clear All" button or by swiping it away individually.
176 *
177 * This probably shouldn't be launching an activity since several of those will be sent
178 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 */
180 public PendingIntent deleteIntent;
181
182 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700183 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800184 *
185 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400186 */
187 public PendingIntent fullScreenIntent;
188
189 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 * Text to scroll across the screen when this item is added to
Joe Onoratoef1e7762010-09-17 18:38:38 -0400191 * the status bar on large and smaller devices.
192 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800193 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 */
195 public CharSequence tickerText;
196
197 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800198 * The view to show as the ticker in the status bar when the notification
199 * is posted.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400200 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800201 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400202
203 /**
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400204 * The view that will represent this notification in the expanded status bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 */
206 public RemoteViews contentView;
207
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400208 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400209 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400210 * opportunity to show more detail. The system UI may choose to show this
211 * instead of the normal content view at its discretion.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400212 */
213 public RemoteViews bigContentView;
214
Chris Wren8fd39ec2014-02-27 17:43:26 -0500215
216 /**
217 * @hide
218 * A medium-format version of {@link #contentView}, giving the Notification an
219 * opportunity to add action buttons to contentView. The system UI may
220 * choose to show this as a popup notification at its discretion.
221 */
222 public RemoteViews headsUpContentView;
223
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400224 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800225 * The bitmap that may escape the bounds of the panel and bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800227 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228
229 /**
230 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500231 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500233 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 * </p>
235 */
236 public Uri sound;
237
238 /**
239 * Use this constant as the value for audioStreamType to request that
240 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700241 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800242 */
243 public static final int STREAM_DEFAULT = -1;
244
245 /**
246 * The audio stream type to use when playing the sound.
247 * Should be one of the STREAM_ constants from
248 * {@link android.media.AudioManager}.
249 */
250 public int audioStreamType = STREAM_DEFAULT;
251
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500253 * The pattern with which to vibrate.
254 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800255 * <p>
256 * To vibrate the default pattern, see {@link #defaults}.
257 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500258 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 * @see android.os.Vibrator#vibrate(long[],int)
260 */
261 public long[] vibrate;
262
263 /**
264 * The color of the led. The hardware will do its best approximation.
265 *
266 * @see #FLAG_SHOW_LIGHTS
267 * @see #flags
268 */
269 public int ledARGB;
270
271 /**
272 * The number of milliseconds for the LED to be on while it's flashing.
273 * The hardware will do its best approximation.
274 *
275 * @see #FLAG_SHOW_LIGHTS
276 * @see #flags
277 */
278 public int ledOnMS;
279
280 /**
281 * The number of milliseconds for the LED to be off while it's flashing.
282 * The hardware will do its best approximation.
283 *
284 * @see #FLAG_SHOW_LIGHTS
285 * @see #flags
286 */
287 public int ledOffMS;
288
289 /**
290 * Specifies which values should be taken from the defaults.
291 * <p>
292 * To set, OR the desired from {@link #DEFAULT_SOUND},
293 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
294 * values, use {@link #DEFAULT_ALL}.
295 * </p>
296 */
297 public int defaults;
298
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800299 /**
300 * Bit to be bitwise-ored into the {@link #flags} field that should be
301 * set if you want the LED on for this notification.
302 * <ul>
303 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
304 * or 0 for both ledOnMS and ledOffMS.</li>
305 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
306 * <li>To flash the LED, pass the number of milliseconds that it should
307 * be on and off to ledOnMS and ledOffMS.</li>
308 * </ul>
309 * <p>
310 * Since hardware varies, you are not guaranteed that any of the values
311 * you pass are honored exactly. Use the system defaults (TODO) if possible
312 * because they will be set to values that work on any given hardware.
313 * <p>
314 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500315 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 */
317 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
318
319 /**
320 * Bit to be bitwise-ored into the {@link #flags} field that should be
321 * set if this notification is in reference to something that is ongoing,
322 * like a phone call. It should not be set if this notification is in
323 * reference to something that happened at a particular point in time,
324 * like a missed phone call.
325 */
326 public static final int FLAG_ONGOING_EVENT = 0x00000002;
327
328 /**
329 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700330 * the audio will be repeated until the notification is
331 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800332 */
333 public static final int FLAG_INSISTENT = 0x00000004;
334
335 /**
336 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700337 * set if you would only like the sound, vibrate and ticker to be played
338 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800339 */
340 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
341
342 /**
343 * Bit to be bitwise-ored into the {@link #flags} field that should be
344 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500345 * user.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500346
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 */
348 public static final int FLAG_AUTO_CANCEL = 0x00000010;
349
350 /**
351 * Bit to be bitwise-ored into the {@link #flags} field that should be
352 * set if the notification should not be canceled when the user clicks
353 * the Clear all button.
354 */
355 public static final int FLAG_NO_CLEAR = 0x00000020;
356
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700357 /**
358 * Bit to be bitwise-ored into the {@link #flags} field that should be
359 * set if this notification represents a currently running service. This
360 * will normally be set for you by {@link Service#startForeground}.
361 */
362 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
363
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400364 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500365 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800366 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500367 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400368 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500369 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400370
Griff Hazendfcb0802014-02-11 12:00:00 -0800371 /**
372 * Bit to be bitswise-ored into the {@link #flags} field that should be
373 * set if this notification is relevant to the current device only
374 * and it is not recommended that it bridge to other devices.
375 */
376 public static final int FLAG_LOCAL_ONLY = 0x00000100;
377
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700378 /**
379 * Bit to be bitswise-ored into the {@link #flags} field that should be
380 * set if this notification is the group summary for a group of notifications.
381 * Grouped notifications may display in a cluster or stack on devices which
382 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
383 */
384 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 public int flags;
387
Tor Norbyed9273d62013-05-30 15:59:53 -0700388 /** @hide */
389 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
390 @Retention(RetentionPolicy.SOURCE)
391 public @interface Priority {}
392
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800393 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500394 * Default notification {@link #priority}. If your application does not prioritize its own
395 * notifications, use this value for all notifications.
396 */
397 public static final int PRIORITY_DEFAULT = 0;
398
399 /**
400 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
401 * items smaller, or at a different position in the list, compared with your app's
402 * {@link #PRIORITY_DEFAULT} items.
403 */
404 public static final int PRIORITY_LOW = -1;
405
406 /**
407 * Lowest {@link #priority}; these items might not be shown to the user except under special
408 * circumstances, such as detailed notification logs.
409 */
410 public static final int PRIORITY_MIN = -2;
411
412 /**
413 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
414 * show these items larger, or at a different position in notification lists, compared with
415 * your app's {@link #PRIORITY_DEFAULT} items.
416 */
417 public static final int PRIORITY_HIGH = 1;
418
419 /**
420 * Highest {@link #priority}, for your application's most important items that require the
421 * user's prompt attention or input.
422 */
423 public static final int PRIORITY_MAX = 2;
424
425 /**
426 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800427 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500428 * Priority is an indication of how much of the user's valuable attention should be consumed by
429 * this notification. Low-priority notifications may be hidden from the user in certain
430 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500431 * system will make a determination about how to interpret this priority when presenting
432 * the notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500433 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700434 @Priority
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500435 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800436
Dan Sandler26e81cf2014-05-06 10:01:27 -0400437 /**
438 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
439 * to be applied by the standard Style templates when presenting this notification.
440 *
441 * The current template design constructs a colorful header image by overlaying the
442 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
443 * ignored.
444 */
445 public int color = COLOR_DEFAULT;
446
447 /**
448 * Special value of {@link #color} telling the system not to decorate this notification with
449 * any special color but instead use default colors when presenting this notification.
450 */
451 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600452
453 /**
454 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
455 * the notification's presence and contents in untrusted situations (namely, on the secure
456 * lockscreen).
457 *
458 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
459 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
460 * shown in all situations, but the contents are only available if the device is unlocked for
461 * the appropriate user.
462 *
463 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
464 * can be read even in an "insecure" context (that is, above a secure lockscreen).
465 * To modify the public version of this notification—for example, to redact some portions—see
466 * {@link Builder#setPublicVersion(Notification)}.
467 *
468 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
469 * and ticker until the user has bypassed the lockscreen.
470 */
471 public int visibility;
472
473 public static final int VISIBILITY_PUBLIC = 1;
474 public static final int VISIBILITY_PRIVATE = 0;
475 public static final int VISIBILITY_SECRET = -1;
476
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500477 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400478 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500479 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400480 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500481
482 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400483 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500484 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400485 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500486
487 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400488 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500489 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400490 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500491
492 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400493 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500494 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400495 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500496
497 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400498 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500499 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400500 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500501
502 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400503 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500504 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400505 public static final String CATEGORY_ALARM = "alarm";
506
507 /**
508 * Notification category: progress of a long-running background operation.
509 */
510 public static final String CATEGORY_PROGRESS = "progress";
511
512 /**
513 * Notification category: social network or sharing update.
514 */
515 public static final String CATEGORY_SOCIAL = "social";
516
517 /**
518 * Notification category: error in background operation or authentication status.
519 */
520 public static final String CATEGORY_ERROR = "err";
521
522 /**
523 * Notification category: media transport control for playback.
524 */
525 public static final String CATEGORY_TRANSPORT = "transport";
526
527 /**
528 * Notification category: system or device status update. Reserved for system use.
529 */
530 public static final String CATEGORY_SYSTEM = "sys";
531
532 /**
533 * Notification category: indication of running background service.
534 */
535 public static final String CATEGORY_SERVICE = "service";
536
537 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400538 * Notification category: a specific, timely recommendation for a single thing.
539 * For example, a news app might want to recommend a news story it believes the user will
540 * want to read next.
541 */
542 public static final String CATEGORY_RECOMMENDATION = "recommendation";
543
544 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400545 * Notification category: ongoing information about device or contextual status.
546 */
547 public static final String CATEGORY_STATUS = "status";
548
549 /**
550 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
551 * that best describes this Notification. May be used by the system for ranking and filtering.
552 */
553 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500554
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700555 private String mGroupKey;
556
557 /**
558 * Get the key used to group this notification into a cluster or stack
559 * with other notifications on devices which support such rendering.
560 */
561 public String getGroup() {
562 return mGroupKey;
563 }
564
565 private String mSortKey;
566
567 /**
568 * Get a sort key that orders this notification among other notifications from the
569 * same package. This can be useful if an external sort was already applied and an app
570 * would like to preserve this. Notifications will be sorted lexicographically using this
571 * value, although providing different priorities in addition to providing sort key may
572 * cause this value to be ignored.
573 *
574 * <p>This sort key can also be used to order members of a notification group. See
575 * {@link Builder#setGroup}.
576 *
577 * @see String#compareTo(String)
578 */
579 public String getSortKey() {
580 return mSortKey;
581 }
582
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500583 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400584 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400585 * <p>
586 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
587 * APIs, and are intended to be used by
588 * {@link android.service.notification.NotificationListenerService} implementations to extract
589 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500590 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400591 public Bundle extras = new Bundle();
592
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400593 /**
594 * {@link #extras} key: this is the title of the notification,
595 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
596 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500597 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400598
599 /**
600 * {@link #extras} key: this is the title of the notification when shown in expanded form,
601 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
602 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400603 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400604
605 /**
606 * {@link #extras} key: this is the main text payload, as supplied to
607 * {@link Builder#setContentText(CharSequence)}.
608 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500609 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400610
611 /**
612 * {@link #extras} key: this is a third line of text, as supplied to
613 * {@link Builder#setSubText(CharSequence)}.
614 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400615 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400616
617 /**
618 * {@link #extras} key: this is a small piece of additional text as supplied to
619 * {@link Builder#setContentInfo(CharSequence)}.
620 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400621 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400622
623 /**
624 * {@link #extras} key: this is a line of summary information intended to be shown
625 * alongside expanded notifications, as supplied to (e.g.)
626 * {@link BigTextStyle#setSummaryText(CharSequence)}.
627 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400628 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400629
630 /**
631 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
632 * supplied to {@link Builder#setSmallIcon(int)}.
633 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500634 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400635
636 /**
637 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
638 * notification payload, as
639 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
640 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400641 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400642
643 /**
644 * {@link #extras} key: this is a bitmap to be used instead of the one from
645 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
646 * shown in its expanded form, as supplied to
647 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
648 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400649 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400650
651 /**
652 * {@link #extras} key: this is the progress value supplied to
653 * {@link Builder#setProgress(int, int, boolean)}.
654 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400655 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400656
657 /**
658 * {@link #extras} key: this is the maximum value supplied to
659 * {@link Builder#setProgress(int, int, boolean)}.
660 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400661 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400662
663 /**
664 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
665 * {@link Builder#setProgress(int, int, boolean)}.
666 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400667 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400668
669 /**
670 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
671 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
672 * {@link Builder#setUsesChronometer(boolean)}.
673 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400674 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400675
676 /**
677 * {@link #extras} key: whether {@link #when} should be shown,
678 * as supplied to {@link Builder#setShowWhen(boolean)}.
679 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400680 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400681
682 /**
683 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
684 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
685 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400686 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400687
688 /**
689 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
690 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
691 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400692 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400693
694 /**
695 * {@link #extras} key: A string representing the name of the specific
696 * {@link android.app.Notification.Style} used to create this notification.
697 */
Chris Wren91ad5632013-06-05 15:05:57 -0400698 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400699
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400700 /**
701 * {@link #extras} key: An array of people that this notification relates to, specified
702 * by contacts provider contact URI.
703 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400704 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500705
706 /**
Scott Greenwald9a05b312013-06-28 00:37:54 -0400707 * @hide
Chris Wrenf9536642014-04-17 10:01:54 -0400708 * Extra added by NotificationManagerService to indicate whether
709 * the Notifications's score has been modified.
Scott Greenwald9a05b312013-06-28 00:37:54 -0400710 */
711 public static final String EXTRA_SCORE_MODIFIED = "android.scoreModified";
712
713 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400714 * Not used.
Chris Wren51c75102013-07-16 20:49:17 -0400715 * @hide
716 */
717 public static final String EXTRA_AS_HEADS_UP = "headsup";
718
719 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400720 * Allow certain system-generated notifications to appear before the device is provisioned.
721 * Only available to notifications coming from the android package.
722 * @hide
723 */
724 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
725
726 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700727 * {@link #extras} key: A
728 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
729 * in the background when the notification is selected. The URI must point to an image stream
730 * suitable for passing into
731 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
732 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
733 * URI used for this purpose must require no permissions to read the image data.
734 */
735 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
736
737 /**
Dan Sandler842dd772014-05-15 09:36:47 -0400738 * {@link #extras} key: A
739 * {@link android.media.session.MediaSessionToken} associated with a
740 * {@link android.app.Notification.MediaStyle} notification.
741 */
742 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
743
744 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400745 * Value for {@link #EXTRA_AS_HEADS_UP}.
Chris Wren51c75102013-07-16 20:49:17 -0400746 * @hide
747 */
748 public static final int HEADS_UP_NEVER = 0;
749
750 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400751 * Default value for {@link #EXTRA_AS_HEADS_UP}.
Chris Wren51c75102013-07-16 20:49:17 -0400752 * @hide
753 */
754 public static final int HEADS_UP_ALLOWED = 1;
755
756 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400757 * Value for {@link #EXTRA_AS_HEADS_UP}.
Chris Wren51c75102013-07-16 20:49:17 -0400758 * @hide
759 */
760 public static final int HEADS_UP_REQUESTED = 2;
761
762 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400763 * Structure to encapsulate a named action that can be shown as part of this notification.
764 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
765 * selected by the user.
766 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -0700767 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
768 * or {@link Notification.Builder#addAction(Notification.Action)}
769 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400770 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500771 public static class Action implements Parcelable {
Griff Hazen959591e2014-05-15 22:26:18 -0700772 private final Bundle mExtras;
Griff Hazen61a9e862014-05-22 16:05:19 -0700773 private final RemoteInput[] mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700774
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400775 /**
776 * Small icon representing the action.
777 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400778 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700779
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400780 /**
781 * Title of the action.
782 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400783 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700784
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400785 /**
786 * Intent to send when the user invokes this action. May be null, in which case the action
787 * may be rendered in a disabled presentation by the system UI.
788 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400789 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -0700790
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400791 private Action(Parcel in) {
792 icon = in.readInt();
793 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
794 if (in.readInt() == 1) {
795 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
796 }
Griff Hazen959591e2014-05-15 22:26:18 -0700797 mExtras = in.readBundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700798 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400799 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700800
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400801 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700802 * Use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400803 */
804 public Action(int icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700805 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700806 }
807
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700808 private Action(int icon, CharSequence title, PendingIntent intent, Bundle extras,
809 RemoteInput[] remoteInputs) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400810 this.icon = icon;
811 this.title = title;
812 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -0700813 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700814 this.mRemoteInputs = remoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700815 }
816
817 /**
818 * Get additional metadata carried around with this Action.
819 */
820 public Bundle getExtras() {
821 return mExtras;
822 }
823
824 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700825 * Get the list of inputs to be collected from the user when this action is sent.
826 * May return null if no remote inputs were added.
827 */
828 public RemoteInput[] getRemoteInputs() {
829 return mRemoteInputs;
830 }
831
832 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700833 * Builder class for {@link Action} objects.
834 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700835 public static final class Builder {
Griff Hazen959591e2014-05-15 22:26:18 -0700836 private final int mIcon;
837 private final CharSequence mTitle;
838 private final PendingIntent mIntent;
839 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700840 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -0700841
842 /**
843 * Construct a new builder for {@link Action} object.
844 * @param icon icon to show for this action
845 * @param title the title of the action
846 * @param intent the {@link PendingIntent} to fire when users trigger this action
847 */
848 public Builder(int icon, CharSequence title, PendingIntent intent) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700849 this(icon, title, intent, new Bundle(), null);
Griff Hazen959591e2014-05-15 22:26:18 -0700850 }
851
852 /**
853 * Construct a new builder for {@link Action} object using the fields from an
854 * {@link Action}.
855 * @param action the action to read fields from.
856 */
857 public Builder(Action action) {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700858 this(action.icon, action.title, action.actionIntent, new Bundle(action.mExtras),
859 action.getRemoteInputs());
Griff Hazen959591e2014-05-15 22:26:18 -0700860 }
861
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700862 private Builder(int icon, CharSequence title, PendingIntent intent, Bundle extras,
863 RemoteInput[] remoteInputs) {
Griff Hazen959591e2014-05-15 22:26:18 -0700864 mIcon = icon;
865 mTitle = title;
866 mIntent = intent;
867 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700868 if (remoteInputs != null) {
869 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
870 Collections.addAll(mRemoteInputs, remoteInputs);
871 }
Griff Hazen959591e2014-05-15 22:26:18 -0700872 }
873
874 /**
875 * Merge additional metadata into this builder.
876 *
877 * <p>Values within the Bundle will replace existing extras values in this Builder.
878 *
879 * @see Notification.Action#extras
880 */
881 public Builder addExtras(Bundle extras) {
882 if (extras != null) {
883 mExtras.putAll(extras);
884 }
885 return this;
886 }
887
888 /**
889 * Get the metadata Bundle used by this Builder.
890 *
891 * <p>The returned Bundle is shared with this Builder.
892 */
893 public Bundle getExtras() {
894 return mExtras;
895 }
896
897 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700898 * Add an input to be collected from the user when this action is sent.
899 * Response values can be retrieved from the fired intent by using the
900 * {@link RemoteInput#getResultsFromIntent} function.
901 * @param remoteInput a {@link RemoteInput} to add to the action
902 * @return this object for method chaining
903 */
904 public Builder addRemoteInput(RemoteInput remoteInput) {
905 if (mRemoteInputs == null) {
906 mRemoteInputs = new ArrayList<RemoteInput>();
907 }
908 mRemoteInputs.add(remoteInput);
909 return this;
910 }
911
912 /**
913 * Apply an extender to this action builder. Extenders may be used to add
914 * metadata or change options on this builder.
915 */
Griff Hazen61a9e862014-05-22 16:05:19 -0700916 public Builder extend(Extender extender) {
917 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700918 return this;
919 }
920
921 /**
Griff Hazen959591e2014-05-15 22:26:18 -0700922 * Combine all of the options that have been set and return a new {@link Action}
923 * object.
924 * @return the built action
925 */
926 public Action build() {
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700927 RemoteInput[] remoteInputs = mRemoteInputs != null
928 ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null;
929 return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs);
Griff Hazen959591e2014-05-15 22:26:18 -0700930 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400931 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400932
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400933 @Override
934 public Action clone() {
935 return new Action(
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700936 icon,
937 title,
938 actionIntent, // safe to alias
939 new Bundle(mExtras),
940 getRemoteInputs());
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400941 }
942 @Override
943 public int describeContents() {
944 return 0;
945 }
946 @Override
947 public void writeToParcel(Parcel out, int flags) {
948 out.writeInt(icon);
949 TextUtils.writeToParcel(title, out, flags);
950 if (actionIntent != null) {
951 out.writeInt(1);
952 actionIntent.writeToParcel(out, flags);
953 } else {
954 out.writeInt(0);
955 }
Griff Hazen959591e2014-05-15 22:26:18 -0700956 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700957 out.writeTypedArray(mRemoteInputs, flags);
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400958 }
Griff Hazen959591e2014-05-15 22:26:18 -0700959 public static final Parcelable.Creator<Action> CREATOR =
960 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400961 public Action createFromParcel(Parcel in) {
962 return new Action(in);
963 }
964 public Action[] newArray(int size) {
965 return new Action[size];
966 }
967 };
Griff Hazen61a9e862014-05-22 16:05:19 -0700968
969 /**
970 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
971 * metadata or change options on an action builder.
972 */
973 public interface Extender {
974 /**
975 * Apply this extender to a notification action builder.
976 * @param builder the builder to be modified.
977 * @return the build object for chaining.
978 */
979 public Builder extend(Builder builder);
980 }
981
982 /**
983 * Wearable extender for notification actions. To add extensions to an action,
984 * create a new {@link android.app.Notification.Action.WearableExtender} object using
985 * the {@code WearableExtender()} constructor and apply it to a
986 * {@link android.app.Notification.Action.Builder} using
987 * {@link android.app.Notification.Action.Builder#extend}.
988 *
989 * <pre class="prettyprint">
990 * Notification.Action action = new Notification.Action.Builder(
991 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -0700992 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -0700993 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -0700994 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -0700995 */
996 public static final class WearableExtender implements Extender {
997 /** Notification action extra which contains wearable extensions */
998 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
999
1000 private static final String KEY_FLAGS = "flags";
1001
1002 // Flags bitwise-ored to mFlags
1003 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
1004
1005 // Default value for flags integer
1006 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1007
1008 private int mFlags = DEFAULT_FLAGS;
1009
1010 /**
1011 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1012 * options.
1013 */
1014 public WearableExtender() {
1015 }
1016
1017 /**
1018 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1019 * wearable options present in an existing notification action.
1020 * @param action the notification action to inspect.
1021 */
1022 public WearableExtender(Action action) {
1023 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1024 if (wearableBundle != null) {
1025 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
1026 }
1027 }
1028
1029 /**
1030 * Apply wearable extensions to a notification action that is being built. This is
1031 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1032 * method of {@link android.app.Notification.Action.Builder}.
1033 */
1034 @Override
1035 public Action.Builder extend(Action.Builder builder) {
1036 Bundle wearableBundle = new Bundle();
1037
1038 if (mFlags != DEFAULT_FLAGS) {
1039 wearableBundle.putInt(KEY_FLAGS, mFlags);
1040 }
1041
1042 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1043 return builder;
1044 }
1045
1046 @Override
1047 public WearableExtender clone() {
1048 WearableExtender that = new WearableExtender();
1049 that.mFlags = this.mFlags;
1050 return that;
1051 }
1052
1053 /**
1054 * Set whether this action is available when the wearable device is not connected to
1055 * a companion device. The user can still trigger this action when the wearable device is
1056 * offline, but a visual hint will indicate that the action may not be available.
1057 * Defaults to true.
1058 */
1059 public WearableExtender setAvailableOffline(boolean availableOffline) {
1060 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1061 return this;
1062 }
1063
1064 /**
1065 * Get whether this action is available when the wearable device is not connected to
1066 * a companion device. The user can still trigger this action when the wearable device is
1067 * offline, but a visual hint will indicate that the action may not be available.
1068 * Defaults to true.
1069 */
1070 public boolean isAvailableOffline() {
1071 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1072 }
1073
1074 private void setFlag(int mask, boolean value) {
1075 if (value) {
1076 mFlags |= mask;
1077 } else {
1078 mFlags &= ~mask;
1079 }
1080 }
1081 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001082 }
1083
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001084 /**
1085 * Array of all {@link Action} structures attached to this notification by
1086 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1087 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1088 * interface for invoking actions.
1089 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001090 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001091
1092 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001093 * Replacement version of this notification whose content will be shown
1094 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1095 * and {@link #VISIBILITY_PUBLIC}.
1096 */
1097 public Notification publicVersion;
1098
1099 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001100 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001101 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 */
1103 public Notification()
1104 {
1105 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001106 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001107 }
1108
1109 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 * @hide
1111 */
1112 public Notification(Context context, int icon, CharSequence tickerText, long when,
1113 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1114 {
1115 this.when = when;
1116 this.icon = icon;
1117 this.tickerText = tickerText;
1118 setLatestEventInfo(context, contentTitle, contentText,
1119 PendingIntent.getActivity(context, 0, contentIntent, 0));
1120 }
1121
1122 /**
1123 * Constructs a Notification object with the information needed to
1124 * have a status bar icon without the standard expanded view.
1125 *
1126 * @param icon The resource id of the icon to put in the status bar.
1127 * @param tickerText The text that flows by in the status bar when the notification first
1128 * activates.
1129 * @param when The time to show in the time field. In the System.currentTimeMillis
1130 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001131 *
1132 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001133 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001134 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 public Notification(int icon, CharSequence tickerText, long when)
1136 {
1137 this.icon = icon;
1138 this.tickerText = tickerText;
1139 this.when = when;
1140 }
1141
1142 /**
1143 * Unflatten the notification from a parcel.
1144 */
1145 public Notification(Parcel parcel)
1146 {
1147 int version = parcel.readInt();
1148
1149 when = parcel.readLong();
1150 icon = parcel.readInt();
1151 number = parcel.readInt();
1152 if (parcel.readInt() != 0) {
1153 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1154 }
1155 if (parcel.readInt() != 0) {
1156 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1157 }
1158 if (parcel.readInt() != 0) {
1159 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1160 }
1161 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001162 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001163 }
1164 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001165 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1166 }
Joe Onorato561d3852010-11-20 18:09:34 -08001167 if (parcel.readInt() != 0) {
1168 largeIcon = Bitmap.CREATOR.createFromParcel(parcel);
1169 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001170 defaults = parcel.readInt();
1171 flags = parcel.readInt();
1172 if (parcel.readInt() != 0) {
1173 sound = Uri.CREATOR.createFromParcel(parcel);
1174 }
1175
1176 audioStreamType = parcel.readInt();
1177 vibrate = parcel.createLongArray();
1178 ledARGB = parcel.readInt();
1179 ledOnMS = parcel.readInt();
1180 ledOffMS = parcel.readInt();
1181 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001182
1183 if (parcel.readInt() != 0) {
1184 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1185 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001186
1187 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001188
John Spurlockfd7f1e02014-03-18 16:41:57 -04001189 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001190
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001191 mGroupKey = parcel.readString();
1192
1193 mSortKey = parcel.readString();
1194
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001195 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001196
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001197 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1198
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001199 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001200 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1201 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001202
Chris Wren8fd39ec2014-02-27 17:43:26 -05001203 if (parcel.readInt() != 0) {
1204 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1205 }
1206
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001207 visibility = parcel.readInt();
1208
1209 if (parcel.readInt() != 0) {
1210 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1211 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001212
1213 color = parcel.readInt();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001214 }
1215
Andy Stadler110988c2010-12-03 14:29:16 -08001216 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001217 public Notification clone() {
1218 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001219 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001220 return that;
1221 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001222
Daniel Sandler1a497d32013-04-18 14:52:45 -04001223 /**
1224 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1225 * of this into that.
1226 * @hide
1227 */
1228 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001229 that.when = this.when;
1230 that.icon = this.icon;
1231 that.number = this.number;
1232
1233 // PendingIntents are global, so there's no reason (or way) to clone them.
1234 that.contentIntent = this.contentIntent;
1235 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001236 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001237
1238 if (this.tickerText != null) {
1239 that.tickerText = this.tickerText.toString();
1240 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001241 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001242 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001243 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001244 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001245 that.contentView = this.contentView.clone();
1246 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001247 if (heavy && this.largeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08001248 that.largeIcon = Bitmap.createBitmap(this.largeIcon);
1249 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001250 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001251 that.sound = this.sound; // android.net.Uri is immutable
1252 that.audioStreamType = this.audioStreamType;
1253
1254 final long[] vibrate = this.vibrate;
1255 if (vibrate != null) {
1256 final int N = vibrate.length;
1257 final long[] vib = that.vibrate = new long[N];
1258 System.arraycopy(vibrate, 0, vib, 0, N);
1259 }
1260
1261 that.ledARGB = this.ledARGB;
1262 that.ledOnMS = this.ledOnMS;
1263 that.ledOffMS = this.ledOffMS;
1264 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001265
Joe Onorato18e69df2010-05-17 22:26:12 -07001266 that.flags = this.flags;
1267
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001268 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001269
John Spurlockfd7f1e02014-03-18 16:41:57 -04001270 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001271
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001272 that.mGroupKey = this.mGroupKey;
1273
1274 that.mSortKey = this.mSortKey;
1275
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001276 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001277 try {
1278 that.extras = new Bundle(this.extras);
1279 // will unparcel
1280 that.extras.size();
1281 } catch (BadParcelableException e) {
1282 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1283 that.extras = null;
1284 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001285 }
1286
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001287 if (this.actions != null) {
1288 that.actions = new Action[this.actions.length];
1289 for(int i=0; i<this.actions.length; i++) {
1290 that.actions[i] = this.actions[i].clone();
1291 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001292 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001293
Daniel Sandler1a497d32013-04-18 14:52:45 -04001294 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001295 that.bigContentView = this.bigContentView.clone();
1296 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001297
Chris Wren8fd39ec2014-02-27 17:43:26 -05001298 if (heavy && this.headsUpContentView != null) {
1299 that.headsUpContentView = this.headsUpContentView.clone();
1300 }
1301
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001302 that.visibility = this.visibility;
1303
1304 if (this.publicVersion != null) {
1305 that.publicVersion = new Notification();
1306 this.publicVersion.cloneInto(that.publicVersion, heavy);
1307 }
1308
Dan Sandler26e81cf2014-05-06 10:01:27 -04001309 that.color = this.color;
1310
Daniel Sandler1a497d32013-04-18 14:52:45 -04001311 if (!heavy) {
1312 that.lightenPayload(); // will clean out extras
1313 }
1314 }
1315
1316 /**
1317 * Removes heavyweight parts of the Notification object for archival or for sending to
1318 * listeners when the full contents are not necessary.
1319 * @hide
1320 */
1321 public final void lightenPayload() {
1322 tickerView = null;
1323 contentView = null;
1324 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001325 headsUpContentView = null;
Daniel Sandler1a497d32013-04-18 14:52:45 -04001326 largeIcon = null;
1327 if (extras != null) {
1328 extras.remove(Notification.EXTRA_LARGE_ICON);
1329 extras.remove(Notification.EXTRA_LARGE_ICON_BIG);
1330 extras.remove(Notification.EXTRA_PICTURE);
1331 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001332 }
1333
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001334 /**
1335 * Make sure this CharSequence is safe to put into a bundle, which basically
1336 * means it had better not be some custom Parcelable implementation.
1337 * @hide
1338 */
1339 public static CharSequence safeCharSequence(CharSequence cs) {
1340 if (cs instanceof Parcelable) {
1341 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
1342 + " instance is a custom Parcelable and not allowed in Notification");
1343 return cs.toString();
1344 }
1345
1346 return cs;
1347 }
1348
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 public int describeContents() {
1350 return 0;
1351 }
1352
1353 /**
1354 * Flatten this notification from a parcel.
1355 */
1356 public void writeToParcel(Parcel parcel, int flags)
1357 {
1358 parcel.writeInt(1);
1359
1360 parcel.writeLong(when);
1361 parcel.writeInt(icon);
1362 parcel.writeInt(number);
1363 if (contentIntent != null) {
1364 parcel.writeInt(1);
1365 contentIntent.writeToParcel(parcel, 0);
1366 } else {
1367 parcel.writeInt(0);
1368 }
1369 if (deleteIntent != null) {
1370 parcel.writeInt(1);
1371 deleteIntent.writeToParcel(parcel, 0);
1372 } else {
1373 parcel.writeInt(0);
1374 }
1375 if (tickerText != null) {
1376 parcel.writeInt(1);
1377 TextUtils.writeToParcel(tickerText, parcel, flags);
1378 } else {
1379 parcel.writeInt(0);
1380 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001381 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04001382 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08001383 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001384 } else {
1385 parcel.writeInt(0);
1386 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001387 if (contentView != null) {
1388 parcel.writeInt(1);
1389 contentView.writeToParcel(parcel, 0);
1390 } else {
1391 parcel.writeInt(0);
1392 }
Joe Onorato561d3852010-11-20 18:09:34 -08001393 if (largeIcon != null) {
1394 parcel.writeInt(1);
1395 largeIcon.writeToParcel(parcel, 0);
1396 } else {
1397 parcel.writeInt(0);
1398 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001399
1400 parcel.writeInt(defaults);
1401 parcel.writeInt(this.flags);
1402
1403 if (sound != null) {
1404 parcel.writeInt(1);
1405 sound.writeToParcel(parcel, 0);
1406 } else {
1407 parcel.writeInt(0);
1408 }
1409 parcel.writeInt(audioStreamType);
1410 parcel.writeLongArray(vibrate);
1411 parcel.writeInt(ledARGB);
1412 parcel.writeInt(ledOnMS);
1413 parcel.writeInt(ledOffMS);
1414 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001415
1416 if (fullScreenIntent != null) {
1417 parcel.writeInt(1);
1418 fullScreenIntent.writeToParcel(parcel, 0);
1419 } else {
1420 parcel.writeInt(0);
1421 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001422
1423 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08001424
John Spurlockfd7f1e02014-03-18 16:41:57 -04001425 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08001426
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001427 parcel.writeString(mGroupKey);
1428
1429 parcel.writeString(mSortKey);
1430
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001431 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001432
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001433 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001434
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001435 if (bigContentView != null) {
1436 parcel.writeInt(1);
1437 bigContentView.writeToParcel(parcel, 0);
1438 } else {
1439 parcel.writeInt(0);
1440 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001441
Chris Wren8fd39ec2014-02-27 17:43:26 -05001442 if (headsUpContentView != null) {
1443 parcel.writeInt(1);
1444 headsUpContentView.writeToParcel(parcel, 0);
1445 } else {
1446 parcel.writeInt(0);
1447 }
1448
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001449 parcel.writeInt(visibility);
1450
1451 if (publicVersion != null) {
1452 parcel.writeInt(1);
1453 publicVersion.writeToParcel(parcel, 0);
1454 } else {
1455 parcel.writeInt(0);
1456 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001457
1458 parcel.writeInt(color);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001459 }
1460
1461 /**
1462 * Parcelable.Creator that instantiates Notification objects
1463 */
1464 public static final Parcelable.Creator<Notification> CREATOR
1465 = new Parcelable.Creator<Notification>()
1466 {
1467 public Notification createFromParcel(Parcel parcel)
1468 {
1469 return new Notification(parcel);
1470 }
1471
1472 public Notification[] newArray(int size)
1473 {
1474 return new Notification[size];
1475 }
1476 };
1477
1478 /**
1479 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
1480 * layout.
1481 *
1482 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
1483 * in the view.</p>
1484 * @param context The context for your application / activity.
1485 * @param contentTitle The title that goes in the expanded entry.
1486 * @param contentText The text that goes in the expanded entry.
1487 * @param contentIntent The intent to launch when the user clicks the expanded notification.
1488 * If this is an activity, it must include the
1489 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08001490 * that you take care of task management as described in the
1491 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
1492 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001493 *
Joe Onorato46439ce2010-11-19 13:56:21 -08001494 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001495 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001496 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001497 public void setLatestEventInfo(Context context,
1498 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001499 Notification.Builder builder = new Notification.Builder(context);
1500
1501 // First, ensure that key pieces of information that may have been set directly
1502 // are preserved
1503 builder.setWhen(this.when);
1504 builder.setSmallIcon(this.icon);
1505 builder.setPriority(this.priority);
1506 builder.setTicker(this.tickerText);
1507 builder.setNumber(this.number);
1508 builder.mFlags = this.flags;
1509 builder.setSound(this.sound, this.audioStreamType);
1510 builder.setDefaults(this.defaults);
1511 builder.setVibrate(this.vibrate);
1512
1513 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001514 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001515 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001516 }
1517 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001518 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001519 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001520 builder.setContentIntent(contentIntent);
1521 builder.buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001522 }
1523
1524 @Override
1525 public String toString() {
1526 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001527 sb.append("Notification(pri=");
1528 sb.append(priority);
1529 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08001530 if (contentView != null) {
1531 sb.append(contentView.getPackage());
1532 sb.append("/0x");
1533 sb.append(Integer.toHexString(contentView.getLayoutId()));
1534 } else {
1535 sb.append("null");
1536 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001537 // TODO(dsandler): defaults take precedence over local values, so reorder the branches below
Joe Onoratoc9596d62011-01-12 17:03:11 -08001538 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001539 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
1540 sb.append("default");
1541 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001542 int N = this.vibrate.length-1;
1543 sb.append("[");
1544 for (int i=0; i<N; i++) {
1545 sb.append(this.vibrate[i]);
1546 sb.append(',');
1547 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02001548 if (N != -1) {
1549 sb.append(this.vibrate[N]);
1550 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001551 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001552 } else {
1553 sb.append("null");
1554 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001555 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001556 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05001558 } else if (this.sound != null) {
1559 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001560 } else {
1561 sb.append("null");
1562 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001563 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001565 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001566 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04001567 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001568 if (this.category != null) {
1569 sb.append(" category=");
1570 sb.append(this.category);
1571 }
1572 if (this.mGroupKey != null) {
1573 sb.append(" groupKey=");
1574 sb.append(this.mGroupKey);
1575 }
1576 if (this.mSortKey != null) {
1577 sb.append(" sortKey=");
1578 sb.append(this.mSortKey);
1579 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001580 if (actions != null) {
1581 sb.append(" ");
1582 sb.append(actions.length);
1583 sb.append(" action");
1584 if (actions.length > 1) sb.append("s");
1585 }
1586 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001587 return sb.toString();
1588 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001589
Jeff Sharkey6d515712012-09-20 16:06:08 -07001590 /** {@hide} */
1591 public void setUser(UserHandle user) {
Amith Yamasaniecbd68b2012-11-02 12:17:19 -07001592 if (user.getIdentifier() == UserHandle.USER_ALL) {
1593 user = UserHandle.OWNER;
1594 }
Jeff Sharkey6d515712012-09-20 16:06:08 -07001595 if (tickerView != null) {
1596 tickerView.setUser(user);
1597 }
1598 if (contentView != null) {
1599 contentView.setUser(user);
1600 }
1601 if (bigContentView != null) {
1602 bigContentView.setUser(user);
1603 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05001604 if (headsUpContentView != null) {
1605 headsUpContentView.setUser(user);
1606 }
Jeff Sharkey6d515712012-09-20 16:06:08 -07001607 }
1608
Joe Onoratocb109a02011-01-18 17:57:41 -08001609 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001610 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08001611 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001612 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07001613 * content views using the platform's notification layout template. If your app supports
1614 * versions of Android as old as API level 4, you can instead use
1615 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
1616 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
1617 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08001618 *
Scott Main183bf112012-08-13 19:12:13 -07001619 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08001620 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001621 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07001622 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001623 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
1624 * .setContentText(subject)
1625 * .setSmallIcon(R.drawable.new_mail)
1626 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001627 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001628 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08001629 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001630 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -04001631 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001632
Joe Onorato46439ce2010-11-19 13:56:21 -08001633 private Context mContext;
1634
1635 private long mWhen;
1636 private int mSmallIcon;
1637 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08001638 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08001639 private CharSequence mContentTitle;
1640 private CharSequence mContentText;
1641 private CharSequence mContentInfo;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001642 private CharSequence mSubText;
Joe Onorato46439ce2010-11-19 13:56:21 -08001643 private PendingIntent mContentIntent;
1644 private RemoteViews mContentView;
1645 private PendingIntent mDeleteIntent;
1646 private PendingIntent mFullScreenIntent;
1647 private CharSequence mTickerText;
1648 private RemoteViews mTickerView;
1649 private Bitmap mLargeIcon;
1650 private Uri mSound;
1651 private int mAudioStreamType;
1652 private long[] mVibrate;
1653 private int mLedArgb;
1654 private int mLedOnMs;
1655 private int mLedOffMs;
1656 private int mDefaults;
1657 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -07001658 private int mProgressMax;
1659 private int mProgress;
1660 private boolean mProgressIndeterminate;
John Spurlockfd7f1e02014-03-18 16:41:57 -04001661 private String mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001662 private String mGroupKey;
1663 private String mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001664 private Bundle mExtras;
1665 private int mPriority;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001666 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001667 private boolean mUseChronometer;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001668 private Style mStyle;
Daniel Sandler0c890492012-09-12 17:23:10 -07001669 private boolean mShowWhen = true;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001670 private int mVisibility = VISIBILITY_PRIVATE;
1671 private Notification mPublicVersion = null;
Dan Sandler26e81cf2014-05-06 10:01:27 -04001672 private final NotificationColorUtil mColorUtil;
Chris Wrendde75302014-03-26 17:24:15 -04001673 private ArrayList<String> mPeople;
Dan Sandler26e81cf2014-05-06 10:01:27 -04001674 private boolean mPreQuantum;
1675 private int mColor = COLOR_DEFAULT;
Joe Onorato46439ce2010-11-19 13:56:21 -08001676
Joe Onoratocb109a02011-01-18 17:57:41 -08001677 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001678 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08001679 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001680
1681 * <table>
1682 * <tr><th align=right>priority</th>
1683 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
1684 * <tr><th align=right>when</th>
1685 * <td>now ({@link System#currentTimeMillis()})</td></tr>
1686 * <tr><th align=right>audio stream</th>
1687 * <td>{@link #STREAM_DEFAULT}</td></tr>
1688 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08001689 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001690
1691 * @param context
1692 * A {@link Context} that will be used by the Builder to construct the
1693 * RemoteViews. The Context will not be held past the lifetime of this Builder
1694 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08001695 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001696 public Builder(Context context) {
1697 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -08001698
1699 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -08001700 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -08001701 mAudioStreamType = STREAM_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001702 mPriority = PRIORITY_DEFAULT;
Chris Wrendde75302014-03-26 17:24:15 -04001703 mPeople = new ArrayList<String>();
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01001704
Dan Sandler26e81cf2014-05-06 10:01:27 -04001705 mPreQuantum = context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.L;
1706 mColorUtil = NotificationColorUtil.getInstance();
Joe Onorato46439ce2010-11-19 13:56:21 -08001707 }
1708
Joe Onoratocb109a02011-01-18 17:57:41 -08001709 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001710 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07001711 * It will be shown in the notification content view by default; use
Griff Hazen50c11652014-05-16 09:46:31 -07001712 * {@link #setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001713 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001714 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08001715 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001716 public Builder setWhen(long when) {
1717 mWhen = when;
1718 return this;
1719 }
1720
Joe Onoratocb109a02011-01-18 17:57:41 -08001721 /**
Griff Hazen50c11652014-05-16 09:46:31 -07001722 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07001723 * in the content view.
1724 */
1725 public Builder setShowWhen(boolean show) {
1726 mShowWhen = show;
1727 return this;
1728 }
1729
1730 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001731 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08001732 *
1733 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001734 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001735 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001736 * Useful when showing an elapsed time (like an ongoing phone call).
1737 *
1738 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001739 * @see Notification#when
1740 */
1741 public Builder setUsesChronometer(boolean b) {
1742 mUseChronometer = b;
1743 return this;
1744 }
1745
1746 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001747 * Set the small icon resource, which will be used to represent the notification in the
1748 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08001749 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001750
1751 * The platform template for the expanded view will draw this icon in the left, unless a
1752 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
1753 * icon will be moved to the right-hand side.
1754 *
1755
1756 * @param icon
1757 * A resource ID in the application's package of the drawable to use.
1758 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08001759 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001760 public Builder setSmallIcon(int icon) {
1761 mSmallIcon = icon;
1762 return this;
1763 }
1764
Joe Onoratocb109a02011-01-18 17:57:41 -08001765 /**
1766 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
1767 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
1768 * LevelListDrawable}.
1769 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001770 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08001771 * @param level The level to use for the icon.
1772 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001773 * @see Notification#icon
1774 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08001775 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001776 public Builder setSmallIcon(int icon, int level) {
1777 mSmallIcon = icon;
1778 mSmallIconLevel = level;
1779 return this;
1780 }
1781
Joe Onoratocb109a02011-01-18 17:57:41 -08001782 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001783 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001784 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001785 public Builder setContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001786 mContentTitle = safeCharSequence(title);
Joe Onorato46439ce2010-11-19 13:56:21 -08001787 return this;
1788 }
1789
Joe Onoratocb109a02011-01-18 17:57:41 -08001790 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001791 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001792 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001793 public Builder setContentText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001794 mContentText = safeCharSequence(text);
Joe Onorato46439ce2010-11-19 13:56:21 -08001795 return this;
1796 }
1797
Joe Onoratocb109a02011-01-18 17:57:41 -08001798 /**
Joe Malin8d40d042012-11-05 11:36:40 -08001799 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001800 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
1801 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001802 */
1803 public Builder setSubText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001804 mSubText = safeCharSequence(text);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001805 return this;
1806 }
1807
1808 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08001809 * Set the large number at the right-hand side of the notification. This is
1810 * equivalent to setContentInfo, although it might show the number in a different
1811 * font size for readability.
1812 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08001813 public Builder setNumber(int number) {
1814 mNumber = number;
1815 return this;
1816 }
1817
Joe Onoratocb109a02011-01-18 17:57:41 -08001818 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001819 * A small piece of additional information pertaining to this notification.
1820 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001821 * The platform template will draw this on the last line of the notification, at the far
1822 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08001823 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001824 public Builder setContentInfo(CharSequence info) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001825 mContentInfo = safeCharSequence(info);
Joe Onorato46439ce2010-11-19 13:56:21 -08001826 return this;
1827 }
1828
Joe Onoratocb109a02011-01-18 17:57:41 -08001829 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001830 * Set the progress this notification represents.
1831 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001832 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07001833 */
1834 public Builder setProgress(int max, int progress, boolean indeterminate) {
1835 mProgressMax = max;
1836 mProgress = progress;
1837 mProgressIndeterminate = indeterminate;
1838 return this;
1839 }
1840
1841 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001842 * Supply a custom RemoteViews to use instead of the platform template.
1843 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001844 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08001845 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001846 public Builder setContent(RemoteViews views) {
1847 mContentView = views;
1848 return this;
1849 }
1850
Joe Onoratocb109a02011-01-18 17:57:41 -08001851 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001852 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
1853 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001854 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
1855 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
1856 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001857 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001858 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001859 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001860 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001861 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001862 public Builder setContentIntent(PendingIntent intent) {
1863 mContentIntent = intent;
1864 return this;
1865 }
1866
Joe Onoratocb109a02011-01-18 17:57:41 -08001867 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001868 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
1869 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001870 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001871 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001872 public Builder setDeleteIntent(PendingIntent intent) {
1873 mDeleteIntent = intent;
1874 return this;
1875 }
1876
Joe Onoratocb109a02011-01-18 17:57:41 -08001877 /**
1878 * An intent to launch instead of posting the notification to the status bar.
1879 * Only for use with extremely high-priority notifications demanding the user's
1880 * <strong>immediate</strong> attention, such as an incoming phone call or
1881 * alarm clock that the user has explicitly set to a particular time.
1882 * If this facility is used for something else, please give the user an option
1883 * to turn it off and use a normal notification, as this can be extremely
1884 * disruptive.
1885 *
1886 * @param intent The pending intent to launch.
1887 * @param highPriority Passing true will cause this notification to be sent
1888 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001889 *
1890 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001891 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001892 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
1893 mFullScreenIntent = intent;
1894 setFlag(FLAG_HIGH_PRIORITY, highPriority);
1895 return this;
1896 }
1897
Joe Onoratocb109a02011-01-18 17:57:41 -08001898 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001899 * Set the "ticker" text which is displayed in the status bar when the notification first
Joe Onoratocb109a02011-01-18 17:57:41 -08001900 * arrives.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001901 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001902 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08001903 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001904 public Builder setTicker(CharSequence tickerText) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001905 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08001906 return this;
1907 }
1908
Joe Onoratocb109a02011-01-18 17:57:41 -08001909 /**
1910 * Set the text that is displayed in the status bar when the notification first
1911 * arrives, and also a RemoteViews object that may be displayed instead on some
1912 * devices.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001913 *
1914 * @see Notification#tickerText
1915 * @see Notification#tickerView
Joe Onoratocb109a02011-01-18 17:57:41 -08001916 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001917 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001918 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08001919 mTickerView = views;
1920 return this;
1921 }
1922
Joe Onoratocb109a02011-01-18 17:57:41 -08001923 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001924 * Add a large icon to the notification (and the ticker on some devices).
1925 *
1926 * In the platform template, this image will be shown on the left of the notification view
1927 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side).
1928 *
1929 * @see Notification#largeIcon
Joe Onoratocb109a02011-01-18 17:57:41 -08001930 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001931 public Builder setLargeIcon(Bitmap icon) {
1932 mLargeIcon = icon;
1933 return this;
1934 }
1935
Joe Onoratocb109a02011-01-18 17:57:41 -08001936 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001937 * Set the sound to play.
1938 *
1939 * It will be played on the {@link #STREAM_DEFAULT default stream} for notifications.
1940 *
1941 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001942 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08001943 public Builder setSound(Uri sound) {
1944 mSound = sound;
1945 mAudioStreamType = STREAM_DEFAULT;
1946 return this;
1947 }
1948
Joe Onoratocb109a02011-01-18 17:57:41 -08001949 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001950 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08001951 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001952 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
1953 *
1954 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001955 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001956 public Builder setSound(Uri sound, int streamType) {
1957 mSound = sound;
1958 mAudioStreamType = streamType;
1959 return this;
1960 }
1961
Joe Onoratocb109a02011-01-18 17:57:41 -08001962 /**
1963 * Set the vibration pattern to use.
1964 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001965
1966 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
1967 * <code>pattern</code> parameter.
1968 *
1969
1970 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08001971 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001972 public Builder setVibrate(long[] pattern) {
1973 mVibrate = pattern;
1974 return this;
1975 }
1976
Joe Onoratocb109a02011-01-18 17:57:41 -08001977 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001978 * Set the desired color for the indicator LED on the device, as well as the
1979 * blink duty cycle (specified in milliseconds).
1980 *
1981
1982 * Not all devices will honor all (or even any) of these values.
1983 *
1984
1985 * @see Notification#ledARGB
1986 * @see Notification#ledOnMS
1987 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08001988 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001989 public Builder setLights(int argb, int onMs, int offMs) {
1990 mLedArgb = argb;
1991 mLedOnMs = onMs;
1992 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08001993 return this;
1994 }
1995
Joe Onoratocb109a02011-01-18 17:57:41 -08001996 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001997 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08001998 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001999
2000 * Ongoing notifications cannot be dismissed by the user, so your application or service
2001 * must take care of canceling them.
2002 *
2003
2004 * They are typically used to indicate a background task that the user is actively engaged
2005 * with (e.g., playing music) or is pending in some way and therefore occupying the device
2006 * (e.g., a file download, sync operation, active network connection).
2007 *
2008
2009 * @see Notification#FLAG_ONGOING_EVENT
2010 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08002011 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002012 public Builder setOngoing(boolean ongoing) {
2013 setFlag(FLAG_ONGOING_EVENT, ongoing);
2014 return this;
2015 }
2016
Joe Onoratocb109a02011-01-18 17:57:41 -08002017 /**
2018 * Set this flag if you would only like the sound, vibrate
2019 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002020 *
2021 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08002022 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002023 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
2024 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
2025 return this;
2026 }
2027
Joe Onoratocb109a02011-01-18 17:57:41 -08002028 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002029 * Make this notification automatically dismissed when the user touches it. The
2030 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
2031 *
2032 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08002033 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002034 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08002035 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08002036 return this;
2037 }
2038
Joe Onoratocb109a02011-01-18 17:57:41 -08002039 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08002040 * Set whether or not this notification should not bridge to other devices.
2041 *
2042 * <p>Some notifications can be bridged to other devices for remote display.
2043 * This hint can be set to recommend this notification not be bridged.
2044 */
2045 public Builder setLocalOnly(boolean localOnly) {
2046 setFlag(FLAG_LOCAL_ONLY, localOnly);
2047 return this;
2048 }
2049
2050 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002051 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08002052 * <p>
2053 * The value should be one or more of the following fields combined with
2054 * bitwise-or:
2055 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
2056 * <p>
2057 * For all default values, use {@link #DEFAULT_ALL}.
2058 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002059 public Builder setDefaults(int defaults) {
2060 mDefaults = defaults;
2061 return this;
2062 }
2063
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002064 /**
2065 * Set the priority of this notification.
2066 *
2067 * @see Notification#priority
2068 */
Tor Norbyed9273d62013-05-30 15:59:53 -07002069 public Builder setPriority(@Priority int pri) {
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002070 mPriority = pri;
2071 return this;
2072 }
Joe Malin8d40d042012-11-05 11:36:40 -08002073
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002074 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04002075 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08002076 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04002077 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002078 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04002079 public Builder setCategory(String category) {
2080 mCategory = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002081 return this;
2082 }
2083
2084 /**
Chris Wrendde75302014-03-26 17:24:15 -04002085 * Add a person that is relevant to this notification.
2086 *
2087 * @see Notification#EXTRA_PEOPLE
2088 */
2089 public Builder addPerson(String handle) {
2090 mPeople.add(handle);
2091 return this;
2092 }
2093
2094 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002095 * Set this notification to be part of a group of notifications sharing the same key.
2096 * Grouped notifications may display in a cluster or stack on devices which
2097 * support such rendering.
2098 *
2099 * <p>To make this notification the summary for its group, also call
2100 * {@link #setGroupSummary}. A sort order can be specified for group members by using
2101 * {@link #setSortKey}.
2102 * @param groupKey The group key of the group.
2103 * @return this object for method chaining
2104 */
2105 public Builder setGroup(String groupKey) {
2106 mGroupKey = groupKey;
2107 return this;
2108 }
2109
2110 /**
2111 * Set this notification to be the group summary for a group of notifications.
2112 * Grouped notifications may display in a cluster or stack on devices which
2113 * support such rendering. Requires a group key also be set using {@link #setGroup}.
2114 * @param isGroupSummary Whether this notification should be a group summary.
2115 * @return this object for method chaining
2116 */
2117 public Builder setGroupSummary(boolean isGroupSummary) {
2118 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
2119 return this;
2120 }
2121
2122 /**
2123 * Set a sort key that orders this notification among other notifications from the
2124 * same package. This can be useful if an external sort was already applied and an app
2125 * would like to preserve this. Notifications will be sorted lexicographically using this
2126 * value, although providing different priorities in addition to providing sort key may
2127 * cause this value to be ignored.
2128 *
2129 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07002130 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002131 *
2132 * @see String#compareTo(String)
2133 */
2134 public Builder setSortKey(String sortKey) {
2135 mSortKey = sortKey;
2136 return this;
2137 }
2138
2139 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002140 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002141 *
Griff Hazen720042b2014-02-24 15:46:56 -08002142 * <p>Values within the Bundle will replace existing extras values in this Builder.
2143 *
2144 * @see Notification#extras
2145 */
Griff Hazen959591e2014-05-15 22:26:18 -07002146 public Builder addExtras(Bundle extras) {
2147 if (extras != null) {
2148 if (mExtras == null) {
2149 mExtras = new Bundle(extras);
2150 } else {
2151 mExtras.putAll(extras);
2152 }
Griff Hazen720042b2014-02-24 15:46:56 -08002153 }
2154 return this;
2155 }
2156
2157 /**
2158 * Set metadata for this notification.
2159 *
2160 * <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 -04002161 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002162 * called.
2163 *
Griff Hazen720042b2014-02-24 15:46:56 -08002164 * <p>Replaces any existing extras values with those from the provided Bundle.
2165 * Use {@link #addExtras} to merge in metadata instead.
2166 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002167 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002168 */
Griff Hazen959591e2014-05-15 22:26:18 -07002169 public Builder setExtras(Bundle extras) {
2170 mExtras = extras;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002171 return this;
2172 }
2173
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002174 /**
Griff Hazen720042b2014-02-24 15:46:56 -08002175 * Get the current metadata Bundle used by this notification Builder.
2176 *
2177 * <p>The returned Bundle is shared with this Builder.
2178 *
2179 * <p>The current contents of this Bundle are copied into the Notification each time
2180 * {@link #build()} is called.
2181 *
2182 * @see Notification#extras
2183 */
2184 public Bundle getExtras() {
2185 if (mExtras == null) {
2186 mExtras = new Bundle();
2187 }
2188 return mExtras;
2189 }
2190
2191 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002192 * Add an action to this notification. Actions are typically displayed by
2193 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002194 * <p>
2195 * Every action must have an icon (32dp square and matching the
2196 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2197 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2198 * <p>
2199 * A notification in its expanded form can display up to 3 actions, from left to right in
2200 * the order they were added. Actions will not be displayed when the notification is
2201 * collapsed, however, so be sure that any essential functions may be accessed by the user
2202 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002203 *
2204 * @param icon Resource ID of a drawable that represents the action.
2205 * @param title Text describing the action.
2206 * @param intent PendingIntent to be fired when the action is invoked.
2207 */
2208 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002209 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002210 return this;
2211 }
2212
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002213 /**
Griff Hazen959591e2014-05-15 22:26:18 -07002214 * Add an action to this notification. Actions are typically displayed by
2215 * the system as a button adjacent to the notification content.
2216 * <p>
2217 * Every action must have an icon (32dp square and matching the
2218 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
2219 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
2220 * <p>
2221 * A notification in its expanded form can display up to 3 actions, from left to right in
2222 * the order they were added. Actions will not be displayed when the notification is
2223 * collapsed, however, so be sure that any essential functions may be accessed by the user
2224 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
2225 *
2226 * @param action The action to add.
2227 */
2228 public Builder addAction(Action action) {
2229 mActions.add(action);
2230 return this;
2231 }
2232
2233 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002234 * Add a rich notification style to be applied at build time.
2235 *
2236 * @param style Object responsible for modifying the notification style.
2237 */
2238 public Builder setStyle(Style style) {
2239 if (mStyle != style) {
2240 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002241 if (mStyle != null) {
2242 mStyle.setBuilder(this);
2243 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002244 }
2245 return this;
2246 }
2247
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002248 /**
2249 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07002250 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002251 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
2252 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
2253 *
2254 * @return The same Builder.
2255 */
2256 public Builder setVisibility(int visibility) {
2257 mVisibility = visibility;
2258 return this;
2259 }
2260
2261 /**
2262 * Supply a replacement Notification whose contents should be shown in insecure contexts
2263 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
2264 * @param n A replacement notification, presumably with some or all info redacted.
2265 * @return The same Builder.
2266 */
2267 public Builder setPublicVersion(Notification n) {
2268 mPublicVersion = n;
2269 return this;
2270 }
2271
Griff Hazenb720abe2014-05-20 13:15:30 -07002272 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002273 * Apply an extender to this notification builder. Extenders may be used to add
2274 * metadata or change options on this builder.
2275 */
Griff Hazen61a9e862014-05-22 16:05:19 -07002276 public Builder extend(Extender extender) {
2277 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002278 return this;
2279 }
2280
Joe Onorato46439ce2010-11-19 13:56:21 -08002281 private void setFlag(int mask, boolean value) {
2282 if (value) {
2283 mFlags |= mask;
2284 } else {
2285 mFlags &= ~mask;
2286 }
2287 }
2288
Dan Sandler26e81cf2014-05-06 10:01:27 -04002289 /**
2290 * Sets {@link Notification#color}.
2291 *
2292 * @param argb The accent color to use
2293 *
2294 * @return The same Builder.
2295 */
2296 public Builder setColor(int argb) {
2297 mColor = argb;
2298 return this;
2299 }
2300
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002301 private RemoteViews applyStandardTemplate(int resId, boolean fitIn1U) {
Joe Onorato561d3852010-11-20 18:09:34 -08002302 RemoteViews contentView = new RemoteViews(mContext.getPackageName(), resId);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002303 boolean showLine3 = false;
2304 boolean showLine2 = false;
Dan Sandler190d58d2014-05-15 09:33:39 -04002305
Dan Sandler26e81cf2014-05-06 10:01:27 -04002306 if (mPriority < PRIORITY_LOW) {
2307 // TODO: Low priority presentation
Daniel Sandlere95658c2012-05-10 00:33:54 -04002308 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002309 if (mLargeIcon != null) {
2310 contentView.setImageViewBitmap(R.id.icon, mLargeIcon);
Dan Sandler26e81cf2014-05-06 10:01:27 -04002311 processLargeIcon(mLargeIcon, contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04002312 contentView.setImageViewResource(R.id.right_icon, mSmallIcon);
2313 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
2314 processSmallRightIcon(mSmallIcon, contentView);
2315 } else { // small icon at left
2316 contentView.setImageViewResource(R.id.icon, mSmallIcon);
2317 contentView.setViewVisibility(R.id.icon, View.VISIBLE);
2318 processSmallIconAsLarge(mSmallIcon, contentView);
Joe Onorato561d3852010-11-20 18:09:34 -08002319 }
2320 if (mContentTitle != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002321 contentView.setTextViewText(R.id.title, processLegacyText(mContentTitle));
Joe Onorato561d3852010-11-20 18:09:34 -08002322 }
2323 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002324 contentView.setTextViewText(R.id.text, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002325 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002326 }
2327 if (mContentInfo != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002328 contentView.setTextViewText(R.id.info, processLegacyText(mContentInfo));
Jeff Sharkey1c400132011-08-05 14:50:13 -07002329 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002330 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002331 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04002332 final int tooBig = mContext.getResources().getInteger(
2333 R.integer.status_bar_notification_info_maxnum);
2334 if (mNumber > tooBig) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002335 contentView.setTextViewText(R.id.info, processLegacyText(
2336 mContext.getResources().getString(
2337 R.string.status_bar_notification_info_overflow)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002338 } else {
2339 NumberFormat f = NumberFormat.getIntegerInstance();
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002340 contentView.setTextViewText(R.id.info, processLegacyText(f.format(mNumber)));
Joe Onorato059a2f82011-01-04 10:27:01 -08002341 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002342 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002343 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08002344 } else {
2345 contentView.setViewVisibility(R.id.info, View.GONE);
2346 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002347
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002348 // Need to show three lines?
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002349 if (mSubText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002350 contentView.setTextViewText(R.id.text, processLegacyText(mSubText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002351 if (mContentText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002352 contentView.setTextViewText(R.id.text2, processLegacyText(mContentText));
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002353 contentView.setViewVisibility(R.id.text2, View.VISIBLE);
2354 showLine2 = true;
2355 } else {
2356 contentView.setViewVisibility(R.id.text2, View.GONE);
2357 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002358 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002359 contentView.setViewVisibility(R.id.text2, View.GONE);
2360 if (mProgressMax != 0 || mProgressIndeterminate) {
2361 contentView.setProgressBar(
2362 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
2363 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002364 showLine2 = true;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002365 } else {
2366 contentView.setViewVisibility(R.id.progress, View.GONE);
2367 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07002368 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002369 if (showLine2) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002370 if (fitIn1U) {
2371 // need to shrink all the type to make sure everything fits
2372 final Resources res = mContext.getResources();
2373 final float subTextSize = res.getDimensionPixelSize(
2374 R.dimen.notification_subtext_size);
2375 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
2376 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002377 // vertical centering
2378 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
2379 }
2380
Daniel Sandler0c890492012-09-12 17:23:10 -07002381 if (mWhen != 0 && mShowWhen) {
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002382 if (mUseChronometer) {
2383 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
2384 contentView.setLong(R.id.chronometer, "setBase",
2385 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
2386 contentView.setBoolean(R.id.chronometer, "setStarted", true);
2387 } else {
2388 contentView.setViewVisibility(R.id.time, View.VISIBLE);
2389 contentView.setLong(R.id.time, "setTime", mWhen);
2390 }
Daniel Sandler0c890492012-09-12 17:23:10 -07002391 } else {
2392 contentView.setViewVisibility(R.id.time, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08002393 }
Daniel Sandler0c890492012-09-12 17:23:10 -07002394
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002395 contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002396 contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08002397 return contentView;
2398 }
2399
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002400 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002401 RemoteViews big = applyStandardTemplate(layoutId, false);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002402
2403 int N = mActions.size();
2404 if (N > 0) {
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002405 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002406 big.setViewVisibility(R.id.action_divider, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04002407 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Chris Wren2c22eb02012-05-08 09:49:13 -04002408 big.removeAllViews(R.id.actions);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002409 for (int i=0; i<N; i++) {
2410 final RemoteViews button = generateActionButton(mActions.get(i));
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002411 big.addView(R.id.actions, button);
2412 }
2413 }
2414 return big;
2415 }
2416
Joe Onorato46439ce2010-11-19 13:56:21 -08002417 private RemoteViews makeContentView() {
2418 if (mContentView != null) {
2419 return mContentView;
2420 } else {
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002421 return applyStandardTemplate(getBaseLayoutResource(), true); // no more special large_icon flavor
Joe Onorato46439ce2010-11-19 13:56:21 -08002422 }
2423 }
2424
2425 private RemoteViews makeTickerView() {
2426 if (mTickerView != null) {
2427 return mTickerView;
2428 } else {
Joe Onorato561d3852010-11-20 18:09:34 -08002429 if (mContentView == null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002430 return applyStandardTemplate(mLargeIcon == null
Joe Onorato561d3852010-11-20 18:09:34 -08002431 ? R.layout.status_bar_latest_event_ticker
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002432 : R.layout.status_bar_latest_event_ticker_large_icon, true);
Joe Onorato561d3852010-11-20 18:09:34 -08002433 } else {
2434 return null;
2435 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002436 }
2437 }
2438
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002439 private RemoteViews makeBigContentView() {
2440 if (mActions.size() == 0) return null;
2441
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002442 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002443 }
2444
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002445 private RemoteViews makeHeadsUpContentView() {
Chris Wren8fd39ec2014-02-27 17:43:26 -05002446 if (mActions.size() == 0) return null;
2447
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002448 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05002449 }
2450
2451
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002452 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04002453 final boolean tombstone = (action.actionIntent == null);
Joe Malin8d40d042012-11-05 11:36:40 -08002454 RemoteViews button = new RemoteViews(mContext.getPackageName(),
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002455 tombstone ? getActionTombstoneLayoutResource()
2456 : getActionLayoutResource());
Chris Wren8749ac8a2013-12-03 14:31:01 -05002457 button.setTextViewCompoundDrawablesRelative(R.id.action0, action.icon, 0, 0, 0);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002458 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Daniel Sandler8680bf82012-05-15 16:52:52 -04002459 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04002460 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04002461 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002462 button.setContentDescription(R.id.action0, action.title);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002463 processLegacyAction(action, button);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002464 return button;
2465 }
2466
Joe Onoratocb109a02011-01-18 17:57:41 -08002467 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002468 * @return Whether we are currently building a notification from a legacy (an app that
2469 * doesn't create quantum notifications by itself) app.
2470 */
2471 private boolean isLegacy() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002472 return mColorUtil != null;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002473 }
2474
2475 private void processLegacyAction(Action action, RemoteViews button) {
2476 if (isLegacy()) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002477 if (mColorUtil.isGrayscale(mContext, action.icon)) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002478 button.setTextViewCompoundDrawablesRelativeColorFilter(R.id.action0, 0,
2479 mContext.getResources().getColor(
2480 R.color.notification_action_legacy_color_filter),
2481 PorterDuff.Mode.MULTIPLY);
2482 }
2483 }
2484 }
2485
2486 private CharSequence processLegacyText(CharSequence charSequence) {
2487 if (isLegacy()) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002488 return mColorUtil.invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002489 } else {
2490 return charSequence;
2491 }
2492 }
2493
Dan Sandler26e81cf2014-05-06 10:01:27 -04002494 /**
2495 * Apply any necessary background to smallIcons being used in the largeIcon spot.
2496 */
2497 private void processSmallIconAsLarge(int largeIconId, RemoteViews contentView) {
2498 if (!isLegacy() || mColorUtil.isGrayscale(mContext, largeIconId)) {
2499 applyLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002500 }
2501 }
2502
Dan Sandler26e81cf2014-05-06 10:01:27 -04002503 /**
2504 * Apply any necessary background to a largeIcon if it's a fake smallIcon (that is,
2505 * if it's grayscale).
2506 */
2507 // TODO: also check bounds, transparency, that sort of thing.
2508 private void processLargeIcon(Bitmap largeIcon, RemoteViews contentView) {
2509 if (!isLegacy() || mColorUtil.isGrayscale(largeIcon)) {
2510 applyLargeIconBackground(contentView);
Dan Sandler190d58d2014-05-15 09:33:39 -04002511 } else {
2512 removeLargeIconBackground(contentView);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002513 }
2514 }
2515
Dan Sandler26e81cf2014-05-06 10:01:27 -04002516 /**
2517 * Add a colored circle behind the largeIcon slot.
2518 */
2519 private void applyLargeIconBackground(RemoteViews contentView) {
2520 contentView.setInt(R.id.icon, "setBackgroundResource",
2521 R.drawable.notification_icon_legacy_bg_inset);
2522
2523 contentView.setDrawableParameters(
2524 R.id.icon,
2525 true,
2526 -1,
2527 mColor,
2528 PorterDuff.Mode.SRC_ATOP,
2529 -1);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002530 }
2531
Dan Sandler190d58d2014-05-15 09:33:39 -04002532 private void removeLargeIconBackground(RemoteViews contentView) {
2533 contentView.setInt(R.id.icon, "setBackgroundResource", 0);
2534 }
2535
Dan Sandler26e81cf2014-05-06 10:01:27 -04002536 /**
2537 * Recolor small icons when used in the R.id.right_icon slot.
2538 */
Dan Sandler190d58d2014-05-15 09:33:39 -04002539 private void processSmallRightIcon(int smallIconDrawableId,
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002540 RemoteViews contentView) {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002541 if (!isLegacy() || mColorUtil.isGrayscale(mContext, smallIconDrawableId)) {
Dan Sandler190d58d2014-05-15 09:33:39 -04002542 contentView.setDrawableParameters(R.id.right_icon, false, -1,
2543 0xFFFFFFFF,
2544 PorterDuff.Mode.SRC_ATOP, -1);
2545
2546 contentView.setInt(R.id.right_icon,
2547 "setBackgroundResource",
2548 R.drawable.notification_icon_legacy_bg);
2549
2550 contentView.setDrawableParameters(
2551 R.id.right_icon,
2552 true,
2553 -1,
2554 mColor,
2555 PorterDuff.Mode.SRC_ATOP,
2556 -1);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002557 }
2558 }
2559
Dan Sandler26e81cf2014-05-06 10:01:27 -04002560 private int resolveColor() {
2561 if (mColor == COLOR_DEFAULT) {
2562 mColor = mContext.getResources().getColor(R.color.notification_icon_bg_color);
2563 } else {
2564 mColor |= 0xFF000000; // no alpha for custom colors
2565 }
2566 return mColor;
2567 }
2568
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002569 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002570 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002571 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08002572 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002573 public Notification buildUnstyled() {
Joe Onorato46439ce2010-11-19 13:56:21 -08002574 Notification n = new Notification();
2575 n.when = mWhen;
2576 n.icon = mSmallIcon;
2577 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002578 n.number = mNumber;
Dan Sandler26e81cf2014-05-06 10:01:27 -04002579
2580 n.color = resolveColor();
2581
Joe Onorato46439ce2010-11-19 13:56:21 -08002582 n.contentView = makeContentView();
2583 n.contentIntent = mContentIntent;
2584 n.deleteIntent = mDeleteIntent;
2585 n.fullScreenIntent = mFullScreenIntent;
2586 n.tickerText = mTickerText;
2587 n.tickerView = makeTickerView();
2588 n.largeIcon = mLargeIcon;
2589 n.sound = mSound;
2590 n.audioStreamType = mAudioStreamType;
2591 n.vibrate = mVibrate;
2592 n.ledARGB = mLedArgb;
2593 n.ledOnMS = mLedOnMs;
2594 n.ledOffMS = mLedOffMs;
2595 n.defaults = mDefaults;
2596 n.flags = mFlags;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04002597 n.bigContentView = makeBigContentView();
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002598 n.headsUpContentView = makeHeadsUpContentView();
Daniel Sandler26c13432013-04-04 11:01:04 -04002599 if (mLedOnMs != 0 || mLedOffMs != 0) {
Joe Onorato8d0b6552010-11-22 16:09:29 -08002600 n.flags |= FLAG_SHOW_LIGHTS;
2601 }
2602 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
2603 n.flags |= FLAG_SHOW_LIGHTS;
2604 }
John Spurlockfd7f1e02014-03-18 16:41:57 -04002605 n.category = mCategory;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002606 n.mGroupKey = mGroupKey;
2607 n.mSortKey = mSortKey;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002608 n.priority = mPriority;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002609 if (mActions.size() > 0) {
2610 n.actions = new Action[mActions.size()];
2611 mActions.toArray(n.actions);
2612 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002613 n.visibility = mVisibility;
2614
2615 if (mPublicVersion != null) {
2616 n.publicVersion = new Notification();
2617 mPublicVersion.cloneInto(n.publicVersion, true);
2618 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002619
Joe Onorato46439ce2010-11-19 13:56:21 -08002620 return n;
2621 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002622
2623 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002624 * Capture, in the provided bundle, semantic information used in the construction of
2625 * this Notification object.
2626 * @hide
2627 */
Griff Hazen720042b2014-02-24 15:46:56 -08002628 public void populateExtras(Bundle extras) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002629 // Store original information used in the construction of this object
2630 extras.putCharSequence(EXTRA_TITLE, mContentTitle);
2631 extras.putCharSequence(EXTRA_TEXT, mContentText);
2632 extras.putCharSequence(EXTRA_SUB_TEXT, mSubText);
2633 extras.putCharSequence(EXTRA_INFO_TEXT, mContentInfo);
2634 extras.putInt(EXTRA_SMALL_ICON, mSmallIcon);
2635 extras.putInt(EXTRA_PROGRESS, mProgress);
2636 extras.putInt(EXTRA_PROGRESS_MAX, mProgressMax);
2637 extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mProgressIndeterminate);
2638 extras.putBoolean(EXTRA_SHOW_CHRONOMETER, mUseChronometer);
2639 extras.putBoolean(EXTRA_SHOW_WHEN, mShowWhen);
John Spurlockac08a472013-06-10 11:37:08 -04002640 if (mLargeIcon != null) {
2641 extras.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
2642 }
Chris Wrendde75302014-03-26 17:24:15 -04002643 if (!mPeople.isEmpty()) {
2644 extras.putStringArray(EXTRA_PEOPLE, mPeople.toArray(new String[mPeople.size()]));
2645 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002646 }
2647
2648 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002649 * @deprecated Use {@link #build()} instead.
2650 */
2651 @Deprecated
2652 public Notification getNotification() {
2653 return build();
2654 }
2655
2656 /**
2657 * Combine all of the options that have been set and return a new {@link Notification}
2658 * object.
2659 */
2660 public Notification build() {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002661 Notification n = buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002662
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002663 if (mStyle != null) {
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002664 n = mStyle.buildStyled(n);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002665 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002666
2667 n.extras = mExtras != null ? new Bundle(mExtras) : new Bundle();
2668
Griff Hazen720042b2014-02-24 15:46:56 -08002669 populateExtras(n.extras);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002670 if (mStyle != null) {
2671 mStyle.addExtras(n.extras);
2672 }
2673
2674 return n;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002675 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002676
2677 /**
2678 * Apply this Builder to an existing {@link Notification} object.
2679 *
2680 * @hide
2681 */
2682 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04002683 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002684 return n;
2685 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002686
2687
2688 private int getBaseLayoutResource() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002689 return R.layout.notification_template_quantum_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002690 }
2691
2692 private int getBigBaseLayoutResource() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002693 return R.layout.notification_template_quantum_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002694 }
2695
2696 private int getBigPictureLayoutResource() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002697 return R.layout.notification_template_quantum_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002698 }
2699
2700 private int getBigTextLayoutResource() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002701 return R.layout.notification_template_quantum_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002702 }
2703
2704 private int getInboxLayoutResource() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002705 return R.layout.notification_template_quantum_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002706 }
2707
2708 private int getActionLayoutResource() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002709 return R.layout.notification_quantum_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002710 }
2711
2712 private int getActionTombstoneLayoutResource() {
Dan Sandler26e81cf2014-05-06 10:01:27 -04002713 return R.layout.notification_quantum_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002714 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002715 }
2716
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002717 /**
2718 * An object that can apply a rich notification style to a {@link Notification.Builder}
2719 * object.
2720 */
Griff Hazendfcb0802014-02-11 12:00:00 -08002721 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04002722 private CharSequence mBigContentTitle;
2723 private CharSequence mSummaryText = null;
Daniel Sandler619738c2012-06-07 16:33:08 -04002724 private boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04002725
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002726 protected Builder mBuilder;
2727
Chris Wrend6297db2012-05-03 16:20:13 -04002728 /**
2729 * Overrides ContentTitle in the big form of the template.
2730 * This defaults to the value passed to setContentTitle().
2731 */
2732 protected void internalSetBigContentTitle(CharSequence title) {
2733 mBigContentTitle = title;
2734 }
2735
2736 /**
2737 * Set the first line of text after the detail section in the big form of the template.
2738 */
2739 protected void internalSetSummaryText(CharSequence cs) {
2740 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04002741 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04002742 }
2743
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002744 public void setBuilder(Builder builder) {
2745 if (mBuilder != builder) {
2746 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07002747 if (mBuilder != null) {
2748 mBuilder.setStyle(this);
2749 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002750 }
2751 }
2752
Chris Wrend6297db2012-05-03 16:20:13 -04002753 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002754 if (mBuilder == null) {
2755 throw new IllegalArgumentException("Style requires a valid Builder object");
2756 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002757 }
Chris Wrend6297db2012-05-03 16:20:13 -04002758
2759 protected RemoteViews getStandardView(int layoutId) {
2760 checkBuilder();
2761
2762 if (mBigContentTitle != null) {
2763 mBuilder.setContentTitle(mBigContentTitle);
2764 }
2765
Chris Wrend6297db2012-05-03 16:20:13 -04002766 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
2767
Chris Wrend6297db2012-05-03 16:20:13 -04002768 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
2769 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04002770 } else {
2771 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04002772 }
2773
Daniel Sandler619738c2012-06-07 16:33:08 -04002774 // The last line defaults to the subtext, but can be replaced by mSummaryText
2775 final CharSequence overflowText =
2776 mSummaryTextSet ? mSummaryText
2777 : mBuilder.mSubText;
2778 if (overflowText != null) {
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002779 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(overflowText));
Daniel Sandler619738c2012-06-07 16:33:08 -04002780 contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04002781 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
Daniel Sandler916ad912012-06-13 12:17:07 -04002782 } else {
2783 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
2784 contentView.setViewVisibility(R.id.line3, View.GONE);
Chris Wrend6297db2012-05-03 16:20:13 -04002785 }
2786
2787 return contentView;
2788 }
2789
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002790 /**
2791 * @hide
2792 */
2793 public void addExtras(Bundle extras) {
2794 if (mSummaryTextSet) {
2795 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
2796 }
2797 if (mBigContentTitle != null) {
2798 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
2799 }
Chris Wren91ad5632013-06-05 15:05:57 -04002800 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002801 }
2802
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002803 /**
2804 * @hide
2805 */
2806 public abstract Notification buildStyled(Notification wip);
2807
2808 /**
2809 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
2810 * attached to.
2811 *
2812 * @return the fully constructed Notification.
2813 */
2814 public Notification build() {
2815 checkBuilder();
2816 return mBuilder.build();
2817 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002818 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002819
2820 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002821 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08002822 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002823 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002824 * <pre class="prettyprint">
2825 * Notification noti = new Notification.BigPictureStyle(
2826 * new Notification.Builder()
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002827 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002828 * .setContentText(subject)
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002829 * .setSmallIcon(R.drawable.new_post)
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002830 * .setLargeIcon(aBitmap))
2831 * .bigPicture(aBigBitmap)
2832 * .build();
2833 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08002834 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002835 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002836 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002837 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002838 private Bitmap mPicture;
Chris Wren3745a3d2012-05-22 15:11:52 -04002839 private Bitmap mBigLargeIcon;
2840 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002841
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002842 public BigPictureStyle() {
2843 }
2844
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002845 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002846 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002847 }
2848
Chris Wrend6297db2012-05-03 16:20:13 -04002849 /**
2850 * Overrides ContentTitle in the big form of the template.
2851 * This defaults to the value passed to setContentTitle().
2852 */
2853 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002854 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04002855 return this;
2856 }
2857
2858 /**
2859 * Set the first line of text after the detail section in the big form of the template.
2860 */
2861 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002862 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04002863 return this;
2864 }
2865
Chris Wren0bd664d2012-08-01 13:56:56 -04002866 /**
2867 * Provide the bitmap to be used as the payload for the BigPicture notification.
2868 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002869 public BigPictureStyle bigPicture(Bitmap b) {
2870 mPicture = b;
2871 return this;
2872 }
2873
Chris Wren3745a3d2012-05-22 15:11:52 -04002874 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04002875 * Override the large icon when the big notification is shown.
2876 */
2877 public BigPictureStyle bigLargeIcon(Bitmap b) {
2878 mBigLargeIconSet = true;
2879 mBigLargeIcon = b;
2880 return this;
2881 }
2882
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002883 private RemoteViews makeBigContentView() {
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002884 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002885
2886 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
2887
2888 return contentView;
2889 }
2890
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002891 /**
2892 * @hide
2893 */
2894 public void addExtras(Bundle extras) {
2895 super.addExtras(extras);
2896
2897 if (mBigLargeIconSet) {
2898 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
2899 }
2900 extras.putParcelable(EXTRA_PICTURE, mPicture);
2901 }
2902
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002903 /**
2904 * @hide
2905 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002906 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002907 public Notification buildStyled(Notification wip) {
Chris Wren3745a3d2012-05-22 15:11:52 -04002908 if (mBigLargeIconSet ) {
2909 mBuilder.mLargeIcon = mBigLargeIcon;
2910 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002911 wip.bigContentView = makeBigContentView();
2912 return wip;
2913 }
2914 }
2915
2916 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002917 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08002918 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002919 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002920 * <pre class="prettyprint">
Daniel Sandler87682782012-11-07 14:04:42 -05002921 * Notification noti = new Notification.BigTextStyle(
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002922 * new Notification.Builder()
2923 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2924 * .setContentText(subject)
2925 * .setSmallIcon(R.drawable.new_mail)
2926 * .setLargeIcon(aBitmap))
2927 * .bigText(aVeryLongString)
2928 * .build();
2929 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08002930 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04002931 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002932 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002933 public static class BigTextStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002934 private CharSequence mBigText;
2935
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002936 public BigTextStyle() {
2937 }
2938
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002939 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002940 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002941 }
2942
Chris Wrend6297db2012-05-03 16:20:13 -04002943 /**
2944 * Overrides ContentTitle in the big form of the template.
2945 * This defaults to the value passed to setContentTitle().
2946 */
2947 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002948 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04002949 return this;
2950 }
2951
2952 /**
2953 * Set the first line of text after the detail section in the big form of the template.
2954 */
2955 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002956 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04002957 return this;
2958 }
2959
Chris Wren0bd664d2012-08-01 13:56:56 -04002960 /**
2961 * Provide the longer text to be displayed in the big form of the
2962 * template in place of the content text.
2963 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002964 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002965 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002966 return this;
2967 }
2968
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002969 /**
2970 * @hide
2971 */
2972 public void addExtras(Bundle extras) {
2973 super.addExtras(extras);
2974
2975 extras.putCharSequence(EXTRA_TEXT, mBigText);
2976 }
2977
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002978 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04002979 // Remove the content text so line3 only shows if you have a summary
2980 final boolean hadThreeLines = (mBuilder.mContentText != null && mBuilder.mSubText != null);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04002981 mBuilder.mContentText = null;
Daniel Sandler916ad912012-06-13 12:17:07 -04002982
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01002983 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08002984
Daniel Sandler619738c2012-06-07 16:33:08 -04002985 if (hadThreeLines) {
2986 // vertical centering
2987 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
2988 }
2989
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01002990 contentView.setTextViewText(R.id.big_text, mBuilder.processLegacyText(mBigText));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002991 contentView.setViewVisibility(R.id.big_text, View.VISIBLE);
Chris Wren3c5f92432012-05-04 16:31:17 -04002992 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002993
2994 return contentView;
2995 }
2996
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04002997 /**
2998 * @hide
2999 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003000 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003001 public Notification buildStyled(Notification wip) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003002 wip.bigContentView = makeBigContentView();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003003
3004 wip.extras.putCharSequence(EXTRA_TEXT, mBigText);
3005
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003006 return wip;
3007 }
3008 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04003009
3010 /**
3011 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08003012 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04003013 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
3014 * <pre class="prettyprint">
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003015 * Notification noti = new Notification.InboxStyle(
Daniel Sandler879c5e02012-04-17 16:46:51 -04003016 * new Notification.Builder()
Chris Wrend6297db2012-05-03 16:20:13 -04003017 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
Daniel Sandler879c5e02012-04-17 16:46:51 -04003018 * .setContentText(subject)
3019 * .setSmallIcon(R.drawable.new_mail)
3020 * .setLargeIcon(aBitmap))
3021 * .addLine(str1)
3022 * .addLine(str2)
Chris Wrend6297db2012-05-03 16:20:13 -04003023 * .setContentTitle("")
3024 * .setSummaryText(&quot;+3 more&quot;)
Daniel Sandler879c5e02012-04-17 16:46:51 -04003025 * .build();
3026 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08003027 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04003028 * @see Notification#bigContentView
3029 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003030 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04003031 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
3032
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003033 public InboxStyle() {
3034 }
3035
Daniel Sandler879c5e02012-04-17 16:46:51 -04003036 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003037 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04003038 }
3039
Chris Wrend6297db2012-05-03 16:20:13 -04003040 /**
3041 * Overrides ContentTitle in the big form of the template.
3042 * This defaults to the value passed to setContentTitle().
3043 */
3044 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003045 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04003046 return this;
3047 }
3048
3049 /**
3050 * Set the first line of text after the detail section in the big form of the template.
3051 */
3052 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003053 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04003054 return this;
3055 }
3056
Chris Wren0bd664d2012-08-01 13:56:56 -04003057 /**
3058 * Append a line to the digest section of the Inbox notification.
3059 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04003060 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003061 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04003062 return this;
3063 }
3064
Daniel Sandlerf45564e2013-04-15 15:05:08 -04003065 /**
3066 * @hide
3067 */
3068 public void addExtras(Bundle extras) {
3069 super.addExtras(extras);
3070 CharSequence[] a = new CharSequence[mTexts.size()];
3071 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
3072 }
3073
Daniel Sandler879c5e02012-04-17 16:46:51 -04003074 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04003075 // Remove the content text so line3 disappears unless you have a summary
3076 mBuilder.mContentText = null;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01003077 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04003078
Chris Wrend6297db2012-05-03 16:20:13 -04003079 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandler879c5e02012-04-17 16:46:51 -04003080
Chris Wrend6297db2012-05-03 16:20:13 -04003081 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 -04003082 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04003083
Chris Wren4ed80d52012-05-17 09:30:03 -04003084 // Make sure all rows are gone in case we reuse a view.
3085 for (int rowId : rowIds) {
3086 contentView.setViewVisibility(rowId, View.GONE);
3087 }
3088
Chris Wren683ab002012-09-20 10:35:54 -04003089
Daniel Sandler879c5e02012-04-17 16:46:51 -04003090 int i=0;
3091 while (i < mTexts.size() && i < rowIds.length) {
3092 CharSequence str = mTexts.get(i);
3093 if (str != null && !str.equals("")) {
3094 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01003095 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Daniel Sandler879c5e02012-04-17 16:46:51 -04003096 }
3097 i++;
3098 }
3099
Chris Wren683ab002012-09-20 10:35:54 -04003100 contentView.setViewVisibility(R.id.inbox_end_pad,
3101 mTexts.size() > 0 ? View.VISIBLE : View.GONE);
3102
3103 contentView.setViewVisibility(R.id.inbox_more,
3104 mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE);
Chris Wren29bb6d92012-05-17 18:09:42 -04003105
Daniel Sandler879c5e02012-04-17 16:46:51 -04003106 return contentView;
3107 }
3108
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003109 /**
3110 * @hide
3111 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003112 @Override
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003113 public Notification buildStyled(Notification wip) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04003114 wip.bigContentView = makeBigContentView();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05003115
Daniel Sandler879c5e02012-04-17 16:46:51 -04003116 return wip;
3117 }
3118 }
Dan Sandler842dd772014-05-15 09:36:47 -04003119
3120 /**
3121 * Notification style for media playback notifications.
3122 *
3123 * In the expanded form, {@link Notification#bigContentView}, up to 5
3124 * {@link Notification.Action}s specified with
3125 * {@link Notification.Builder#addAction(int, CharSequence, PendingIntent) addAction} will be
3126 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
3127 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
3128 * treated as album artwork.
3129 *
3130 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
3131 * {@link Notification#contentView}; by providing action indices to
3132 * {@link #setShowActionsInCompactView(int...)} you can promote up to 2 actions to be displayed
3133 * in the standard view alongside the usual content.
3134 *
3135 * Finally, if you attach a {@link android.media.session.MediaSessionToken} using
3136 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSessionToken)},
3137 * the System UI can identify this as a notification representing an active media session
3138 * and respond accordingly (by showing album artwork in the lockscreen, for example).
3139 *
3140 * To use this style with your Notification, feed it to
3141 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
3142 * <pre class="prettyprint">
3143 * Notification noti = new Notification.Builder()
3144 * .setSmallIcon(R.drawable.ic_stat_player)
3145 * .setContentTitle(&quot;Track title&quot;) // these three lines are optional
3146 * .setContentText(&quot;Artist - Album&quot;) // if you use
3147 * .setLargeIcon(albumArtBitmap)) // setMediaSession(token, true)
3148 * .setMediaSession(mySession, true)
3149 * .setStyle(<b>new Notification.MediaStyle()</b>)
3150 * .build();
3151 * </pre>
3152 *
3153 * @see Notification#bigContentView
3154 */
3155 public static class MediaStyle extends Style {
3156 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 2;
3157 static final int MAX_MEDIA_BUTTONS = 5;
3158
3159 private int[] mActionsToShowInCompact = null;
3160 private MediaSessionToken mToken;
3161
3162 public MediaStyle() {
3163 }
3164
3165 public MediaStyle(Builder builder) {
3166 setBuilder(builder);
3167 }
3168
3169 /**
3170 * Request up to 2 actions (by index in the order of addition) to be shown in the compact
3171 * notification view.
3172 */
3173 public MediaStyle setShowActionsInCompactView(int...actions) {
3174 mActionsToShowInCompact = actions;
3175 return this;
3176 }
3177
3178 /**
3179 * Attach a {@link android.media.session.MediaSessionToken} to this Notification to provide
3180 * additional playback information and control to the SystemUI.
3181 */
3182 public MediaStyle setMediaSession(MediaSessionToken token) {
3183 mToken = token;
3184 return this;
3185 }
3186
3187 @Override
3188 public Notification buildStyled(Notification wip) {
3189 wip.contentView = makeMediaContentView();
3190 wip.bigContentView = makeMediaBigContentView();
3191
3192 return wip;
3193 }
3194
3195 /** @hide */
3196 @Override
3197 public void addExtras(Bundle extras) {
3198 super.addExtras(extras);
3199
3200 if (mToken != null) {
3201 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
3202 }
3203 }
3204
3205 private RemoteViews generateMediaActionButton(Action action) {
3206 final boolean tombstone = (action.actionIntent == null);
3207 RemoteViews button = new RemoteViews(mBuilder.mContext.getPackageName(),
3208 R.layout.notification_quantum_media_action);
3209 button.setImageViewResource(R.id.action0, action.icon);
3210 if (!tombstone) {
3211 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
3212 }
3213 button.setContentDescription(R.id.action0, action.title);
3214 return button;
3215 }
3216
3217 private RemoteViews makeMediaContentView() {
3218 RemoteViews view = mBuilder.applyStandardTemplate(
3219 R.layout.notification_template_quantum_media, true /* 1U */);
3220
3221 final int numActions = mBuilder.mActions.size();
3222 final int N = mActionsToShowInCompact == null
3223 ? 0
3224 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
3225 if (N > 0) {
3226 view.removeAllViews(R.id.actions);
3227 for (int i = 0; i < N; i++) {
3228 if (i >= numActions) {
3229 throw new IllegalArgumentException(String.format(
3230 "setShowActionsInCompactView: action %d out of bounds (max %d)",
3231 i, numActions - 1));
3232 }
3233
3234 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
3235 final RemoteViews button = generateMediaActionButton(action);
3236 view.addView(R.id.actions, button);
3237 }
3238 }
3239 return view;
3240 }
3241
3242 private RemoteViews makeMediaBigContentView() {
3243 RemoteViews big = mBuilder.applyStandardTemplate(
3244 R.layout.notification_template_quantum_big_media, false);
3245
3246 final int N = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
3247 if (N > 0) {
3248 big.removeAllViews(R.id.actions);
3249 for (int i=0; i<N; i++) {
3250 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i));
3251 big.addView(R.id.actions, button);
3252 }
3253 }
3254 return big;
3255 }
3256 }
Griff Hazen61a9e862014-05-22 16:05:19 -07003257
3258 /**
3259 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
3260 * metadata or change options on a notification builder.
3261 */
3262 public interface Extender {
3263 /**
3264 * Apply this extender to a notification builder.
3265 * @param builder the builder to be modified.
3266 * @return the build object for chaining.
3267 */
3268 public Builder extend(Builder builder);
3269 }
3270
3271 /**
3272 * Helper class to add wearable extensions to notifications.
3273 * <p class="note"> See
3274 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
3275 * for Android Wear</a> for more information on how to use this class.
3276 * <p>
3277 * To create a notification with wearable extensions:
3278 * <ol>
3279 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
3280 * properties.
3281 * <li>Create a {@link android.app.Notification.WearableExtender}.
3282 * <li>Set wearable-specific properties using the
3283 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
3284 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
3285 * notification.
3286 * <li>Post the notification to the notification system with the
3287 * {@code NotificationManager.notify(...)} methods.
3288 * </ol>
3289 *
3290 * <pre class="prettyprint">
3291 * Notification notif = new Notification.Builder(mContext)
3292 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
3293 * .setContentText(subject)
3294 * .setSmallIcon(R.drawable.new_mail)
3295 * .extend(new Notification.WearableExtender()
3296 * .setContentIcon(R.drawable.new_mail))
3297 * .build();
3298 * NotificationManager notificationManger =
3299 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
3300 * notificationManger.notify(0, notif);</pre>
3301 *
3302 * <p>Wearable extensions can be accessed on an existing notification by using the
3303 * {@code WearableExtender(Notification)} constructor,
3304 * and then using the {@code get} methods to access values.
3305 *
3306 * <pre class="prettyprint">
3307 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
3308 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07003309 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07003310 */
3311 public static final class WearableExtender implements Extender {
3312 /**
3313 * Sentinel value for an action index that is unset.
3314 */
3315 public static final int UNSET_ACTION_INDEX = -1;
3316
3317 /**
3318 * Size value for use with {@link #setCustomSizePreset} to show this notification with
3319 * default sizing.
3320 * <p>For custom display notifications created using {@link #setDisplayIntent},
3321 * the default is {@link #SIZE_LARGE}. All other notifications size automatically based
3322 * on their content.
3323 */
3324 public static final int SIZE_DEFAULT = 0;
3325
3326 /**
3327 * Size value for use with {@link #setCustomSizePreset} to show this notification
3328 * with an extra small size.
3329 * <p>This value is only applicable for custom display notifications created using
3330 * {@link #setDisplayIntent}.
3331 */
3332 public static final int SIZE_XSMALL = 1;
3333
3334 /**
3335 * Size value for use with {@link #setCustomSizePreset} to show this notification
3336 * with a small size.
3337 * <p>This value is only applicable for custom display notifications created using
3338 * {@link #setDisplayIntent}.
3339 */
3340 public static final int SIZE_SMALL = 2;
3341
3342 /**
3343 * Size value for use with {@link #setCustomSizePreset} to show this notification
3344 * with a medium size.
3345 * <p>This value is only applicable for custom display notifications created using
3346 * {@link #setDisplayIntent}.
3347 */
3348 public static final int SIZE_MEDIUM = 3;
3349
3350 /**
3351 * Size value for use with {@link #setCustomSizePreset} to show this notification
3352 * with a large size.
3353 * <p>This value is only applicable for custom display notifications created using
3354 * {@link #setDisplayIntent}.
3355 */
3356 public static final int SIZE_LARGE = 4;
3357
3358 /** Notification extra which contains wearable extensions */
3359 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
3360
3361 // Keys within EXTRA_WEARABLE_OPTIONS for wearable options.
3362 private static final String KEY_ACTIONS = "actions";
3363 private static final String KEY_FLAGS = "flags";
3364 private static final String KEY_DISPLAY_INTENT = "displayIntent";
3365 private static final String KEY_PAGES = "pages";
3366 private static final String KEY_BACKGROUND = "background";
3367 private static final String KEY_CONTENT_ICON = "contentIcon";
3368 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
3369 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
3370 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
3371 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
3372 private static final String KEY_GRAVITY = "gravity";
3373
3374 // Flags bitwise-ored to mFlags
3375 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
3376 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
3377 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
3378 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
3379
3380 // Default value for flags integer
3381 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
3382
3383 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
3384 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
3385
3386 private ArrayList<Action> mActions = new ArrayList<Action>();
3387 private int mFlags = DEFAULT_FLAGS;
3388 private PendingIntent mDisplayIntent;
3389 private ArrayList<Notification> mPages = new ArrayList<Notification>();
3390 private Bitmap mBackground;
3391 private int mContentIcon;
3392 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
3393 private int mContentActionIndex = UNSET_ACTION_INDEX;
3394 private int mCustomSizePreset = SIZE_DEFAULT;
3395 private int mCustomContentHeight;
3396 private int mGravity = DEFAULT_GRAVITY;
3397
3398 /**
3399 * Create a {@link android.app.Notification.WearableExtender} with default
3400 * options.
3401 */
3402 public WearableExtender() {
3403 }
3404
3405 public WearableExtender(Notification notif) {
3406 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
3407 if (wearableBundle != null) {
3408 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
3409 if (actions != null) {
3410 mActions.addAll(actions);
3411 }
3412
3413 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
3414 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
3415
3416 Notification[] pages = getNotificationArrayFromBundle(
3417 wearableBundle, KEY_PAGES);
3418 if (pages != null) {
3419 Collections.addAll(mPages, pages);
3420 }
3421
3422 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
3423 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
3424 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
3425 DEFAULT_CONTENT_ICON_GRAVITY);
3426 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
3427 UNSET_ACTION_INDEX);
3428 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
3429 SIZE_DEFAULT);
3430 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
3431 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
3432 }
3433 }
3434
3435 /**
3436 * Apply wearable extensions to a notification that is being built. This is typically
3437 * called by the {@link android.app.Notification.Builder#extend} method of
3438 * {@link android.app.Notification.Builder}.
3439 */
3440 @Override
3441 public Notification.Builder extend(Notification.Builder builder) {
3442 Bundle wearableBundle = new Bundle();
3443
3444 if (!mActions.isEmpty()) {
3445 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
3446 }
3447 if (mFlags != DEFAULT_FLAGS) {
3448 wearableBundle.putInt(KEY_FLAGS, mFlags);
3449 }
3450 if (mDisplayIntent != null) {
3451 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
3452 }
3453 if (!mPages.isEmpty()) {
3454 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
3455 new Notification[mPages.size()]));
3456 }
3457 if (mBackground != null) {
3458 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
3459 }
3460 if (mContentIcon != 0) {
3461 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
3462 }
3463 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
3464 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
3465 }
3466 if (mContentActionIndex != UNSET_ACTION_INDEX) {
3467 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
3468 mContentActionIndex);
3469 }
3470 if (mCustomSizePreset != SIZE_DEFAULT) {
3471 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
3472 }
3473 if (mCustomContentHeight != 0) {
3474 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
3475 }
3476 if (mGravity != DEFAULT_GRAVITY) {
3477 wearableBundle.putInt(KEY_GRAVITY, mGravity);
3478 }
3479
3480 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
3481 return builder;
3482 }
3483
3484 @Override
3485 public WearableExtender clone() {
3486 WearableExtender that = new WearableExtender();
3487 that.mActions = new ArrayList<Action>(this.mActions);
3488 that.mFlags = this.mFlags;
3489 that.mDisplayIntent = this.mDisplayIntent;
3490 that.mPages = new ArrayList<Notification>(this.mPages);
3491 that.mBackground = this.mBackground;
3492 that.mContentIcon = this.mContentIcon;
3493 that.mContentIconGravity = this.mContentIconGravity;
3494 that.mContentActionIndex = this.mContentActionIndex;
3495 that.mCustomSizePreset = this.mCustomSizePreset;
3496 that.mCustomContentHeight = this.mCustomContentHeight;
3497 that.mGravity = this.mGravity;
3498 return that;
3499 }
3500
3501 /**
3502 * Add a wearable action to this notification.
3503 *
3504 * <p>When wearable actions are added using this method, the set of actions that
3505 * show on a wearable device splits from devices that only show actions added
3506 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
3507 * of which actions display on different devices.
3508 *
3509 * @param action the action to add to this notification
3510 * @return this object for method chaining
3511 * @see android.app.Notification.Action
3512 */
3513 public WearableExtender addAction(Action action) {
3514 mActions.add(action);
3515 return this;
3516 }
3517
3518 /**
3519 * Adds wearable actions to this notification.
3520 *
3521 * <p>When wearable actions are added using this method, the set of actions that
3522 * show on a wearable device splits from devices that only show actions added
3523 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
3524 * of which actions display on different devices.
3525 *
3526 * @param actions the actions to add to this notification
3527 * @return this object for method chaining
3528 * @see android.app.Notification.Action
3529 */
3530 public WearableExtender addActions(List<Action> actions) {
3531 mActions.addAll(actions);
3532 return this;
3533 }
3534
3535 /**
3536 * Clear all wearable actions present on this builder.
3537 * @return this object for method chaining.
3538 * @see #addAction
3539 */
3540 public WearableExtender clearActions() {
3541 mActions.clear();
3542 return this;
3543 }
3544
3545 /**
3546 * Get the wearable actions present on this notification.
3547 */
3548 public List<Action> getActions() {
3549 return mActions;
3550 }
3551
3552 /**
3553 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07003554 * this notification. The {@link PendingIntent} provided should be for an activity.
3555 *
3556 * <pre class="prettyprint">
3557 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
3558 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
3559 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
3560 * Notification notif = new Notification.Builder(context)
3561 * .extend(new Notification.WearableExtender()
3562 * .setDisplayIntent(displayPendingIntent)
3563 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
3564 * .build();</pre>
3565 *
3566 * <p>The activity to launch needs to allow embedding, must be exported, and
3567 * should have an empty task affinity.
3568 *
3569 * <p>Example AndroidManifest.xml entry:
3570 * <pre class="prettyprint">
3571 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
3572 * android:exported=&quot;true&quot;
3573 * android:allowEmbedded=&quot;true&quot;
3574 * android:taskAffinity=&quot;&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07003575 *
3576 * @param intent the {@link PendingIntent} for an activity
3577 * @return this object for method chaining
3578 * @see android.app.Notification.WearableExtender#getDisplayIntent
3579 */
3580 public WearableExtender setDisplayIntent(PendingIntent intent) {
3581 mDisplayIntent = intent;
3582 return this;
3583 }
3584
3585 /**
3586 * Get the intent to launch inside of an activity view when displaying this
3587 * notification. This {@code PendingIntent} should be for an activity.
3588 */
3589 public PendingIntent getDisplayIntent() {
3590 return mDisplayIntent;
3591 }
3592
3593 /**
3594 * Add an additional page of content to display with this notification. The current
3595 * notification forms the first page, and pages added using this function form
3596 * subsequent pages. This field can be used to separate a notification into multiple
3597 * sections.
3598 *
3599 * @param page the notification to add as another page
3600 * @return this object for method chaining
3601 * @see android.app.Notification.WearableExtender#getPages
3602 */
3603 public WearableExtender addPage(Notification page) {
3604 mPages.add(page);
3605 return this;
3606 }
3607
3608 /**
3609 * Add additional pages of content to display with this notification. The current
3610 * notification forms the first page, and pages added using this function form
3611 * subsequent pages. This field can be used to separate a notification into multiple
3612 * sections.
3613 *
3614 * @param pages a list of notifications
3615 * @return this object for method chaining
3616 * @see android.app.Notification.WearableExtender#getPages
3617 */
3618 public WearableExtender addPages(List<Notification> pages) {
3619 mPages.addAll(pages);
3620 return this;
3621 }
3622
3623 /**
3624 * Clear all additional pages present on this builder.
3625 * @return this object for method chaining.
3626 * @see #addPage
3627 */
3628 public WearableExtender clearPages() {
3629 mPages.clear();
3630 return this;
3631 }
3632
3633 /**
3634 * Get the array of additional pages of content for displaying this notification. The
3635 * current notification forms the first page, and elements within this array form
3636 * subsequent pages. This field can be used to separate a notification into multiple
3637 * sections.
3638 * @return the pages for this notification
3639 */
3640 public List<Notification> getPages() {
3641 return mPages;
3642 }
3643
3644 /**
3645 * Set a background image to be displayed behind the notification content.
3646 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
3647 * will work with any notification style.
3648 *
3649 * @param background the background bitmap
3650 * @return this object for method chaining
3651 * @see android.app.Notification.WearableExtender#getBackground
3652 */
3653 public WearableExtender setBackground(Bitmap background) {
3654 mBackground = background;
3655 return this;
3656 }
3657
3658 /**
3659 * Get a background image to be displayed behind the notification content.
3660 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
3661 * will work with any notification style.
3662 *
3663 * @return the background image
3664 * @see android.app.Notification.WearableExtender#setBackground
3665 */
3666 public Bitmap getBackground() {
3667 return mBackground;
3668 }
3669
3670 /**
3671 * Set an icon that goes with the content of this notification.
3672 */
3673 public WearableExtender setContentIcon(int icon) {
3674 mContentIcon = icon;
3675 return this;
3676 }
3677
3678 /**
3679 * Get an icon that goes with the content of this notification.
3680 */
3681 public int getContentIcon() {
3682 return mContentIcon;
3683 }
3684
3685 /**
3686 * Set the gravity that the content icon should have within the notification display.
3687 * Supported values include {@link android.view.Gravity#START} and
3688 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
3689 * @see #setContentIcon
3690 */
3691 public WearableExtender setContentIconGravity(int contentIconGravity) {
3692 mContentIconGravity = contentIconGravity;
3693 return this;
3694 }
3695
3696 /**
3697 * Get the gravity that the content icon should have within the notification display.
3698 * Supported values include {@link android.view.Gravity#START} and
3699 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
3700 * @see #getContentIcon
3701 */
3702 public int getContentIconGravity() {
3703 return mContentIconGravity;
3704 }
3705
3706 /**
3707 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07003708 * this notification. This action will no longer display separately from the
3709 * notification's content.
3710 *
3711 * <p>For notifications with multiple pages, child pages can also have content action's
3712 * set, although the list of available actions comes from the main notification and not
3713 * from the child page's notification.
3714 *
3715 * @param actionIndex The index of the action to hoist onto the current notification page.
3716 * If wearable actions were added to the main notification, this index
3717 * will apply to that list, otherwise it will apply to the regular
3718 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07003719 */
3720 public WearableExtender setContentAction(int actionIndex) {
3721 mContentActionIndex = actionIndex;
3722 return this;
3723 }
3724
3725 /**
Griff Hazen14f57992014-05-26 09:07:14 -07003726 * Get the action index from this notification's actions to be clickable with the
3727 * content of this notification. This action will no longer display separately
3728 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07003729 *
Griff Hazen14f57992014-05-26 09:07:14 -07003730 * <p>For notifications with multiple pages, child pages can also have content action's
3731 * set, although the list of available actions comes from the main notification and not
3732 * from the child page's notification.
3733 *
3734 * <p>If wearable specific actions were added to the main notification, this index will
3735 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07003736 */
3737 public int getContentAction() {
3738 return mContentActionIndex;
3739 }
3740
3741 /**
3742 * Set the gravity that this notification should have within the available viewport space.
3743 * Supported values include {@link android.view.Gravity#TOP},
3744 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
3745 * The default value is {@link android.view.Gravity#BOTTOM}.
3746 */
3747 public WearableExtender setGravity(int gravity) {
3748 mGravity = gravity;
3749 return this;
3750 }
3751
3752 /**
3753 * Get the gravity that this notification should have within the available viewport space.
3754 * Supported values include {@link android.view.Gravity#TOP},
3755 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
3756 * The default value is {@link android.view.Gravity#BOTTOM}.
3757 */
3758 public int getGravity() {
3759 return mGravity;
3760 }
3761
3762 /**
3763 * Set the custom size preset for the display of this notification out of the available
3764 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
3765 * {@link #SIZE_LARGE}.
3766 * <p>Some custom size presets are only applicable for custom display notifications created
3767 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
3768 * documentation for the preset in question. See also
3769 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
3770 */
3771 public WearableExtender setCustomSizePreset(int sizePreset) {
3772 mCustomSizePreset = sizePreset;
3773 return this;
3774 }
3775
3776 /**
3777 * Get the custom size preset for the display of this notification out of the available
3778 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
3779 * {@link #SIZE_LARGE}.
3780 * <p>Some custom size presets are only applicable for custom display notifications created
3781 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
3782 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
3783 */
3784 public int getCustomSizePreset() {
3785 return mCustomSizePreset;
3786 }
3787
3788 /**
3789 * Set the custom height in pixels for the display of this notification's content.
3790 * <p>This option is only available for custom display notifications created
3791 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
3792 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
3793 * {@link #getCustomContentHeight}.
3794 */
3795 public WearableExtender setCustomContentHeight(int height) {
3796 mCustomContentHeight = height;
3797 return this;
3798 }
3799
3800 /**
3801 * Get the custom height in pixels for the display of this notification's content.
3802 * <p>This option is only available for custom display notifications created
3803 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
3804 * {@link #setCustomContentHeight}.
3805 */
3806 public int getCustomContentHeight() {
3807 return mCustomContentHeight;
3808 }
3809
3810 /**
3811 * Set whether the scrolling position for the contents of this notification should start
3812 * at the bottom of the contents instead of the top when the contents are too long to
3813 * display within the screen. Default is false (start scroll at the top).
3814 */
3815 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
3816 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
3817 return this;
3818 }
3819
3820 /**
3821 * Get whether the scrolling position for the contents of this notification should start
3822 * at the bottom of the contents instead of the top when the contents are too long to
3823 * display within the screen. Default is false (start scroll at the top).
3824 */
3825 public boolean getStartScrollBottom() {
3826 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
3827 }
3828
3829 /**
3830 * Set whether the content intent is available when the wearable device is not connected
3831 * to a companion device. The user can still trigger this intent when the wearable device
3832 * is offline, but a visual hint will indicate that the content intent may not be available.
3833 * Defaults to true.
3834 */
3835 public WearableExtender setContentIntentAvailableOffline(
3836 boolean contentIntentAvailableOffline) {
3837 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
3838 return this;
3839 }
3840
3841 /**
3842 * Get whether the content intent is available when the wearable device is not connected
3843 * to a companion device. The user can still trigger this intent when the wearable device
3844 * is offline, but a visual hint will indicate that the content intent may not be available.
3845 * Defaults to true.
3846 */
3847 public boolean getContentIntentAvailableOffline() {
3848 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
3849 }
3850
3851 /**
3852 * Set a hint that this notification's icon should not be displayed.
3853 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
3854 * @return this object for method chaining
3855 */
3856 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
3857 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
3858 return this;
3859 }
3860
3861 /**
3862 * Get a hint that this notification's icon should not be displayed.
3863 * @return {@code true} if this icon should not be displayed, false otherwise.
3864 * The default value is {@code false} if this was never set.
3865 */
3866 public boolean getHintHideIcon() {
3867 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
3868 }
3869
3870 /**
3871 * Set a visual hint that only the background image of this notification should be
3872 * displayed, and other semantic content should be hidden. This hint is only applicable
3873 * to sub-pages added using {@link #addPage}.
3874 */
3875 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
3876 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
3877 return this;
3878 }
3879
3880 /**
3881 * Get a visual hint that only the background image of this notification should be
3882 * displayed, and other semantic content should be hidden. This hint is only applicable
3883 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
3884 */
3885 public boolean getHintShowBackgroundOnly() {
3886 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
3887 }
3888
3889 private void setFlag(int mask, boolean value) {
3890 if (value) {
3891 mFlags |= mask;
3892 } else {
3893 mFlags &= ~mask;
3894 }
3895 }
3896 }
3897
3898 /**
3899 * Get an array of Notification objects from a parcelable array bundle field.
3900 * Update the bundle to have a typed array so fetches in the future don't need
3901 * to do an array copy.
3902 */
3903 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
3904 Parcelable[] array = bundle.getParcelableArray(key);
3905 if (array instanceof Notification[] || array == null) {
3906 return (Notification[]) array;
3907 }
3908 Notification[] typedArray = Arrays.copyOf(array, array.length,
3909 Notification[].class);
3910 bundle.putParcelableArray(key, typedArray);
3911 return typedArray;
3912 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003913}