blob: 2c92d09388db7eb679308d7e037aba17a3d9a284 [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 Sandler2561b0b2012-02-13 21:04:12 -050027import android.os.Bundle;
Daniel Sandlera0a938c2012-03-15 08:42:37 -040028import android.os.IBinder;
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 Sandlera0a938c2012-03-15 08:42:37 -040034import android.util.IntProperty;
Daniel Sandler96fd7c12012-03-30 16:37:36 -040035import android.util.Log;
Daniel Sandler9f7936a2012-05-21 16:14:28 -040036import android.util.Slog;
37import android.util.TypedValue;
Joe Onorato8595a3d2010-11-19 18:12:07 -080038import android.view.View;
Jeff Sharkey1c400132011-08-05 14:50:13 -070039import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040import android.widget.RemoteViews;
41
Andy Stadler110988c2010-12-03 14:29:16 -080042import java.text.NumberFormat;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050043import java.util.ArrayList;
Joe Onorato561d3852010-11-20 18:09:34 -080044
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080045/**
46 * A class that represents how a persistent notification is to be presented to
47 * the user using the {@link android.app.NotificationManager}.
48 *
Joe Onoratocb109a02011-01-18 17:57:41 -080049 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
50 * easier to construct Notifications.</p>
51 *
Joe Fernandez558459f2011-10-13 16:47:36 -070052 * <div class="special reference">
53 * <h3>Developer Guides</h3>
54 * <p>For a guide to creating notifications, read the
55 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
56 * developer guide.</p>
57 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080058 */
59public class Notification implements Parcelable
60{
61 /**
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 Sandler2561b0b2012-02-13 21:04:12 -0500325 * user. On tablets, the
326
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
Joe Malin8d40d042012-11-05 11:36:40 -0800391 * system will make a determination about how to interpret notification priority as described in
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500392 * MUMBLE MUMBLE.
393 */
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 /**
434 * Extra key for people values (type TBD).
435 *
436 * @hide
437 */
438 public static final String EXTRA_PEOPLE = "android.people";
439
440 private Bundle extras;
441
442 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400443 * Structure to encapsulate an "action", including title and icon, that can be attached to a Notification.
444 * @hide
445 */
446 private static class Action implements Parcelable {
447 public int icon;
448 public CharSequence title;
449 public PendingIntent actionIntent;
450 @SuppressWarnings("unused")
451 public Action() { }
452 private Action(Parcel in) {
453 icon = in.readInt();
454 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
455 if (in.readInt() == 1) {
456 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
457 }
458 }
459 public Action(int icon_, CharSequence title_, PendingIntent intent_) {
460 this.icon = icon_;
461 this.title = title_;
462 this.actionIntent = intent_;
463 }
464 @Override
465 public Action clone() {
466 return new Action(
467 this.icon,
468 this.title.toString(),
469 this.actionIntent // safe to alias
470 );
471 }
472 @Override
473 public int describeContents() {
474 return 0;
475 }
476 @Override
477 public void writeToParcel(Parcel out, int flags) {
478 out.writeInt(icon);
479 TextUtils.writeToParcel(title, out, flags);
480 if (actionIntent != null) {
481 out.writeInt(1);
482 actionIntent.writeToParcel(out, flags);
483 } else {
484 out.writeInt(0);
485 }
486 }
487 public static final Parcelable.Creator<Action> CREATOR
488 = new Parcelable.Creator<Action>() {
489 public Action createFromParcel(Parcel in) {
490 return new Action(in);
491 }
492 public Action[] newArray(int size) {
493 return new Action[size];
494 }
495 };
496 }
497
498 private Action[] actions;
499
500 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500501 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -0800502 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800503 */
504 public Notification()
505 {
506 this.when = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500507 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800508 }
509
510 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800511 * @hide
512 */
513 public Notification(Context context, int icon, CharSequence tickerText, long when,
514 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
515 {
516 this.when = when;
517 this.icon = icon;
518 this.tickerText = tickerText;
519 setLatestEventInfo(context, contentTitle, contentText,
520 PendingIntent.getActivity(context, 0, contentIntent, 0));
521 }
522
523 /**
524 * Constructs a Notification object with the information needed to
525 * have a status bar icon without the standard expanded view.
526 *
527 * @param icon The resource id of the icon to put in the status bar.
528 * @param tickerText The text that flows by in the status bar when the notification first
529 * activates.
530 * @param when The time to show in the time field. In the System.currentTimeMillis
531 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -0800532 *
533 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800534 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800535 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800536 public Notification(int icon, CharSequence tickerText, long when)
537 {
538 this.icon = icon;
539 this.tickerText = tickerText;
540 this.when = when;
541 }
542
543 /**
544 * Unflatten the notification from a parcel.
545 */
546 public Notification(Parcel parcel)
547 {
548 int version = parcel.readInt();
549
550 when = parcel.readLong();
551 icon = parcel.readInt();
552 number = parcel.readInt();
553 if (parcel.readInt() != 0) {
554 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
555 }
556 if (parcel.readInt() != 0) {
557 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
558 }
559 if (parcel.readInt() != 0) {
560 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
561 }
562 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -0800563 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -0400564 }
565 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
567 }
Joe Onorato561d3852010-11-20 18:09:34 -0800568 if (parcel.readInt() != 0) {
569 largeIcon = Bitmap.CREATOR.createFromParcel(parcel);
570 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800571 defaults = parcel.readInt();
572 flags = parcel.readInt();
573 if (parcel.readInt() != 0) {
574 sound = Uri.CREATOR.createFromParcel(parcel);
575 }
576
577 audioStreamType = parcel.readInt();
578 vibrate = parcel.createLongArray();
579 ledARGB = parcel.readInt();
580 ledOnMS = parcel.readInt();
581 ledOffMS = parcel.readInt();
582 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400583
584 if (parcel.readInt() != 0) {
585 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
586 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500587
588 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400589
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500590 kind = parcel.createStringArray(); // may set kind to null
591
592 if (parcel.readInt() != 0) {
593 extras = parcel.readBundle();
594 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400595
596 actions = parcel.createTypedArray(Action.CREATOR);
597 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400598 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
599 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 }
601
Andy Stadler110988c2010-12-03 14:29:16 -0800602 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -0700603 public Notification clone() {
604 Notification that = new Notification();
605
606 that.when = this.when;
607 that.icon = this.icon;
608 that.number = this.number;
609
610 // PendingIntents are global, so there's no reason (or way) to clone them.
611 that.contentIntent = this.contentIntent;
612 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400613 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -0700614
615 if (this.tickerText != null) {
616 that.tickerText = this.tickerText.toString();
617 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800618 if (this.tickerView != null) {
619 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -0400620 }
Joe Onorato18e69df2010-05-17 22:26:12 -0700621 if (this.contentView != null) {
622 that.contentView = this.contentView.clone();
623 }
Joe Onorato561d3852010-11-20 18:09:34 -0800624 if (this.largeIcon != null) {
625 that.largeIcon = Bitmap.createBitmap(this.largeIcon);
626 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +0100627 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -0700628 that.sound = this.sound; // android.net.Uri is immutable
629 that.audioStreamType = this.audioStreamType;
630
631 final long[] vibrate = this.vibrate;
632 if (vibrate != null) {
633 final int N = vibrate.length;
634 final long[] vib = that.vibrate = new long[N];
635 System.arraycopy(vibrate, 0, vib, 0, N);
636 }
637
638 that.ledARGB = this.ledARGB;
639 that.ledOnMS = this.ledOnMS;
640 that.ledOffMS = this.ledOffMS;
641 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500642
Joe Onorato18e69df2010-05-17 22:26:12 -0700643 that.flags = this.flags;
644
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500645 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800646
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500647 final String[] thiskind = this.kind;
648 if (thiskind != null) {
649 final int N = thiskind.length;
650 final String[] thatkind = that.kind = new String[N];
651 System.arraycopy(thiskind, 0, thatkind, 0, N);
652 }
653
654 if (this.extras != null) {
655 that.extras = new Bundle(this.extras);
656
657 }
658
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400659 that.actions = new Action[this.actions.length];
660 for(int i=0; i<this.actions.length; i++) {
661 that.actions[i] = this.actions[i].clone();
662 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400663 if (this.bigContentView != null) {
664 that.bigContentView = this.bigContentView.clone();
665 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400666
Joe Onorato18e69df2010-05-17 22:26:12 -0700667 return that;
668 }
669
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800670 public int describeContents() {
671 return 0;
672 }
673
674 /**
675 * Flatten this notification from a parcel.
676 */
677 public void writeToParcel(Parcel parcel, int flags)
678 {
679 parcel.writeInt(1);
680
681 parcel.writeLong(when);
682 parcel.writeInt(icon);
683 parcel.writeInt(number);
684 if (contentIntent != null) {
685 parcel.writeInt(1);
686 contentIntent.writeToParcel(parcel, 0);
687 } else {
688 parcel.writeInt(0);
689 }
690 if (deleteIntent != null) {
691 parcel.writeInt(1);
692 deleteIntent.writeToParcel(parcel, 0);
693 } else {
694 parcel.writeInt(0);
695 }
696 if (tickerText != null) {
697 parcel.writeInt(1);
698 TextUtils.writeToParcel(tickerText, parcel, flags);
699 } else {
700 parcel.writeInt(0);
701 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800702 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -0400703 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -0800704 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -0400705 } else {
706 parcel.writeInt(0);
707 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 if (contentView != null) {
709 parcel.writeInt(1);
710 contentView.writeToParcel(parcel, 0);
711 } else {
712 parcel.writeInt(0);
713 }
Joe Onorato561d3852010-11-20 18:09:34 -0800714 if (largeIcon != null) {
715 parcel.writeInt(1);
716 largeIcon.writeToParcel(parcel, 0);
717 } else {
718 parcel.writeInt(0);
719 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800720
721 parcel.writeInt(defaults);
722 parcel.writeInt(this.flags);
723
724 if (sound != null) {
725 parcel.writeInt(1);
726 sound.writeToParcel(parcel, 0);
727 } else {
728 parcel.writeInt(0);
729 }
730 parcel.writeInt(audioStreamType);
731 parcel.writeLongArray(vibrate);
732 parcel.writeInt(ledARGB);
733 parcel.writeInt(ledOnMS);
734 parcel.writeInt(ledOffMS);
735 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400736
737 if (fullScreenIntent != null) {
738 parcel.writeInt(1);
739 fullScreenIntent.writeToParcel(parcel, 0);
740 } else {
741 parcel.writeInt(0);
742 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500743
744 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -0800745
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500746 parcel.writeStringArray(kind); // ok for null
Joe Malin8d40d042012-11-05 11:36:40 -0800747
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500748 if (extras != null) {
749 parcel.writeInt(1);
750 extras.writeToParcel(parcel, 0);
751 } else {
752 parcel.writeInt(0);
753 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400754
755 parcel.writeTypedArray(actions, 0);
756
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400757 if (bigContentView != null) {
758 parcel.writeInt(1);
759 bigContentView.writeToParcel(parcel, 0);
760 } else {
761 parcel.writeInt(0);
762 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800763 }
764
765 /**
766 * Parcelable.Creator that instantiates Notification objects
767 */
768 public static final Parcelable.Creator<Notification> CREATOR
769 = new Parcelable.Creator<Notification>()
770 {
771 public Notification createFromParcel(Parcel parcel)
772 {
773 return new Notification(parcel);
774 }
775
776 public Notification[] newArray(int size)
777 {
778 return new Notification[size];
779 }
780 };
781
782 /**
783 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
784 * layout.
785 *
786 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
787 * in the view.</p>
788 * @param context The context for your application / activity.
789 * @param contentTitle The title that goes in the expanded entry.
790 * @param contentText The text that goes in the expanded entry.
791 * @param contentIntent The intent to launch when the user clicks the expanded notification.
792 * If this is an activity, it must include the
793 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800794 * that you take care of task management as described in the
795 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
796 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500797 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800798 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800799 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800800 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 public void setLatestEventInfo(Context context,
802 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Daniel Sandlercde8aae2012-04-04 23:40:22 -0400803 // TODO: rewrite this to use Builder
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800804 RemoteViews contentView = new RemoteViews(context.getPackageName(),
Daniel Sandler96fd7c12012-03-30 16:37:36 -0400805 R.layout.notification_template_base);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 if (this.icon != 0) {
Joe Onorato561d3852010-11-20 18:09:34 -0800807 contentView.setImageViewResource(R.id.icon, this.icon);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 }
Daniel Sandlere95658c2012-05-10 00:33:54 -0400809 if (priority < PRIORITY_LOW) {
810 contentView.setInt(R.id.icon,
811 "setBackgroundResource", R.drawable.notification_template_icon_low_bg);
812 contentView.setInt(R.id.status_bar_latest_event_content,
813 "setBackgroundResource", R.drawable.notification_bg_low);
814 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800815 if (contentTitle != null) {
Joe Onorato561d3852010-11-20 18:09:34 -0800816 contentView.setTextViewText(R.id.title, contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800817 }
818 if (contentText != null) {
Joe Onorato561d3852010-11-20 18:09:34 -0800819 contentView.setTextViewText(R.id.text, contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800820 }
821 if (this.when != 0) {
Daniel Sandlercde8aae2012-04-04 23:40:22 -0400822 contentView.setViewVisibility(R.id.time, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -0800823 contentView.setLong(R.id.time, "setTime", when);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800824 }
Daniel Sandlerb4b22232012-04-30 10:38:31 -0400825 if (this.number != 0) {
826 NumberFormat f = NumberFormat.getIntegerInstance();
827 contentView.setTextViewText(R.id.info, f.format(this.number));
828 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800829
830 this.contentView = contentView;
831 this.contentIntent = contentIntent;
832 }
833
834 @Override
835 public String toString() {
836 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500837 sb.append("Notification(pri=");
838 sb.append(priority);
839 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -0800840 if (contentView != null) {
841 sb.append(contentView.getPackage());
842 sb.append("/0x");
843 sb.append(Integer.toHexString(contentView.getLayoutId()));
844 } else {
845 sb.append("null");
846 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500847 // TODO(dsandler): defaults take precedence over local values, so reorder the branches below
Joe Onoratoc9596d62011-01-12 17:03:11 -0800848 sb.append(" vibrate=");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 if (this.vibrate != null) {
850 int N = this.vibrate.length-1;
851 sb.append("[");
852 for (int i=0; i<N; i++) {
853 sb.append(this.vibrate[i]);
854 sb.append(',');
855 }
Simon Schoar8cf97d92009-06-10 22:08:37 +0200856 if (N != -1) {
857 sb.append(this.vibrate[N]);
858 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800859 sb.append("]");
860 } else if ((this.defaults & DEFAULT_VIBRATE) != 0) {
861 sb.append("default");
862 } else {
863 sb.append("null");
864 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500865 sb.append(" sound=");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 if (this.sound != null) {
867 sb.append(this.sound.toString());
868 } else if ((this.defaults & DEFAULT_SOUND) != 0) {
869 sb.append("default");
870 } else {
871 sb.append("null");
872 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500873 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500875 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400876 sb.append(Integer.toHexString(this.flags));
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500877 sb.append(" kind=[");
878 if (this.kind == null) {
879 sb.append("null");
880 } else {
881 for (int i=0; i<this.kind.length; i++) {
882 if (i>0) sb.append(",");
883 sb.append(this.kind[i]);
884 }
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400885 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400886 sb.append("]");
887 if (actions != null) {
888 sb.append(" ");
889 sb.append(actions.length);
890 sb.append(" action");
891 if (actions.length > 1) sb.append("s");
892 }
893 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 return sb.toString();
895 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800896
Jeff Sharkey6d515712012-09-20 16:06:08 -0700897 /** {@hide} */
898 public void setUser(UserHandle user) {
Amith Yamasaniecbd68b2012-11-02 12:17:19 -0700899 if (user.getIdentifier() == UserHandle.USER_ALL) {
900 user = UserHandle.OWNER;
901 }
Jeff Sharkey6d515712012-09-20 16:06:08 -0700902 if (tickerView != null) {
903 tickerView.setUser(user);
904 }
905 if (contentView != null) {
906 contentView.setUser(user);
907 }
908 if (bigContentView != null) {
909 bigContentView.setUser(user);
910 }
911 }
912
Joe Onoratocb109a02011-01-18 17:57:41 -0800913 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500914 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -0800915 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500916 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -0700917 * content views using the platform's notification layout template. If your app supports
918 * versions of Android as old as API level 4, you can instead use
919 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
920 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
921 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -0800922 *
Scott Main183bf112012-08-13 19:12:13 -0700923 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -0800924 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500925 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -0700926 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500927 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
928 * .setContentText(subject)
929 * .setSmallIcon(R.drawable.new_mail)
930 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -0400931 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500932 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -0800933 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800934 public static class Builder {
Daniel Sandler602ad1c2012-06-12 16:06:27 -0400935 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -0400936
Joe Onorato46439ce2010-11-19 13:56:21 -0800937 private Context mContext;
938
939 private long mWhen;
940 private int mSmallIcon;
941 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -0800942 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -0800943 private CharSequence mContentTitle;
944 private CharSequence mContentText;
945 private CharSequence mContentInfo;
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400946 private CharSequence mSubText;
Joe Onorato46439ce2010-11-19 13:56:21 -0800947 private PendingIntent mContentIntent;
948 private RemoteViews mContentView;
949 private PendingIntent mDeleteIntent;
950 private PendingIntent mFullScreenIntent;
951 private CharSequence mTickerText;
952 private RemoteViews mTickerView;
953 private Bitmap mLargeIcon;
954 private Uri mSound;
955 private int mAudioStreamType;
956 private long[] mVibrate;
957 private int mLedArgb;
958 private int mLedOnMs;
959 private int mLedOffMs;
960 private int mDefaults;
961 private int mFlags;
Jeff Sharkey1c400132011-08-05 14:50:13 -0700962 private int mProgressMax;
963 private int mProgress;
964 private boolean mProgressIndeterminate;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500965 private ArrayList<String> mKindList = new ArrayList<String>(1);
966 private Bundle mExtras;
967 private int mPriority;
Daniel Sandler8680bf82012-05-15 16:52:52 -0400968 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
Daniel Sandlera2985ed2012-04-03 16:42:00 -0400969 private boolean mUseChronometer;
Chris Wrenfbd96ba2012-05-01 12:03:58 -0400970 private Style mStyle;
Daniel Sandler0c890492012-09-12 17:23:10 -0700971 private boolean mShowWhen = true;
Joe Onorato46439ce2010-11-19 13:56:21 -0800972
Joe Onoratocb109a02011-01-18 17:57:41 -0800973 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500974 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -0800975 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500976
977 * <table>
978 * <tr><th align=right>priority</th>
979 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
980 * <tr><th align=right>when</th>
981 * <td>now ({@link System#currentTimeMillis()})</td></tr>
982 * <tr><th align=right>audio stream</th>
983 * <td>{@link #STREAM_DEFAULT}</td></tr>
984 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -0800985 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500986
987 * @param context
988 * A {@link Context} that will be used by the Builder to construct the
989 * RemoteViews. The Context will not be held past the lifetime of this Builder
990 * object.
Joe Onoratocb109a02011-01-18 17:57:41 -0800991 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800992 public Builder(Context context) {
993 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -0800994
995 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -0800996 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -0800997 mAudioStreamType = STREAM_DEFAULT;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500998 mPriority = PRIORITY_DEFAULT;
Joe Onorato46439ce2010-11-19 13:56:21 -0800999 }
1000
Joe Onoratocb109a02011-01-18 17:57:41 -08001001 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001002 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Daniel Sandler0c890492012-09-12 17:23:10 -07001003 * It will be shown in the notification content view by default; use
1004 * {@link Builder#setShowWhen(boolean) setShowWhen} to control this.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001005 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001006 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08001007 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001008 public Builder setWhen(long when) {
1009 mWhen = when;
1010 return this;
1011 }
1012
Joe Onoratocb109a02011-01-18 17:57:41 -08001013 /**
Daniel Sandler0c890492012-09-12 17:23:10 -07001014 * Control whether the timestamp set with {@link Builder#setWhen(long) setWhen} is shown
1015 * in the content view.
1016 */
1017 public Builder setShowWhen(boolean show) {
1018 mShowWhen = show;
1019 return this;
1020 }
1021
1022 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001023 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08001024 *
1025 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001026 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001027 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001028 * Useful when showing an elapsed time (like an ongoing phone call).
1029 *
1030 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001031 * @see Notification#when
1032 */
1033 public Builder setUsesChronometer(boolean b) {
1034 mUseChronometer = b;
1035 return this;
1036 }
1037
1038 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001039 * Set the small icon resource, which will be used to represent the notification in the
1040 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08001041 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001042
1043 * The platform template for the expanded view will draw this icon in the left, unless a
1044 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
1045 * icon will be moved to the right-hand side.
1046 *
1047
1048 * @param icon
1049 * A resource ID in the application's package of the drawable to use.
1050 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08001051 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001052 public Builder setSmallIcon(int icon) {
1053 mSmallIcon = icon;
1054 return this;
1055 }
1056
Joe Onoratocb109a02011-01-18 17:57:41 -08001057 /**
1058 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
1059 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
1060 * LevelListDrawable}.
1061 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001062 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08001063 * @param level The level to use for the icon.
1064 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001065 * @see Notification#icon
1066 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08001067 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001068 public Builder setSmallIcon(int icon, int level) {
1069 mSmallIcon = icon;
1070 mSmallIconLevel = level;
1071 return this;
1072 }
1073
Joe Onoratocb109a02011-01-18 17:57:41 -08001074 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001075 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001076 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001077 public Builder setContentTitle(CharSequence title) {
1078 mContentTitle = title;
1079 return this;
1080 }
1081
Joe Onoratocb109a02011-01-18 17:57:41 -08001082 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001083 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08001084 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001085 public Builder setContentText(CharSequence text) {
1086 mContentText = text;
1087 return this;
1088 }
1089
Joe Onoratocb109a02011-01-18 17:57:41 -08001090 /**
Joe Malin8d40d042012-11-05 11:36:40 -08001091 * Set the third line of text in the platform notification template.
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001092 * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the same location in the standard template.
1093 */
1094 public Builder setSubText(CharSequence text) {
1095 mSubText = text;
1096 return this;
1097 }
1098
1099 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08001100 * Set the large number at the right-hand side of the notification. This is
1101 * equivalent to setContentInfo, although it might show the number in a different
1102 * font size for readability.
1103 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08001104 public Builder setNumber(int number) {
1105 mNumber = number;
1106 return this;
1107 }
1108
Joe Onoratocb109a02011-01-18 17:57:41 -08001109 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001110 * A small piece of additional information pertaining to this notification.
1111 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001112 * The platform template will draw this on the last line of the notification, at the far
1113 * right (to the right of a smallIcon if it has been placed there).
Joe Onoratocb109a02011-01-18 17:57:41 -08001114 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001115 public Builder setContentInfo(CharSequence info) {
1116 mContentInfo = info;
1117 return this;
1118 }
1119
Joe Onoratocb109a02011-01-18 17:57:41 -08001120 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001121 * Set the progress this notification represents.
1122 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001123 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07001124 */
1125 public Builder setProgress(int max, int progress, boolean indeterminate) {
1126 mProgressMax = max;
1127 mProgress = progress;
1128 mProgressIndeterminate = indeterminate;
1129 return this;
1130 }
1131
1132 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001133 * Supply a custom RemoteViews to use instead of the platform template.
1134 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001135 * @see Notification#contentView
Joe Onoratocb109a02011-01-18 17:57:41 -08001136 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001137 public Builder setContent(RemoteViews views) {
1138 mContentView = views;
1139 return this;
1140 }
1141
Joe Onoratocb109a02011-01-18 17:57:41 -08001142 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001143 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
1144 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001145 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
1146 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
1147 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001148 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001149 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001150 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001151 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001152 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001153 public Builder setContentIntent(PendingIntent intent) {
1154 mContentIntent = intent;
1155 return this;
1156 }
1157
Joe Onoratocb109a02011-01-18 17:57:41 -08001158 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001159 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
1160 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001161 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001162 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001163 public Builder setDeleteIntent(PendingIntent intent) {
1164 mDeleteIntent = intent;
1165 return this;
1166 }
1167
Joe Onoratocb109a02011-01-18 17:57:41 -08001168 /**
1169 * An intent to launch instead of posting the notification to the status bar.
1170 * Only for use with extremely high-priority notifications demanding the user's
1171 * <strong>immediate</strong> attention, such as an incoming phone call or
1172 * alarm clock that the user has explicitly set to a particular time.
1173 * If this facility is used for something else, please give the user an option
1174 * to turn it off and use a normal notification, as this can be extremely
1175 * disruptive.
1176 *
1177 * @param intent The pending intent to launch.
1178 * @param highPriority Passing true will cause this notification to be sent
1179 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001180 *
1181 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08001182 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001183 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
1184 mFullScreenIntent = intent;
1185 setFlag(FLAG_HIGH_PRIORITY, highPriority);
1186 return this;
1187 }
1188
Joe Onoratocb109a02011-01-18 17:57:41 -08001189 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001190 * Set the "ticker" text which is displayed in the status bar when the notification first
Joe Onoratocb109a02011-01-18 17:57:41 -08001191 * arrives.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001192 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001193 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08001194 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001195 public Builder setTicker(CharSequence tickerText) {
1196 mTickerText = tickerText;
1197 return this;
1198 }
1199
Joe Onoratocb109a02011-01-18 17:57:41 -08001200 /**
1201 * Set the text that is displayed in the status bar when the notification first
1202 * arrives, and also a RemoteViews object that may be displayed instead on some
1203 * devices.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001204 *
1205 * @see Notification#tickerText
1206 * @see Notification#tickerView
Joe Onoratocb109a02011-01-18 17:57:41 -08001207 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001208 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
1209 mTickerText = tickerText;
1210 mTickerView = views;
1211 return this;
1212 }
1213
Joe Onoratocb109a02011-01-18 17:57:41 -08001214 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001215 * Add a large icon to the notification (and the ticker on some devices).
1216 *
1217 * In the platform template, this image will be shown on the left of the notification view
1218 * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side).
1219 *
1220 * @see Notification#largeIcon
Joe Onoratocb109a02011-01-18 17:57:41 -08001221 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001222 public Builder setLargeIcon(Bitmap icon) {
1223 mLargeIcon = icon;
1224 return this;
1225 }
1226
Joe Onoratocb109a02011-01-18 17:57:41 -08001227 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001228 * Set the sound to play.
1229 *
1230 * It will be played on the {@link #STREAM_DEFAULT default stream} for notifications.
1231 *
1232 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001233 */
Joe Onorato52f80cd2010-11-21 15:34:48 -08001234 public Builder setSound(Uri sound) {
1235 mSound = sound;
1236 mAudioStreamType = STREAM_DEFAULT;
1237 return this;
1238 }
1239
Joe Onoratocb109a02011-01-18 17:57:41 -08001240 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001241 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08001242 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001243 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
1244 *
1245 * @see Notification#sound
Joe Onoratocb109a02011-01-18 17:57:41 -08001246 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001247 public Builder setSound(Uri sound, int streamType) {
1248 mSound = sound;
1249 mAudioStreamType = streamType;
1250 return this;
1251 }
1252
Joe Onoratocb109a02011-01-18 17:57:41 -08001253 /**
1254 * Set the vibration pattern to use.
1255 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001256
1257 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
1258 * <code>pattern</code> parameter.
1259 *
1260
1261 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08001262 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001263 public Builder setVibrate(long[] pattern) {
1264 mVibrate = pattern;
1265 return this;
1266 }
1267
Joe Onoratocb109a02011-01-18 17:57:41 -08001268 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001269 * Set the desired color for the indicator LED on the device, as well as the
1270 * blink duty cycle (specified in milliseconds).
1271 *
1272
1273 * Not all devices will honor all (or even any) of these values.
1274 *
1275
1276 * @see Notification#ledARGB
1277 * @see Notification#ledOnMS
1278 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08001279 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001280 public Builder setLights(int argb, int onMs, int offMs) {
1281 mLedArgb = argb;
1282 mLedOnMs = onMs;
1283 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -08001284 return this;
1285 }
1286
Joe Onoratocb109a02011-01-18 17:57:41 -08001287 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001288 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08001289 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001290
1291 * Ongoing notifications cannot be dismissed by the user, so your application or service
1292 * must take care of canceling them.
1293 *
1294
1295 * They are typically used to indicate a background task that the user is actively engaged
1296 * with (e.g., playing music) or is pending in some way and therefore occupying the device
1297 * (e.g., a file download, sync operation, active network connection).
1298 *
1299
1300 * @see Notification#FLAG_ONGOING_EVENT
1301 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08001302 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001303 public Builder setOngoing(boolean ongoing) {
1304 setFlag(FLAG_ONGOING_EVENT, ongoing);
1305 return this;
1306 }
1307
Joe Onoratocb109a02011-01-18 17:57:41 -08001308 /**
1309 * Set this flag if you would only like the sound, vibrate
1310 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001311 *
1312 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08001313 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001314 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
1315 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
1316 return this;
1317 }
1318
Joe Onoratocb109a02011-01-18 17:57:41 -08001319 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001320 * Make this notification automatically dismissed when the user touches it. The
1321 * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens.
1322 *
1323 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08001324 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001325 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08001326 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08001327 return this;
1328 }
1329
Joe Onoratocb109a02011-01-18 17:57:41 -08001330 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001331 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08001332 * <p>
1333 * The value should be one or more of the following fields combined with
1334 * bitwise-or:
1335 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
1336 * <p>
1337 * For all default values, use {@link #DEFAULT_ALL}.
1338 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001339 public Builder setDefaults(int defaults) {
1340 mDefaults = defaults;
1341 return this;
1342 }
1343
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001344 /**
1345 * Set the priority of this notification.
1346 *
1347 * @see Notification#priority
1348 */
1349 public Builder setPriority(int pri) {
1350 mPriority = pri;
1351 return this;
1352 }
Joe Malin8d40d042012-11-05 11:36:40 -08001353
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001354 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04001355 * @hide
Joe Malin8d40d042012-11-05 11:36:40 -08001356 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001357 * Add a kind (category) to this notification. Optional.
Joe Malin8d40d042012-11-05 11:36:40 -08001358 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001359 * @see Notification#kind
1360 */
1361 public Builder addKind(String k) {
1362 mKindList.add(k);
1363 return this;
1364 }
1365
1366 /**
1367 * Add metadata to this notification.
1368 *
1369 * A reference to the Bundle is held for the lifetime of this Builder, and the Bundle's
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001370 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001371 * called.
1372 *
1373 * @see Notification#extras
1374 * @hide
1375 */
1376 public Builder setExtras(Bundle bag) {
1377 mExtras = bag;
1378 return this;
1379 }
1380
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001381 /**
1382 * Add an action to this notification. Actions are typically displayed by
1383 * the system as a button adjacent to the notification content.
Joe Malin8d40d042012-11-05 11:36:40 -08001384 * <br>
1385 * A notification displays up to 3 actions, from left to right in the order they were added.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001386 *
1387 * @param icon Resource ID of a drawable that represents the action.
1388 * @param title Text describing the action.
1389 * @param intent PendingIntent to be fired when the action is invoked.
1390 */
1391 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
1392 mActions.add(new Action(icon, title, intent));
1393 return this;
1394 }
1395
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001396 /**
1397 * Add a rich notification style to be applied at build time.
1398 *
1399 * @param style Object responsible for modifying the notification style.
1400 */
1401 public Builder setStyle(Style style) {
1402 if (mStyle != style) {
1403 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07001404 if (mStyle != null) {
1405 mStyle.setBuilder(this);
1406 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001407 }
1408 return this;
1409 }
1410
Joe Onorato46439ce2010-11-19 13:56:21 -08001411 private void setFlag(int mask, boolean value) {
1412 if (value) {
1413 mFlags |= mask;
1414 } else {
1415 mFlags &= ~mask;
1416 }
1417 }
1418
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001419 private RemoteViews applyStandardTemplate(int resId, boolean fitIn1U) {
Joe Onorato561d3852010-11-20 18:09:34 -08001420 RemoteViews contentView = new RemoteViews(mContext.getPackageName(), resId);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001421 boolean showLine3 = false;
1422 boolean showLine2 = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001423 int smallIconImageViewId = R.id.icon;
1424 if (mLargeIcon != null) {
1425 contentView.setImageViewBitmap(R.id.icon, mLargeIcon);
1426 smallIconImageViewId = R.id.right_icon;
1427 }
Daniel Sandlere95658c2012-05-10 00:33:54 -04001428 if (mPriority < PRIORITY_LOW) {
1429 contentView.setInt(R.id.icon,
1430 "setBackgroundResource", R.drawable.notification_template_icon_low_bg);
1431 contentView.setInt(R.id.status_bar_latest_event_content,
1432 "setBackgroundResource", R.drawable.notification_bg_low);
1433 }
Joe Onorato561d3852010-11-20 18:09:34 -08001434 if (mSmallIcon != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001435 contentView.setImageViewResource(smallIconImageViewId, mSmallIcon);
1436 contentView.setViewVisibility(smallIconImageViewId, View.VISIBLE);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001437 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001438 contentView.setViewVisibility(smallIconImageViewId, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001439 }
1440 if (mContentTitle != null) {
1441 contentView.setTextViewText(R.id.title, mContentTitle);
1442 }
1443 if (mContentText != null) {
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001444 contentView.setTextViewText(R.id.text, mContentText);
1445 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001446 }
1447 if (mContentInfo != null) {
1448 contentView.setTextViewText(R.id.info, mContentInfo);
Jeff Sharkey1c400132011-08-05 14:50:13 -07001449 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001450 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001451 } else if (mNumber > 0) {
Daniel Sandlerebce0112011-06-16 16:44:51 -04001452 final int tooBig = mContext.getResources().getInteger(
1453 R.integer.status_bar_notification_info_maxnum);
1454 if (mNumber > tooBig) {
1455 contentView.setTextViewText(R.id.info, mContext.getResources().getString(
1456 R.string.status_bar_notification_info_overflow));
Joe Onorato059a2f82011-01-04 10:27:01 -08001457 } else {
1458 NumberFormat f = NumberFormat.getIntegerInstance();
1459 contentView.setTextViewText(R.id.info, f.format(mNumber));
1460 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001461 contentView.setViewVisibility(R.id.info, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001462 showLine3 = true;
Joe Onorato561d3852010-11-20 18:09:34 -08001463 } else {
1464 contentView.setViewVisibility(R.id.info, View.GONE);
1465 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001466
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001467 // Need to show three lines?
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001468 if (mSubText != null) {
1469 contentView.setTextViewText(R.id.text, mSubText);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001470 if (mContentText != null) {
1471 contentView.setTextViewText(R.id.text2, mContentText);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001472 contentView.setViewVisibility(R.id.text2, View.VISIBLE);
1473 showLine2 = true;
1474 } else {
1475 contentView.setViewVisibility(R.id.text2, View.GONE);
1476 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001477 } else {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001478 contentView.setViewVisibility(R.id.text2, View.GONE);
1479 if (mProgressMax != 0 || mProgressIndeterminate) {
1480 contentView.setProgressBar(
1481 R.id.progress, mProgressMax, mProgress, mProgressIndeterminate);
1482 contentView.setViewVisibility(R.id.progress, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001483 showLine2 = true;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001484 } else {
1485 contentView.setViewVisibility(R.id.progress, View.GONE);
1486 }
Jeff Sharkey1c400132011-08-05 14:50:13 -07001487 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001488 if (showLine2) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001489 if (fitIn1U) {
1490 // need to shrink all the type to make sure everything fits
1491 final Resources res = mContext.getResources();
1492 final float subTextSize = res.getDimensionPixelSize(
1493 R.dimen.notification_subtext_size);
1494 contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize);
1495 }
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001496 // vertical centering
1497 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
1498 }
1499
Daniel Sandler0c890492012-09-12 17:23:10 -07001500 if (mWhen != 0 && mShowWhen) {
Daniel Sandlera2985ed2012-04-03 16:42:00 -04001501 if (mUseChronometer) {
1502 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
1503 contentView.setLong(R.id.chronometer, "setBase",
1504 mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
1505 contentView.setBoolean(R.id.chronometer, "setStarted", true);
1506 } else {
1507 contentView.setViewVisibility(R.id.time, View.VISIBLE);
1508 contentView.setLong(R.id.time, "setTime", mWhen);
1509 }
Daniel Sandler0c890492012-09-12 17:23:10 -07001510 } else {
1511 contentView.setViewVisibility(R.id.time, View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001512 }
Daniel Sandler0c890492012-09-12 17:23:10 -07001513
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001514 contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001515 contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE);
Joe Onorato561d3852010-11-20 18:09:34 -08001516 return contentView;
1517 }
1518
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001519 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001520 RemoteViews big = applyStandardTemplate(layoutId, false);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001521
1522 int N = mActions.size();
1523 if (N > 0) {
Chris Wrend6297db2012-05-03 16:20:13 -04001524 // Log.d("Notification", "has actions: " + mContentText);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001525 big.setViewVisibility(R.id.actions, View.VISIBLE);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001526 big.setViewVisibility(R.id.action_divider, View.VISIBLE);
Daniel Sandler8680bf82012-05-15 16:52:52 -04001527 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Chris Wren2c22eb02012-05-08 09:49:13 -04001528 big.removeAllViews(R.id.actions);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001529 for (int i=0; i<N; i++) {
1530 final RemoteViews button = generateActionButton(mActions.get(i));
Chris Wrend6297db2012-05-03 16:20:13 -04001531 //Log.d("Notification", "adding action " + i + ": " + mActions.get(i).title);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001532 big.addView(R.id.actions, button);
1533 }
1534 }
1535 return big;
1536 }
1537
Joe Onorato46439ce2010-11-19 13:56:21 -08001538 private RemoteViews makeContentView() {
1539 if (mContentView != null) {
1540 return mContentView;
1541 } else {
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001542 return applyStandardTemplate(R.layout.notification_template_base, true); // no more special large_icon flavor
Joe Onorato46439ce2010-11-19 13:56:21 -08001543 }
1544 }
1545
1546 private RemoteViews makeTickerView() {
1547 if (mTickerView != null) {
1548 return mTickerView;
1549 } else {
Joe Onorato561d3852010-11-20 18:09:34 -08001550 if (mContentView == null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001551 return applyStandardTemplate(mLargeIcon == null
Joe Onorato561d3852010-11-20 18:09:34 -08001552 ? R.layout.status_bar_latest_event_ticker
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001553 : R.layout.status_bar_latest_event_ticker_large_icon, true);
Joe Onorato561d3852010-11-20 18:09:34 -08001554 } else {
1555 return null;
1556 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001557 }
1558 }
1559
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001560 private RemoteViews makeBigContentView() {
1561 if (mActions.size() == 0) return null;
1562
Chris Wrenb023bf82012-04-23 16:05:42 -04001563 return applyStandardTemplateWithActions(R.layout.notification_template_big_base);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001564 }
1565
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001566 private RemoteViews generateActionButton(Action action) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04001567 final boolean tombstone = (action.actionIntent == null);
Joe Malin8d40d042012-11-05 11:36:40 -08001568 RemoteViews button = new RemoteViews(mContext.getPackageName(),
Daniel Sandler8680bf82012-05-15 16:52:52 -04001569 tombstone ? R.layout.notification_action_tombstone
1570 : R.layout.notification_action);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001571 button.setTextViewCompoundDrawables(R.id.action0, action.icon, 0, 0, 0);
1572 button.setTextViewText(R.id.action0, action.title);
Daniel Sandler8680bf82012-05-15 16:52:52 -04001573 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04001574 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04001575 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001576 button.setContentDescription(R.id.action0, action.title);
1577 return button;
1578 }
1579
Joe Onoratocb109a02011-01-18 17:57:41 -08001580 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001581 * Apply the unstyled operations and return a new {@link Notification} object.
Joe Onoratocb109a02011-01-18 17:57:41 -08001582 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001583 private Notification buildUnstyled() {
Joe Onorato46439ce2010-11-19 13:56:21 -08001584 Notification n = new Notification();
1585 n.when = mWhen;
1586 n.icon = mSmallIcon;
1587 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -08001588 n.number = mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -08001589 n.contentView = makeContentView();
1590 n.contentIntent = mContentIntent;
1591 n.deleteIntent = mDeleteIntent;
1592 n.fullScreenIntent = mFullScreenIntent;
1593 n.tickerText = mTickerText;
1594 n.tickerView = makeTickerView();
1595 n.largeIcon = mLargeIcon;
1596 n.sound = mSound;
1597 n.audioStreamType = mAudioStreamType;
1598 n.vibrate = mVibrate;
1599 n.ledARGB = mLedArgb;
1600 n.ledOnMS = mLedOnMs;
1601 n.ledOffMS = mLedOffMs;
1602 n.defaults = mDefaults;
1603 n.flags = mFlags;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04001604 n.bigContentView = makeBigContentView();
Joe Onorato8d0b6552010-11-22 16:09:29 -08001605 if (mLedOnMs != 0 && mLedOffMs != 0) {
1606 n.flags |= FLAG_SHOW_LIGHTS;
1607 }
1608 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
1609 n.flags |= FLAG_SHOW_LIGHTS;
1610 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001611 if (mKindList.size() > 0) {
1612 n.kind = new String[mKindList.size()];
1613 mKindList.toArray(n.kind);
1614 } else {
1615 n.kind = null;
1616 }
1617 n.priority = mPriority;
1618 n.extras = mExtras != null ? new Bundle(mExtras) : null;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001619 if (mActions.size() > 0) {
1620 n.actions = new Action[mActions.size()];
1621 mActions.toArray(n.actions);
1622 }
Joe Onorato46439ce2010-11-19 13:56:21 -08001623 return n;
1624 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001625
1626 /**
1627 * @deprecated Use {@link #build()} instead.
1628 */
1629 @Deprecated
1630 public Notification getNotification() {
1631 return build();
1632 }
1633
1634 /**
1635 * Combine all of the options that have been set and return a new {@link Notification}
1636 * object.
1637 */
1638 public Notification build() {
1639 if (mStyle != null) {
1640 return mStyle.build();
1641 } else {
1642 return buildUnstyled();
1643 }
1644 }
1645 }
1646
1647
1648 /**
1649 * An object that can apply a rich notification style to a {@link Notification.Builder}
1650 * object.
1651 */
Chris Wrend6297db2012-05-03 16:20:13 -04001652 public static abstract class Style
1653 {
1654 private CharSequence mBigContentTitle;
1655 private CharSequence mSummaryText = null;
Daniel Sandler619738c2012-06-07 16:33:08 -04001656 private boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04001657
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001658 protected Builder mBuilder;
1659
Chris Wrend6297db2012-05-03 16:20:13 -04001660 /**
1661 * Overrides ContentTitle in the big form of the template.
1662 * This defaults to the value passed to setContentTitle().
1663 */
1664 protected void internalSetBigContentTitle(CharSequence title) {
1665 mBigContentTitle = title;
1666 }
1667
1668 /**
1669 * Set the first line of text after the detail section in the big form of the template.
1670 */
1671 protected void internalSetSummaryText(CharSequence cs) {
1672 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04001673 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04001674 }
1675
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001676 public void setBuilder(Builder builder) {
1677 if (mBuilder != builder) {
1678 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07001679 if (mBuilder != null) {
1680 mBuilder.setStyle(this);
1681 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001682 }
1683 }
1684
Chris Wrend6297db2012-05-03 16:20:13 -04001685 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001686 if (mBuilder == null) {
1687 throw new IllegalArgumentException("Style requires a valid Builder object");
1688 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001689 }
Chris Wrend6297db2012-05-03 16:20:13 -04001690
1691 protected RemoteViews getStandardView(int layoutId) {
1692 checkBuilder();
1693
1694 if (mBigContentTitle != null) {
1695 mBuilder.setContentTitle(mBigContentTitle);
1696 }
1697
Chris Wrend6297db2012-05-03 16:20:13 -04001698 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
1699
Chris Wrend6297db2012-05-03 16:20:13 -04001700 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
1701 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04001702 } else {
1703 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04001704 }
1705
Daniel Sandler619738c2012-06-07 16:33:08 -04001706 // The last line defaults to the subtext, but can be replaced by mSummaryText
1707 final CharSequence overflowText =
1708 mSummaryTextSet ? mSummaryText
1709 : mBuilder.mSubText;
1710 if (overflowText != null) {
1711 contentView.setTextViewText(R.id.text, overflowText);
1712 contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE);
Daniel Sandler9f7936a2012-05-21 16:14:28 -04001713 contentView.setViewVisibility(R.id.line3, View.VISIBLE);
Daniel Sandler916ad912012-06-13 12:17:07 -04001714 } else {
1715 contentView.setViewVisibility(R.id.overflow_divider, View.GONE);
1716 contentView.setViewVisibility(R.id.line3, View.GONE);
Chris Wrend6297db2012-05-03 16:20:13 -04001717 }
1718
1719 return contentView;
1720 }
1721
1722 public abstract Notification build();
Joe Onorato46439ce2010-11-19 13:56:21 -08001723 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001724
1725 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001726 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08001727 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001728 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001729 * <pre class="prettyprint">
1730 * Notification noti = new Notification.BigPictureStyle(
1731 * new Notification.Builder()
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001732 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001733 * .setContentText(subject)
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001734 * .setSmallIcon(R.drawable.new_post)
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001735 * .setLargeIcon(aBitmap))
1736 * .bigPicture(aBigBitmap)
1737 * .build();
1738 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08001739 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001740 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001741 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001742 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001743 private Bitmap mPicture;
Chris Wren3745a3d2012-05-22 15:11:52 -04001744 private Bitmap mBigLargeIcon;
1745 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001746
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001747 public BigPictureStyle() {
1748 }
1749
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001750 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001751 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001752 }
1753
Chris Wrend6297db2012-05-03 16:20:13 -04001754 /**
1755 * Overrides ContentTitle in the big form of the template.
1756 * This defaults to the value passed to setContentTitle().
1757 */
1758 public BigPictureStyle setBigContentTitle(CharSequence title) {
1759 internalSetBigContentTitle(title);
1760 return this;
1761 }
1762
1763 /**
1764 * Set the first line of text after the detail section in the big form of the template.
1765 */
1766 public BigPictureStyle setSummaryText(CharSequence cs) {
1767 internalSetSummaryText(cs);
1768 return this;
1769 }
1770
Chris Wren0bd664d2012-08-01 13:56:56 -04001771 /**
1772 * Provide the bitmap to be used as the payload for the BigPicture notification.
1773 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001774 public BigPictureStyle bigPicture(Bitmap b) {
1775 mPicture = b;
1776 return this;
1777 }
1778
Chris Wren3745a3d2012-05-22 15:11:52 -04001779 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04001780 * Override the large icon when the big notification is shown.
1781 */
1782 public BigPictureStyle bigLargeIcon(Bitmap b) {
1783 mBigLargeIconSet = true;
1784 mBigLargeIcon = b;
1785 return this;
1786 }
1787
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001788 private RemoteViews makeBigContentView() {
Chris Wrend6297db2012-05-03 16:20:13 -04001789 RemoteViews contentView = getStandardView(R.layout.notification_template_big_picture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001790
1791 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
1792
1793 return contentView;
1794 }
1795
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001796 @Override
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001797 public Notification build() {
Chris Wrend6297db2012-05-03 16:20:13 -04001798 checkBuilder();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001799 Notification wip = mBuilder.buildUnstyled();
Chris Wren3745a3d2012-05-22 15:11:52 -04001800 if (mBigLargeIconSet ) {
1801 mBuilder.mLargeIcon = mBigLargeIcon;
1802 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001803 wip.bigContentView = makeBigContentView();
1804 return wip;
1805 }
1806 }
1807
1808 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001809 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08001810 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001811 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001812 * <pre class="prettyprint">
Daniel Sandler87682782012-11-07 14:04:42 -05001813 * Notification noti = new Notification.BigTextStyle(
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001814 * new Notification.Builder()
1815 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
1816 * .setContentText(subject)
1817 * .setSmallIcon(R.drawable.new_mail)
1818 * .setLargeIcon(aBitmap))
1819 * .bigText(aVeryLongString)
1820 * .build();
1821 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08001822 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04001823 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001824 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001825 public static class BigTextStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001826 private CharSequence mBigText;
1827
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001828 public BigTextStyle() {
1829 }
1830
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001831 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001832 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001833 }
1834
Chris Wrend6297db2012-05-03 16:20:13 -04001835 /**
1836 * Overrides ContentTitle in the big form of the template.
1837 * This defaults to the value passed to setContentTitle().
1838 */
1839 public BigTextStyle setBigContentTitle(CharSequence title) {
1840 internalSetBigContentTitle(title);
1841 return this;
1842 }
1843
1844 /**
1845 * Set the first line of text after the detail section in the big form of the template.
1846 */
1847 public BigTextStyle setSummaryText(CharSequence cs) {
1848 internalSetSummaryText(cs);
1849 return this;
1850 }
1851
Chris Wren0bd664d2012-08-01 13:56:56 -04001852 /**
1853 * Provide the longer text to be displayed in the big form of the
1854 * template in place of the content text.
1855 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001856 public BigTextStyle bigText(CharSequence cs) {
1857 mBigText = cs;
1858 return this;
1859 }
1860
1861 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04001862 // Remove the content text so line3 only shows if you have a summary
1863 final boolean hadThreeLines = (mBuilder.mContentText != null && mBuilder.mSubText != null);
Daniel Sandler6387d2f2012-05-22 13:44:09 -04001864 mBuilder.mContentText = null;
Daniel Sandler916ad912012-06-13 12:17:07 -04001865
Chris Wrend6297db2012-05-03 16:20:13 -04001866 RemoteViews contentView = getStandardView(R.layout.notification_template_big_text);
Joe Malin8d40d042012-11-05 11:36:40 -08001867
Daniel Sandler619738c2012-06-07 16:33:08 -04001868 if (hadThreeLines) {
1869 // vertical centering
1870 contentView.setViewPadding(R.id.line1, 0, 0, 0, 0);
1871 }
1872
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001873 contentView.setTextViewText(R.id.big_text, mBigText);
1874 contentView.setViewVisibility(R.id.big_text, View.VISIBLE);
Chris Wren3c5f92432012-05-04 16:31:17 -04001875 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001876
1877 return contentView;
1878 }
1879
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001880 @Override
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001881 public Notification build() {
Chris Wrend6297db2012-05-03 16:20:13 -04001882 checkBuilder();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001883 Notification wip = mBuilder.buildUnstyled();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001884 wip.bigContentView = makeBigContentView();
1885 return wip;
1886 }
1887 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04001888
1889 /**
1890 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08001891 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04001892 * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so:
1893 * <pre class="prettyprint">
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001894 * Notification noti = new Notification.InboxStyle(
Daniel Sandler879c5e02012-04-17 16:46:51 -04001895 * new Notification.Builder()
Chris Wrend6297db2012-05-03 16:20:13 -04001896 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
Daniel Sandler879c5e02012-04-17 16:46:51 -04001897 * .setContentText(subject)
1898 * .setSmallIcon(R.drawable.new_mail)
1899 * .setLargeIcon(aBitmap))
1900 * .addLine(str1)
1901 * .addLine(str2)
Chris Wrend6297db2012-05-03 16:20:13 -04001902 * .setContentTitle("")
1903 * .setSummaryText(&quot;+3 more&quot;)
Daniel Sandler879c5e02012-04-17 16:46:51 -04001904 * .build();
1905 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08001906 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04001907 * @see Notification#bigContentView
1908 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001909 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04001910 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
1911
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001912 public InboxStyle() {
1913 }
1914
Daniel Sandler879c5e02012-04-17 16:46:51 -04001915 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001916 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04001917 }
1918
Chris Wrend6297db2012-05-03 16:20:13 -04001919 /**
1920 * Overrides ContentTitle in the big form of the template.
1921 * This defaults to the value passed to setContentTitle().
1922 */
1923 public InboxStyle setBigContentTitle(CharSequence title) {
1924 internalSetBigContentTitle(title);
1925 return this;
1926 }
1927
1928 /**
1929 * Set the first line of text after the detail section in the big form of the template.
1930 */
1931 public InboxStyle setSummaryText(CharSequence cs) {
1932 internalSetSummaryText(cs);
1933 return this;
1934 }
1935
Chris Wren0bd664d2012-08-01 13:56:56 -04001936 /**
1937 * Append a line to the digest section of the Inbox notification.
1938 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04001939 public InboxStyle addLine(CharSequence cs) {
1940 mTexts.add(cs);
1941 return this;
1942 }
1943
1944 private RemoteViews makeBigContentView() {
Daniel Sandler619738c2012-06-07 16:33:08 -04001945 // Remove the content text so line3 disappears unless you have a summary
1946 mBuilder.mContentText = null;
Chris Wrend6297db2012-05-03 16:20:13 -04001947 RemoteViews contentView = getStandardView(R.layout.notification_template_inbox);
Daniel Sandler619738c2012-06-07 16:33:08 -04001948
Chris Wrend6297db2012-05-03 16:20:13 -04001949 contentView.setViewVisibility(R.id.text2, View.GONE);
Daniel Sandler879c5e02012-04-17 16:46:51 -04001950
Chris Wrend6297db2012-05-03 16:20:13 -04001951 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 -04001952 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04001953
Chris Wren4ed80d52012-05-17 09:30:03 -04001954 // Make sure all rows are gone in case we reuse a view.
1955 for (int rowId : rowIds) {
1956 contentView.setViewVisibility(rowId, View.GONE);
1957 }
1958
Chris Wren683ab002012-09-20 10:35:54 -04001959
Daniel Sandler879c5e02012-04-17 16:46:51 -04001960 int i=0;
1961 while (i < mTexts.size() && i < rowIds.length) {
1962 CharSequence str = mTexts.get(i);
1963 if (str != null && !str.equals("")) {
1964 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
1965 contentView.setTextViewText(rowIds[i], str);
1966 }
1967 i++;
1968 }
1969
Chris Wren683ab002012-09-20 10:35:54 -04001970 contentView.setViewVisibility(R.id.inbox_end_pad,
1971 mTexts.size() > 0 ? View.VISIBLE : View.GONE);
1972
1973 contentView.setViewVisibility(R.id.inbox_more,
1974 mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE);
Chris Wren29bb6d92012-05-17 18:09:42 -04001975
Daniel Sandler879c5e02012-04-17 16:46:51 -04001976 return contentView;
1977 }
1978
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001979 @Override
Daniel Sandler879c5e02012-04-17 16:46:51 -04001980 public Notification build() {
Chris Wrend6297db2012-05-03 16:20:13 -04001981 checkBuilder();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04001982 Notification wip = mBuilder.buildUnstyled();
Daniel Sandler879c5e02012-04-17 16:46:51 -04001983 wip.bigContentView = makeBigContentView();
1984 return wip;
1985 }
1986 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987}