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