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