blob: 7eab7c84d7ca59762ca5027e19e07e617703dfbe [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
Joe Onorato8595a3d2010-11-19 18:12:07 -080019import java.text.NumberFormat;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import java.util.Date;
21
22import android.app.PendingIntent;
23import android.content.Context;
24import android.content.Intent;
Joe Onoratoef1e7762010-09-17 18:38:38 -040025import android.graphics.Bitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.media.AudioManager;
27import android.net.Uri;
28import android.os.Parcel;
29import android.os.Parcelable;
30import android.text.TextUtils;
31import android.text.format.DateFormat;
32import android.text.format.DateUtils;
Joe Onorato46439ce2010-11-19 13:56:21 -080033import android.util.Slog;
Joe Onorato8595a3d2010-11-19 18:12:07 -080034import android.view.View;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.widget.RemoteViews;
36
37/**
38 * A class that represents how a persistent notification is to be presented to
39 * the user using the {@link android.app.NotificationManager}.
40 *
Scott Mainb8b36452009-04-26 15:50:49 -070041 * <p>For a guide to creating notifications, see the
42 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Creating Status
43 * Bar Notifications</a> document in the Dev Guide.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044 */
45public class Notification implements Parcelable
46{
Joe Onorato46439ce2010-11-19 13:56:21 -080047 private static final String TAG = "Notification";
48
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049 /**
50 * Use all default values (where applicable).
51 */
52 public static final int DEFAULT_ALL = ~0;
53
54 /**
55 * Use the default notification sound. This will ignore any given
56 * {@link #sound}.
57 *
58 * @see #defaults
59 */
60 public static final int DEFAULT_SOUND = 1;
61
62 /**
63 * Use the default notification vibrate. This will ignore any given
Scott Mainb8b36452009-04-26 15:50:49 -070064 * {@link #vibrate}. Using phone vibration requires the
65 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 *
67 * @see #defaults
68 */
69 public static final int DEFAULT_VIBRATE = 2;
70
71 /**
72 * Use the default notification lights. This will ignore the
73 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
74 * {@link #ledOnMS}.
75 *
76 * @see #defaults
77 */
78 public static final int DEFAULT_LIGHTS = 4;
79
80 /**
81 * The timestamp for the notification. The icons and expanded views
82 * are sorted by this key.
83 */
84 public long when;
85
86 /**
87 * The resource id of a drawable to use as the icon in the status bar.
88 */
89 public int icon;
90
91 /**
Joe Onorato46439ce2010-11-19 13:56:21 -080092 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
93 * leave it at its default value of 0.
94 *
95 * @see android.widget.ImageView#setImageLevel
96 * @see android.graphics.drawable#setLevel
97 */
98 public int iconLevel;
99
100 /**
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400101 * The number of events that this notification represents. For example, in a new mail
102 * notification, this could be the number of unread messages. This number is superimposed over
103 * the icon in the status bar. If the number is 0 or negative, it is not shown in the status
104 * bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 */
106 public int number;
107
108 /**
109 * The intent to execute when the expanded status entry is clicked. If
110 * this is an activity, it must include the
111 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
112 * that you take care of task management as described in the <em>Activities and Tasks</em>
113 * section of the <a href="{@docRoot}guide/topics/fundamentals.html#acttask">Application
114 * Fundamentals</a> document.
115 */
116 public PendingIntent contentIntent;
117
118 /**
119 * The intent to execute when the status entry is deleted by the user
120 * with the "Clear All Notifications" button. This probably shouldn't
121 * be launching an activity since several of those will be sent at the
122 * same time.
123 */
124 public PendingIntent deleteIntent;
125
126 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700127 * An intent to launch instead of posting the notification to the status bar.
128 * Only for use with extremely high-priority notifications demanding the user's
Dianne Hackborn392fea52010-09-09 16:44:01 -0700129 * <strong>immediate</strong> attention, such as an incoming phone call or
Dianne Hackborn170bae72010-09-03 15:14:28 -0700130 * alarm clock that the user has explicitly set to a particular time.
Dianne Hackborn170bae72010-09-03 15:14:28 -0700131 * If this facility is used for something else, please give the user an option
132 * to turn it off and use a normal notification, as this can be extremely
133 * disruptive.
Dianne Hackborn61714012010-09-03 16:40:58 -0700134 *
135 * <p>Use with {@link #FLAG_HIGH_PRIORITY} to ensure that this notification
136 * will reach the user even when other notifications are suppressed.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400137 */
138 public PendingIntent fullScreenIntent;
139
140 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 * Text to scroll across the screen when this item is added to
Joe Onoratoef1e7762010-09-17 18:38:38 -0400142 * the status bar on large and smaller devices.
143 *
144 * <p>This field is provided separately from the other ticker fields
145 * both for compatibility and to allow an application to choose different
146 * text for when the text scrolls in and when it is displayed all at once
147 * in conjunction with one or more icons.
148 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800149 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 */
151 public CharSequence tickerText;
152
153 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800154 * The view to show as the ticker in the status bar when the notification
155 * is posted.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400156 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800157 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400158
159 /**
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400160 * The view that will represent this notification in the expanded status bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 */
162 public RemoteViews contentView;
163
164 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800165 * The bitmap that may escape the bounds of the panel and bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800167 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168
169 /**
170 * The sound to play.
171 *
172 * <p>
173 * To play the default notification sound, see {@link #defaults}.
174 * </p>
175 */
176 public Uri sound;
177
178 /**
179 * Use this constant as the value for audioStreamType to request that
180 * the default stream type for notifications be used. Currently the
181 * default stream type is STREAM_RING.
182 */
183 public static final int STREAM_DEFAULT = -1;
184
185 /**
186 * The audio stream type to use when playing the sound.
187 * Should be one of the STREAM_ constants from
188 * {@link android.media.AudioManager}.
189 */
190 public int audioStreamType = STREAM_DEFAULT;
191
192
193 /**
Scott Mainb8b36452009-04-26 15:50:49 -0700194 * The pattern with which to vibrate.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800195 *
196 * <p>
197 * To vibrate the default pattern, see {@link #defaults}.
198 * </p>
199 *
200 * @see android.os.Vibrator#vibrate(long[],int)
201 */
202 public long[] vibrate;
203
204 /**
205 * The color of the led. The hardware will do its best approximation.
206 *
207 * @see #FLAG_SHOW_LIGHTS
208 * @see #flags
209 */
210 public int ledARGB;
211
212 /**
213 * The number of milliseconds for the LED to be on while it's flashing.
214 * The hardware will do its best approximation.
215 *
216 * @see #FLAG_SHOW_LIGHTS
217 * @see #flags
218 */
219 public int ledOnMS;
220
221 /**
222 * The number of milliseconds for the LED to be off while it's flashing.
223 * The hardware will do its best approximation.
224 *
225 * @see #FLAG_SHOW_LIGHTS
226 * @see #flags
227 */
228 public int ledOffMS;
229
230 /**
231 * Specifies which values should be taken from the defaults.
232 * <p>
233 * To set, OR the desired from {@link #DEFAULT_SOUND},
234 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
235 * values, use {@link #DEFAULT_ALL}.
236 * </p>
237 */
238 public int defaults;
239
240
241 /**
242 * Bit to be bitwise-ored into the {@link #flags} field that should be
243 * set if you want the LED on for this notification.
244 * <ul>
245 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
246 * or 0 for both ledOnMS and ledOffMS.</li>
247 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
248 * <li>To flash the LED, pass the number of milliseconds that it should
249 * be on and off to ledOnMS and ledOffMS.</li>
250 * </ul>
251 * <p>
252 * Since hardware varies, you are not guaranteed that any of the values
253 * you pass are honored exactly. Use the system defaults (TODO) if possible
254 * because they will be set to values that work on any given hardware.
255 * <p>
256 * The alpha channel must be set for forward compatibility.
257 *
258 */
259 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
260
261 /**
262 * Bit to be bitwise-ored into the {@link #flags} field that should be
263 * set if this notification is in reference to something that is ongoing,
264 * like a phone call. It should not be set if this notification is in
265 * reference to something that happened at a particular point in time,
266 * like a missed phone call.
267 */
268 public static final int FLAG_ONGOING_EVENT = 0x00000002;
269
270 /**
271 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700272 * the audio will be repeated until the notification is
273 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800274 */
275 public static final int FLAG_INSISTENT = 0x00000004;
276
277 /**
278 * Bit to be bitwise-ored into the {@link #flags} field that should be
279 * set if you want the sound and/or vibration play each time the
280 * notification is sent, even if it has not been canceled before that.
281 */
282 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
283
284 /**
285 * Bit to be bitwise-ored into the {@link #flags} field that should be
286 * set if the notification should be canceled when it is clicked by the
287 * user.
288 */
289 public static final int FLAG_AUTO_CANCEL = 0x00000010;
290
291 /**
292 * Bit to be bitwise-ored into the {@link #flags} field that should be
293 * set if the notification should not be canceled when the user clicks
294 * the Clear all button.
295 */
296 public static final int FLAG_NO_CLEAR = 0x00000020;
297
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700298 /**
299 * Bit to be bitwise-ored into the {@link #flags} field that should be
300 * set if this notification represents a currently running service. This
301 * will normally be set for you by {@link Service#startForeground}.
302 */
303 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
304
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400305 /**
306 * Bit to be bitwise-ored into the {@link #flags} field that should be set if this notification
307 * represents a high-priority event that may be shown to the user even if notifications are
308 * otherwise unavailable (that is, when the status bar is hidden). This flag is ideally used
309 * in conjunction with {@link #fullScreenIntent}.
310 */
311 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
312
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 public int flags;
314
315 /**
316 * Constructs a Notification object with everything set to 0.
Joe Onorato46439ce2010-11-19 13:56:21 -0800317 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800318 */
319 public Notification()
320 {
321 this.when = System.currentTimeMillis();
322 }
323
324 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 * @hide
326 */
327 public Notification(Context context, int icon, CharSequence tickerText, long when,
328 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
329 {
330 this.when = when;
331 this.icon = icon;
332 this.tickerText = tickerText;
333 setLatestEventInfo(context, contentTitle, contentText,
334 PendingIntent.getActivity(context, 0, contentIntent, 0));
335 }
336
337 /**
338 * Constructs a Notification object with the information needed to
339 * have a status bar icon without the standard expanded view.
340 *
341 * @param icon The resource id of the icon to put in the status bar.
342 * @param tickerText The text that flows by in the status bar when the notification first
343 * activates.
344 * @param when The time to show in the time field. In the System.currentTimeMillis
345 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -0800346 *
347 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800348 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800349 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350 public Notification(int icon, CharSequence tickerText, long when)
351 {
352 this.icon = icon;
353 this.tickerText = tickerText;
354 this.when = when;
355 }
356
357 /**
358 * Unflatten the notification from a parcel.
359 */
360 public Notification(Parcel parcel)
361 {
362 int version = parcel.readInt();
363
364 when = parcel.readLong();
365 icon = parcel.readInt();
366 number = parcel.readInt();
367 if (parcel.readInt() != 0) {
368 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
369 }
370 if (parcel.readInt() != 0) {
371 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
372 }
373 if (parcel.readInt() != 0) {
374 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
375 }
376 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -0800377 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -0400378 }
379 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800380 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
381 }
382 defaults = parcel.readInt();
383 flags = parcel.readInt();
384 if (parcel.readInt() != 0) {
385 sound = Uri.CREATOR.createFromParcel(parcel);
386 }
387
388 audioStreamType = parcel.readInt();
389 vibrate = parcel.createLongArray();
390 ledARGB = parcel.readInt();
391 ledOnMS = parcel.readInt();
392 ledOffMS = parcel.readInt();
393 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400394
395 if (parcel.readInt() != 0) {
396 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
397 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800398 }
399
Joe Onorato18e69df2010-05-17 22:26:12 -0700400 public Notification clone() {
401 Notification that = new Notification();
402
403 that.when = this.when;
404 that.icon = this.icon;
405 that.number = this.number;
406
407 // PendingIntents are global, so there's no reason (or way) to clone them.
408 that.contentIntent = this.contentIntent;
409 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400410 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -0700411
412 if (this.tickerText != null) {
413 that.tickerText = this.tickerText.toString();
414 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800415 if (this.tickerView != null) {
416 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -0400417 }
Joe Onorato18e69df2010-05-17 22:26:12 -0700418 if (this.contentView != null) {
419 that.contentView = this.contentView.clone();
420 }
421 that.iconLevel = that.iconLevel;
422 that.sound = this.sound; // android.net.Uri is immutable
423 that.audioStreamType = this.audioStreamType;
424
425 final long[] vibrate = this.vibrate;
426 if (vibrate != null) {
427 final int N = vibrate.length;
428 final long[] vib = that.vibrate = new long[N];
429 System.arraycopy(vibrate, 0, vib, 0, N);
430 }
431
432 that.ledARGB = this.ledARGB;
433 that.ledOnMS = this.ledOnMS;
434 that.ledOffMS = this.ledOffMS;
435 that.defaults = this.defaults;
436
437 that.flags = this.flags;
438
439 return that;
440 }
441
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800442 public int describeContents() {
443 return 0;
444 }
445
446 /**
447 * Flatten this notification from a parcel.
448 */
449 public void writeToParcel(Parcel parcel, int flags)
450 {
451 parcel.writeInt(1);
452
453 parcel.writeLong(when);
454 parcel.writeInt(icon);
455 parcel.writeInt(number);
456 if (contentIntent != null) {
457 parcel.writeInt(1);
458 contentIntent.writeToParcel(parcel, 0);
459 } else {
460 parcel.writeInt(0);
461 }
462 if (deleteIntent != null) {
463 parcel.writeInt(1);
464 deleteIntent.writeToParcel(parcel, 0);
465 } else {
466 parcel.writeInt(0);
467 }
468 if (tickerText != null) {
469 parcel.writeInt(1);
470 TextUtils.writeToParcel(tickerText, parcel, flags);
471 } else {
472 parcel.writeInt(0);
473 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800474 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -0400475 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -0800476 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -0400477 } else {
478 parcel.writeInt(0);
479 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800480 if (contentView != null) {
481 parcel.writeInt(1);
482 contentView.writeToParcel(parcel, 0);
483 } else {
484 parcel.writeInt(0);
485 }
486
487 parcel.writeInt(defaults);
488 parcel.writeInt(this.flags);
489
490 if (sound != null) {
491 parcel.writeInt(1);
492 sound.writeToParcel(parcel, 0);
493 } else {
494 parcel.writeInt(0);
495 }
496 parcel.writeInt(audioStreamType);
497 parcel.writeLongArray(vibrate);
498 parcel.writeInt(ledARGB);
499 parcel.writeInt(ledOnMS);
500 parcel.writeInt(ledOffMS);
501 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400502
503 if (fullScreenIntent != null) {
504 parcel.writeInt(1);
505 fullScreenIntent.writeToParcel(parcel, 0);
506 } else {
507 parcel.writeInt(0);
508 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800509 }
510
511 /**
512 * Parcelable.Creator that instantiates Notification objects
513 */
514 public static final Parcelable.Creator<Notification> CREATOR
515 = new Parcelable.Creator<Notification>()
516 {
517 public Notification createFromParcel(Parcel parcel)
518 {
519 return new Notification(parcel);
520 }
521
522 public Notification[] newArray(int size)
523 {
524 return new Notification[size];
525 }
526 };
527
528 /**
529 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
530 * layout.
531 *
532 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
533 * in the view.</p>
534 * @param context The context for your application / activity.
535 * @param contentTitle The title that goes in the expanded entry.
536 * @param contentText The text that goes in the expanded entry.
537 * @param contentIntent The intent to launch when the user clicks the expanded notification.
538 * If this is an activity, it must include the
539 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
540 * that you take care of task management as described in
541 * <a href="{@docRoot}guide/topics/fundamentals.html#lcycles">Application Fundamentals: Activities and Tasks</a>.
Joe Onorato46439ce2010-11-19 13:56:21 -0800542 *
543 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800544 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800545 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800546 public void setLatestEventInfo(Context context,
547 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
548 RemoteViews contentView = new RemoteViews(context.getPackageName(),
549 com.android.internal.R.layout.status_bar_latest_event_content);
550 if (this.icon != 0) {
551 contentView.setImageViewResource(com.android.internal.R.id.icon, this.icon);
552 }
553 if (contentTitle != null) {
554 contentView.setTextViewText(com.android.internal.R.id.title, contentTitle);
555 }
556 if (contentText != null) {
557 contentView.setTextViewText(com.android.internal.R.id.text, contentText);
558 }
559 if (this.when != 0) {
Joe Onoratoc83bb732010-01-19 16:32:22 -0800560 contentView.setLong(com.android.internal.R.id.time, "setTime", when);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 }
562
563 this.contentView = contentView;
564 this.contentIntent = contentIntent;
565 }
566
567 @Override
568 public String toString() {
569 StringBuilder sb = new StringBuilder();
570 sb.append("Notification(vibrate=");
571 if (this.vibrate != null) {
572 int N = this.vibrate.length-1;
573 sb.append("[");
574 for (int i=0; i<N; i++) {
575 sb.append(this.vibrate[i]);
576 sb.append(',');
577 }
Simon Schoar8cf97d92009-06-10 22:08:37 +0200578 if (N != -1) {
579 sb.append(this.vibrate[N]);
580 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 sb.append("]");
582 } else if ((this.defaults & DEFAULT_VIBRATE) != 0) {
583 sb.append("default");
584 } else {
585 sb.append("null");
586 }
587 sb.append(",sound=");
588 if (this.sound != null) {
589 sb.append(this.sound.toString());
590 } else if ((this.defaults & DEFAULT_SOUND) != 0) {
591 sb.append("default");
592 } else {
593 sb.append("null");
594 }
595 sb.append(",defaults=0x");
596 sb.append(Integer.toHexString(this.defaults));
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400597 sb.append(",flags=0x");
598 sb.append(Integer.toHexString(this.flags));
599 if ((this.flags & FLAG_HIGH_PRIORITY) != 0) {
600 sb.append("!!!1!one!");
601 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 sb.append(")");
603 return sb.toString();
604 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800605
606 public static class Builder {
607 private Context mContext;
608
609 private long mWhen;
610 private int mSmallIcon;
611 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -0800612 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -0800613 private CharSequence mContentTitle;
614 private CharSequence mContentText;
615 private CharSequence mContentInfo;
616 private PendingIntent mContentIntent;
617 private RemoteViews mContentView;
618 private PendingIntent mDeleteIntent;
619 private PendingIntent mFullScreenIntent;
620 private CharSequence mTickerText;
621 private RemoteViews mTickerView;
622 private Bitmap mLargeIcon;
623 private Uri mSound;
624 private int mAudioStreamType;
625 private long[] mVibrate;
626 private int mLedArgb;
627 private int mLedOnMs;
628 private int mLedOffMs;
629 private int mDefaults;
630 private int mFlags;
631
632 public Builder(Context context) {
633 mContext = context;
634 mWhen = System.currentTimeMillis();
635 }
636
637 public Builder setWhen(long when) {
638 mWhen = when;
639 return this;
640 }
641
642 public Builder setSmallIcon(int icon) {
643 mSmallIcon = icon;
644 return this;
645 }
646
647 public Builder setSmallIcon(int icon, int level) {
648 mSmallIcon = icon;
649 mSmallIconLevel = level;
650 return this;
651 }
652
Joe Onorato46439ce2010-11-19 13:56:21 -0800653 public Builder setContentTitle(CharSequence title) {
654 mContentTitle = title;
655 return this;
656 }
657
658 public Builder setContentText(CharSequence text) {
659 mContentText = text;
660 return this;
661 }
662
Joe Onorato8595a3d2010-11-19 18:12:07 -0800663 public Builder setNumber(int number) {
664 mNumber = number;
665 return this;
666 }
667
Joe Onorato46439ce2010-11-19 13:56:21 -0800668 public Builder setContentInfo(CharSequence info) {
669 mContentInfo = info;
670 return this;
671 }
672
673 public Builder setContent(RemoteViews views) {
674 mContentView = views;
675 return this;
676 }
677
678 public Builder setContentIntent(PendingIntent intent) {
679 mContentIntent = intent;
680 return this;
681 }
682
683 public Builder setDeleteIntent(PendingIntent intent) {
684 mDeleteIntent = intent;
685 return this;
686 }
687
688 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
689 mFullScreenIntent = intent;
690 setFlag(FLAG_HIGH_PRIORITY, highPriority);
691 return this;
692 }
693
694 public Builder setTicker(CharSequence tickerText) {
695 mTickerText = tickerText;
696 return this;
697 }
698
699 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
700 mTickerText = tickerText;
701 mTickerView = views;
702 return this;
703 }
704
705 public Builder setLargeIcon(Bitmap icon) {
706 mLargeIcon = icon;
707 return this;
708 }
709
710 public Builder setSound(Uri sound, int streamType) {
711 mSound = sound;
712 mAudioStreamType = streamType;
713 return this;
714 }
715
716 public Builder setVibrate(long[] pattern) {
717 mVibrate = pattern;
718 return this;
719 }
720
721 public Builder setLights(int argb, int onMs, int offMs) {
722 mLedArgb = argb;
723 mLedOnMs = onMs;
724 mLedOffMs = offMs;
725 mFlags |= FLAG_SHOW_LIGHTS;
726 return this;
727 }
728
729 public Builder setOngoing(boolean ongoing) {
730 setFlag(FLAG_ONGOING_EVENT, ongoing);
731 return this;
732 }
733
734 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
735 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
736 return this;
737 }
738
739 public Builder setAutoCancel(boolean autoCancel) {
740 setFlag(FLAG_ONLY_ALERT_ONCE, autoCancel);
741 return this;
742 }
743
744 public Builder setDefaults(int defaults) {
745 mDefaults = defaults;
746 return this;
747 }
748
749 private void setFlag(int mask, boolean value) {
750 if (value) {
751 mFlags |= mask;
752 } else {
753 mFlags &= ~mask;
754 }
755 }
756
757 private RemoteViews makeContentView() {
758 if (mContentView != null) {
759 return mContentView;
760 } else {
761 RemoteViews contentView = new RemoteViews(mContext.getPackageName(),
762 com.android.internal.R.layout.status_bar_latest_event_content);
763 if (mSmallIcon != 0) {
764 contentView.setImageViewResource(com.android.internal.R.id.icon, mSmallIcon);
765 }
766 if (mContentTitle != null) {
767 contentView.setTextViewText(com.android.internal.R.id.title, mContentTitle);
768 }
769 if (mContentText != null) {
770 contentView.setTextViewText(com.android.internal.R.id.text, mContentText);
771 }
Joe Onorato8595a3d2010-11-19 18:12:07 -0800772 if (mContentInfo != null) {
773 contentView.setTextViewText(com.android.internal.R.id.info, mContentInfo);
774 } else if (mNumber > 0) {
775 NumberFormat f = NumberFormat.getIntegerInstance();
776 contentView.setTextViewText(com.android.internal.R.id.info, f.format(mNumber));
777 contentView.setFloat(com.android.internal.R.id.info, "setTextSize",
778 mContext.getResources().getDimensionPixelSize(
779 com.android.internal.R.dimen.status_bar_content_number_size));
780 } else {
781 contentView.setViewVisibility(com.android.internal.R.id.info, View.GONE);
782 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800783 if (mWhen != 0) {
784 contentView.setLong(com.android.internal.R.id.time, "setTime", mWhen);
785 }
786 return contentView;
787 }
788 }
789
790 private RemoteViews makeTickerView() {
791 if (mTickerView != null) {
792 return mTickerView;
793 } else {
794 return makeContentView();
795 }
796 }
797
798 public Notification getNotification() {
799 Notification n = new Notification();
800 n.when = mWhen;
801 n.icon = mSmallIcon;
802 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -0800803 n.number = mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -0800804 n.contentView = makeContentView();
805 n.contentIntent = mContentIntent;
806 n.deleteIntent = mDeleteIntent;
807 n.fullScreenIntent = mFullScreenIntent;
808 n.tickerText = mTickerText;
809 n.tickerView = makeTickerView();
810 n.largeIcon = mLargeIcon;
811 n.sound = mSound;
812 n.audioStreamType = mAudioStreamType;
813 n.vibrate = mVibrate;
814 n.ledARGB = mLedArgb;
815 n.ledOnMS = mLedOnMs;
816 n.ledOffMS = mLedOffMs;
817 n.defaults = mDefaults;
818 n.flags = mFlags;
819 return n;
820 }
821 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822}