The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.app; |
| 18 | |
Joe Onorato | 8595a3d | 2010-11-19 18:12:07 -0800 | [diff] [blame^] | 19 | import java.text.NumberFormat; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import java.util.Date; |
| 21 | |
| 22 | import android.app.PendingIntent; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 25 | import android.graphics.Bitmap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.media.AudioManager; |
| 27 | import android.net.Uri; |
| 28 | import android.os.Parcel; |
| 29 | import android.os.Parcelable; |
| 30 | import android.text.TextUtils; |
| 31 | import android.text.format.DateFormat; |
| 32 | import android.text.format.DateUtils; |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 33 | import android.util.Slog; |
Joe Onorato | 8595a3d | 2010-11-19 18:12:07 -0800 | [diff] [blame^] | 34 | import android.view.View; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import 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 Main | b8b3645 | 2009-04-26 15:50:49 -0700 | [diff] [blame] | 41 | * <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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | */ |
| 45 | public class Notification implements Parcelable |
| 46 | { |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 47 | private static final String TAG = "Notification"; |
| 48 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | /** |
| 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 Main | b8b3645 | 2009-04-26 15:50:49 -0700 | [diff] [blame] | 64 | * {@link #vibrate}. Using phone vibration requires the |
| 65 | * {@link android.Manifest.permission#VIBRATE VIBRATE} permission. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | * |
| 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 Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 92 | * 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 Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 101 | * 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | */ |
| 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 Hackborn | 170bae7 | 2010-09-03 15:14:28 -0700 | [diff] [blame] | 127 | * 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 Hackborn | 392fea5 | 2010-09-09 16:44:01 -0700 | [diff] [blame] | 129 | * <strong>immediate</strong> attention, such as an incoming phone call or |
Dianne Hackborn | 170bae7 | 2010-09-03 15:14:28 -0700 | [diff] [blame] | 130 | * alarm clock that the user has explicitly set to a particular time. |
Dianne Hackborn | 170bae7 | 2010-09-03 15:14:28 -0700 | [diff] [blame] | 131 | * 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 Hackborn | 6171401 | 2010-09-03 16:40:58 -0700 | [diff] [blame] | 134 | * |
| 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 Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 137 | */ |
| 138 | public PendingIntent fullScreenIntent; |
| 139 | |
| 140 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | * Text to scroll across the screen when this item is added to |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 142 | * 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 Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 149 | * @see #tickerView |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | */ |
| 151 | public CharSequence tickerText; |
| 152 | |
| 153 | /** |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 154 | * The view to show as the ticker in the status bar when the notification |
| 155 | * is posted. |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 156 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 157 | public RemoteViews tickerView; |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 158 | |
| 159 | /** |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 160 | * The view that will represent this notification in the expanded status bar. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | */ |
| 162 | public RemoteViews contentView; |
| 163 | |
| 164 | /** |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 165 | * The bitmap that may escape the bounds of the panel and bar. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 167 | public Bitmap largeIcon; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | |
| 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 Main | b8b3645 | 2009-04-26 15:50:49 -0700 | [diff] [blame] | 194 | * The pattern with which to vibrate. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | * |
| 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 Main | b8b3645 | 2009-04-26 15:50:49 -0700 | [diff] [blame] | 272 | * the audio will be repeated until the notification is |
| 273 | * cancelled or the notification window is opened. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 274 | */ |
| 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 Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 298 | /** |
| 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 Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 305 | /** |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | public int flags; |
| 314 | |
| 315 | /** |
| 316 | * Constructs a Notification object with everything set to 0. |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 317 | * You might want to consider using {@link Builder} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 318 | */ |
| 319 | public Notification() |
| 320 | { |
| 321 | this.when = System.currentTimeMillis(); |
| 322 | } |
| 323 | |
| 324 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | * @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 Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 346 | * |
| 347 | * @deprecated Use {@link Builder} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 348 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 349 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 350 | 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 Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 377 | tickerView = RemoteViews.CREATOR.createFromParcel(parcel); |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 378 | } |
| 379 | if (parcel.readInt() != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 380 | 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 Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 394 | |
| 395 | if (parcel.readInt() != 0) { |
| 396 | fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel); |
| 397 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | } |
| 399 | |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 400 | 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 Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 410 | that.fullScreenIntent = this.fullScreenIntent; |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 411 | |
| 412 | if (this.tickerText != null) { |
| 413 | that.tickerText = this.tickerText.toString(); |
| 414 | } |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 415 | if (this.tickerView != null) { |
| 416 | that.tickerView = this.tickerView.clone(); |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 417 | } |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 418 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | 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 Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 474 | if (tickerView != null) { |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 475 | parcel.writeInt(1); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 476 | tickerView.writeToParcel(parcel, 0); |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 477 | } else { |
| 478 | parcel.writeInt(0); |
| 479 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | 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 Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 502 | |
| 503 | if (fullScreenIntent != null) { |
| 504 | parcel.writeInt(1); |
| 505 | fullScreenIntent.writeToParcel(parcel, 0); |
| 506 | } else { |
| 507 | parcel.writeInt(0); |
| 508 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 509 | } |
| 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 Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 542 | * |
| 543 | * @deprecated Use {@link Builder} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 544 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 545 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 546 | 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 Onorato | c83bb73 | 2010-01-19 16:32:22 -0800 | [diff] [blame] | 560 | contentView.setLong(com.android.internal.R.id.time, "setTime", when); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 561 | } |
| 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 Schoar | 8cf97d9 | 2009-06-10 22:08:37 +0200 | [diff] [blame] | 578 | if (N != -1) { |
| 579 | sb.append(this.vibrate[N]); |
| 580 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 581 | 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 Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 597 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 602 | sb.append(")"); |
| 603 | return sb.toString(); |
| 604 | } |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 605 | |
| 606 | public static class Builder { |
| 607 | private Context mContext; |
| 608 | |
| 609 | private long mWhen; |
| 610 | private int mSmallIcon; |
| 611 | private int mSmallIconLevel; |
Joe Onorato | 8595a3d | 2010-11-19 18:12:07 -0800 | [diff] [blame^] | 612 | private int mNumber; |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 613 | 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 Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 653 | 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 Onorato | 8595a3d | 2010-11-19 18:12:07 -0800 | [diff] [blame^] | 663 | public Builder setNumber(int number) { |
| 664 | mNumber = number; |
| 665 | return this; |
| 666 | } |
| 667 | |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 668 | 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 Onorato | 8595a3d | 2010-11-19 18:12:07 -0800 | [diff] [blame^] | 772 | 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 Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 783 | 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 Onorato | 8595a3d | 2010-11-19 18:12:07 -0800 | [diff] [blame^] | 803 | n.number = mNumber; |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 804 | 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 822 | } |