blob: 0243b02524e424019d24d29fdab8f6a2450a150c [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;
Joe Onoratoef1e7762010-09-17 18:38:38 -040023import android.graphics.Bitmap;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024import android.net.Uri;
25import android.os.Parcel;
26import android.os.Parcelable;
27import android.text.TextUtils;
Joe Onorato8595a3d2010-11-19 18:12:07 -080028import android.view.View;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080029import android.widget.RemoteViews;
30
Andy Stadler110988c2010-12-03 14:29:16 -080031import java.text.NumberFormat;
Joe Onorato561d3852010-11-20 18:09:34 -080032
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080033/**
34 * A class that represents how a persistent notification is to be presented to
35 * the user using the {@link android.app.NotificationManager}.
36 *
Scott Mainb8b36452009-04-26 15:50:49 -070037 * <p>For a guide to creating notifications, see the
38 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Creating Status
39 * Bar Notifications</a> document in the Dev Guide.</p>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040 */
41public class Notification implements Parcelable
42{
43 /**
44 * Use all default values (where applicable).
45 */
46 public static final int DEFAULT_ALL = ~0;
47
48 /**
49 * Use the default notification sound. This will ignore any given
50 * {@link #sound}.
51 *
52 * @see #defaults
53 */
54 public static final int DEFAULT_SOUND = 1;
55
56 /**
57 * Use the default notification vibrate. This will ignore any given
Scott Mainb8b36452009-04-26 15:50:49 -070058 * {@link #vibrate}. Using phone vibration requires the
59 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080060 *
61 * @see #defaults
62 */
63 public static final int DEFAULT_VIBRATE = 2;
64
65 /**
66 * Use the default notification lights. This will ignore the
67 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
68 * {@link #ledOnMS}.
69 *
70 * @see #defaults
71 */
72 public static final int DEFAULT_LIGHTS = 4;
73
74 /**
75 * The timestamp for the notification. The icons and expanded views
76 * are sorted by this key.
77 */
78 public long when;
79
80 /**
81 * The resource id of a drawable to use as the icon in the status bar.
82 */
83 public int icon;
84
85 /**
Joe Onorato46439ce2010-11-19 13:56:21 -080086 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
87 * leave it at its default value of 0.
88 *
89 * @see android.widget.ImageView#setImageLevel
90 * @see android.graphics.drawable#setLevel
91 */
92 public int iconLevel;
93
94 /**
Daniel Sandlere46cbd32010-06-17 10:35:26 -040095 * The number of events that this notification represents. For example, in a new mail
96 * notification, this could be the number of unread messages. This number is superimposed over
97 * the icon in the status bar. If the number is 0 or negative, it is not shown in the status
98 * bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 */
100 public int number;
101
102 /**
103 * The intent to execute when the expanded status entry is clicked. If
104 * this is an activity, it must include the
105 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
106 * that you take care of task management as described in the <em>Activities and Tasks</em>
107 * section of the <a href="{@docRoot}guide/topics/fundamentals.html#acttask">Application
108 * Fundamentals</a> document.
109 */
110 public PendingIntent contentIntent;
111
112 /**
113 * The intent to execute when the status entry is deleted by the user
114 * with the "Clear All Notifications" button. This probably shouldn't
115 * be launching an activity since several of those will be sent at the
116 * same time.
117 */
118 public PendingIntent deleteIntent;
119
120 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700121 * An intent to launch instead of posting the notification to the status bar.
122 * Only for use with extremely high-priority notifications demanding the user's
Dianne Hackborn392fea52010-09-09 16:44:01 -0700123 * <strong>immediate</strong> attention, such as an incoming phone call or
Dianne Hackborn170bae72010-09-03 15:14:28 -0700124 * alarm clock that the user has explicitly set to a particular time.
Dianne Hackborn170bae72010-09-03 15:14:28 -0700125 * If this facility is used for something else, please give the user an option
126 * to turn it off and use a normal notification, as this can be extremely
127 * disruptive.
Dianne Hackborn61714012010-09-03 16:40:58 -0700128 *
129 * <p>Use with {@link #FLAG_HIGH_PRIORITY} to ensure that this notification
130 * will reach the user even when other notifications are suppressed.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400131 */
132 public PendingIntent fullScreenIntent;
133
134 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 * Text to scroll across the screen when this item is added to
Joe Onoratoef1e7762010-09-17 18:38:38 -0400136 * the status bar on large and smaller devices.
137 *
138 * <p>This field is provided separately from the other ticker fields
139 * both for compatibility and to allow an application to choose different
140 * text for when the text scrolls in and when it is displayed all at once
141 * in conjunction with one or more icons.
142 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800143 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800144 */
145 public CharSequence tickerText;
146
147 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800148 * The view to show as the ticker in the status bar when the notification
149 * is posted.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400150 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800151 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400152
153 /**
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400154 * The view that will represent this notification in the expanded status bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800155 */
156 public RemoteViews contentView;
157
158 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800159 * The bitmap that may escape the bounds of the panel and bar.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800161 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800162
163 /**
164 * The sound to play.
165 *
166 * <p>
167 * To play the default notification sound, see {@link #defaults}.
168 * </p>
169 */
170 public Uri sound;
171
172 /**
173 * Use this constant as the value for audioStreamType to request that
174 * the default stream type for notifications be used. Currently the
175 * default stream type is STREAM_RING.
176 */
177 public static final int STREAM_DEFAULT = -1;
178
179 /**
180 * The audio stream type to use when playing the sound.
181 * Should be one of the STREAM_ constants from
182 * {@link android.media.AudioManager}.
183 */
184 public int audioStreamType = STREAM_DEFAULT;
185
186
187 /**
Scott Mainb8b36452009-04-26 15:50:49 -0700188 * The pattern with which to vibrate.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800189 *
190 * <p>
191 * To vibrate the default pattern, see {@link #defaults}.
192 * </p>
193 *
194 * @see android.os.Vibrator#vibrate(long[],int)
195 */
196 public long[] vibrate;
197
198 /**
199 * The color of the led. The hardware will do its best approximation.
200 *
201 * @see #FLAG_SHOW_LIGHTS
202 * @see #flags
203 */
204 public int ledARGB;
205
206 /**
207 * The number of milliseconds for the LED to be on while it's flashing.
208 * The hardware will do its best approximation.
209 *
210 * @see #FLAG_SHOW_LIGHTS
211 * @see #flags
212 */
213 public int ledOnMS;
214
215 /**
216 * The number of milliseconds for the LED to be off while it's flashing.
217 * The hardware will do its best approximation.
218 *
219 * @see #FLAG_SHOW_LIGHTS
220 * @see #flags
221 */
222 public int ledOffMS;
223
224 /**
225 * Specifies which values should be taken from the defaults.
226 * <p>
227 * To set, OR the desired from {@link #DEFAULT_SOUND},
228 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
229 * values, use {@link #DEFAULT_ALL}.
230 * </p>
231 */
232 public int defaults;
233
234
235 /**
236 * Bit to be bitwise-ored into the {@link #flags} field that should be
237 * set if you want the LED on for this notification.
238 * <ul>
239 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
240 * or 0 for both ledOnMS and ledOffMS.</li>
241 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
242 * <li>To flash the LED, pass the number of milliseconds that it should
243 * be on and off to ledOnMS and ledOffMS.</li>
244 * </ul>
245 * <p>
246 * Since hardware varies, you are not guaranteed that any of the values
247 * you pass are honored exactly. Use the system defaults (TODO) if possible
248 * because they will be set to values that work on any given hardware.
249 * <p>
250 * The alpha channel must be set for forward compatibility.
251 *
252 */
253 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
254
255 /**
256 * Bit to be bitwise-ored into the {@link #flags} field that should be
257 * set if this notification is in reference to something that is ongoing,
258 * like a phone call. It should not be set if this notification is in
259 * reference to something that happened at a particular point in time,
260 * like a missed phone call.
261 */
262 public static final int FLAG_ONGOING_EVENT = 0x00000002;
263
264 /**
265 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700266 * the audio will be repeated until the notification is
267 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800268 */
269 public static final int FLAG_INSISTENT = 0x00000004;
270
271 /**
272 * Bit to be bitwise-ored into the {@link #flags} field that should be
273 * set if you want the sound and/or vibration play each time the
274 * notification is sent, even if it has not been canceled before that.
275 */
276 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
277
278 /**
279 * Bit to be bitwise-ored into the {@link #flags} field that should be
280 * set if the notification should be canceled when it is clicked by the
281 * user.
282 */
283 public static final int FLAG_AUTO_CANCEL = 0x00000010;
284
285 /**
286 * Bit to be bitwise-ored into the {@link #flags} field that should be
287 * set if the notification should not be canceled when the user clicks
288 * the Clear all button.
289 */
290 public static final int FLAG_NO_CLEAR = 0x00000020;
291
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700292 /**
293 * Bit to be bitwise-ored into the {@link #flags} field that should be
294 * set if this notification represents a currently running service. This
295 * will normally be set for you by {@link Service#startForeground}.
296 */
297 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
298
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400299 /**
300 * Bit to be bitwise-ored into the {@link #flags} field that should be set if this notification
301 * represents a high-priority event that may be shown to the user even if notifications are
302 * otherwise unavailable (that is, when the status bar is hidden). This flag is ideally used
303 * in conjunction with {@link #fullScreenIntent}.
304 */
305 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
306
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800307 public int flags;
308
309 /**
310 * Constructs a Notification object with everything set to 0.
Joe Onorato46439ce2010-11-19 13:56:21 -0800311 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800312 */
313 public Notification()
314 {
315 this.when = System.currentTimeMillis();
316 }
317
318 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800319 * @hide
320 */
321 public Notification(Context context, int icon, CharSequence tickerText, long when,
322 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
323 {
324 this.when = when;
325 this.icon = icon;
326 this.tickerText = tickerText;
327 setLatestEventInfo(context, contentTitle, contentText,
328 PendingIntent.getActivity(context, 0, contentIntent, 0));
329 }
330
331 /**
332 * Constructs a Notification object with the information needed to
333 * have a status bar icon without the standard expanded view.
334 *
335 * @param icon The resource id of the icon to put in the status bar.
336 * @param tickerText The text that flows by in the status bar when the notification first
337 * activates.
338 * @param when The time to show in the time field. In the System.currentTimeMillis
339 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -0800340 *
341 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800342 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800343 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800344 public Notification(int icon, CharSequence tickerText, long when)
345 {
346 this.icon = icon;
347 this.tickerText = tickerText;
348 this.when = when;
349 }
350
351 /**
352 * Unflatten the notification from a parcel.
353 */
354 public Notification(Parcel parcel)
355 {
356 int version = parcel.readInt();
357
358 when = parcel.readLong();
359 icon = parcel.readInt();
360 number = parcel.readInt();
361 if (parcel.readInt() != 0) {
362 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
363 }
364 if (parcel.readInt() != 0) {
365 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
366 }
367 if (parcel.readInt() != 0) {
368 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
369 }
370 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -0800371 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -0400372 }
373 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
375 }
Joe Onorato561d3852010-11-20 18:09:34 -0800376 if (parcel.readInt() != 0) {
377 largeIcon = Bitmap.CREATOR.createFromParcel(parcel);
378 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800379 defaults = parcel.readInt();
380 flags = parcel.readInt();
381 if (parcel.readInt() != 0) {
382 sound = Uri.CREATOR.createFromParcel(parcel);
383 }
384
385 audioStreamType = parcel.readInt();
386 vibrate = parcel.createLongArray();
387 ledARGB = parcel.readInt();
388 ledOnMS = parcel.readInt();
389 ledOffMS = parcel.readInt();
390 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400391
392 if (parcel.readInt() != 0) {
393 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
394 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800395 }
396
Andy Stadler110988c2010-12-03 14:29:16 -0800397 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -0700398 public Notification clone() {
399 Notification that = new Notification();
400
401 that.when = this.when;
402 that.icon = this.icon;
403 that.number = this.number;
404
405 // PendingIntents are global, so there's no reason (or way) to clone them.
406 that.contentIntent = this.contentIntent;
407 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400408 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -0700409
410 if (this.tickerText != null) {
411 that.tickerText = this.tickerText.toString();
412 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800413 if (this.tickerView != null) {
414 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -0400415 }
Joe Onorato18e69df2010-05-17 22:26:12 -0700416 if (this.contentView != null) {
417 that.contentView = this.contentView.clone();
418 }
Joe Onorato561d3852010-11-20 18:09:34 -0800419 if (this.largeIcon != null) {
420 that.largeIcon = Bitmap.createBitmap(this.largeIcon);
421 }
Joe Onorato18e69df2010-05-17 22:26:12 -0700422 that.iconLevel = that.iconLevel;
423 that.sound = this.sound; // android.net.Uri is immutable
424 that.audioStreamType = this.audioStreamType;
425
426 final long[] vibrate = this.vibrate;
427 if (vibrate != null) {
428 final int N = vibrate.length;
429 final long[] vib = that.vibrate = new long[N];
430 System.arraycopy(vibrate, 0, vib, 0, N);
431 }
432
433 that.ledARGB = this.ledARGB;
434 that.ledOnMS = this.ledOnMS;
435 that.ledOffMS = this.ledOffMS;
436 that.defaults = this.defaults;
437
438 that.flags = this.flags;
439
440 return that;
441 }
442
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800443 public int describeContents() {
444 return 0;
445 }
446
447 /**
448 * Flatten this notification from a parcel.
449 */
450 public void writeToParcel(Parcel parcel, int flags)
451 {
452 parcel.writeInt(1);
453
454 parcel.writeLong(when);
455 parcel.writeInt(icon);
456 parcel.writeInt(number);
457 if (contentIntent != null) {
458 parcel.writeInt(1);
459 contentIntent.writeToParcel(parcel, 0);
460 } else {
461 parcel.writeInt(0);
462 }
463 if (deleteIntent != null) {
464 parcel.writeInt(1);
465 deleteIntent.writeToParcel(parcel, 0);
466 } else {
467 parcel.writeInt(0);
468 }
469 if (tickerText != null) {
470 parcel.writeInt(1);
471 TextUtils.writeToParcel(tickerText, parcel, flags);
472 } else {
473 parcel.writeInt(0);
474 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800475 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -0400476 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -0800477 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -0400478 } else {
479 parcel.writeInt(0);
480 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800481 if (contentView != null) {
482 parcel.writeInt(1);
483 contentView.writeToParcel(parcel, 0);
484 } else {
485 parcel.writeInt(0);
486 }
Joe Onorato561d3852010-11-20 18:09:34 -0800487 if (largeIcon != null) {
488 parcel.writeInt(1);
489 largeIcon.writeToParcel(parcel, 0);
490 } else {
491 parcel.writeInt(0);
492 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800493
494 parcel.writeInt(defaults);
495 parcel.writeInt(this.flags);
496
497 if (sound != null) {
498 parcel.writeInt(1);
499 sound.writeToParcel(parcel, 0);
500 } else {
501 parcel.writeInt(0);
502 }
503 parcel.writeInt(audioStreamType);
504 parcel.writeLongArray(vibrate);
505 parcel.writeInt(ledARGB);
506 parcel.writeInt(ledOnMS);
507 parcel.writeInt(ledOffMS);
508 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400509
510 if (fullScreenIntent != null) {
511 parcel.writeInt(1);
512 fullScreenIntent.writeToParcel(parcel, 0);
513 } else {
514 parcel.writeInt(0);
515 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800516 }
517
518 /**
519 * Parcelable.Creator that instantiates Notification objects
520 */
521 public static final Parcelable.Creator<Notification> CREATOR
522 = new Parcelable.Creator<Notification>()
523 {
524 public Notification createFromParcel(Parcel parcel)
525 {
526 return new Notification(parcel);
527 }
528
529 public Notification[] newArray(int size)
530 {
531 return new Notification[size];
532 }
533 };
534
535 /**
536 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
537 * layout.
538 *
539 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
540 * in the view.</p>
541 * @param context The context for your application / activity.
542 * @param contentTitle The title that goes in the expanded entry.
543 * @param contentText The text that goes in the expanded entry.
544 * @param contentIntent The intent to launch when the user clicks the expanded notification.
545 * If this is an activity, it must include the
546 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
547 * that you take care of task management as described in
548 * <a href="{@docRoot}guide/topics/fundamentals.html#lcycles">Application Fundamentals: Activities and Tasks</a>.
Joe Onorato46439ce2010-11-19 13:56:21 -0800549 *
550 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551 */
Joe Onorato46439ce2010-11-19 13:56:21 -0800552 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800553 public void setLatestEventInfo(Context context,
554 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
555 RemoteViews contentView = new RemoteViews(context.getPackageName(),
Joe Onorato561d3852010-11-20 18:09:34 -0800556 R.layout.status_bar_latest_event_content);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 if (this.icon != 0) {
Joe Onorato561d3852010-11-20 18:09:34 -0800558 contentView.setImageViewResource(R.id.icon, this.icon);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 }
560 if (contentTitle != null) {
Joe Onorato561d3852010-11-20 18:09:34 -0800561 contentView.setTextViewText(R.id.title, contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 }
563 if (contentText != null) {
Joe Onorato561d3852010-11-20 18:09:34 -0800564 contentView.setTextViewText(R.id.text, contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800565 }
566 if (this.when != 0) {
Joe Onorato561d3852010-11-20 18:09:34 -0800567 contentView.setLong(R.id.time, "setTime", when);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 }
569
570 this.contentView = contentView;
571 this.contentIntent = contentIntent;
572 }
573
574 @Override
575 public String toString() {
576 StringBuilder sb = new StringBuilder();
Joe Onoratoc9596d62011-01-12 17:03:11 -0800577 sb.append("Notification(contentView=");
578 if (contentView != null) {
579 sb.append(contentView.getPackage());
580 sb.append("/0x");
581 sb.append(Integer.toHexString(contentView.getLayoutId()));
582 } else {
583 sb.append("null");
584 }
585 sb.append(" vibrate=");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586 if (this.vibrate != null) {
587 int N = this.vibrate.length-1;
588 sb.append("[");
589 for (int i=0; i<N; i++) {
590 sb.append(this.vibrate[i]);
591 sb.append(',');
592 }
Simon Schoar8cf97d92009-06-10 22:08:37 +0200593 if (N != -1) {
594 sb.append(this.vibrate[N]);
595 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800596 sb.append("]");
597 } else if ((this.defaults & DEFAULT_VIBRATE) != 0) {
598 sb.append("default");
599 } else {
600 sb.append("null");
601 }
602 sb.append(",sound=");
603 if (this.sound != null) {
604 sb.append(this.sound.toString());
605 } else if ((this.defaults & DEFAULT_SOUND) != 0) {
606 sb.append("default");
607 } else {
608 sb.append("null");
609 }
610 sb.append(",defaults=0x");
611 sb.append(Integer.toHexString(this.defaults));
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400612 sb.append(",flags=0x");
613 sb.append(Integer.toHexString(this.flags));
614 if ((this.flags & FLAG_HIGH_PRIORITY) != 0) {
615 sb.append("!!!1!one!");
616 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800617 sb.append(")");
618 return sb.toString();
619 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800620
621 public static class Builder {
622 private Context mContext;
623
624 private long mWhen;
625 private int mSmallIcon;
626 private int mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -0800627 private int mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -0800628 private CharSequence mContentTitle;
629 private CharSequence mContentText;
630 private CharSequence mContentInfo;
631 private PendingIntent mContentIntent;
632 private RemoteViews mContentView;
633 private PendingIntent mDeleteIntent;
634 private PendingIntent mFullScreenIntent;
635 private CharSequence mTickerText;
636 private RemoteViews mTickerView;
637 private Bitmap mLargeIcon;
638 private Uri mSound;
639 private int mAudioStreamType;
640 private long[] mVibrate;
641 private int mLedArgb;
642 private int mLedOnMs;
643 private int mLedOffMs;
644 private int mDefaults;
645 private int mFlags;
646
647 public Builder(Context context) {
648 mContext = context;
Andy Stadler110988c2010-12-03 14:29:16 -0800649
650 // Set defaults to match the defaults of a Notification
Joe Onorato46439ce2010-11-19 13:56:21 -0800651 mWhen = System.currentTimeMillis();
Andy Stadler110988c2010-12-03 14:29:16 -0800652 mAudioStreamType = STREAM_DEFAULT;
Joe Onorato46439ce2010-11-19 13:56:21 -0800653 }
654
655 public Builder setWhen(long when) {
656 mWhen = when;
657 return this;
658 }
659
660 public Builder setSmallIcon(int icon) {
661 mSmallIcon = icon;
662 return this;
663 }
664
665 public Builder setSmallIcon(int icon, int level) {
666 mSmallIcon = icon;
667 mSmallIconLevel = level;
668 return this;
669 }
670
Joe Onorato46439ce2010-11-19 13:56:21 -0800671 public Builder setContentTitle(CharSequence title) {
672 mContentTitle = title;
673 return this;
674 }
675
676 public Builder setContentText(CharSequence text) {
677 mContentText = text;
678 return this;
679 }
680
Joe Onorato8595a3d2010-11-19 18:12:07 -0800681 public Builder setNumber(int number) {
682 mNumber = number;
683 return this;
684 }
685
Joe Onorato46439ce2010-11-19 13:56:21 -0800686 public Builder setContentInfo(CharSequence info) {
687 mContentInfo = info;
688 return this;
689 }
690
691 public Builder setContent(RemoteViews views) {
692 mContentView = views;
693 return this;
694 }
695
696 public Builder setContentIntent(PendingIntent intent) {
697 mContentIntent = intent;
698 return this;
699 }
700
701 public Builder setDeleteIntent(PendingIntent intent) {
702 mDeleteIntent = intent;
703 return this;
704 }
705
706 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
707 mFullScreenIntent = intent;
708 setFlag(FLAG_HIGH_PRIORITY, highPriority);
709 return this;
710 }
711
712 public Builder setTicker(CharSequence tickerText) {
713 mTickerText = tickerText;
714 return this;
715 }
716
717 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
718 mTickerText = tickerText;
719 mTickerView = views;
720 return this;
721 }
722
723 public Builder setLargeIcon(Bitmap icon) {
724 mLargeIcon = icon;
725 return this;
726 }
727
Joe Onorato52f80cd2010-11-21 15:34:48 -0800728 public Builder setSound(Uri sound) {
729 mSound = sound;
730 mAudioStreamType = STREAM_DEFAULT;
731 return this;
732 }
733
Joe Onorato46439ce2010-11-19 13:56:21 -0800734 public Builder setSound(Uri sound, int streamType) {
735 mSound = sound;
736 mAudioStreamType = streamType;
737 return this;
738 }
739
740 public Builder setVibrate(long[] pattern) {
741 mVibrate = pattern;
742 return this;
743 }
744
745 public Builder setLights(int argb, int onMs, int offMs) {
746 mLedArgb = argb;
747 mLedOnMs = onMs;
748 mLedOffMs = offMs;
Joe Onorato46439ce2010-11-19 13:56:21 -0800749 return this;
750 }
751
752 public Builder setOngoing(boolean ongoing) {
753 setFlag(FLAG_ONGOING_EVENT, ongoing);
754 return this;
755 }
756
757 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
758 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
759 return this;
760 }
761
762 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -0800763 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -0800764 return this;
765 }
766
767 public Builder setDefaults(int defaults) {
768 mDefaults = defaults;
769 return this;
770 }
771
772 private void setFlag(int mask, boolean value) {
773 if (value) {
774 mFlags |= mask;
775 } else {
776 mFlags &= ~mask;
777 }
778 }
779
Joe Onorato561d3852010-11-20 18:09:34 -0800780 private RemoteViews makeRemoteViews(int resId) {
781 RemoteViews contentView = new RemoteViews(mContext.getPackageName(), resId);
782 if (mSmallIcon != 0) {
783 contentView.setImageViewResource(R.id.icon, mSmallIcon);
784 }
785 if (mContentTitle != null) {
786 contentView.setTextViewText(R.id.title, mContentTitle);
787 }
788 if (mContentText != null) {
789 contentView.setTextViewText(R.id.text, mContentText);
790 }
791 if (mContentInfo != null) {
792 contentView.setTextViewText(R.id.info, mContentInfo);
793 } else if (mNumber > 0) {
Joe Onorato059a2f82011-01-04 10:27:01 -0800794 if (mNumber > 100) {
795 contentView.setTextViewText(R.id.info, mContext.getString(
796 R.string.status_bar_notification_info_overflow));
797 } else {
798 NumberFormat f = NumberFormat.getIntegerInstance();
799 contentView.setTextViewText(R.id.info, f.format(mNumber));
800 }
Joe Onorato561d3852010-11-20 18:09:34 -0800801 contentView.setFloat(R.id.info, "setTextSize",
802 mContext.getResources().getDimensionPixelSize(
803 R.dimen.status_bar_content_number_size));
804 } else {
805 contentView.setViewVisibility(R.id.info, View.GONE);
806 }
807 if (mWhen != 0) {
808 contentView.setLong(R.id.time, "setTime", mWhen);
809 }
810 return contentView;
811 }
812
Joe Onorato46439ce2010-11-19 13:56:21 -0800813 private RemoteViews makeContentView() {
814 if (mContentView != null) {
815 return mContentView;
816 } else {
Joe Onorato561d3852010-11-20 18:09:34 -0800817 return makeRemoteViews(mLargeIcon == null
818 ? R.layout.status_bar_latest_event_content
819 : R.layout.status_bar_latest_event_content_large_icon);
Joe Onorato46439ce2010-11-19 13:56:21 -0800820 }
821 }
822
823 private RemoteViews makeTickerView() {
824 if (mTickerView != null) {
825 return mTickerView;
826 } else {
Joe Onorato561d3852010-11-20 18:09:34 -0800827 if (mContentView == null) {
828 return makeRemoteViews(mLargeIcon == null
829 ? R.layout.status_bar_latest_event_ticker
830 : R.layout.status_bar_latest_event_ticker_large_icon);
831 } else {
832 return null;
833 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800834 }
835 }
836
837 public Notification getNotification() {
838 Notification n = new Notification();
839 n.when = mWhen;
840 n.icon = mSmallIcon;
841 n.iconLevel = mSmallIconLevel;
Joe Onorato8595a3d2010-11-19 18:12:07 -0800842 n.number = mNumber;
Joe Onorato46439ce2010-11-19 13:56:21 -0800843 n.contentView = makeContentView();
844 n.contentIntent = mContentIntent;
845 n.deleteIntent = mDeleteIntent;
846 n.fullScreenIntent = mFullScreenIntent;
847 n.tickerText = mTickerText;
848 n.tickerView = makeTickerView();
849 n.largeIcon = mLargeIcon;
850 n.sound = mSound;
851 n.audioStreamType = mAudioStreamType;
852 n.vibrate = mVibrate;
853 n.ledARGB = mLedArgb;
854 n.ledOnMS = mLedOnMs;
855 n.ledOffMS = mLedOffMs;
856 n.defaults = mDefaults;
857 n.flags = mFlags;
Joe Onorato8d0b6552010-11-22 16:09:29 -0800858 if (mLedOnMs != 0 && mLedOffMs != 0) {
859 n.flags |= FLAG_SHOW_LIGHTS;
860 }
861 if ((mDefaults & DEFAULT_LIGHTS) != 0) {
862 n.flags |= FLAG_SHOW_LIGHTS;
863 }
Joe Onorato46439ce2010-11-19 13:56:21 -0800864 return n;
865 }
866 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867}