blob: bffbb519a89a03b67fea5d43cac9e7766f77142c [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
Andy Stadler110988c2010-12-03 14:29:16 -080019import com.android.internal.R;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.content.Context;
22import android.content.Intent;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040023import android.content.res.Resources;
Joe Onoratoef1e7762010-09-17 18:38:38 -040024import android.graphics.Bitmap;
Jeff Sharkey098d5802012-04-26 17:30:34 -070025import android.media.AudioManager;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040027import android.os.BadParcelableException;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050028import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.os.Parcel;
30import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040031import android.os.SystemClock;
Jeff Sharkey6d515712012-09-20 16:06:08 -070032import android.os.UserHandle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033import android.text.TextUtils;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040034import android.util.Log;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040035import android.util.TypedValue;
Joe Onorato8595a3d2010-11-19 18:12:07 -080036import android.view.View;
Jeff Sharkey1c400132011-08-05 14:50:13 -070037import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080038import android.widget.RemoteViews;
39
Andy Stadler110988c2010-12-03 14:29:16 -080040import java.text.NumberFormat;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050041import java.util.ArrayList;
Joe Onorato561d3852010-11-20 18:09:34 -080042
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043/**
44 * A class that represents how a persistent notification is to be presented to
45 * the user using the {@link android.app.NotificationManager}.
46 *
Joe Onoratocb109a02011-01-18 17:57:41 -080047 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
48 * easier to construct Notifications.</p>
49 *
Joe Fernandez558459f2011-10-13 16:47:36 -070050 * <div class="special reference">
51 * <h3>Developer Guides</h3>
52 * <p>For a guide to creating notifications, read the
53 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
54 * developer guide.</p>
55 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056 */
57public class Notification implements Parcelable
58{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040059 private static final String TAG = "Notification";
60
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080061 /**
62 * Use all default values (where applicable).
63 */
64 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 /**
67 * Use the default notification sound. This will ignore any given
68 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050069 *
70
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050072 */
73
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 public static final int DEFAULT_SOUND = 1;
75
76 /**
77 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -050078 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -070079 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050080 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050082 */
83
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 /**
87 * Use the default notification lights. This will ignore the
88 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
89 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -050090 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -050092 */
93
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050095
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080096 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -050097 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -080098 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -050099 * Default value: {@link System#currentTimeMillis() Now}.
100 *
101 * Choose a timestamp that will be most relevant to the user. For most finite events, this
102 * corresponds to the time the event happened (or will happen, in the case of events that have
103 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800104 * timestamped according to when the activity began.
105 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500106 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800107 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500108 * <ul>
109 * <li>Notification of a new chat message should be stamped when the message was received.</li>
110 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
111 * <li>Notification of a completed file download should be stamped when the download finished.</li>
112 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
113 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
114 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800115 * </ul>
116 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 */
118 public long when;
119
120 /**
121 * The resource id of a drawable to use as the icon in the status bar.
Daniel Sandlerd952dae2011-02-07 16:33:36 -0500122 * This is required; notifications with an invalid icon resource will not be shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 */
124 public int icon;
125
126 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800127 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
128 * leave it at its default value of 0.
129 *
130 * @see android.widget.ImageView#setImageLevel
131 * @see android.graphics.drawable#setLevel
132 */
133 public int iconLevel;
134
135 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500136 * The number of events that this notification represents. For example, in a new mail
137 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800138 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500139 * The system may or may not use this field to modify the appearance of the notification. For
140 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
141 * superimposed over the icon in the status bar. Starting with
142 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
143 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Joe Malin8d40d042012-11-05 11:36:40 -0800144 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500145 * If the number is 0 or negative, it is never shown.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 */
147 public int number;
148
149 /**
150 * The intent to execute when the expanded status entry is clicked. If
151 * this is an activity, it must include the
152 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800153 * that you take care of task management as described in the
154 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800155 * Stack</a> document. In particular, make sure to read the notification section
156 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
157 * Notifications</a> for the correct ways to launch an application from a
158 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 */
160 public PendingIntent contentIntent;
161
162 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500163 * The intent to execute when the notification is explicitly dismissed by the user, either with
164 * the "Clear All" button or by swiping it away individually.
165 *
166 * This probably shouldn't be launching an activity since several of those will be sent
167 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 */
169 public PendingIntent deleteIntent;
170
171 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700172 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800173 *
174 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400175 */
176 public PendingIntent fullScreenIntent;
177
178 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 * Text to scroll across the screen when this item is added to
Joe Onoratoef1e7762010-09-17 18:38:38 -0400180 * the status bar on large and smaller devices.
181 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800182 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 */
184 public CharSequence tickerText;
185
186 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800187 * The view to show as the ticker in the status bar when the notification
188 * is posted.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400189 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800190 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400191
192 /**
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400193 * The view that will represent this notification in the expanded status bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 */
195 public RemoteViews contentView;
196
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400197 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400198 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400199 * opportunity to show more detail. The system UI may choose to show this
200 * instead of the normal content view at its discretion.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400201 */
202 public RemoteViews bigContentView;
203
204 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800205 * The bitmap that may escape the bounds of the panel and bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800206 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800207 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208
209 /**
210 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500211 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500213 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 * </p>
215 */
216 public Uri sound;
217
218 /**
219 * Use this constant as the value for audioStreamType to request that
220 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700221 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 */
223 public static final int STREAM_DEFAULT = -1;
224
225 /**
226 * The audio stream type to use when playing the sound.
227 * Should be one of the STREAM_ constants from
228 * {@link android.media.AudioManager}.
229 */
230 public int audioStreamType = STREAM_DEFAULT;
231
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500233 * The pattern with which to vibrate.
234 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800235 * <p>
236 * To vibrate the default pattern, see {@link #defaults}.
237 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500238 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800239 * @see android.os.Vibrator#vibrate(long[],int)
240 */
241 public long[] vibrate;
242
243 /**
244 * The color of the led. The hardware will do its best approximation.
245 *
246 * @see #FLAG_SHOW_LIGHTS
247 * @see #flags
248 */
249 public int ledARGB;
250
251 /**
252 * The number of milliseconds for the LED to be on while it's flashing.
253 * The hardware will do its best approximation.
254 *
255 * @see #FLAG_SHOW_LIGHTS
256 * @see #flags
257 */
258 public int ledOnMS;
259
260 /**
261 * The number of milliseconds for the LED to be off while it's flashing.
262 * The hardware will do its best approximation.
263 *
264 * @see #FLAG_SHOW_LIGHTS
265 * @see #flags
266 */
267 public int ledOffMS;
268
269 /**
270 * Specifies which values should be taken from the defaults.
271 * <p>
272 * To set, OR the desired from {@link #DEFAULT_SOUND},
273 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
274 * values, use {@link #DEFAULT_ALL}.
275 * </p>
276 */
277 public int defaults;
278
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 /**
280 * Bit to be bitwise-ored into the {@link #flags} field that should be
281 * set if you want the LED on for this notification.
282 * <ul>
283 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
284 * or 0 for both ledOnMS and ledOffMS.</li>
285 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
286 * <li>To flash the LED, pass the number of milliseconds that it should
287 * be on and off to ledOnMS and ledOffMS.</li>
288 * </ul>
289 * <p>
290 * Since hardware varies, you are not guaranteed that any of the values
291 * you pass are honored exactly. Use the system defaults (TODO) if possible
292 * because they will be set to values that work on any given hardware.
293 * <p>
294 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500295 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800296 */
297 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
298
299 /**
300 * Bit to be bitwise-ored into the {@link #flags} field that should be
301 * set if this notification is in reference to something that is ongoing,
302 * like a phone call. It should not be set if this notification is in
303 * reference to something that happened at a particular point in time,
304 * like a missed phone call.
305 */
306 public static final int FLAG_ONGOING_EVENT = 0x00000002;
307
308 /**
309 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700310 * the audio will be repeated until the notification is
311 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 */
313 public static final int FLAG_INSISTENT = 0x00000004;
314
315 /**
316 * Bit to be bitwise-ored into the {@link #flags} field that should be
317 * set if you want the sound and/or vibration play each time the
318 * notification is sent, even if it has not been canceled before that.
319 */
320 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
321
322 /**
323 * Bit to be bitwise-ored into the {@link #flags} field that should be
324 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500325 * user.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500326
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800327 */
328 public static final int FLAG_AUTO_CANCEL = 0x00000010;
329
330 /**
331 * Bit to be bitwise-ored into the {@link #flags} field that should be
332 * set if the notification should not be canceled when the user clicks
333 * the Clear all button.
334 */
335 public static final int FLAG_NO_CLEAR = 0x00000020;
336
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700337 /**
338 * Bit to be bitwise-ored into the {@link #flags} field that should be
339 * set if this notification represents a currently running service. This
340 * will normally be set for you by {@link Service#startForeground}.
341 */
342 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
343
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400344 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500345 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800346 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500347 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400348 */
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500349 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400350
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800351 public int flags;
352
353 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500354 * Default notification {@link #priority}. If your application does not prioritize its own
355 * notifications, use this value for all notifications.
356 */
357 public static final int PRIORITY_DEFAULT = 0;
358
359 /**
360 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
361 * items smaller, or at a different position in the list, compared with your app's
362 * {@link #PRIORITY_DEFAULT} items.
363 */
364 public static final int PRIORITY_LOW = -1;
365
366 /**
367 * Lowest {@link #priority}; these items might not be shown to the user except under special
368 * circumstances, such as detailed notification logs.
369 */
370 public static final int PRIORITY_MIN = -2;
371
372 /**
373 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
374 * show these items larger, or at a different position in notification lists, compared with
375 * your app's {@link #PRIORITY_DEFAULT} items.
376 */
377 public static final int PRIORITY_HIGH = 1;
378
379 /**
380 * Highest {@link #priority}, for your application's most important items that require the
381 * user's prompt attention or input.
382 */
383 public static final int PRIORITY_MAX = 2;
384
385 /**
386 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800387 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500388 * Priority is an indication of how much of the user's valuable attention should be consumed by
389 * this notification. Low-priority notifications may be hidden from the user in certain
390 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500391 * system will make a determination about how to interpret this priority when presenting
392 * the notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500393 */
394 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800395
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500396 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -0400397 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500398 * Notification type: incoming call (voice or video) or similar synchronous communication request.
399 */
400 public static final String KIND_CALL = "android.call";
401
402 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -0400403 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500404 * Notification type: incoming direct message (SMS, instant message, etc.).
405 */
406 public static final String KIND_MESSAGE = "android.message";
407
408 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -0400409 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500410 * Notification type: asynchronous bulk message (email).
411 */
412 public static final String KIND_EMAIL = "android.email";
413
414 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -0400415 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500416 * Notification type: calendar event.
417 */
418 public static final String KIND_EVENT = "android.event";
419
420 /**
Daniel Sandlera90513d2012-06-04 02:11:17 -0400421 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500422 * Notification type: promotion or advertisement.
423 */
424 public static final String KIND_PROMO = "android.promo";
425
426 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -0400427 * @hide
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500428 * If this notification matches of one or more special types (see the <code>KIND_*</code>
429 * constants), add them here, best match first.
430 */
431 public String[] kind;
432
433 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400434 * Additional semantic data to be carried around with this Notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500435 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400436 public Bundle extras = new Bundle();
437
438 // extras keys for Builder inputs
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500439 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400440 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500441 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400442 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400443 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400444 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500445 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400446 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400447 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400448 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400449 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400450 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400451 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400452 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400453 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400454 public static final String EXTRA_TEXT_LINES = "android.textLines";
455
456 // extras keys for other interesting pieces of information
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400457 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500458
459 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400460 * Structure to encapsulate an "action", including title and icon, that can be attached to a Notification.
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400461 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500462 public static class Action implements Parcelable {
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400463 public int icon;
464 public CharSequence title;
465 public PendingIntent actionIntent;
466 @SuppressWarnings("unused")
467 public Action() { }
468 private Action(Parcel in) {
469 icon = in.readInt();
470 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
471 if (in.readInt() == 1) {
472 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
473 }
474 }
475 public Action(int icon_, CharSequence title_, PendingIntent intent_) {
476 this.icon = icon_;
477 this.title = title_;
478 this.actionIntent = intent_;
479 }
480 @Override
481 public Action clone() {
482 return new Action(
483 this.icon,
484 this.title.toString(),
485 this.actionIntent // safe to alias
486 );
487 }
488 @Override
489 public int describeContents() {
490 return 0;
491 }
492 @Override
493 public void writeToParcel(Parcel out, int flags) {
494 out.writeInt(icon);
495 TextUtils.writeToParcel(title, out, flags);
496 if (actionIntent != null) {
497 out.writeInt(1);
498 actionIntent.writeToParcel(out, flags);
499 } else {
500 out.writeInt(0);
501 }
502 }
503 public static final Parcelable.Creator<Action> CREATOR
504 = new Parcelable.Creator<Action>() {
505 public Action createFromParcel(Parcel in) {
506 return new Action(in);
507 }
508 public Action[] newArray(int size) {
509 return new Action[size];
510 }
511 };
512 }
513
Daniel Sandlerea2a3172013-02-20 22:24:20 -0500514 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400515
516 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500517 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -0800518 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800519 */
520 public Notification()
521 {
522 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500523 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800524 }
525
526 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800527 * @hide
528 */
529 public Notification(Context context, int icon, CharSequence tickerText, long when,
530 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
531 {
532 this.when = when;
533 this.icon = icon;
534 this.tickerText = tickerText;
535 setLatestEventInfo(context, contentTitle, contentText,
536 PendingIntent.getActivity(context, 0, contentIntent, 0));
537 }
538
539 /**
540 * Constructs a Notification object with the information needed to
541 * have a status bar icon without the standard expanded view.
542 *
543 * @param icon The resource id of the icon to put in the status bar.
544 * @param tickerText The text that flows by in the status bar when the notification first
545 * activates.
546 * @param when The time to show in the time field. In the System.currentTimeMillis
547 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -0800548 *
549 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800550 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800551 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800552 public Notification(int icon, CharSequence tickerText, long when)
553 {
554 this.icon = icon;
555 this.tickerText = tickerText;
556 this.when = when;
557 }
558
559 /**
560 * Unflatten the notification from a parcel.
561 */
562 public Notification(Parcel parcel)
563 {
564 int version = parcel.readInt();
565
566 when = parcel.readLong();
567 icon = parcel.readInt();
568 number = parcel.readInt();
569 if (parcel.readInt() != 0) {
570 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
571 }
572 if (parcel.readInt() != 0) {
573 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
574 }
575 if (parcel.readInt() != 0) {
576 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
577 }
578 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -0800579 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -0400580 }
581 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800582 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
583 }
Joe Onorato561d3852010-11-20 18:09:34 -0800584 if (parcel.readInt() != 0) {
585 largeIcon = Bitmap.CREATOR.createFromParcel(parcel);
586 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800587 defaults = parcel.readInt();
588 flags = parcel.readInt();
589 if (parcel.readInt() != 0) {
590 sound = Uri.CREATOR.createFromParcel(parcel);
591 }
592
593 audioStreamType = parcel.readInt();
594 vibrate = parcel.createLongArray();
595 ledARGB = parcel.readInt();
596 ledOnMS = parcel.readInt();
597 ledOffMS = parcel.readInt();
598 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400599
600 if (parcel.readInt() != 0) {
601 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
602 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500603
604 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400605
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500606 kind = parcel.createStringArray(); // may set kind to null
607
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500608 extras = parcel.readBundle(); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400609
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500610 actions = parcel.createTypedArray(Action.CREATOR); // may be null
611
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400612 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400613 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
614 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800615 }
616
Andy Stadler110988c2010-12-03 14:29:16 -0800617 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -0700618 public Notification clone() {
619 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -0400620 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500621 return that;
622 }
Joe Onorato18e69df2010-05-17 22:26:12 -0700623
Daniel Sandler1a497d32013-04-18 14:52:45 -0400624 /**
625 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
626 * of this into that.
627 * @hide
628 */
629 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -0700630 that.when = this.when;
631 that.icon = this.icon;
632 that.number = this.number;
633
634 // PendingIntents are global, so there's no reason (or way) to clone them.
635 that.contentIntent = this.contentIntent;
636 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400637 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -0700638
639 if (this.tickerText != null) {
640 that.tickerText = this.tickerText.toString();
641 }
Daniel Sandler1a497d32013-04-18 14:52:45 -0400642 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -0800643 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -0400644 }
Daniel Sandler1a497d32013-04-18 14:52:45 -0400645 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -0700646 that.contentView = this.contentView.clone();
647 }
Daniel Sandler1a497d32013-04-18 14:52:45 -0400648 if (heavy && this.largeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -0800649 that.largeIcon = Bitmap.createBitmap(this.largeIcon);
650 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +0100651 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -0700652 that.sound = this.sound; // android.net.Uri is immutable
653 that.audioStreamType = this.audioStreamType;
654
655 final long[] vibrate = this.vibrate;
656 if (vibrate != null) {
657 final int N = vibrate.length;
658 final long[] vib = that.vibrate = new long[N];
659 System.arraycopy(vibrate, 0, vib, 0, N);
660 }
661
662 that.ledARGB = this.ledARGB;
663 that.ledOnMS = this.ledOnMS;
664 that.ledOffMS = this.ledOffMS;
665 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500666
Joe Onorato18e69df2010-05-17 22:26:12 -0700667 that.flags = this.flags;
668
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500669 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800670
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500671 final String[] thiskind = this.kind;
672 if (thiskind != null) {
673 final int N = thiskind.length;
674 final String[] thatkind = that.kind = new String[N];
675 System.arraycopy(thiskind, 0, thatkind, 0, N);
676 }
677
678 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400679 try {
680 that.extras = new Bundle(this.extras);
681 // will unparcel
682 that.extras.size();
683 } catch (BadParcelableException e) {
684 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
685 that.extras = null;
686 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500687 }
688
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500689 if (this.actions != null) {
690 that.actions = new Action[this.actions.length];
691 for(int i=0; i<this.actions.length; i++) {
692 that.actions[i] = this.actions[i].clone();
693 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400694 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500695
Daniel Sandler1a497d32013-04-18 14:52:45 -0400696 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400697 that.bigContentView = this.bigContentView.clone();
698 }
Daniel Sandler1a497d32013-04-18 14:52:45 -0400699
700 if (!heavy) {
701 that.lightenPayload(); // will clean out extras
702 }
703 }
704
705 /**
706 * Removes heavyweight parts of the Notification object for archival or for sending to
707 * listeners when the full contents are not necessary.
708 * @hide
709 */
710 public final void lightenPayload() {
711 tickerView = null;
712 contentView = null;
713 bigContentView = null;
714 largeIcon = null;
715 if (extras != null) {
716 extras.remove(Notification.EXTRA_LARGE_ICON);
717 extras.remove(Notification.EXTRA_LARGE_ICON_BIG);
718 extras.remove(Notification.EXTRA_PICTURE);
719 }
Joe Onorato18e69df2010-05-17 22:26:12 -0700720 }
721
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400722 /**
723 * Make sure this CharSequence is safe to put into a bundle, which basically
724 * means it had better not be some custom Parcelable implementation.
725 * @hide
726 */
727 public static CharSequence safeCharSequence(CharSequence cs) {
728 if (cs instanceof Parcelable) {
729 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
730 + " instance is a custom Parcelable and not allowed in Notification");
731 return cs.toString();
732 }
733
734 return cs;
735 }
736
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800737 public int describeContents() {
738 return 0;
739 }
740
741 /**
742 * Flatten this notification from a parcel.
743 */
744 public void writeToParcel(Parcel parcel, int flags)
745 {
746 parcel.writeInt(1);
747
748 parcel.writeLong(when);
749 parcel.writeInt(icon);
750 parcel.writeInt(number);
751 if (contentIntent != null) {
752 parcel.writeInt(1);
753 contentIntent.writeToParcel(parcel, 0);
754 } else {
755 parcel.writeInt(0);
756 }
757 if (deleteIntent != null) {
758 parcel.writeInt(1);
759 deleteIntent.writeToParcel(parcel, 0);
760 } else {
761 parcel.writeInt(0);
762 }
763 if (tickerText != null) {
764 parcel.writeInt(1);
765 TextUtils.writeToParcel(tickerText, parcel, flags);
766 } else {
767 parcel.writeInt(0);
768 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800769 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -0400770 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -0800771 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -0400772 } else {
773 parcel.writeInt(0);
774 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800775 if (contentView != null) {
776 parcel.writeInt(1);
777 contentView.writeToParcel(parcel, 0);
778 } else {
779 parcel.writeInt(0);
780 }
Joe Onorato561d3852010-11-20 18:09:34 -0800781 if (largeIcon != null) {
782 parcel.writeInt(1);
783 largeIcon.writeToParcel(parcel, 0);
784 } else {
785 parcel.writeInt(0);
786 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800787
788 parcel.writeInt(defaults);
789 parcel.writeInt(this.flags);
790
791 if (sound != null) {
792 parcel.writeInt(1);
793 sound.writeToParcel(parcel, 0);
794 } else {
795 parcel.writeInt(0);
796 }
797 parcel.writeInt(audioStreamType);
798 parcel.writeLongArray(vibrate);
799 parcel.writeInt(ledARGB);
800 parcel.writeInt(ledOnMS);
801 parcel.writeInt(ledOffMS);
802 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400803
804 if (fullScreenIntent != null) {
805 parcel.writeInt(1);
806 fullScreenIntent.writeToParcel(parcel, 0);
807 } else {
808 parcel.writeInt(0);
809 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500810
811 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -0800812
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500813 parcel.writeStringArray(kind); // ok for null
Joe Malin8d40d042012-11-05 11:36:40 -0800814
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500815 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400816
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500817 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400818
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400819 if (bigContentView != null) {
820 parcel.writeInt(1);
821 bigContentView.writeToParcel(parcel, 0);
822 } else {
823 parcel.writeInt(0);
824 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800825 }
826
827 /**
828 * Parcelable.Creator that instantiates Notification objects
829 */
830 public static final Parcelable.Creator<Notification> CREATOR
831 = new Parcelable.Creator<Notification>()
832 {
833 public Notification createFromParcel(Parcel parcel)
834 {
835 return new Notification(parcel);
836 }
837
838 public Notification[] newArray(int size)
839 {
840 return new Notification[size];
841 }
842 };
843
844 /**
845 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
846 * layout.
847 *
848 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
849 * in the view.</p>
850 * @param context The context for your application / activity.
851 * @param contentTitle The title that goes in the expanded entry.
852 * @param contentText The text that goes in the expanded entry.
853 * @param contentIntent The intent to launch when the user clicks the expanded notification.
854 * If this is an activity, it must include the
855 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800856 * that you take care of task management as described in the
857 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
858 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500859 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800860 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800861 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800862 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 public void setLatestEventInfo(Context context,
864 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500865 Notification.Builder builder = new Notification.Builder(context);
866
867 // First, ensure that key pieces of information that may have been set directly
868 // are preserved
869 builder.setWhen(this.when);
870 builder.setSmallIcon(this.icon);
871 builder.setPriority(this.priority);
872 builder.setTicker(this.tickerText);
873 builder.setNumber(this.number);
874 builder.mFlags = this.flags;
875 builder.setSound(this.sound, this.audioStreamType);
876 builder.setDefaults(this.defaults);
877 builder.setVibrate(this.vibrate);
878
879 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500881 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 }
883 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500884 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800885 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500886 builder.setContentIntent(contentIntent);
887 builder.buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 }
889
890 @Override
891 public String toString() {
892 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500893 sb.append("Notification(pri=");
894 sb.append(priority);
895 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -0800896 if (contentView != null) {
897 sb.append(contentView.getPackage());
898 sb.append("/0x");
899 sb.append(Integer.toHexString(contentView.getLayoutId()));
900 } else {
901 sb.append("null");
902 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500903 // TODO(dsandler): defaults take precedence over local values, so reorder the branches below
Joe Onoratoc9596d62011-01-12 17:03:11 -0800904 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -0500905 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
906 sb.append("default");
907 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800908 int N = this.vibrate.length-1;
909 sb.append("[");
910 for (int i=0; i<N; i++) {
911 sb.append(this.vibrate[i]);
912 sb.append(',');
913 }
Simon Schoar8cf97d92009-06-10 22:08:37 +0200914 if (N != -1) {
915 sb.append(this.vibrate[N]);
916 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800917 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800918 } else {
919 sb.append("null");
920 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500921 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -0500922 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -0500924 } else if (this.sound != null) {
925 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800926 } else {
927 sb.append("null");
928 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500929 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800930 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500931 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400932 sb.append(Integer.toHexString(this.flags));
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500933 sb.append(" kind=[");
934 if (this.kind == null) {
935 sb.append("null");
936 } else {
937 for (int i=0; i<this.kind.length; i++) {
938 if (i>0) sb.append(",");
939 sb.append(this.kind[i]);
940 }
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400941 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400942 sb.append("]");
943 if (actions != null) {
944 sb.append(" ");
945 sb.append(actions.length);
946 sb.append(" action");
947 if (actions.length > 1) sb.append("s");
948 }
949 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800950 return sb.toString();
951 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800952
Jeff Sharkey6d515712012-09-20 16:06:08 -0700953 /** {@hide} */
954 public void setUser(UserHandle user) {
Amith Yamasaniecbd68b2012-11-02 12:17:19 -0700955 if (user.getIdentifier() == UserHandle.USER_ALL) {
956 user = UserHandle.OWNER;
957 }
Jeff Sharkey6d515712012-09-20 16:06:08 -0700958 if (tickerView != null) {
959 tickerView.setUser(user);
960 }
961 if (contentView != null) {
962 contentView.setUser(user);
963 }
964 if (bigContentView != null) {
965 bigContentView.setUser(user);
966 }
967 }
968
Joe Onoratocb109a02011-01-18 17:57:41 -0800969 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500970 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -0800971 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500972 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -0700973 * content views using the platform's notification layout template. If your app supports
974 * versions of Android as old as API level 4, you can instead use
975 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
976 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
977 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -0800978 *
Scott Main183bf112012-08-13 19:12:13 -0700979 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -0800980 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500981 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -0700982 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500983 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
984 * .setContentText(subject)
985 * .setSmallIcon(R.drawable.new_mail)
986 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -0400987 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500988 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -0800989 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800990 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -0400991 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -0400992
Joe Onorato46439ce2010-11-19 13:56:21 -0800993 private Context mContext;
994
995 private long mWhen;
996 private int mSmallIcon;
997 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -0800998 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -0800999 private CharSequence mContentTitle;
1000 private CharSequence mContentText;
1001 private CharSequence mContentInfo;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001002 private CharSequence mSubText;
Joe Onorato46439ce2010-11-19 13:56:21 -08001003 private PendingIntent mContentIntent;
1004 private RemoteViews mContentView;
1005 private PendingIntent mDeleteIntent;
1006 private PendingIntent mFullScreenIntent;
1007 private CharSequence mTickerText;
1008 private RemoteViews mTickerView;
1009 private Bitmap mLargeIcon;
1010 private Uri mSound;
1011 private int mAudioStreamType;
1012 private long[] mVibrate;
1013 private int mLedArgb;
1014 private int mLedOnMs;
1015 private int mLedOffMs;
1016 private int mDefaults;
1017 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -07001018 private int mProgressMax;
1019 private int mProgress;
1020 private boolean mProgressIndeterminate;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001021 private ArrayList<String> mKindList = new ArrayList<String>(1);
1022 private Bundle mExtras;
1023 private int mPriority;
Daniel Sandler8680bf82012-05-15 16:52:52 -04001024 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001025 private boolean mUseChronometer;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001026 private Style mStyle;
Daniel Sandler0c890492012-09-12 17:23:10 -07001027 private boolean mShowWhen = true;
Joe Onorato46439ce2010-11-19 13:56:21 -08001028
Joe Onoratocb109a02011-01-18 17:57:41 -08001029 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001030 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08001031 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001032
1033 * <table>
1034 * <tr><th align=right>priority</th>
1035 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
1036 * <tr><th align=right>when</th>
1037 * <td>now ({@link System#currentTimeMillis()})</td></tr>
1038 * <tr><th align=right>audio stream</th>
1039 * <td>{@link #STREAM_DEFAULT}</td></tr>
1040 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08001041 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001042
1043 * @param context
1044 * A {@link Context} that will be used by the Builder to construct the
1045 * RemoteViews. The Context will not be held past the lifetime of this Builder
1046 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -08001047 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001048 public Builder(Context context) {
1049 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -08001050
1051 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -08001052 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -08001053 mAudioStreamType = STREAM_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001054 mPriority = PRIORITY_DEFAULT;
Joe Onorato46439ce2010-11-19 13:56:21 -08001055 }
1056
Joe Onoratocb109a02011-01-18 17:57:41 -08001057 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001058 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07001059 * It will be shown in the notification content view by default; use
1060 * {@link Builder#setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001061 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001062 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08001063 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001064 public Builder setWhen(long when) {
1065 mWhen = when;
1066 return this;
1067 }
1068
Joe Onoratocb109a02011-01-18 17:57:41 -08001069 /**
Daniel Sandler0c890492012-09-12 17:23:10 -07001070 * Control whether the timestamp set with {@link Builder#setWhen(long) setWhen} is shown
1071 * in the content view.
1072 */
1073 public Builder setShowWhen(boolean show) {
1074 mShowWhen = show;
1075 return this;
1076 }
1077
1078 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001079 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08001080 *
1081 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001082 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001083 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001084 * Useful when showing an elapsed time (like an ongoing phone call).
1085 *
1086 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001087 * @see Notification#when
1088 */
1089 public Builder setUsesChronometer(boolean b) {
1090 mUseChronometer = b;
1091 return this;
1092 }
1093
1094 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001095 * Set the small icon resource, which will be used to represent the notification in the
1096 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08001097 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001098
1099 * The platform template for the expanded view will draw this icon in the left, unless a
1100 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
1101 * icon will be moved to the right-hand side.
1102 *
1103
1104 * @param icon
1105 * A resource ID in the application's package of the drawable to use.
1106 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08001107 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001108 public Builder setSmallIcon(int icon) {
1109 mSmallIcon = icon;
1110 return this;
1111 }
1112
Joe Onoratocb109a02011-01-18 17:57:41 -08001113 /**
1114 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
1115 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
1116 * LevelListDrawable}.
1117 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001118 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08001119 * @param level The level to use for the icon.
1120 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001121 * @see Notification#icon
1122 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08001123 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001124 public Builder setSmallIcon(int icon, int level) {
1125 mSmallIcon = icon;
1126 mSmallIconLevel = level;
1127 return this;
1128 }
1129
Joe Onoratocb109a02011-01-18 17:57:41 -08001130 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001131 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001132 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001133 public Builder setContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001134 mContentTitle = safeCharSequence(title);
Joe Onorato46439ce2010-11-19 13:56:21 -08001135 return this;
1136 }
1137
Joe Onoratocb109a02011-01-18 17:57:41 -08001138 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001139 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001140 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001141 public Builder setContentText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001142 mContentText = safeCharSequence(text);
Joe Onorato46439ce2010-11-19 13:56:21 -08001143 return this;
1144 }
1145
Joe Onoratocb109a02011-01-18 17:57:41 -08001146 /**
Joe Malin8d40d042012-11-05 11:36:40 -08001147 * Set the third line of text in the platform notification template.
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001148 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the
1149 * same location in the standard template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001150 */
1151 public Builder setSubText(CharSequence text) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001152 mSubText = safeCharSequence(text);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001153 return this;
1154 }
1155
1156 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08001157 * Set the large number at the right-hand side of the notification. This is
1158 * equivalent to setContentInfo, although it might show the number in a different
1159 * font size for readability.
1160 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08001161 public Builder setNumber(int number) {
1162 mNumber = number;
1163 return this;
1164 }
1165
Joe Onoratocb109a02011-01-18 17:57:41 -08001166 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001167 * A small piece of additional information pertaining to this notification.
1168 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001169 * The platform template will draw this on the last line of the notification, at the far
1170 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08001171 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001172 public Builder setContentInfo(CharSequence info) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001173 mContentInfo = safeCharSequence(info);
Joe Onorato46439ce2010-11-19 13:56:21 -08001174 return this;
1175 }
1176
Joe Onoratocb109a02011-01-18 17:57:41 -08001177 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001178 * Set the progress this notification represents.
1179 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001180 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07001181 */
1182 public Builder setProgress(int max, int progress, boolean indeterminate) {
1183 mProgressMax = max;
1184 mProgress = progress;
1185 mProgressIndeterminate = indeterminate;
1186 return this;
1187 }
1188
1189 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001190 * Supply a custom RemoteViews to use instead of the platform template.
1191 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001192 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08001193 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001194 public Builder setContent(RemoteViews views) {
1195 mContentView = views;
1196 return this;
1197 }
1198
Joe Onoratocb109a02011-01-18 17:57:41 -08001199 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001200 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
1201 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001202 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
1203 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
1204 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001205 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001206 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001207 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001208 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001209 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001210 public Builder setContentIntent(PendingIntent intent) {
1211 mContentIntent = intent;
1212 return this;
1213 }
1214
Joe Onoratocb109a02011-01-18 17:57:41 -08001215 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001216 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
1217 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001218 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001219 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001220 public Builder setDeleteIntent(PendingIntent intent) {
1221 mDeleteIntent = intent;
1222 return this;
1223 }
1224
Joe Onoratocb109a02011-01-18 17:57:41 -08001225 /**
1226 * An intent to launch instead of posting the notification to the status bar.
1227 * Only for use with extremely high-priority notifications demanding the user's
1228 * <strong>immediate</strong> attention, such as an incoming phone call or
1229 * alarm clock that the user has explicitly set to a particular time.
1230 * If this facility is used for something else, please give the user an option
1231 * to turn it off and use a normal notification, as this can be extremely
1232 * disruptive.
1233 *
1234 * @param intent The pending intent to launch.
1235 * @param highPriority Passing true will cause this notification to be sent
1236 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001237 *
1238 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001239 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001240 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
1241 mFullScreenIntent = intent;
1242 setFlag(FLAG_HIGH_PRIORITY, highPriority);
1243 return this;
1244 }
1245
Joe Onoratocb109a02011-01-18 17:57:41 -08001246 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001247 * Set the "ticker" text which is displayed in the status bar when the notification first
Joe Onoratocb109a02011-01-18 17:57:41 -08001248 * arrives.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001249 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001250 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08001251 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001252 public Builder setTicker(CharSequence tickerText) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001253 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08001254 return this;
1255 }
1256
Joe Onoratocb109a02011-01-18 17:57:41 -08001257 /**
1258 * Set the text that is displayed in the status bar when the notification first
1259 * arrives, and also a RemoteViews object that may be displayed instead on some
1260 * devices.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001261 *
1262 * @see Notification#tickerText
1263 * @see Notification#tickerView
Joe Onoratocb109a02011-01-18 17:57:41 -08001264 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001265 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001266 mTickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08001267 mTickerView = views;
1268 return this;
1269 }
1270
Joe Onoratocb109a02011-01-18 17:57:41 -08001271 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001272 * Add a large icon to the notification (and the ticker on some devices).
1273 *
1274 * In the platform template, this image will be shown on the left of the notification view
1275 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side).
1276 *
1277 * @see Notification#largeIcon
Joe Onoratocb109a02011-01-18 17:57:41 -08001278 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001279 public Builder setLargeIcon(Bitmap icon) {
1280 mLargeIcon = icon;
1281 return this;
1282 }
1283
Joe Onoratocb109a02011-01-18 17:57:41 -08001284 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001285 * Set the sound to play.
1286 *
1287 * It will be played on the {@link #STREAM_DEFAULT default stream} for notifications.
1288 *
1289 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001290 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08001291 public Builder setSound(Uri sound) {
1292 mSound = sound;
1293 mAudioStreamType = STREAM_DEFAULT;
1294 return this;
1295 }
1296
Joe Onoratocb109a02011-01-18 17:57:41 -08001297 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001298 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08001299 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001300 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
1301 *
1302 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001303 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001304 public Builder setSound(Uri sound, int streamType) {
1305 mSound = sound;
1306 mAudioStreamType = streamType;
1307 return this;
1308 }
1309
Joe Onoratocb109a02011-01-18 17:57:41 -08001310 /**
1311 * Set the vibration pattern to use.
1312 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001313
1314 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
1315 * <code>pattern</code> parameter.
1316 *
1317
1318 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08001319 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001320 public Builder setVibrate(long[] pattern) {
1321 mVibrate = pattern;
1322 return this;
1323 }
1324
Joe Onoratocb109a02011-01-18 17:57:41 -08001325 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001326 * Set the desired color for the indicator LED on the device, as well as the
1327 * blink duty cycle (specified in milliseconds).
1328 *
1329
1330 * Not all devices will honor all (or even any) of these values.
1331 *
1332
1333 * @see Notification#ledARGB
1334 * @see Notification#ledOnMS
1335 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08001336 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001337 public Builder setLights(int argb, int onMs, int offMs) {
1338 mLedArgb = argb;
1339 mLedOnMs = onMs;
1340 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08001341 return this;
1342 }
1343
Joe Onoratocb109a02011-01-18 17:57:41 -08001344 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001345 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08001346 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001347
1348 * Ongoing notifications cannot be dismissed by the user, so your application or service
1349 * must take care of canceling them.
1350 *
1351
1352 * They are typically used to indicate a background task that the user is actively engaged
1353 * with (e.g., playing music) or is pending in some way and therefore occupying the device
1354 * (e.g., a file download, sync operation, active network connection).
1355 *
1356
1357 * @see Notification#FLAG_ONGOING_EVENT
1358 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08001359 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001360 public Builder setOngoing(boolean ongoing) {
1361 setFlag(FLAG_ONGOING_EVENT, ongoing);
1362 return this;
1363 }
1364
Joe Onoratocb109a02011-01-18 17:57:41 -08001365 /**
1366 * Set this flag if you would only like the sound, vibrate
1367 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001368 *
1369 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08001370 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001371 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
1372 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
1373 return this;
1374 }
1375
Joe Onoratocb109a02011-01-18 17:57:41 -08001376 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001377 * Make this notification automatically dismissed when the user touches it. The
1378 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
1379 *
1380 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08001381 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001382 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08001383 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08001384 return this;
1385 }
1386
Joe Onoratocb109a02011-01-18 17:57:41 -08001387 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001388 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08001389 * <p>
1390 * The value should be one or more of the following fields combined with
1391 * bitwise-or:
1392 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
1393 * <p>
1394 * For all default values, use {@link #DEFAULT_ALL}.
1395 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001396 public Builder setDefaults(int defaults) {
1397 mDefaults = defaults;
1398 return this;
1399 }
1400
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001401 /**
1402 * Set the priority of this notification.
1403 *
1404 * @see Notification#priority
1405 */
1406 public Builder setPriority(int pri) {
1407 mPriority = pri;
1408 return this;
1409 }
Joe Malin8d40d042012-11-05 11:36:40 -08001410
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001411 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001412 * @hide
Joe Malin8d40d042012-11-05 11:36:40 -08001413 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001414 * Add a kind (category) to this notification. Optional.
Joe Malin8d40d042012-11-05 11:36:40 -08001415 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001416 * @see Notification#kind
1417 */
1418 public Builder addKind(String k) {
1419 mKindList.add(k);
1420 return this;
1421 }
1422
1423 /**
1424 * Add metadata to this notification.
1425 *
1426 * A reference to the Bundle is held for the lifetime of this Builder, and the Bundle's
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001427 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001428 * called.
1429 *
1430 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001431 */
1432 public Builder setExtras(Bundle bag) {
1433 mExtras = bag;
1434 return this;
1435 }
1436
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001437 /**
1438 * Add an action to this notification. Actions are typically displayed by
1439 * the system as a button adjacent to the notification content.
Joe Malin8d40d042012-11-05 11:36:40 -08001440 * <br>
1441 * A notification displays up to 3 actions, from left to right in the order they were added.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001442 *
1443 * @param icon Resource ID of a drawable that represents the action.
1444 * @param title Text describing the action.
1445 * @param intent PendingIntent to be fired when the action is invoked.
1446 */
1447 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001448 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001449 return this;
1450 }
1451
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001452 /**
1453 * Add a rich notification style to be applied at build time.
1454 *
1455 * @param style Object responsible for modifying the notification style.
1456 */
1457 public Builder setStyle(Style style) {
1458 if (mStyle != style) {
1459 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07001460 if (mStyle != null) {
1461 mStyle.setBuilder(this);
1462 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001463 }
1464 return this;
1465 }
1466
Joe Onorato46439ce2010-11-19 13:56:21 -08001467 private void setFlag(int mask, boolean value) {
1468 if (value) {
1469 mFlags |= mask;
1470 } else {
1471 mFlags &= ~mask;
1472 }
1473 }
1474
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001475 private RemoteViews applyStandardTemplate(int resId, boolean fitIn1U) {
Joe Onorato561d3852010-11-20 18:09:34 -08001476 RemoteViews contentView = new RemoteViews(mContext.getPackageName(), resId);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001477 boolean showLine3 = false;
1478 boolean showLine2 = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001479 int smallIconImageViewId = R.id.icon;
1480 if (mLargeIcon != null) {
1481 contentView.setImageViewBitmap(R.id.icon, mLargeIcon);
1482 smallIconImageViewId = R.id.right_icon;
1483 }
Daniel Sandlere95658c2012-05-10 00:33:54 -04001484 if (mPriority < PRIORITY_LOW) {
1485 contentView.setInt(R.id.icon,
1486 "setBackgroundResource", R.drawable.notification_template_icon_low_bg);
1487 contentView.setInt(R.id.status_bar_latest_event_content,
1488 "setBackgroundResource", R.drawable.notification_bg_low);
1489 }
Joe Onorato561d3852010-11-20 18:09:34 -08001490 if (mSmallIcon != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001491 contentView.setImageViewResource(smallIconImageViewId, mSmallIcon);
1492 contentView.setViewVisibility(smallIconImageViewId, View.VISIBLE);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001493 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001494 contentView.setViewVisibility(smallIconImageViewId, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001495 }
1496 if (mContentTitle != null) {
1497 contentView.setTextViewText(R.id.title, mContentTitle);
1498 }
1499 if (mContentText != null) {
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001500 contentView.setTextViewText(R.id.text, mContentText);
1501 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001502 }
1503 if (mContentInfo != null) {
1504 contentView.setTextViewText(R.id.info, mContentInfo);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001505 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001506 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001507 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04001508 final int tooBig = mContext.getResources().getInteger(
1509 R.integer.status_bar_notification_info_maxnum);
1510 if (mNumber > tooBig) {
1511 contentView.setTextViewText(R.id.info, mContext.getResources().getString(
1512 R.string.status_bar_notification_info_overflow));
Joe Onorato059a2f82011-01-04 10:27:01 -08001513 } else {
1514 NumberFormat f = NumberFormat.getIntegerInstance();
1515 contentView.setTextViewText(R.id.info, f.format(mNumber));
1516 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001517 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001518 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001519 } else {
1520 contentView.setViewVisibility(R.id.info, View.GONE);
1521 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001522
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001523 // Need to show three lines?
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001524 if (mSubText != null) {
1525 contentView.setTextViewText(R.id.text, mSubText);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001526 if (mContentText != null) {
1527 contentView.setTextViewText(R.id.text2, mContentText);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001528 contentView.setViewVisibility(R.id.text2, View.VISIBLE);
1529 showLine2 = true;
1530 } else {
1531 contentView.setViewVisibility(R.id.text2, View.GONE);
1532 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001533 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001534 contentView.setViewVisibility(R.id.text2, View.GONE);
1535 if (mProgressMax != 0 || mProgressIndeterminate) {
1536 contentView.setProgressBar(
1537 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
1538 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001539 showLine2 = true;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001540 } else {
1541 contentView.setViewVisibility(R.id.progress, View.GONE);
1542 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001543 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001544 if (showLine2) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001545 if (fitIn1U) {
1546 // need to shrink all the type to make sure everything fits
1547 final Resources res = mContext.getResources();
1548 final float subTextSize = res.getDimensionPixelSize(
1549 R.dimen.notification_subtext_size);
1550 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
1551 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001552 // vertical centering
1553 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
1554 }
1555
Daniel Sandler0c890492012-09-12 17:23:10 -07001556 if (mWhen != 0 && mShowWhen) {
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001557 if (mUseChronometer) {
1558 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
1559 contentView.setLong(R.id.chronometer, "setBase",
1560 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
1561 contentView.setBoolean(R.id.chronometer, "setStarted", true);
1562 } else {
1563 contentView.setViewVisibility(R.id.time, View.VISIBLE);
1564 contentView.setLong(R.id.time, "setTime", mWhen);
1565 }
Daniel Sandler0c890492012-09-12 17:23:10 -07001566 } else {
1567 contentView.setViewVisibility(R.id.time, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001568 }
Daniel Sandler0c890492012-09-12 17:23:10 -07001569
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001570 contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001571 contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001572 return contentView;
1573 }
1574
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001575 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001576 RemoteViews big = applyStandardTemplate(layoutId, false);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001577
1578 int N = mActions.size();
1579 if (N > 0) {
Chris Wrend6297db2012-05-03 16:20:13 -04001580 // Log.d("Notification", "has actions: " + mContentText);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001581 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001582 big.setViewVisibility(R.id.action_divider, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04001583 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Chris Wren2c22eb02012-05-08 09:49:13 -04001584 big.removeAllViews(R.id.actions);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001585 for (int i=0; i<N; i++) {
1586 final RemoteViews button = generateActionButton(mActions.get(i));
Chris Wrend6297db2012-05-03 16:20:13 -04001587 //Log.d("Notification", "adding action " + i + ": " + mActions.get(i).title);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001588 big.addView(R.id.actions, button);
1589 }
1590 }
1591 return big;
1592 }
1593
Joe Onorato46439ce2010-11-19 13:56:21 -08001594 private RemoteViews makeContentView() {
1595 if (mContentView != null) {
1596 return mContentView;
1597 } else {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001598 return applyStandardTemplate(R.layout.notification_template_base, true); // no more special large_icon flavor
Joe Onorato46439ce2010-11-19 13:56:21 -08001599 }
1600 }
1601
1602 private RemoteViews makeTickerView() {
1603 if (mTickerView != null) {
1604 return mTickerView;
1605 } else {
Joe Onorato561d3852010-11-20 18:09:34 -08001606 if (mContentView == null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001607 return applyStandardTemplate(mLargeIcon == null
Joe Onorato561d3852010-11-20 18:09:34 -08001608 ? R.layout.status_bar_latest_event_ticker
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001609 : R.layout.status_bar_latest_event_ticker_large_icon, true);
Joe Onorato561d3852010-11-20 18:09:34 -08001610 } else {
1611 return null;
1612 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001613 }
1614 }
1615
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001616 private RemoteViews makeBigContentView() {
1617 if (mActions.size() == 0) return null;
1618
Chris Wrenb023bf82012-04-23 16:05:42 -04001619 return applyStandardTemplateWithActions(R.layout.notification_template_big_base);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001620 }
1621
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001622 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04001623 final boolean tombstone = (action.actionIntent == null);
Joe Malin8d40d042012-11-05 11:36:40 -08001624 RemoteViews button = new RemoteViews(mContext.getPackageName(),
Daniel Sandler8680bf82012-05-15 16:52:52 -04001625 tombstone ? R.layout.notification_action_tombstone
1626 : R.layout.notification_action);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001627 button.setTextViewCompoundDrawables(R.id.action0, action.icon, 0, 0, 0);
1628 button.setTextViewText(R.id.action0, action.title);
Daniel Sandler8680bf82012-05-15 16:52:52 -04001629 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04001630 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04001631 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001632 button.setContentDescription(R.id.action0, action.title);
1633 return button;
1634 }
1635
Joe Onoratocb109a02011-01-18 17:57:41 -08001636 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001637 * Apply the unstyled operations and return a new {@link Notification} object.
Joe Onoratocb109a02011-01-18 17:57:41 -08001638 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001639 private Notification buildUnstyled() {
Joe Onorato46439ce2010-11-19 13:56:21 -08001640 Notification n = new Notification();
1641 n.when = mWhen;
1642 n.icon = mSmallIcon;
1643 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08001644 n.number = mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08001645 n.contentView = makeContentView();
1646 n.contentIntent = mContentIntent;
1647 n.deleteIntent = mDeleteIntent;
1648 n.fullScreenIntent = mFullScreenIntent;
1649 n.tickerText = mTickerText;
1650 n.tickerView = makeTickerView();
1651 n.largeIcon = mLargeIcon;
1652 n.sound = mSound;
1653 n.audioStreamType = mAudioStreamType;
1654 n.vibrate = mVibrate;
1655 n.ledARGB = mLedArgb;
1656 n.ledOnMS = mLedOnMs;
1657 n.ledOffMS = mLedOffMs;
1658 n.defaults = mDefaults;
1659 n.flags = mFlags;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001660 n.bigContentView = makeBigContentView();
Daniel Sandler26c13432013-04-04 11:01:04 -04001661 if (mLedOnMs != 0 || mLedOffMs != 0) {
Joe Onorato8d0b6552010-11-22 16:09:29 -08001662 n.flags |= FLAG_SHOW_LIGHTS;
1663 }
1664 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
1665 n.flags |= FLAG_SHOW_LIGHTS;
1666 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001667 if (mKindList.size() > 0) {
1668 n.kind = new String[mKindList.size()];
1669 mKindList.toArray(n.kind);
1670 } else {
1671 n.kind = null;
1672 }
1673 n.priority = mPriority;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001674 if (mActions.size() > 0) {
1675 n.actions = new Action[mActions.size()];
1676 mActions.toArray(n.actions);
1677 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001678
Joe Onorato46439ce2010-11-19 13:56:21 -08001679 return n;
1680 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001681
1682 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001683 * Capture, in the provided bundle, semantic information used in the construction of
1684 * this Notification object.
1685 * @hide
1686 */
1687 public void addExtras(Bundle extras) {
1688 // Store original information used in the construction of this object
1689 extras.putCharSequence(EXTRA_TITLE, mContentTitle);
1690 extras.putCharSequence(EXTRA_TEXT, mContentText);
1691 extras.putCharSequence(EXTRA_SUB_TEXT, mSubText);
1692 extras.putCharSequence(EXTRA_INFO_TEXT, mContentInfo);
1693 extras.putInt(EXTRA_SMALL_ICON, mSmallIcon);
1694 extras.putInt(EXTRA_PROGRESS, mProgress);
1695 extras.putInt(EXTRA_PROGRESS_MAX, mProgressMax);
1696 extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mProgressIndeterminate);
1697 extras.putBoolean(EXTRA_SHOW_CHRONOMETER, mUseChronometer);
1698 extras.putBoolean(EXTRA_SHOW_WHEN, mShowWhen);
John Spurlockac08a472013-06-10 11:37:08 -04001699 if (mLargeIcon != null) {
1700 extras.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
1701 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001702 }
1703
1704 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001705 * @deprecated Use {@link #build()} instead.
1706 */
1707 @Deprecated
1708 public Notification getNotification() {
1709 return build();
1710 }
1711
1712 /**
1713 * Combine all of the options that have been set and return a new {@link Notification}
1714 * object.
1715 */
1716 public Notification build() {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001717 final Notification n;
1718
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001719 if (mStyle != null) {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001720 n = mStyle.build();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001721 } else {
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001722 n = buildUnstyled();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001723 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001724
1725 n.extras = mExtras != null ? new Bundle(mExtras) : new Bundle();
1726
1727 addExtras(n.extras);
1728 if (mStyle != null) {
1729 mStyle.addExtras(n.extras);
1730 }
1731
1732 return n;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001733 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001734
1735 /**
1736 * Apply this Builder to an existing {@link Notification} object.
1737 *
1738 * @hide
1739 */
1740 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04001741 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001742 return n;
1743 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001744 }
1745
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001746 /**
1747 * An object that can apply a rich notification style to a {@link Notification.Builder}
1748 * object.
1749 */
Chris Wrend6297db2012-05-03 16:20:13 -04001750 public static abstract class Style
1751 {
1752 private CharSequence mBigContentTitle;
1753 private CharSequence mSummaryText = null;
Daniel Sandler619738c2012-06-07 16:33:08 -04001754 private boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04001755
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001756 protected Builder mBuilder;
1757
Chris Wrend6297db2012-05-03 16:20:13 -04001758 /**
1759 * Overrides ContentTitle in the big form of the template.
1760 * This defaults to the value passed to setContentTitle().
1761 */
1762 protected void internalSetBigContentTitle(CharSequence title) {
1763 mBigContentTitle = title;
1764 }
1765
1766 /**
1767 * Set the first line of text after the detail section in the big form of the template.
1768 */
1769 protected void internalSetSummaryText(CharSequence cs) {
1770 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04001771 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04001772 }
1773
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001774 public void setBuilder(Builder builder) {
1775 if (mBuilder != builder) {
1776 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07001777 if (mBuilder != null) {
1778 mBuilder.setStyle(this);
1779 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001780 }
1781 }
1782
Chris Wrend6297db2012-05-03 16:20:13 -04001783 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001784 if (mBuilder == null) {
1785 throw new IllegalArgumentException("Style requires a valid Builder object");
1786 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001787 }
Chris Wrend6297db2012-05-03 16:20:13 -04001788
1789 protected RemoteViews getStandardView(int layoutId) {
1790 checkBuilder();
1791
1792 if (mBigContentTitle != null) {
1793 mBuilder.setContentTitle(mBigContentTitle);
1794 }
1795
Chris Wrend6297db2012-05-03 16:20:13 -04001796 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
1797
Chris Wrend6297db2012-05-03 16:20:13 -04001798 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
1799 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04001800 } else {
1801 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04001802 }
1803
Daniel Sandler619738c2012-06-07 16:33:08 -04001804 // The last line defaults to the subtext, but can be replaced by mSummaryText
1805 final CharSequence overflowText =
1806 mSummaryTextSet ? mSummaryText
1807 : mBuilder.mSubText;
1808 if (overflowText != null) {
1809 contentView.setTextViewText(R.id.text, overflowText);
1810 contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001811 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
Daniel Sandler916ad912012-06-13 12:17:07 -04001812 } else {
1813 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
1814 contentView.setViewVisibility(R.id.line3, View.GONE);
Chris Wrend6297db2012-05-03 16:20:13 -04001815 }
1816
1817 return contentView;
1818 }
1819
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001820 /**
1821 * @hide
1822 */
1823 public void addExtras(Bundle extras) {
1824 if (mSummaryTextSet) {
1825 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
1826 }
1827 if (mBigContentTitle != null) {
1828 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
1829 }
1830 }
1831
Chris Wrend6297db2012-05-03 16:20:13 -04001832 public abstract Notification build();
Joe Onorato46439ce2010-11-19 13:56:21 -08001833 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001834
1835 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001836 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08001837 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001838 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001839 * <pre class="prettyprint">
1840 * Notification noti = new Notification.BigPictureStyle(
1841 * new Notification.Builder()
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001842 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001843 * .setContentText(subject)
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001844 * .setSmallIcon(R.drawable.new_post)
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001845 * .setLargeIcon(aBitmap))
1846 * .bigPicture(aBigBitmap)
1847 * .build();
1848 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08001849 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001850 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001851 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001852 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001853 private Bitmap mPicture;
Chris Wren3745a3d2012-05-22 15:11:52 -04001854 private Bitmap mBigLargeIcon;
1855 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001856
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001857 public BigPictureStyle() {
1858 }
1859
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001860 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001861 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001862 }
1863
Chris Wrend6297db2012-05-03 16:20:13 -04001864 /**
1865 * Overrides ContentTitle in the big form of the template.
1866 * This defaults to the value passed to setContentTitle().
1867 */
1868 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001869 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04001870 return this;
1871 }
1872
1873 /**
1874 * Set the first line of text after the detail section in the big form of the template.
1875 */
1876 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001877 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04001878 return this;
1879 }
1880
Chris Wren0bd664d2012-08-01 13:56:56 -04001881 /**
1882 * Provide the bitmap to be used as the payload for the BigPicture notification.
1883 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001884 public BigPictureStyle bigPicture(Bitmap b) {
1885 mPicture = b;
1886 return this;
1887 }
1888
Chris Wren3745a3d2012-05-22 15:11:52 -04001889 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04001890 * Override the large icon when the big notification is shown.
1891 */
1892 public BigPictureStyle bigLargeIcon(Bitmap b) {
1893 mBigLargeIconSet = true;
1894 mBigLargeIcon = b;
1895 return this;
1896 }
1897
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001898 private RemoteViews makeBigContentView() {
Chris Wrend6297db2012-05-03 16:20:13 -04001899 RemoteViews contentView = getStandardView(R.layout.notification_template_big_picture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001900
1901 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
1902
1903 return contentView;
1904 }
1905
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001906 /**
1907 * @hide
1908 */
1909 public void addExtras(Bundle extras) {
1910 super.addExtras(extras);
1911
1912 if (mBigLargeIconSet) {
1913 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
1914 }
1915 extras.putParcelable(EXTRA_PICTURE, mPicture);
1916 }
1917
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001918 @Override
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001919 public Notification build() {
Chris Wrend6297db2012-05-03 16:20:13 -04001920 checkBuilder();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001921 Notification wip = mBuilder.buildUnstyled();
Chris Wren3745a3d2012-05-22 15:11:52 -04001922 if (mBigLargeIconSet ) {
1923 mBuilder.mLargeIcon = mBigLargeIcon;
1924 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001925 wip.bigContentView = makeBigContentView();
1926 return wip;
1927 }
1928 }
1929
1930 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001931 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08001932 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001933 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001934 * <pre class="prettyprint">
Daniel Sandler87682782012-11-07 14:04:42 -05001935 * Notification noti = new Notification.BigTextStyle(
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001936 * new Notification.Builder()
1937 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
1938 * .setContentText(subject)
1939 * .setSmallIcon(R.drawable.new_mail)
1940 * .setLargeIcon(aBitmap))
1941 * .bigText(aVeryLongString)
1942 * .build();
1943 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08001944 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001945 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001946 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001947 public static class BigTextStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001948 private CharSequence mBigText;
1949
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001950 public BigTextStyle() {
1951 }
1952
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001953 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001954 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001955 }
1956
Chris Wrend6297db2012-05-03 16:20:13 -04001957 /**
1958 * Overrides ContentTitle in the big form of the template.
1959 * This defaults to the value passed to setContentTitle().
1960 */
1961 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001962 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04001963 return this;
1964 }
1965
1966 /**
1967 * Set the first line of text after the detail section in the big form of the template.
1968 */
1969 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001970 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04001971 return this;
1972 }
1973
Chris Wren0bd664d2012-08-01 13:56:56 -04001974 /**
1975 * Provide the longer text to be displayed in the big form of the
1976 * template in place of the content text.
1977 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001978 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001979 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001980 return this;
1981 }
1982
Daniel Sandlerf45564e2013-04-15 15:05:08 -04001983 /**
1984 * @hide
1985 */
1986 public void addExtras(Bundle extras) {
1987 super.addExtras(extras);
1988
1989 extras.putCharSequence(EXTRA_TEXT, mBigText);
1990 }
1991
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001992 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04001993 // Remove the content text so line3 only shows if you have a summary
1994 final boolean hadThreeLines = (mBuilder.mContentText != null && mBuilder.mSubText != null);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001995 mBuilder.mContentText = null;
Daniel Sandler916ad912012-06-13 12:17:07 -04001996
Chris Wrend6297db2012-05-03 16:20:13 -04001997 RemoteViews contentView = getStandardView(R.layout.notification_template_big_text);
Joe Malin8d40d042012-11-05 11:36:40 -08001998
Daniel Sandler619738c2012-06-07 16:33:08 -04001999 if (hadThreeLines) {
2000 // vertical centering
2001 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
2002 }
2003
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002004 contentView.setTextViewText(R.id.big_text, mBigText);
2005 contentView.setViewVisibility(R.id.big_text, View.VISIBLE);
Chris Wren3c5f92432012-05-04 16:31:17 -04002006 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002007
2008 return contentView;
2009 }
2010
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002011 @Override
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002012 public Notification build() {
Chris Wrend6297db2012-05-03 16:20:13 -04002013 checkBuilder();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002014 Notification wip = mBuilder.buildUnstyled();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002015 wip.bigContentView = makeBigContentView();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002016
2017 wip.extras.putCharSequence(EXTRA_TEXT, mBigText);
2018
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002019 return wip;
2020 }
2021 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04002022
2023 /**
2024 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08002025 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04002026 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
2027 * <pre class="prettyprint">
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002028 * Notification noti = new Notification.InboxStyle(
Daniel Sandler879c5e02012-04-17 16:46:51 -04002029 * new Notification.Builder()
Chris Wrend6297db2012-05-03 16:20:13 -04002030 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
Daniel Sandler879c5e02012-04-17 16:46:51 -04002031 * .setContentText(subject)
2032 * .setSmallIcon(R.drawable.new_mail)
2033 * .setLargeIcon(aBitmap))
2034 * .addLine(str1)
2035 * .addLine(str2)
Chris Wrend6297db2012-05-03 16:20:13 -04002036 * .setContentTitle("")
2037 * .setSummaryText(&quot;+3 more&quot;)
Daniel Sandler879c5e02012-04-17 16:46:51 -04002038 * .build();
2039 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08002040 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04002041 * @see Notification#bigContentView
2042 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002043 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04002044 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
2045
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002046 public InboxStyle() {
2047 }
2048
Daniel Sandler879c5e02012-04-17 16:46:51 -04002049 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002050 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04002051 }
2052
Chris Wrend6297db2012-05-03 16:20:13 -04002053 /**
2054 * Overrides ContentTitle in the big form of the template.
2055 * This defaults to the value passed to setContentTitle().
2056 */
2057 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002058 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04002059 return this;
2060 }
2061
2062 /**
2063 * Set the first line of text after the detail section in the big form of the template.
2064 */
2065 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002066 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04002067 return this;
2068 }
2069
Chris Wren0bd664d2012-08-01 13:56:56 -04002070 /**
2071 * Append a line to the digest section of the Inbox notification.
2072 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04002073 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002074 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04002075 return this;
2076 }
2077
Daniel Sandlerf45564e2013-04-15 15:05:08 -04002078 /**
2079 * @hide
2080 */
2081 public void addExtras(Bundle extras) {
2082 super.addExtras(extras);
2083 CharSequence[] a = new CharSequence[mTexts.size()];
2084 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
2085 }
2086
Daniel Sandler879c5e02012-04-17 16:46:51 -04002087 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04002088 // Remove the content text so line3 disappears unless you have a summary
2089 mBuilder.mContentText = null;
Chris Wrend6297db2012-05-03 16:20:13 -04002090 RemoteViews contentView = getStandardView(R.layout.notification_template_inbox);
Daniel Sandler619738c2012-06-07 16:33:08 -04002091
Chris Wrend6297db2012-05-03 16:20:13 -04002092 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandler879c5e02012-04-17 16:46:51 -04002093
Chris Wrend6297db2012-05-03 16:20:13 -04002094 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 -04002095 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04002096
Chris Wren4ed80d52012-05-17 09:30:03 -04002097 // Make sure all rows are gone in case we reuse a view.
2098 for (int rowId : rowIds) {
2099 contentView.setViewVisibility(rowId, View.GONE);
2100 }
2101
Chris Wren683ab002012-09-20 10:35:54 -04002102
Daniel Sandler879c5e02012-04-17 16:46:51 -04002103 int i=0;
2104 while (i < mTexts.size() && i < rowIds.length) {
2105 CharSequence str = mTexts.get(i);
2106 if (str != null && !str.equals("")) {
2107 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
2108 contentView.setTextViewText(rowIds[i], str);
2109 }
2110 i++;
2111 }
2112
Chris Wren683ab002012-09-20 10:35:54 -04002113 contentView.setViewVisibility(R.id.inbox_end_pad,
2114 mTexts.size() > 0 ? View.VISIBLE : View.GONE);
2115
2116 contentView.setViewVisibility(R.id.inbox_more,
2117 mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE);
Chris Wren29bb6d92012-05-17 18:09:42 -04002118
Daniel Sandler879c5e02012-04-17 16:46:51 -04002119 return contentView;
2120 }
2121
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002122 @Override
Daniel Sandler879c5e02012-04-17 16:46:51 -04002123 public Notification build() {
Chris Wrend6297db2012-05-03 16:20:13 -04002124 checkBuilder();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002125 Notification wip = mBuilder.buildUnstyled();
Daniel Sandler879c5e02012-04-17 16:46:51 -04002126 wip.bigContentView = makeBigContentView();
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002127
Daniel Sandler879c5e02012-04-17 16:46:51 -04002128 return wip;
2129 }
2130 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002131}