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 | |
Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 19 | import com.android.internal.R; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 23 | import android.graphics.Bitmap; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.net.Uri; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 25 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.os.Parcel; |
| 27 | import android.os.Parcelable; |
| 28 | import android.text.TextUtils; |
Joe Onorato | 8595a3d | 2010-11-19 18:12:07 -0800 | [diff] [blame] | 29 | import android.view.View; |
Jeff Sharkey | 1c40013 | 2011-08-05 14:50:13 -0700 | [diff] [blame] | 30 | import android.widget.ProgressBar; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.widget.RemoteViews; |
| 32 | |
Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 33 | import java.text.NumberFormat; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 34 | import java.util.ArrayList; |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 35 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | /** |
| 37 | * A class that represents how a persistent notification is to be presented to |
| 38 | * the user using the {@link android.app.NotificationManager}. |
| 39 | * |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 40 | * <p>The {@link Notification.Builder Notification.Builder} has been added to make it |
| 41 | * easier to construct Notifications.</p> |
| 42 | * |
Joe Fernandez | 558459f | 2011-10-13 16:47:36 -0700 | [diff] [blame] | 43 | * <div class="special reference"> |
| 44 | * <h3>Developer Guides</h3> |
| 45 | * <p>For a guide to creating notifications, read the |
| 46 | * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a> |
| 47 | * developer guide.</p> |
| 48 | * </div> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | */ |
| 50 | public class Notification implements Parcelable |
| 51 | { |
| 52 | /** |
| 53 | * Use all default values (where applicable). |
| 54 | */ |
| 55 | public static final int DEFAULT_ALL = ~0; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 56 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | /** |
| 58 | * Use the default notification sound. This will ignore any given |
| 59 | * {@link #sound}. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 60 | * |
| 61 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | * @see #defaults |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 63 | */ |
| 64 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | public static final int DEFAULT_SOUND = 1; |
| 66 | |
| 67 | /** |
| 68 | * Use the default notification vibrate. This will ignore any given |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 69 | * {@link #vibrate}. Using phone vibration requires the |
Scott Main | b8b3645 | 2009-04-26 15:50:49 -0700 | [diff] [blame] | 70 | * {@link android.Manifest.permission#VIBRATE VIBRATE} permission. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 71 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 72 | * @see #defaults |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 73 | */ |
| 74 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | public static final int DEFAULT_VIBRATE = 2; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 76 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | /** |
| 78 | * Use the default notification lights. This will ignore the |
| 79 | * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or |
| 80 | * {@link #ledOnMS}. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 81 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | * @see #defaults |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 83 | */ |
| 84 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 85 | public static final int DEFAULT_LIGHTS = 4; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 86 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 88 | * A timestamp related to this notification, in milliseconds since the epoch. |
| 89 | * |
| 90 | * Default value: {@link System#currentTimeMillis() Now}. |
| 91 | * |
| 92 | * Choose a timestamp that will be most relevant to the user. For most finite events, this |
| 93 | * corresponds to the time the event happened (or will happen, in the case of events that have |
| 94 | * yet to occur but about which the user is being informed). Indefinite events should be |
| 95 | * timestamped according to when the activity began. |
| 96 | * |
| 97 | * Some examples: |
| 98 | * |
| 99 | * <ul> |
| 100 | * <li>Notification of a new chat message should be stamped when the message was received.</li> |
| 101 | * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li> |
| 102 | * <li>Notification of a completed file download should be stamped when the download finished.</li> |
| 103 | * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li> |
| 104 | * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time. |
| 105 | * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time. |
| 106 | * </ul> |
| 107 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | */ |
| 109 | public long when; |
| 110 | |
| 111 | /** |
| 112 | * The resource id of a drawable to use as the icon in the status bar. |
Daniel Sandler | d952dae | 2011-02-07 16:33:36 -0500 | [diff] [blame] | 113 | * This is required; notifications with an invalid icon resource will not be shown. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | */ |
| 115 | public int icon; |
| 116 | |
| 117 | /** |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 118 | * If the icon in the status bar is to have more than one level, you can set this. Otherwise, |
| 119 | * leave it at its default value of 0. |
| 120 | * |
| 121 | * @see android.widget.ImageView#setImageLevel |
| 122 | * @see android.graphics.drawable#setLevel |
| 123 | */ |
| 124 | public int iconLevel; |
| 125 | |
| 126 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 127 | * The number of events that this notification represents. For example, in a new mail |
| 128 | * notification, this could be the number of unread messages. |
| 129 | * |
| 130 | * The system may or may not use this field to modify the appearance of the notification. For |
| 131 | * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was |
| 132 | * superimposed over the icon in the status bar. Starting with |
| 133 | * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by |
| 134 | * {@link Notification.Builder} has displayed the number in the expanded notification view. |
| 135 | * |
| 136 | * If the number is 0 or negative, it is never shown. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | */ |
| 138 | public int number; |
| 139 | |
| 140 | /** |
| 141 | * The intent to execute when the expanded status entry is clicked. If |
| 142 | * this is an activity, it must include the |
| 143 | * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 144 | * that you take care of task management as described in the |
| 145 | * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back |
Dianne Hackborn | 6ceca58 | 2012-01-10 15:24:26 -0800 | [diff] [blame] | 146 | * Stack</a> document. In particular, make sure to read the notification section |
| 147 | * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling |
| 148 | * Notifications</a> for the correct ways to launch an application from a |
| 149 | * notification. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 150 | */ |
| 151 | public PendingIntent contentIntent; |
| 152 | |
| 153 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 154 | * The intent to execute when the notification is explicitly dismissed by the user, either with |
| 155 | * the "Clear All" button or by swiping it away individually. |
| 156 | * |
| 157 | * This probably shouldn't be launching an activity since several of those will be sent |
| 158 | * at the same time. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | */ |
| 160 | public PendingIntent deleteIntent; |
| 161 | |
| 162 | /** |
Dianne Hackborn | 170bae7 | 2010-09-03 15:14:28 -0700 | [diff] [blame] | 163 | * An intent to launch instead of posting the notification to the status bar. |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 164 | * |
| 165 | * @see Notification.Builder#setFullScreenIntent |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 166 | */ |
| 167 | public PendingIntent fullScreenIntent; |
| 168 | |
| 169 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | * Text to scroll across the screen when this item is added to |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 171 | * the status bar on large and smaller devices. |
| 172 | * |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 173 | * @see #tickerView |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | */ |
| 175 | public CharSequence tickerText; |
| 176 | |
| 177 | /** |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 178 | * The view to show as the ticker in the status bar when the notification |
| 179 | * is posted. |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 180 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 181 | public RemoteViews tickerView; |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 182 | |
| 183 | /** |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 184 | * 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] | 185 | */ |
| 186 | public RemoteViews contentView; |
| 187 | |
| 188 | /** |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 189 | * 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] | 190 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 191 | public Bitmap largeIcon; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | |
| 193 | /** |
| 194 | * The sound to play. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 195 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | * <p> |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 197 | * To play the default notification sound, see {@link #defaults}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | * </p> |
| 199 | */ |
| 200 | public Uri sound; |
| 201 | |
| 202 | /** |
| 203 | * Use this constant as the value for audioStreamType to request that |
| 204 | * the default stream type for notifications be used. Currently the |
| 205 | * default stream type is STREAM_RING. |
| 206 | */ |
| 207 | public static final int STREAM_DEFAULT = -1; |
| 208 | |
| 209 | /** |
| 210 | * The audio stream type to use when playing the sound. |
| 211 | * Should be one of the STREAM_ constants from |
| 212 | * {@link android.media.AudioManager}. |
| 213 | */ |
| 214 | public int audioStreamType = STREAM_DEFAULT; |
| 215 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 217 | * The pattern with which to vibrate. |
| 218 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | * <p> |
| 220 | * To vibrate the default pattern, see {@link #defaults}. |
| 221 | * </p> |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 222 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 223 | * @see android.os.Vibrator#vibrate(long[],int) |
| 224 | */ |
| 225 | public long[] vibrate; |
| 226 | |
| 227 | /** |
| 228 | * The color of the led. The hardware will do its best approximation. |
| 229 | * |
| 230 | * @see #FLAG_SHOW_LIGHTS |
| 231 | * @see #flags |
| 232 | */ |
| 233 | public int ledARGB; |
| 234 | |
| 235 | /** |
| 236 | * The number of milliseconds for the LED to be on while it's flashing. |
| 237 | * The hardware will do its best approximation. |
| 238 | * |
| 239 | * @see #FLAG_SHOW_LIGHTS |
| 240 | * @see #flags |
| 241 | */ |
| 242 | public int ledOnMS; |
| 243 | |
| 244 | /** |
| 245 | * The number of milliseconds for the LED to be off while it's flashing. |
| 246 | * The hardware will do its best approximation. |
| 247 | * |
| 248 | * @see #FLAG_SHOW_LIGHTS |
| 249 | * @see #flags |
| 250 | */ |
| 251 | public int ledOffMS; |
| 252 | |
| 253 | /** |
| 254 | * Specifies which values should be taken from the defaults. |
| 255 | * <p> |
| 256 | * To set, OR the desired from {@link #DEFAULT_SOUND}, |
| 257 | * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default |
| 258 | * values, use {@link #DEFAULT_ALL}. |
| 259 | * </p> |
| 260 | */ |
| 261 | public int defaults; |
| 262 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 263 | /** |
| 264 | * Bit to be bitwise-ored into the {@link #flags} field that should be |
| 265 | * set if you want the LED on for this notification. |
| 266 | * <ul> |
| 267 | * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB |
| 268 | * or 0 for both ledOnMS and ledOffMS.</li> |
| 269 | * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li> |
| 270 | * <li>To flash the LED, pass the number of milliseconds that it should |
| 271 | * be on and off to ledOnMS and ledOffMS.</li> |
| 272 | * </ul> |
| 273 | * <p> |
| 274 | * Since hardware varies, you are not guaranteed that any of the values |
| 275 | * you pass are honored exactly. Use the system defaults (TODO) if possible |
| 276 | * because they will be set to values that work on any given hardware. |
| 277 | * <p> |
| 278 | * The alpha channel must be set for forward compatibility. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 279 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 280 | */ |
| 281 | public static final int FLAG_SHOW_LIGHTS = 0x00000001; |
| 282 | |
| 283 | /** |
| 284 | * Bit to be bitwise-ored into the {@link #flags} field that should be |
| 285 | * set if this notification is in reference to something that is ongoing, |
| 286 | * like a phone call. It should not be set if this notification is in |
| 287 | * reference to something that happened at a particular point in time, |
| 288 | * like a missed phone call. |
| 289 | */ |
| 290 | public static final int FLAG_ONGOING_EVENT = 0x00000002; |
| 291 | |
| 292 | /** |
| 293 | * 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] | 294 | * the audio will be repeated until the notification is |
| 295 | * cancelled or the notification window is opened. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | */ |
| 297 | public static final int FLAG_INSISTENT = 0x00000004; |
| 298 | |
| 299 | /** |
| 300 | * Bit to be bitwise-ored into the {@link #flags} field that should be |
| 301 | * set if you want the sound and/or vibration play each time the |
| 302 | * notification is sent, even if it has not been canceled before that. |
| 303 | */ |
| 304 | public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008; |
| 305 | |
| 306 | /** |
| 307 | * Bit to be bitwise-ored into the {@link #flags} field that should be |
| 308 | * set if the notification should be canceled when it is clicked by the |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 309 | * user. On tablets, the |
| 310 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 311 | */ |
| 312 | public static final int FLAG_AUTO_CANCEL = 0x00000010; |
| 313 | |
| 314 | /** |
| 315 | * Bit to be bitwise-ored into the {@link #flags} field that should be |
| 316 | * set if the notification should not be canceled when the user clicks |
| 317 | * the Clear all button. |
| 318 | */ |
| 319 | public static final int FLAG_NO_CLEAR = 0x00000020; |
| 320 | |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 321 | /** |
| 322 | * Bit to be bitwise-ored into the {@link #flags} field that should be |
| 323 | * set if this notification represents a currently running service. This |
| 324 | * will normally be set for you by {@link Service#startForeground}. |
| 325 | */ |
| 326 | public static final int FLAG_FOREGROUND_SERVICE = 0x00000040; |
| 327 | |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 328 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 329 | * Obsolete flag indicating high-priority notifications; use the priority field instead. |
| 330 | * |
| 331 | * @deprecated Use {@link #priority} with a positive value. |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 332 | */ |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 333 | public static final int FLAG_HIGH_PRIORITY = 0x00000080; |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 334 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | public int flags; |
| 336 | |
| 337 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 338 | * Default notification {@link #priority}. If your application does not prioritize its own |
| 339 | * notifications, use this value for all notifications. |
| 340 | */ |
| 341 | public static final int PRIORITY_DEFAULT = 0; |
| 342 | |
| 343 | /** |
| 344 | * Lower {@link #priority}, for items that are less important. The UI may choose to show these |
| 345 | * items smaller, or at a different position in the list, compared with your app's |
| 346 | * {@link #PRIORITY_DEFAULT} items. |
| 347 | */ |
| 348 | public static final int PRIORITY_LOW = -1; |
| 349 | |
| 350 | /** |
| 351 | * Lowest {@link #priority}; these items might not be shown to the user except under special |
| 352 | * circumstances, such as detailed notification logs. |
| 353 | */ |
| 354 | public static final int PRIORITY_MIN = -2; |
| 355 | |
| 356 | /** |
| 357 | * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to |
| 358 | * show these items larger, or at a different position in notification lists, compared with |
| 359 | * your app's {@link #PRIORITY_DEFAULT} items. |
| 360 | */ |
| 361 | public static final int PRIORITY_HIGH = 1; |
| 362 | |
| 363 | /** |
| 364 | * Highest {@link #priority}, for your application's most important items that require the |
| 365 | * user's prompt attention or input. |
| 366 | */ |
| 367 | public static final int PRIORITY_MAX = 2; |
| 368 | |
| 369 | /** |
| 370 | * Relative priority for this notification. |
| 371 | * |
| 372 | * Priority is an indication of how much of the user's valuable attention should be consumed by |
| 373 | * this notification. Low-priority notifications may be hidden from the user in certain |
| 374 | * situations, while the user might be interrupted for a higher-priority notification. The |
| 375 | * system will make a determination about how to interpret notification priority as described in |
| 376 | * MUMBLE MUMBLE. |
| 377 | */ |
| 378 | public int priority; |
| 379 | |
| 380 | /** |
| 381 | * Notification type: incoming call (voice or video) or similar synchronous communication request. |
| 382 | */ |
| 383 | public static final String KIND_CALL = "android.call"; |
| 384 | |
| 385 | /** |
| 386 | * Notification type: incoming direct message (SMS, instant message, etc.). |
| 387 | */ |
| 388 | public static final String KIND_MESSAGE = "android.message"; |
| 389 | |
| 390 | /** |
| 391 | * Notification type: asynchronous bulk message (email). |
| 392 | */ |
| 393 | public static final String KIND_EMAIL = "android.email"; |
| 394 | |
| 395 | /** |
| 396 | * Notification type: calendar event. |
| 397 | */ |
| 398 | public static final String KIND_EVENT = "android.event"; |
| 399 | |
| 400 | /** |
| 401 | * Notification type: promotion or advertisement. |
| 402 | */ |
| 403 | public static final String KIND_PROMO = "android.promo"; |
| 404 | |
| 405 | /** |
| 406 | * If this notification matches of one or more special types (see the <code>KIND_*</code> |
| 407 | * constants), add them here, best match first. |
| 408 | */ |
| 409 | public String[] kind; |
| 410 | |
| 411 | /** |
| 412 | * Extra key for people values (type TBD). |
| 413 | * |
| 414 | * @hide |
| 415 | */ |
| 416 | public static final String EXTRA_PEOPLE = "android.people"; |
| 417 | |
| 418 | private Bundle extras; |
| 419 | |
| 420 | /** |
| 421 | * Constructs a Notification object with default values. |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 422 | * You might want to consider using {@link Builder} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 423 | */ |
| 424 | public Notification() |
| 425 | { |
| 426 | this.when = System.currentTimeMillis(); |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 427 | this.priority = PRIORITY_DEFAULT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 431 | * @hide |
| 432 | */ |
| 433 | public Notification(Context context, int icon, CharSequence tickerText, long when, |
| 434 | CharSequence contentTitle, CharSequence contentText, Intent contentIntent) |
| 435 | { |
| 436 | this.when = when; |
| 437 | this.icon = icon; |
| 438 | this.tickerText = tickerText; |
| 439 | setLatestEventInfo(context, contentTitle, contentText, |
| 440 | PendingIntent.getActivity(context, 0, contentIntent, 0)); |
| 441 | } |
| 442 | |
| 443 | /** |
| 444 | * Constructs a Notification object with the information needed to |
| 445 | * have a status bar icon without the standard expanded view. |
| 446 | * |
| 447 | * @param icon The resource id of the icon to put in the status bar. |
| 448 | * @param tickerText The text that flows by in the status bar when the notification first |
| 449 | * activates. |
| 450 | * @param when The time to show in the time field. In the System.currentTimeMillis |
| 451 | * timebase. |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 452 | * |
| 453 | * @deprecated Use {@link Builder} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 455 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 456 | public Notification(int icon, CharSequence tickerText, long when) |
| 457 | { |
| 458 | this.icon = icon; |
| 459 | this.tickerText = tickerText; |
| 460 | this.when = when; |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Unflatten the notification from a parcel. |
| 465 | */ |
| 466 | public Notification(Parcel parcel) |
| 467 | { |
| 468 | int version = parcel.readInt(); |
| 469 | |
| 470 | when = parcel.readLong(); |
| 471 | icon = parcel.readInt(); |
| 472 | number = parcel.readInt(); |
| 473 | if (parcel.readInt() != 0) { |
| 474 | contentIntent = PendingIntent.CREATOR.createFromParcel(parcel); |
| 475 | } |
| 476 | if (parcel.readInt() != 0) { |
| 477 | deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel); |
| 478 | } |
| 479 | if (parcel.readInt() != 0) { |
| 480 | tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel); |
| 481 | } |
| 482 | if (parcel.readInt() != 0) { |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 483 | tickerView = RemoteViews.CREATOR.createFromParcel(parcel); |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 484 | } |
| 485 | if (parcel.readInt() != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 486 | contentView = RemoteViews.CREATOR.createFromParcel(parcel); |
| 487 | } |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 488 | if (parcel.readInt() != 0) { |
| 489 | largeIcon = Bitmap.CREATOR.createFromParcel(parcel); |
| 490 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 491 | defaults = parcel.readInt(); |
| 492 | flags = parcel.readInt(); |
| 493 | if (parcel.readInt() != 0) { |
| 494 | sound = Uri.CREATOR.createFromParcel(parcel); |
| 495 | } |
| 496 | |
| 497 | audioStreamType = parcel.readInt(); |
| 498 | vibrate = parcel.createLongArray(); |
| 499 | ledARGB = parcel.readInt(); |
| 500 | ledOnMS = parcel.readInt(); |
| 501 | ledOffMS = parcel.readInt(); |
| 502 | iconLevel = parcel.readInt(); |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 503 | |
| 504 | if (parcel.readInt() != 0) { |
| 505 | fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel); |
| 506 | } |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 507 | |
| 508 | priority = parcel.readInt(); |
| 509 | |
| 510 | kind = parcel.createStringArray(); // may set kind to null |
| 511 | |
| 512 | if (parcel.readInt() != 0) { |
| 513 | extras = parcel.readBundle(); |
| 514 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 515 | } |
| 516 | |
Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 517 | @Override |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 518 | public Notification clone() { |
| 519 | Notification that = new Notification(); |
| 520 | |
| 521 | that.when = this.when; |
| 522 | that.icon = this.icon; |
| 523 | that.number = this.number; |
| 524 | |
| 525 | // PendingIntents are global, so there's no reason (or way) to clone them. |
| 526 | that.contentIntent = this.contentIntent; |
| 527 | that.deleteIntent = this.deleteIntent; |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 528 | that.fullScreenIntent = this.fullScreenIntent; |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 529 | |
| 530 | if (this.tickerText != null) { |
| 531 | that.tickerText = this.tickerText.toString(); |
| 532 | } |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 533 | if (this.tickerView != null) { |
| 534 | that.tickerView = this.tickerView.clone(); |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 535 | } |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 536 | if (this.contentView != null) { |
| 537 | that.contentView = this.contentView.clone(); |
| 538 | } |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 539 | if (this.largeIcon != null) { |
| 540 | that.largeIcon = Bitmap.createBitmap(this.largeIcon); |
| 541 | } |
Jozef BABJAK | a8b9183 | 2011-02-22 08:05:08 +0100 | [diff] [blame] | 542 | that.iconLevel = this.iconLevel; |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 543 | that.sound = this.sound; // android.net.Uri is immutable |
| 544 | that.audioStreamType = this.audioStreamType; |
| 545 | |
| 546 | final long[] vibrate = this.vibrate; |
| 547 | if (vibrate != null) { |
| 548 | final int N = vibrate.length; |
| 549 | final long[] vib = that.vibrate = new long[N]; |
| 550 | System.arraycopy(vibrate, 0, vib, 0, N); |
| 551 | } |
| 552 | |
| 553 | that.ledARGB = this.ledARGB; |
| 554 | that.ledOnMS = this.ledOnMS; |
| 555 | that.ledOffMS = this.ledOffMS; |
| 556 | that.defaults = this.defaults; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 557 | |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 558 | that.flags = this.flags; |
| 559 | |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 560 | that.priority = this.priority; |
| 561 | |
| 562 | final String[] thiskind = this.kind; |
| 563 | if (thiskind != null) { |
| 564 | final int N = thiskind.length; |
| 565 | final String[] thatkind = that.kind = new String[N]; |
| 566 | System.arraycopy(thiskind, 0, thatkind, 0, N); |
| 567 | } |
| 568 | |
| 569 | if (this.extras != null) { |
| 570 | that.extras = new Bundle(this.extras); |
| 571 | |
| 572 | } |
| 573 | |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 574 | return that; |
| 575 | } |
| 576 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 577 | public int describeContents() { |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | /** |
| 582 | * Flatten this notification from a parcel. |
| 583 | */ |
| 584 | public void writeToParcel(Parcel parcel, int flags) |
| 585 | { |
| 586 | parcel.writeInt(1); |
| 587 | |
| 588 | parcel.writeLong(when); |
| 589 | parcel.writeInt(icon); |
| 590 | parcel.writeInt(number); |
| 591 | if (contentIntent != null) { |
| 592 | parcel.writeInt(1); |
| 593 | contentIntent.writeToParcel(parcel, 0); |
| 594 | } else { |
| 595 | parcel.writeInt(0); |
| 596 | } |
| 597 | if (deleteIntent != null) { |
| 598 | parcel.writeInt(1); |
| 599 | deleteIntent.writeToParcel(parcel, 0); |
| 600 | } else { |
| 601 | parcel.writeInt(0); |
| 602 | } |
| 603 | if (tickerText != null) { |
| 604 | parcel.writeInt(1); |
| 605 | TextUtils.writeToParcel(tickerText, parcel, flags); |
| 606 | } else { |
| 607 | parcel.writeInt(0); |
| 608 | } |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 609 | if (tickerView != null) { |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 610 | parcel.writeInt(1); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 611 | tickerView.writeToParcel(parcel, 0); |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 612 | } else { |
| 613 | parcel.writeInt(0); |
| 614 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 615 | if (contentView != null) { |
| 616 | parcel.writeInt(1); |
| 617 | contentView.writeToParcel(parcel, 0); |
| 618 | } else { |
| 619 | parcel.writeInt(0); |
| 620 | } |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 621 | if (largeIcon != null) { |
| 622 | parcel.writeInt(1); |
| 623 | largeIcon.writeToParcel(parcel, 0); |
| 624 | } else { |
| 625 | parcel.writeInt(0); |
| 626 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 627 | |
| 628 | parcel.writeInt(defaults); |
| 629 | parcel.writeInt(this.flags); |
| 630 | |
| 631 | if (sound != null) { |
| 632 | parcel.writeInt(1); |
| 633 | sound.writeToParcel(parcel, 0); |
| 634 | } else { |
| 635 | parcel.writeInt(0); |
| 636 | } |
| 637 | parcel.writeInt(audioStreamType); |
| 638 | parcel.writeLongArray(vibrate); |
| 639 | parcel.writeInt(ledARGB); |
| 640 | parcel.writeInt(ledOnMS); |
| 641 | parcel.writeInt(ledOffMS); |
| 642 | parcel.writeInt(iconLevel); |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 643 | |
| 644 | if (fullScreenIntent != null) { |
| 645 | parcel.writeInt(1); |
| 646 | fullScreenIntent.writeToParcel(parcel, 0); |
| 647 | } else { |
| 648 | parcel.writeInt(0); |
| 649 | } |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 650 | |
| 651 | parcel.writeInt(priority); |
| 652 | |
| 653 | parcel.writeStringArray(kind); // ok for null |
| 654 | |
| 655 | if (extras != null) { |
| 656 | parcel.writeInt(1); |
| 657 | extras.writeToParcel(parcel, 0); |
| 658 | } else { |
| 659 | parcel.writeInt(0); |
| 660 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | /** |
| 664 | * Parcelable.Creator that instantiates Notification objects |
| 665 | */ |
| 666 | public static final Parcelable.Creator<Notification> CREATOR |
| 667 | = new Parcelable.Creator<Notification>() |
| 668 | { |
| 669 | public Notification createFromParcel(Parcel parcel) |
| 670 | { |
| 671 | return new Notification(parcel); |
| 672 | } |
| 673 | |
| 674 | public Notification[] newArray(int size) |
| 675 | { |
| 676 | return new Notification[size]; |
| 677 | } |
| 678 | }; |
| 679 | |
| 680 | /** |
| 681 | * Sets the {@link #contentView} field to be a view with the standard "Latest Event" |
| 682 | * layout. |
| 683 | * |
| 684 | * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields |
| 685 | * in the view.</p> |
| 686 | * @param context The context for your application / activity. |
| 687 | * @param contentTitle The title that goes in the expanded entry. |
| 688 | * @param contentText The text that goes in the expanded entry. |
| 689 | * @param contentIntent The intent to launch when the user clicks the expanded notification. |
| 690 | * If this is an activity, it must include the |
| 691 | * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 692 | * that you take care of task management as described in the |
| 693 | * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back |
| 694 | * Stack</a> document. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 695 | * |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 696 | * @deprecated Use {@link Builder} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 697 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 698 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 699 | public void setLatestEventInfo(Context context, |
| 700 | CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) { |
| 701 | RemoteViews contentView = new RemoteViews(context.getPackageName(), |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 702 | R.layout.status_bar_latest_event_content); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 703 | if (this.icon != 0) { |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 704 | contentView.setImageViewResource(R.id.icon, this.icon); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 705 | } |
| 706 | if (contentTitle != null) { |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 707 | contentView.setTextViewText(R.id.title, contentTitle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 708 | } |
| 709 | if (contentText != null) { |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 710 | contentView.setTextViewText(R.id.text, contentText); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 711 | } |
| 712 | if (this.when != 0) { |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 713 | contentView.setLong(R.id.time, "setTime", when); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | this.contentView = contentView; |
| 717 | this.contentIntent = contentIntent; |
| 718 | } |
| 719 | |
| 720 | @Override |
| 721 | public String toString() { |
| 722 | StringBuilder sb = new StringBuilder(); |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 723 | sb.append("Notification(pri="); |
| 724 | sb.append(priority); |
| 725 | sb.append(" contentView="); |
Joe Onorato | c9596d6 | 2011-01-12 17:03:11 -0800 | [diff] [blame] | 726 | if (contentView != null) { |
| 727 | sb.append(contentView.getPackage()); |
| 728 | sb.append("/0x"); |
| 729 | sb.append(Integer.toHexString(contentView.getLayoutId())); |
| 730 | } else { |
| 731 | sb.append("null"); |
| 732 | } |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 733 | // TODO(dsandler): defaults take precedence over local values, so reorder the branches below |
Joe Onorato | c9596d6 | 2011-01-12 17:03:11 -0800 | [diff] [blame] | 734 | sb.append(" vibrate="); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 735 | if (this.vibrate != null) { |
| 736 | int N = this.vibrate.length-1; |
| 737 | sb.append("["); |
| 738 | for (int i=0; i<N; i++) { |
| 739 | sb.append(this.vibrate[i]); |
| 740 | sb.append(','); |
| 741 | } |
Simon Schoar | 8cf97d9 | 2009-06-10 22:08:37 +0200 | [diff] [blame] | 742 | if (N != -1) { |
| 743 | sb.append(this.vibrate[N]); |
| 744 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 745 | sb.append("]"); |
| 746 | } else if ((this.defaults & DEFAULT_VIBRATE) != 0) { |
| 747 | sb.append("default"); |
| 748 | } else { |
| 749 | sb.append("null"); |
| 750 | } |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 751 | sb.append(" sound="); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 752 | if (this.sound != null) { |
| 753 | sb.append(this.sound.toString()); |
| 754 | } else if ((this.defaults & DEFAULT_SOUND) != 0) { |
| 755 | sb.append("default"); |
| 756 | } else { |
| 757 | sb.append("null"); |
| 758 | } |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 759 | sb.append(" defaults=0x"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 760 | sb.append(Integer.toHexString(this.defaults)); |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 761 | sb.append(" flags=0x"); |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 762 | sb.append(Integer.toHexString(this.flags)); |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 763 | sb.append(" kind=["); |
| 764 | if (this.kind == null) { |
| 765 | sb.append("null"); |
| 766 | } else { |
| 767 | for (int i=0; i<this.kind.length; i++) { |
| 768 | if (i>0) sb.append(","); |
| 769 | sb.append(this.kind[i]); |
| 770 | } |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 771 | } |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 772 | sb.append("])"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 773 | return sb.toString(); |
| 774 | } |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 775 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 776 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 777 | * Builder class for {@link Notification} objects. |
| 778 | * |
| 779 | * Provides a convenient way to set the various fields of a {@link Notification} and generate |
| 780 | * content views using the platform's notification layout template. |
| 781 | * |
| 782 | * Example: |
| 783 | * |
| 784 | * <pre class="prettyprint"> |
| 785 | * Notification noti = new Notification.Builder() |
| 786 | * .setContentTitle("New mail from " + sender.toString()) |
| 787 | * .setContentText(subject) |
| 788 | * .setSmallIcon(R.drawable.new_mail) |
| 789 | * .setLargeIcon(aBitmap) |
| 790 | * .getNotification(); |
| 791 | * </pre> |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 792 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 793 | public static class Builder { |
| 794 | private Context mContext; |
| 795 | |
| 796 | private long mWhen; |
| 797 | private int mSmallIcon; |
| 798 | private int mSmallIconLevel; |
Joe Onorato | 8595a3d | 2010-11-19 18:12:07 -0800 | [diff] [blame] | 799 | private int mNumber; |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 800 | private CharSequence mContentTitle; |
| 801 | private CharSequence mContentText; |
| 802 | private CharSequence mContentInfo; |
| 803 | private PendingIntent mContentIntent; |
| 804 | private RemoteViews mContentView; |
| 805 | private PendingIntent mDeleteIntent; |
| 806 | private PendingIntent mFullScreenIntent; |
| 807 | private CharSequence mTickerText; |
| 808 | private RemoteViews mTickerView; |
| 809 | private Bitmap mLargeIcon; |
| 810 | private Uri mSound; |
| 811 | private int mAudioStreamType; |
| 812 | private long[] mVibrate; |
| 813 | private int mLedArgb; |
| 814 | private int mLedOnMs; |
| 815 | private int mLedOffMs; |
| 816 | private int mDefaults; |
| 817 | private int mFlags; |
Jeff Sharkey | 1c40013 | 2011-08-05 14:50:13 -0700 | [diff] [blame] | 818 | private int mProgressMax; |
| 819 | private int mProgress; |
| 820 | private boolean mProgressIndeterminate; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 821 | private ArrayList<String> mKindList = new ArrayList<String>(1); |
| 822 | private Bundle mExtras; |
| 823 | private int mPriority; |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 824 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 825 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 826 | * Constructs a new Builder with the defaults: |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 827 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 828 | |
| 829 | * <table> |
| 830 | * <tr><th align=right>priority</th> |
| 831 | * <td>{@link #PRIORITY_DEFAULT}</td></tr> |
| 832 | * <tr><th align=right>when</th> |
| 833 | * <td>now ({@link System#currentTimeMillis()})</td></tr> |
| 834 | * <tr><th align=right>audio stream</th> |
| 835 | * <td>{@link #STREAM_DEFAULT}</td></tr> |
| 836 | * </table> |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 837 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 838 | |
| 839 | * @param context |
| 840 | * A {@link Context} that will be used by the Builder to construct the |
| 841 | * RemoteViews. The Context will not be held past the lifetime of this Builder |
| 842 | * object. |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 843 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 844 | public Builder(Context context) { |
| 845 | mContext = context; |
Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 846 | |
| 847 | // Set defaults to match the defaults of a Notification |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 848 | mWhen = System.currentTimeMillis(); |
Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 849 | mAudioStreamType = STREAM_DEFAULT; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 850 | mPriority = PRIORITY_DEFAULT; |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 851 | } |
| 852 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 853 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 854 | * Add a timestamp pertaining to the notification (usually the time the event occurred). |
| 855 | * |
| 856 | |
| 857 | * @see Notification#when |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 858 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 859 | public Builder setWhen(long when) { |
| 860 | mWhen = when; |
| 861 | return this; |
| 862 | } |
| 863 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 864 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 865 | * Set the small icon resource, which will be used to represent the notification in the |
| 866 | * status bar. |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 867 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 868 | |
| 869 | * The platform template for the expanded view will draw this icon in the left, unless a |
| 870 | * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small |
| 871 | * icon will be moved to the right-hand side. |
| 872 | * |
| 873 | |
| 874 | * @param icon |
| 875 | * A resource ID in the application's package of the drawable to use. |
| 876 | * @see Notification#icon |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 877 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 878 | public Builder setSmallIcon(int icon) { |
| 879 | mSmallIcon = icon; |
| 880 | return this; |
| 881 | } |
| 882 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 883 | /** |
| 884 | * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional |
| 885 | * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable |
| 886 | * LevelListDrawable}. |
| 887 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 888 | * @param icon A resource ID in the application's package of the drawable to use. |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 889 | * @param level The level to use for the icon. |
| 890 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 891 | * @see Notification#icon |
| 892 | * @see Notification#iconLevel |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 893 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 894 | public Builder setSmallIcon(int icon, int level) { |
| 895 | mSmallIcon = icon; |
| 896 | mSmallIconLevel = level; |
| 897 | return this; |
| 898 | } |
| 899 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 900 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 901 | * Set the first line of text in the platform notification template. |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 902 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 903 | public Builder setContentTitle(CharSequence title) { |
| 904 | mContentTitle = title; |
| 905 | return this; |
| 906 | } |
| 907 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 908 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 909 | * Set the second line of text in the platform notification template. |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 910 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 911 | public Builder setContentText(CharSequence text) { |
| 912 | mContentText = text; |
| 913 | return this; |
| 914 | } |
| 915 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 916 | /** |
| 917 | * Set the large number at the right-hand side of the notification. This is |
| 918 | * equivalent to setContentInfo, although it might show the number in a different |
| 919 | * font size for readability. |
| 920 | */ |
Joe Onorato | 8595a3d | 2010-11-19 18:12:07 -0800 | [diff] [blame] | 921 | public Builder setNumber(int number) { |
| 922 | mNumber = number; |
| 923 | return this; |
| 924 | } |
| 925 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 926 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 927 | * A small piece of additional information pertaining to this notification. |
| 928 | * |
| 929 | |
| 930 | * The platform template will draw this on the last line of the notification, at the far |
| 931 | * right (to the right of a smallIcon if it has been placed there). |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 932 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 933 | public Builder setContentInfo(CharSequence info) { |
| 934 | mContentInfo = info; |
| 935 | return this; |
| 936 | } |
| 937 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 938 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 939 | * Set the progress this notification represents. |
| 940 | * |
| 941 | |
| 942 | * The platform template will represent this using a {@link ProgressBar}. |
Jeff Sharkey | 1c40013 | 2011-08-05 14:50:13 -0700 | [diff] [blame] | 943 | */ |
| 944 | public Builder setProgress(int max, int progress, boolean indeterminate) { |
| 945 | mProgressMax = max; |
| 946 | mProgress = progress; |
| 947 | mProgressIndeterminate = indeterminate; |
| 948 | return this; |
| 949 | } |
| 950 | |
| 951 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 952 | * Supply a custom RemoteViews to use instead of the platform template. |
| 953 | * |
| 954 | |
| 955 | * @see Notification#contentView |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 956 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 957 | public Builder setContent(RemoteViews views) { |
| 958 | mContentView = views; |
| 959 | return this; |
| 960 | } |
| 961 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 962 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 963 | * Supply a {@link PendingIntent} to be sent when the notification is clicked. |
| 964 | * |
| 965 | |
| 966 | * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you |
| 967 | * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use |
| 968 | * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)} |
| 969 | |
| 970 | * to assign PendingIntents to individual views in that custom layout (i.e., to create |
| 971 | |
| 972 | * clickable buttons inside the |
| 973 | * notification view). |
| 974 | * |
| 975 | |
| 976 | * @see Notification#contentIntent Notification.contentIntent |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 977 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 978 | public Builder setContentIntent(PendingIntent intent) { |
| 979 | mContentIntent = intent; |
| 980 | return this; |
| 981 | } |
| 982 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 983 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 984 | * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user. |
| 985 | * |
| 986 | |
| 987 | * @see Notification#deleteIntent |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 988 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 989 | public Builder setDeleteIntent(PendingIntent intent) { |
| 990 | mDeleteIntent = intent; |
| 991 | return this; |
| 992 | } |
| 993 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 994 | /** |
| 995 | * An intent to launch instead of posting the notification to the status bar. |
| 996 | * Only for use with extremely high-priority notifications demanding the user's |
| 997 | * <strong>immediate</strong> attention, such as an incoming phone call or |
| 998 | * alarm clock that the user has explicitly set to a particular time. |
| 999 | * If this facility is used for something else, please give the user an option |
| 1000 | * to turn it off and use a normal notification, as this can be extremely |
| 1001 | * disruptive. |
| 1002 | * |
| 1003 | * @param intent The pending intent to launch. |
| 1004 | * @param highPriority Passing true will cause this notification to be sent |
| 1005 | * even if other notifications are suppressed. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 1006 | * |
| 1007 | * @see Notification#fullScreenIntent |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1008 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1009 | public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) { |
| 1010 | mFullScreenIntent = intent; |
| 1011 | setFlag(FLAG_HIGH_PRIORITY, highPriority); |
| 1012 | return this; |
| 1013 | } |
| 1014 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1015 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 1016 | * Set the "ticker" text which is displayed in the status bar when the notification first |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1017 | * arrives. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 1018 | * |
| 1019 | |
| 1020 | * @see Notification#tickerText |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1021 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1022 | public Builder setTicker(CharSequence tickerText) { |
| 1023 | mTickerText = tickerText; |
| 1024 | return this; |
| 1025 | } |
| 1026 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1027 | /** |
| 1028 | * Set the text that is displayed in the status bar when the notification first |
| 1029 | * arrives, and also a RemoteViews object that may be displayed instead on some |
| 1030 | * devices. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 1031 | * |
| 1032 | * @see Notification#tickerText |
| 1033 | * @see Notification#tickerView |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1034 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1035 | public Builder setTicker(CharSequence tickerText, RemoteViews views) { |
| 1036 | mTickerText = tickerText; |
| 1037 | mTickerView = views; |
| 1038 | return this; |
| 1039 | } |
| 1040 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1041 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 1042 | * Add a large icon to the notification (and the ticker on some devices). |
| 1043 | * |
| 1044 | * In the platform template, this image will be shown on the left of the notification view |
| 1045 | * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side). |
| 1046 | * |
| 1047 | * @see Notification#largeIcon |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1048 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1049 | public Builder setLargeIcon(Bitmap icon) { |
| 1050 | mLargeIcon = icon; |
| 1051 | return this; |
| 1052 | } |
| 1053 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1054 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 1055 | * Set the sound to play. |
| 1056 | * |
| 1057 | * It will be played on the {@link #STREAM_DEFAULT default stream} for notifications. |
| 1058 | * |
| 1059 | * @see Notification#sound |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1060 | */ |
Joe Onorato | 52f80cd | 2010-11-21 15:34:48 -0800 | [diff] [blame] | 1061 | public Builder setSound(Uri sound) { |
| 1062 | mSound = sound; |
| 1063 | mAudioStreamType = STREAM_DEFAULT; |
| 1064 | return this; |
| 1065 | } |
| 1066 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1067 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 1068 | * Set the sound to play, along with a specific stream on which to play it. |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1069 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 1070 | * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants. |
| 1071 | * |
| 1072 | * @see Notification#sound |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1073 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1074 | public Builder setSound(Uri sound, int streamType) { |
| 1075 | mSound = sound; |
| 1076 | mAudioStreamType = streamType; |
| 1077 | return this; |
| 1078 | } |
| 1079 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1080 | /** |
| 1081 | * Set the vibration pattern to use. |
| 1082 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 1083 | |
| 1084 | * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the |
| 1085 | * <code>pattern</code> parameter. |
| 1086 | * |
| 1087 | |
| 1088 | * @see Notification#vibrate |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1089 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1090 | public Builder setVibrate(long[] pattern) { |
| 1091 | mVibrate = pattern; |
| 1092 | return this; |
| 1093 | } |
| 1094 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1095 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 1096 | * Set the desired color for the indicator LED on the device, as well as the |
| 1097 | * blink duty cycle (specified in milliseconds). |
| 1098 | * |
| 1099 | |
| 1100 | * Not all devices will honor all (or even any) of these values. |
| 1101 | * |
| 1102 | |
| 1103 | * @see Notification#ledARGB |
| 1104 | * @see Notification#ledOnMS |
| 1105 | * @see Notification#ledOffMS |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1106 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1107 | public Builder setLights(int argb, int onMs, int offMs) { |
| 1108 | mLedArgb = argb; |
| 1109 | mLedOnMs = onMs; |
| 1110 | mLedOffMs = offMs; |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1111 | return this; |
| 1112 | } |
| 1113 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1114 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 1115 | * Set whether this is an "ongoing" notification. |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1116 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 1117 | |
| 1118 | * Ongoing notifications cannot be dismissed by the user, so your application or service |
| 1119 | * must take care of canceling them. |
| 1120 | * |
| 1121 | |
| 1122 | * They are typically used to indicate a background task that the user is actively engaged |
| 1123 | * with (e.g., playing music) or is pending in some way and therefore occupying the device |
| 1124 | * (e.g., a file download, sync operation, active network connection). |
| 1125 | * |
| 1126 | |
| 1127 | * @see Notification#FLAG_ONGOING_EVENT |
| 1128 | * @see Service#setForeground(boolean) |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1129 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1130 | public Builder setOngoing(boolean ongoing) { |
| 1131 | setFlag(FLAG_ONGOING_EVENT, ongoing); |
| 1132 | return this; |
| 1133 | } |
| 1134 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1135 | /** |
| 1136 | * Set this flag if you would only like the sound, vibrate |
| 1137 | * and ticker to be played if the notification is not already showing. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 1138 | * |
| 1139 | * @see Notification#FLAG_ONLY_ALERT_ONCE |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1140 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1141 | public Builder setOnlyAlertOnce(boolean onlyAlertOnce) { |
| 1142 | setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce); |
| 1143 | return this; |
| 1144 | } |
| 1145 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1146 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 1147 | * Make this notification automatically dismissed when the user touches it. The |
| 1148 | * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens. |
| 1149 | * |
| 1150 | * @see Notification#FLAG_AUTO_CANCEL |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1151 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1152 | public Builder setAutoCancel(boolean autoCancel) { |
Joe Onorato | 281d83f | 2011-01-04 17:13:10 -0800 | [diff] [blame] | 1153 | setFlag(FLAG_AUTO_CANCEL, autoCancel); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1154 | return this; |
| 1155 | } |
| 1156 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1157 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 1158 | * Set which notification properties will be inherited from system defaults. |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1159 | * <p> |
| 1160 | * The value should be one or more of the following fields combined with |
| 1161 | * bitwise-or: |
| 1162 | * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. |
| 1163 | * <p> |
| 1164 | * For all default values, use {@link #DEFAULT_ALL}. |
| 1165 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1166 | public Builder setDefaults(int defaults) { |
| 1167 | mDefaults = defaults; |
| 1168 | return this; |
| 1169 | } |
| 1170 | |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 1171 | /** |
| 1172 | * Set the priority of this notification. |
| 1173 | * |
| 1174 | * @see Notification#priority |
| 1175 | */ |
| 1176 | public Builder setPriority(int pri) { |
| 1177 | mPriority = pri; |
| 1178 | return this; |
| 1179 | } |
| 1180 | |
| 1181 | /** |
| 1182 | * Add a kind (category) to this notification. Optional. |
| 1183 | * |
| 1184 | * @see Notification#kind |
| 1185 | */ |
| 1186 | public Builder addKind(String k) { |
| 1187 | mKindList.add(k); |
| 1188 | return this; |
| 1189 | } |
| 1190 | |
| 1191 | /** |
| 1192 | * Add metadata to this notification. |
| 1193 | * |
| 1194 | * A reference to the Bundle is held for the lifetime of this Builder, and the Bundle's |
| 1195 | * current contents are copied into the Notification each time {@link #getNotification()} is |
| 1196 | * called. |
| 1197 | * |
| 1198 | * @see Notification#extras |
| 1199 | * @hide |
| 1200 | */ |
| 1201 | public Builder setExtras(Bundle bag) { |
| 1202 | mExtras = bag; |
| 1203 | return this; |
| 1204 | } |
| 1205 | |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1206 | private void setFlag(int mask, boolean value) { |
| 1207 | if (value) { |
| 1208 | mFlags |= mask; |
| 1209 | } else { |
| 1210 | mFlags &= ~mask; |
| 1211 | } |
| 1212 | } |
| 1213 | |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 1214 | private RemoteViews makeRemoteViews(int resId) { |
| 1215 | RemoteViews contentView = new RemoteViews(mContext.getPackageName(), resId); |
Jeff Sharkey | 1c40013 | 2011-08-05 14:50:13 -0700 | [diff] [blame] | 1216 | boolean hasLine3 = false; |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 1217 | if (mSmallIcon != 0) { |
| 1218 | contentView.setImageViewResource(R.id.icon, mSmallIcon); |
Jeff Sharkey | 1c40013 | 2011-08-05 14:50:13 -0700 | [diff] [blame] | 1219 | contentView.setViewVisibility(R.id.icon, View.VISIBLE); |
| 1220 | } else { |
| 1221 | contentView.setViewVisibility(R.id.icon, View.GONE); |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 1222 | } |
| 1223 | if (mContentTitle != null) { |
| 1224 | contentView.setTextViewText(R.id.title, mContentTitle); |
| 1225 | } |
| 1226 | if (mContentText != null) { |
| 1227 | contentView.setTextViewText(R.id.text, mContentText); |
Jeff Sharkey | 1c40013 | 2011-08-05 14:50:13 -0700 | [diff] [blame] | 1228 | hasLine3 = true; |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 1229 | } |
| 1230 | if (mContentInfo != null) { |
| 1231 | contentView.setTextViewText(R.id.info, mContentInfo); |
Jeff Sharkey | 1c40013 | 2011-08-05 14:50:13 -0700 | [diff] [blame] | 1232 | contentView.setViewVisibility(R.id.info, View.VISIBLE); |
| 1233 | hasLine3 = true; |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 1234 | } else if (mNumber > 0) { |
Daniel Sandler | ebce011 | 2011-06-16 16:44:51 -0400 | [diff] [blame] | 1235 | final int tooBig = mContext.getResources().getInteger( |
| 1236 | R.integer.status_bar_notification_info_maxnum); |
| 1237 | if (mNumber > tooBig) { |
| 1238 | contentView.setTextViewText(R.id.info, mContext.getResources().getString( |
| 1239 | R.string.status_bar_notification_info_overflow)); |
Joe Onorato | 059a2f8 | 2011-01-04 10:27:01 -0800 | [diff] [blame] | 1240 | } else { |
| 1241 | NumberFormat f = NumberFormat.getIntegerInstance(); |
| 1242 | contentView.setTextViewText(R.id.info, f.format(mNumber)); |
| 1243 | } |
Jeff Sharkey | 1c40013 | 2011-08-05 14:50:13 -0700 | [diff] [blame] | 1244 | contentView.setViewVisibility(R.id.info, View.VISIBLE); |
| 1245 | hasLine3 = true; |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 1246 | } else { |
| 1247 | contentView.setViewVisibility(R.id.info, View.GONE); |
| 1248 | } |
Jeff Sharkey | 1c40013 | 2011-08-05 14:50:13 -0700 | [diff] [blame] | 1249 | if (mProgressMax != 0 || mProgressIndeterminate) { |
| 1250 | contentView.setProgressBar( |
| 1251 | R.id.progress, mProgressMax, mProgress, mProgressIndeterminate); |
| 1252 | contentView.setViewVisibility(R.id.progress, View.VISIBLE); |
| 1253 | } else { |
| 1254 | contentView.setViewVisibility(R.id.progress, View.GONE); |
| 1255 | } |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 1256 | if (mWhen != 0) { |
| 1257 | contentView.setLong(R.id.time, "setTime", mWhen); |
| 1258 | } |
Jeff Sharkey | 1c40013 | 2011-08-05 14:50:13 -0700 | [diff] [blame] | 1259 | contentView.setViewVisibility(R.id.line3, hasLine3 ? View.VISIBLE : View.GONE); |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 1260 | return contentView; |
| 1261 | } |
| 1262 | |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1263 | private RemoteViews makeContentView() { |
| 1264 | if (mContentView != null) { |
| 1265 | return mContentView; |
| 1266 | } else { |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 1267 | return makeRemoteViews(mLargeIcon == null |
| 1268 | ? R.layout.status_bar_latest_event_content |
| 1269 | : R.layout.status_bar_latest_event_content_large_icon); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | private RemoteViews makeTickerView() { |
| 1274 | if (mTickerView != null) { |
| 1275 | return mTickerView; |
| 1276 | } else { |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 1277 | if (mContentView == null) { |
| 1278 | return makeRemoteViews(mLargeIcon == null |
| 1279 | ? R.layout.status_bar_latest_event_ticker |
| 1280 | : R.layout.status_bar_latest_event_ticker_large_icon); |
| 1281 | } else { |
| 1282 | return null; |
| 1283 | } |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1284 | } |
| 1285 | } |
| 1286 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1287 | /** |
| 1288 | * Combine all of the options that have been set and return a new {@link Notification} |
| 1289 | * object. |
| 1290 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1291 | public Notification getNotification() { |
| 1292 | Notification n = new Notification(); |
| 1293 | n.when = mWhen; |
| 1294 | n.icon = mSmallIcon; |
| 1295 | n.iconLevel = mSmallIconLevel; |
Joe Onorato | 8595a3d | 2010-11-19 18:12:07 -0800 | [diff] [blame] | 1296 | n.number = mNumber; |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1297 | n.contentView = makeContentView(); |
| 1298 | n.contentIntent = mContentIntent; |
| 1299 | n.deleteIntent = mDeleteIntent; |
| 1300 | n.fullScreenIntent = mFullScreenIntent; |
| 1301 | n.tickerText = mTickerText; |
| 1302 | n.tickerView = makeTickerView(); |
| 1303 | n.largeIcon = mLargeIcon; |
| 1304 | n.sound = mSound; |
| 1305 | n.audioStreamType = mAudioStreamType; |
| 1306 | n.vibrate = mVibrate; |
| 1307 | n.ledARGB = mLedArgb; |
| 1308 | n.ledOnMS = mLedOnMs; |
| 1309 | n.ledOffMS = mLedOffMs; |
| 1310 | n.defaults = mDefaults; |
| 1311 | n.flags = mFlags; |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 1312 | if (mLedOnMs != 0 && mLedOffMs != 0) { |
| 1313 | n.flags |= FLAG_SHOW_LIGHTS; |
| 1314 | } |
| 1315 | if ((mDefaults & DEFAULT_LIGHTS) != 0) { |
| 1316 | n.flags |= FLAG_SHOW_LIGHTS; |
| 1317 | } |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame^] | 1318 | if (mKindList.size() > 0) { |
| 1319 | n.kind = new String[mKindList.size()]; |
| 1320 | mKindList.toArray(n.kind); |
| 1321 | } else { |
| 1322 | n.kind = null; |
| 1323 | } |
| 1324 | n.priority = mPriority; |
| 1325 | n.extras = mExtras != null ? new Bundle(mExtras) : null; |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1326 | return n; |
| 1327 | } |
| 1328 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1329 | } |