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; |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 23 | import android.content.res.Resources; |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 24 | import android.graphics.Bitmap; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 25 | import android.media.AudioManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 26 | import android.net.Uri; |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 27 | import android.os.BadParcelableException; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 28 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.os.Parcel; |
| 30 | import android.os.Parcelable; |
Daniel Sandler | a2985ed | 2012-04-03 16:42:00 -0400 | [diff] [blame] | 31 | import android.os.SystemClock; |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 32 | import android.os.UserHandle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.text.TextUtils; |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 34 | import android.util.Log; |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 35 | import android.util.TypedValue; |
Joe Onorato | 8595a3d | 2010-11-19 18:12:07 -0800 | [diff] [blame] | 36 | import android.view.View; |
Jeff Sharkey | 1c40013 | 2011-08-05 14:50:13 -0700 | [diff] [blame] | 37 | import android.widget.ProgressBar; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.widget.RemoteViews; |
| 39 | |
Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 40 | import java.text.NumberFormat; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 41 | import java.util.ArrayList; |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 42 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | /** |
| 44 | * A class that represents how a persistent notification is to be presented to |
| 45 | * the user using the {@link android.app.NotificationManager}. |
| 46 | * |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 47 | * <p>The {@link Notification.Builder Notification.Builder} has been added to make it |
| 48 | * easier to construct Notifications.</p> |
| 49 | * |
Joe Fernandez | 558459f | 2011-10-13 16:47:36 -0700 | [diff] [blame] | 50 | * <div class="special reference"> |
| 51 | * <h3>Developer Guides</h3> |
| 52 | * <p>For a guide to creating notifications, read the |
| 53 | * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a> |
| 54 | * developer guide.</p> |
| 55 | * </div> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | */ |
| 57 | public class Notification implements Parcelable |
| 58 | { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 59 | private static final String TAG = "Notification"; |
| 60 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | /** |
| 62 | * Use all default values (where applicable). |
| 63 | */ |
| 64 | public static final int DEFAULT_ALL = ~0; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 65 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 66 | /** |
| 67 | * Use the default notification sound. This will ignore any given |
| 68 | * {@link #sound}. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 69 | * |
| 70 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | * @see #defaults |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 72 | */ |
| 73 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | public static final int DEFAULT_SOUND = 1; |
| 75 | |
| 76 | /** |
| 77 | * Use the default notification vibrate. This will ignore any given |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 78 | * {@link #vibrate}. Using phone vibration requires the |
Scott Main | b8b3645 | 2009-04-26 15:50:49 -0700 | [diff] [blame] | 79 | * {@link android.Manifest.permission#VIBRATE VIBRATE} permission. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 80 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 81 | * @see #defaults |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 82 | */ |
| 83 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | public static final int DEFAULT_VIBRATE = 2; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 85 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 86 | /** |
| 87 | * Use the default notification lights. This will ignore the |
| 88 | * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or |
| 89 | * {@link #ledOnMS}. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 90 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | * @see #defaults |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 92 | */ |
| 93 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | public static final int DEFAULT_LIGHTS = 4; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 95 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 96 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 97 | * A timestamp related to this notification, in milliseconds since the epoch. |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 98 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 99 | * Default value: {@link System#currentTimeMillis() Now}. |
| 100 | * |
| 101 | * Choose a timestamp that will be most relevant to the user. For most finite events, this |
| 102 | * corresponds to the time the event happened (or will happen, in the case of events that have |
| 103 | * yet to occur but about which the user is being informed). Indefinite events should be |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 104 | * timestamped according to when the activity began. |
| 105 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 106 | * Some examples: |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 107 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 108 | * <ul> |
| 109 | * <li>Notification of a new chat message should be stamped when the message was received.</li> |
| 110 | * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li> |
| 111 | * <li>Notification of a completed file download should be stamped when the download finished.</li> |
| 112 | * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li> |
| 113 | * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time. |
| 114 | * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time. |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 115 | * </ul> |
| 116 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 117 | */ |
| 118 | public long when; |
| 119 | |
| 120 | /** |
| 121 | * 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] | 122 | * 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] | 123 | */ |
| 124 | public int icon; |
| 125 | |
| 126 | /** |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 127 | * If the icon in the status bar is to have more than one level, you can set this. Otherwise, |
| 128 | * leave it at its default value of 0. |
| 129 | * |
| 130 | * @see android.widget.ImageView#setImageLevel |
| 131 | * @see android.graphics.drawable#setLevel |
| 132 | */ |
| 133 | public int iconLevel; |
| 134 | |
| 135 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 136 | * The number of events that this notification represents. For example, in a new mail |
| 137 | * notification, this could be the number of unread messages. |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 138 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 139 | * The system may or may not use this field to modify the appearance of the notification. For |
| 140 | * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was |
| 141 | * superimposed over the icon in the status bar. Starting with |
| 142 | * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by |
| 143 | * {@link Notification.Builder} has displayed the number in the expanded notification view. |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 144 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 145 | * 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] | 146 | */ |
| 147 | public int number; |
| 148 | |
| 149 | /** |
| 150 | * The intent to execute when the expanded status entry is clicked. If |
| 151 | * this is an activity, it must include the |
| 152 | * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 153 | * that you take care of task management as described in the |
| 154 | * <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] | 155 | * Stack</a> document. In particular, make sure to read the notification section |
| 156 | * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling |
| 157 | * Notifications</a> for the correct ways to launch an application from a |
| 158 | * notification. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | */ |
| 160 | public PendingIntent contentIntent; |
| 161 | |
| 162 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 163 | * The intent to execute when the notification is explicitly dismissed by the user, either with |
| 164 | * the "Clear All" button or by swiping it away individually. |
| 165 | * |
| 166 | * This probably shouldn't be launching an activity since several of those will be sent |
| 167 | * at the same time. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | */ |
| 169 | public PendingIntent deleteIntent; |
| 170 | |
| 171 | /** |
Dianne Hackborn | 170bae7 | 2010-09-03 15:14:28 -0700 | [diff] [blame] | 172 | * 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] | 173 | * |
| 174 | * @see Notification.Builder#setFullScreenIntent |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 175 | */ |
| 176 | public PendingIntent fullScreenIntent; |
| 177 | |
| 178 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | * Text to scroll across the screen when this item is added to |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 180 | * the status bar on large and smaller devices. |
| 181 | * |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 182 | * @see #tickerView |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | */ |
| 184 | public CharSequence tickerText; |
| 185 | |
| 186 | /** |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 187 | * The view to show as the ticker in the status bar when the notification |
| 188 | * is posted. |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 189 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 190 | public RemoteViews tickerView; |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 191 | |
| 192 | /** |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 193 | * 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] | 194 | */ |
| 195 | public RemoteViews contentView; |
| 196 | |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 197 | /** |
Daniel Sandler | 4dfbe83 | 2012-04-11 14:51:46 -0400 | [diff] [blame] | 198 | * A large-format version of {@link #contentView}, giving the Notification an |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 199 | * opportunity to show more detail. The system UI may choose to show this |
| 200 | * instead of the normal content view at its discretion. |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 201 | */ |
| 202 | public RemoteViews bigContentView; |
| 203 | |
| 204 | /** |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 205 | * 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] | 206 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 207 | public Bitmap largeIcon; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 208 | |
| 209 | /** |
| 210 | * The sound to play. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 211 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 212 | * <p> |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 213 | * To play the default notification sound, see {@link #defaults}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | * </p> |
| 215 | */ |
| 216 | public Uri sound; |
| 217 | |
| 218 | /** |
| 219 | * Use this constant as the value for audioStreamType to request that |
| 220 | * the default stream type for notifications be used. Currently the |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 221 | * default stream type is {@link AudioManager#STREAM_NOTIFICATION}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | */ |
| 223 | public static final int STREAM_DEFAULT = -1; |
| 224 | |
| 225 | /** |
| 226 | * The audio stream type to use when playing the sound. |
| 227 | * Should be one of the STREAM_ constants from |
| 228 | * {@link android.media.AudioManager}. |
| 229 | */ |
| 230 | public int audioStreamType = STREAM_DEFAULT; |
| 231 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 232 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 233 | * The pattern with which to vibrate. |
| 234 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 235 | * <p> |
| 236 | * To vibrate the default pattern, see {@link #defaults}. |
| 237 | * </p> |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 238 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | * @see android.os.Vibrator#vibrate(long[],int) |
| 240 | */ |
| 241 | public long[] vibrate; |
| 242 | |
| 243 | /** |
| 244 | * The color of the led. The hardware will do its best approximation. |
| 245 | * |
| 246 | * @see #FLAG_SHOW_LIGHTS |
| 247 | * @see #flags |
| 248 | */ |
| 249 | public int ledARGB; |
| 250 | |
| 251 | /** |
| 252 | * The number of milliseconds for the LED to be on while it's flashing. |
| 253 | * The hardware will do its best approximation. |
| 254 | * |
| 255 | * @see #FLAG_SHOW_LIGHTS |
| 256 | * @see #flags |
| 257 | */ |
| 258 | public int ledOnMS; |
| 259 | |
| 260 | /** |
| 261 | * The number of milliseconds for the LED to be off while it's flashing. |
| 262 | * The hardware will do its best approximation. |
| 263 | * |
| 264 | * @see #FLAG_SHOW_LIGHTS |
| 265 | * @see #flags |
| 266 | */ |
| 267 | public int ledOffMS; |
| 268 | |
| 269 | /** |
| 270 | * Specifies which values should be taken from the defaults. |
| 271 | * <p> |
| 272 | * To set, OR the desired from {@link #DEFAULT_SOUND}, |
| 273 | * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default |
| 274 | * values, use {@link #DEFAULT_ALL}. |
| 275 | * </p> |
| 276 | */ |
| 277 | public int defaults; |
| 278 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 279 | /** |
| 280 | * Bit to be bitwise-ored into the {@link #flags} field that should be |
| 281 | * set if you want the LED on for this notification. |
| 282 | * <ul> |
| 283 | * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB |
| 284 | * or 0 for both ledOnMS and ledOffMS.</li> |
| 285 | * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li> |
| 286 | * <li>To flash the LED, pass the number of milliseconds that it should |
| 287 | * be on and off to ledOnMS and ledOffMS.</li> |
| 288 | * </ul> |
| 289 | * <p> |
| 290 | * Since hardware varies, you are not guaranteed that any of the values |
| 291 | * you pass are honored exactly. Use the system defaults (TODO) if possible |
| 292 | * because they will be set to values that work on any given hardware. |
| 293 | * <p> |
| 294 | * The alpha channel must be set for forward compatibility. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 295 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | */ |
| 297 | public static final int FLAG_SHOW_LIGHTS = 0x00000001; |
| 298 | |
| 299 | /** |
| 300 | * Bit to be bitwise-ored into the {@link #flags} field that should be |
| 301 | * set if this notification is in reference to something that is ongoing, |
| 302 | * like a phone call. It should not be set if this notification is in |
| 303 | * reference to something that happened at a particular point in time, |
| 304 | * like a missed phone call. |
| 305 | */ |
| 306 | public static final int FLAG_ONGOING_EVENT = 0x00000002; |
| 307 | |
| 308 | /** |
| 309 | * 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] | 310 | * the audio will be repeated until the notification is |
| 311 | * cancelled or the notification window is opened. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | */ |
| 313 | public static final int FLAG_INSISTENT = 0x00000004; |
| 314 | |
| 315 | /** |
| 316 | * Bit to be bitwise-ored into the {@link #flags} field that should be |
| 317 | * set if you want the sound and/or vibration play each time the |
| 318 | * notification is sent, even if it has not been canceled before that. |
| 319 | */ |
| 320 | public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008; |
| 321 | |
| 322 | /** |
| 323 | * Bit to be bitwise-ored into the {@link #flags} field that should be |
| 324 | * set if the notification should be canceled when it is clicked by the |
Daniel Sandler | 8aa9ae6 | 2012-12-04 23:31:47 -0500 | [diff] [blame] | 325 | * user. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 326 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 327 | */ |
| 328 | public static final int FLAG_AUTO_CANCEL = 0x00000010; |
| 329 | |
| 330 | /** |
| 331 | * Bit to be bitwise-ored into the {@link #flags} field that should be |
| 332 | * set if the notification should not be canceled when the user clicks |
| 333 | * the Clear all button. |
| 334 | */ |
| 335 | public static final int FLAG_NO_CLEAR = 0x00000020; |
| 336 | |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 337 | /** |
| 338 | * Bit to be bitwise-ored into the {@link #flags} field that should be |
| 339 | * set if this notification represents a currently running service. This |
| 340 | * will normally be set for you by {@link Service#startForeground}. |
| 341 | */ |
| 342 | public static final int FLAG_FOREGROUND_SERVICE = 0x00000040; |
| 343 | |
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 | * Obsolete flag indicating high-priority notifications; use the priority field instead. |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 346 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 347 | * @deprecated Use {@link #priority} with a positive value. |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 348 | */ |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 349 | public static final int FLAG_HIGH_PRIORITY = 0x00000080; |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 350 | |
Griff Hazen | dfcb080 | 2014-02-11 12:00:00 -0800 | [diff] [blame^] | 351 | /** |
| 352 | * Bit to be bitswise-ored into the {@link #flags} field that should be |
| 353 | * set if this notification is relevant to the current device only |
| 354 | * and it is not recommended that it bridge to other devices. |
| 355 | */ |
| 356 | public static final int FLAG_LOCAL_ONLY = 0x00000100; |
| 357 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 358 | public int flags; |
| 359 | |
| 360 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 361 | * Default notification {@link #priority}. If your application does not prioritize its own |
| 362 | * notifications, use this value for all notifications. |
| 363 | */ |
| 364 | public static final int PRIORITY_DEFAULT = 0; |
| 365 | |
| 366 | /** |
| 367 | * Lower {@link #priority}, for items that are less important. The UI may choose to show these |
| 368 | * items smaller, or at a different position in the list, compared with your app's |
| 369 | * {@link #PRIORITY_DEFAULT} items. |
| 370 | */ |
| 371 | public static final int PRIORITY_LOW = -1; |
| 372 | |
| 373 | /** |
| 374 | * Lowest {@link #priority}; these items might not be shown to the user except under special |
| 375 | * circumstances, such as detailed notification logs. |
| 376 | */ |
| 377 | public static final int PRIORITY_MIN = -2; |
| 378 | |
| 379 | /** |
| 380 | * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to |
| 381 | * show these items larger, or at a different position in notification lists, compared with |
| 382 | * your app's {@link #PRIORITY_DEFAULT} items. |
| 383 | */ |
| 384 | public static final int PRIORITY_HIGH = 1; |
| 385 | |
| 386 | /** |
| 387 | * Highest {@link #priority}, for your application's most important items that require the |
| 388 | * user's prompt attention or input. |
| 389 | */ |
| 390 | public static final int PRIORITY_MAX = 2; |
| 391 | |
| 392 | /** |
| 393 | * Relative priority for this notification. |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 394 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 395 | * Priority is an indication of how much of the user's valuable attention should be consumed by |
| 396 | * this notification. Low-priority notifications may be hidden from the user in certain |
| 397 | * situations, while the user might be interrupted for a higher-priority notification. The |
Daniel Sandler | 6738eee | 2012-11-16 12:03:32 -0500 | [diff] [blame] | 398 | * system will make a determination about how to interpret this priority when presenting |
| 399 | * the notification. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 400 | */ |
| 401 | public int priority; |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 402 | |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 403 | /** |
Daniel Sandler | d33b803 | 2012-05-10 11:41:48 -0400 | [diff] [blame] | 404 | * @hide |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 405 | * Notification type: incoming call (voice or video) or similar synchronous communication request. |
| 406 | */ |
| 407 | public static final String KIND_CALL = "android.call"; |
| 408 | |
| 409 | /** |
Daniel Sandler | d33b803 | 2012-05-10 11:41:48 -0400 | [diff] [blame] | 410 | * @hide |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 411 | * Notification type: incoming direct message (SMS, instant message, etc.). |
| 412 | */ |
| 413 | public static final String KIND_MESSAGE = "android.message"; |
| 414 | |
| 415 | /** |
Daniel Sandler | d33b803 | 2012-05-10 11:41:48 -0400 | [diff] [blame] | 416 | * @hide |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 417 | * Notification type: asynchronous bulk message (email). |
| 418 | */ |
| 419 | public static final String KIND_EMAIL = "android.email"; |
| 420 | |
| 421 | /** |
Daniel Sandler | d33b803 | 2012-05-10 11:41:48 -0400 | [diff] [blame] | 422 | * @hide |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 423 | * Notification type: calendar event. |
| 424 | */ |
| 425 | public static final String KIND_EVENT = "android.event"; |
| 426 | |
| 427 | /** |
Daniel Sandler | a90513d | 2012-06-04 02:11:17 -0400 | [diff] [blame] | 428 | * @hide |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 429 | * Notification type: promotion or advertisement. |
| 430 | */ |
| 431 | public static final String KIND_PROMO = "android.promo"; |
| 432 | |
| 433 | /** |
Daniel Sandler | d33b803 | 2012-05-10 11:41:48 -0400 | [diff] [blame] | 434 | * @hide |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 435 | * If this notification matches of one or more special types (see the <code>KIND_*</code> |
| 436 | * constants), add them here, best match first. |
| 437 | */ |
| 438 | public String[] kind; |
| 439 | |
| 440 | /** |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 441 | * Additional semantic data to be carried around with this Notification. |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 442 | * <p> |
| 443 | * The extras keys defined here are intended to capture the original inputs to {@link Builder} |
| 444 | * APIs, and are intended to be used by |
| 445 | * {@link android.service.notification.NotificationListenerService} implementations to extract |
| 446 | * detailed information from notification objects. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 447 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 448 | public Bundle extras = new Bundle(); |
| 449 | |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 450 | /** |
| 451 | * {@link #extras} key: this is the title of the notification, |
| 452 | * as supplied to {@link Builder#setContentTitle(CharSequence)}. |
| 453 | */ |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 454 | public static final String EXTRA_TITLE = "android.title"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 455 | |
| 456 | /** |
| 457 | * {@link #extras} key: this is the title of the notification when shown in expanded form, |
| 458 | * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}. |
| 459 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 460 | public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 461 | |
| 462 | /** |
| 463 | * {@link #extras} key: this is the main text payload, as supplied to |
| 464 | * {@link Builder#setContentText(CharSequence)}. |
| 465 | */ |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 466 | public static final String EXTRA_TEXT = "android.text"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 467 | |
| 468 | /** |
| 469 | * {@link #extras} key: this is a third line of text, as supplied to |
| 470 | * {@link Builder#setSubText(CharSequence)}. |
| 471 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 472 | public static final String EXTRA_SUB_TEXT = "android.subText"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 473 | |
| 474 | /** |
| 475 | * {@link #extras} key: this is a small piece of additional text as supplied to |
| 476 | * {@link Builder#setContentInfo(CharSequence)}. |
| 477 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 478 | public static final String EXTRA_INFO_TEXT = "android.infoText"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 479 | |
| 480 | /** |
| 481 | * {@link #extras} key: this is a line of summary information intended to be shown |
| 482 | * alongside expanded notifications, as supplied to (e.g.) |
| 483 | * {@link BigTextStyle#setSummaryText(CharSequence)}. |
| 484 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 485 | public static final String EXTRA_SUMMARY_TEXT = "android.summaryText"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 486 | |
| 487 | /** |
| 488 | * {@link #extras} key: this is the resource ID of the notification's main small icon, as |
| 489 | * supplied to {@link Builder#setSmallIcon(int)}. |
| 490 | */ |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 491 | public static final String EXTRA_SMALL_ICON = "android.icon"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 492 | |
| 493 | /** |
| 494 | * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the |
| 495 | * notification payload, as |
| 496 | * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}. |
| 497 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 498 | public static final String EXTRA_LARGE_ICON = "android.largeIcon"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 499 | |
| 500 | /** |
| 501 | * {@link #extras} key: this is a bitmap to be used instead of the one from |
| 502 | * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is |
| 503 | * shown in its expanded form, as supplied to |
| 504 | * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}. |
| 505 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 506 | public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 507 | |
| 508 | /** |
| 509 | * {@link #extras} key: this is the progress value supplied to |
| 510 | * {@link Builder#setProgress(int, int, boolean)}. |
| 511 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 512 | public static final String EXTRA_PROGRESS = "android.progress"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 513 | |
| 514 | /** |
| 515 | * {@link #extras} key: this is the maximum value supplied to |
| 516 | * {@link Builder#setProgress(int, int, boolean)}. |
| 517 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 518 | public static final String EXTRA_PROGRESS_MAX = "android.progressMax"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 519 | |
| 520 | /** |
| 521 | * {@link #extras} key: whether the progress bar is indeterminate, supplied to |
| 522 | * {@link Builder#setProgress(int, int, boolean)}. |
| 523 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 524 | public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 525 | |
| 526 | /** |
| 527 | * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically |
| 528 | * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to |
| 529 | * {@link Builder#setUsesChronometer(boolean)}. |
| 530 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 531 | public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 532 | |
| 533 | /** |
| 534 | * {@link #extras} key: whether {@link #when} should be shown, |
| 535 | * as supplied to {@link Builder#setShowWhen(boolean)}. |
| 536 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 537 | public static final String EXTRA_SHOW_WHEN = "android.showWhen"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 538 | |
| 539 | /** |
| 540 | * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded |
| 541 | * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}. |
| 542 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 543 | public static final String EXTRA_PICTURE = "android.picture"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 544 | |
| 545 | /** |
| 546 | * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded |
| 547 | * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}. |
| 548 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 549 | public static final String EXTRA_TEXT_LINES = "android.textLines"; |
| 550 | |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 551 | /** |
| 552 | * {@link #extras} key: An array of people that this notification relates to, specified |
| 553 | * by contacts provider contact URI. |
| 554 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 555 | public static final String EXTRA_PEOPLE = "android.people"; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 556 | |
| 557 | /** |
Scott Greenwald | 9a05b31 | 2013-06-28 00:37:54 -0400 | [diff] [blame] | 558 | * @hide |
| 559 | * Extra added by NotificationManagerService to indicate whether a NotificationScorer |
| 560 | * modified the Notifications's score. |
| 561 | */ |
| 562 | public static final String EXTRA_SCORE_MODIFIED = "android.scoreModified"; |
| 563 | |
| 564 | /** |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 565 | * Not used. |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 566 | * @hide |
| 567 | */ |
| 568 | public static final String EXTRA_AS_HEADS_UP = "headsup"; |
| 569 | |
| 570 | /** |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 571 | * Value for {@link #EXTRA_AS_HEADS_UP}. |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 572 | * @hide |
| 573 | */ |
| 574 | public static final int HEADS_UP_NEVER = 0; |
| 575 | |
| 576 | /** |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 577 | * Default value for {@link #EXTRA_AS_HEADS_UP}. |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 578 | * @hide |
| 579 | */ |
| 580 | public static final int HEADS_UP_ALLOWED = 1; |
| 581 | |
| 582 | /** |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 583 | * Value for {@link #EXTRA_AS_HEADS_UP}. |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 584 | * @hide |
| 585 | */ |
| 586 | public static final int HEADS_UP_REQUESTED = 2; |
| 587 | |
| 588 | /** |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 589 | * Structure to encapsulate a named action that can be shown as part of this notification. |
| 590 | * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is |
| 591 | * selected by the user. |
| 592 | * <p> |
| 593 | * Apps should use {@link Builder#addAction(int, CharSequence, PendingIntent)} to create and |
| 594 | * attach actions. |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 595 | */ |
Daniel Sandler | ea2a317 | 2013-02-20 22:24:20 -0500 | [diff] [blame] | 596 | public static class Action implements Parcelable { |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 597 | /** |
| 598 | * Small icon representing the action. |
| 599 | */ |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 600 | public int icon; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 601 | /** |
| 602 | * Title of the action. |
| 603 | */ |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 604 | public CharSequence title; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 605 | /** |
| 606 | * Intent to send when the user invokes this action. May be null, in which case the action |
| 607 | * may be rendered in a disabled presentation by the system UI. |
| 608 | */ |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 609 | public PendingIntent actionIntent; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 610 | |
| 611 | private Action() { } |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 612 | private Action(Parcel in) { |
| 613 | icon = in.readInt(); |
| 614 | title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); |
| 615 | if (in.readInt() == 1) { |
| 616 | actionIntent = PendingIntent.CREATOR.createFromParcel(in); |
| 617 | } |
| 618 | } |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 619 | /** |
| 620 | * Use {@link Builder#addAction(int, CharSequence, PendingIntent)}. |
| 621 | */ |
| 622 | public Action(int icon, CharSequence title, PendingIntent intent) { |
| 623 | this.icon = icon; |
| 624 | this.title = title; |
| 625 | this.actionIntent = intent; |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 626 | } |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 627 | |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 628 | @Override |
| 629 | public Action clone() { |
| 630 | return new Action( |
| 631 | this.icon, |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 632 | this.title, |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 633 | this.actionIntent // safe to alias |
| 634 | ); |
| 635 | } |
| 636 | @Override |
| 637 | public int describeContents() { |
| 638 | return 0; |
| 639 | } |
| 640 | @Override |
| 641 | public void writeToParcel(Parcel out, int flags) { |
| 642 | out.writeInt(icon); |
| 643 | TextUtils.writeToParcel(title, out, flags); |
| 644 | if (actionIntent != null) { |
| 645 | out.writeInt(1); |
| 646 | actionIntent.writeToParcel(out, flags); |
| 647 | } else { |
| 648 | out.writeInt(0); |
| 649 | } |
| 650 | } |
| 651 | public static final Parcelable.Creator<Action> CREATOR |
| 652 | = new Parcelable.Creator<Action>() { |
| 653 | public Action createFromParcel(Parcel in) { |
| 654 | return new Action(in); |
| 655 | } |
| 656 | public Action[] newArray(int size) { |
| 657 | return new Action[size]; |
| 658 | } |
| 659 | }; |
| 660 | } |
| 661 | |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 662 | /** |
| 663 | * Array of all {@link Action} structures attached to this notification by |
| 664 | * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of |
| 665 | * {@link android.service.notification.NotificationListenerService} that provide an alternative |
| 666 | * interface for invoking actions. |
| 667 | */ |
Daniel Sandler | ea2a317 | 2013-02-20 22:24:20 -0500 | [diff] [blame] | 668 | public Action[] actions; |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 669 | |
| 670 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 671 | * Constructs a Notification object with default values. |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 672 | * You might want to consider using {@link Builder} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 673 | */ |
| 674 | public Notification() |
| 675 | { |
| 676 | this.when = System.currentTimeMillis(); |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 677 | this.priority = PRIORITY_DEFAULT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 681 | * @hide |
| 682 | */ |
| 683 | public Notification(Context context, int icon, CharSequence tickerText, long when, |
| 684 | CharSequence contentTitle, CharSequence contentText, Intent contentIntent) |
| 685 | { |
| 686 | this.when = when; |
| 687 | this.icon = icon; |
| 688 | this.tickerText = tickerText; |
| 689 | setLatestEventInfo(context, contentTitle, contentText, |
| 690 | PendingIntent.getActivity(context, 0, contentIntent, 0)); |
| 691 | } |
| 692 | |
| 693 | /** |
| 694 | * Constructs a Notification object with the information needed to |
| 695 | * have a status bar icon without the standard expanded view. |
| 696 | * |
| 697 | * @param icon The resource id of the icon to put in the status bar. |
| 698 | * @param tickerText The text that flows by in the status bar when the notification first |
| 699 | * activates. |
| 700 | * @param when The time to show in the time field. In the System.currentTimeMillis |
| 701 | * timebase. |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 702 | * |
| 703 | * @deprecated Use {@link Builder} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 704 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 705 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 706 | public Notification(int icon, CharSequence tickerText, long when) |
| 707 | { |
| 708 | this.icon = icon; |
| 709 | this.tickerText = tickerText; |
| 710 | this.when = when; |
| 711 | } |
| 712 | |
| 713 | /** |
| 714 | * Unflatten the notification from a parcel. |
| 715 | */ |
| 716 | public Notification(Parcel parcel) |
| 717 | { |
| 718 | int version = parcel.readInt(); |
| 719 | |
| 720 | when = parcel.readLong(); |
| 721 | icon = parcel.readInt(); |
| 722 | number = parcel.readInt(); |
| 723 | if (parcel.readInt() != 0) { |
| 724 | contentIntent = PendingIntent.CREATOR.createFromParcel(parcel); |
| 725 | } |
| 726 | if (parcel.readInt() != 0) { |
| 727 | deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel); |
| 728 | } |
| 729 | if (parcel.readInt() != 0) { |
| 730 | tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel); |
| 731 | } |
| 732 | if (parcel.readInt() != 0) { |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 733 | tickerView = RemoteViews.CREATOR.createFromParcel(parcel); |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 734 | } |
| 735 | if (parcel.readInt() != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 736 | contentView = RemoteViews.CREATOR.createFromParcel(parcel); |
| 737 | } |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 738 | if (parcel.readInt() != 0) { |
| 739 | largeIcon = Bitmap.CREATOR.createFromParcel(parcel); |
| 740 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 741 | defaults = parcel.readInt(); |
| 742 | flags = parcel.readInt(); |
| 743 | if (parcel.readInt() != 0) { |
| 744 | sound = Uri.CREATOR.createFromParcel(parcel); |
| 745 | } |
| 746 | |
| 747 | audioStreamType = parcel.readInt(); |
| 748 | vibrate = parcel.createLongArray(); |
| 749 | ledARGB = parcel.readInt(); |
| 750 | ledOnMS = parcel.readInt(); |
| 751 | ledOffMS = parcel.readInt(); |
| 752 | iconLevel = parcel.readInt(); |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 753 | |
| 754 | if (parcel.readInt() != 0) { |
| 755 | fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel); |
| 756 | } |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 757 | |
| 758 | priority = parcel.readInt(); |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 759 | |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 760 | kind = parcel.createStringArray(); // may set kind to null |
| 761 | |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 762 | extras = parcel.readBundle(); // may be null |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 763 | |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 764 | actions = parcel.createTypedArray(Action.CREATOR); // may be null |
| 765 | |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 766 | if (parcel.readInt() != 0) { |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 767 | bigContentView = RemoteViews.CREATOR.createFromParcel(parcel); |
| 768 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 769 | } |
| 770 | |
Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 771 | @Override |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 772 | public Notification clone() { |
| 773 | Notification that = new Notification(); |
Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 774 | cloneInto(that, true); |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 775 | return that; |
| 776 | } |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 777 | |
Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 778 | /** |
| 779 | * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members |
| 780 | * of this into that. |
| 781 | * @hide |
| 782 | */ |
| 783 | public void cloneInto(Notification that, boolean heavy) { |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 784 | that.when = this.when; |
| 785 | that.icon = this.icon; |
| 786 | that.number = this.number; |
| 787 | |
| 788 | // PendingIntents are global, so there's no reason (or way) to clone them. |
| 789 | that.contentIntent = this.contentIntent; |
| 790 | that.deleteIntent = this.deleteIntent; |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 791 | that.fullScreenIntent = this.fullScreenIntent; |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 792 | |
| 793 | if (this.tickerText != null) { |
| 794 | that.tickerText = this.tickerText.toString(); |
| 795 | } |
Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 796 | if (heavy && this.tickerView != null) { |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 797 | that.tickerView = this.tickerView.clone(); |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 798 | } |
Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 799 | if (heavy && this.contentView != null) { |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 800 | that.contentView = this.contentView.clone(); |
| 801 | } |
Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 802 | if (heavy && this.largeIcon != null) { |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 803 | that.largeIcon = Bitmap.createBitmap(this.largeIcon); |
| 804 | } |
Jozef BABJAK | a8b9183 | 2011-02-22 08:05:08 +0100 | [diff] [blame] | 805 | that.iconLevel = this.iconLevel; |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 806 | that.sound = this.sound; // android.net.Uri is immutable |
| 807 | that.audioStreamType = this.audioStreamType; |
| 808 | |
| 809 | final long[] vibrate = this.vibrate; |
| 810 | if (vibrate != null) { |
| 811 | final int N = vibrate.length; |
| 812 | final long[] vib = that.vibrate = new long[N]; |
| 813 | System.arraycopy(vibrate, 0, vib, 0, N); |
| 814 | } |
| 815 | |
| 816 | that.ledARGB = this.ledARGB; |
| 817 | that.ledOnMS = this.ledOnMS; |
| 818 | that.ledOffMS = this.ledOffMS; |
| 819 | that.defaults = this.defaults; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 820 | |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 821 | that.flags = this.flags; |
| 822 | |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 823 | that.priority = this.priority; |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 824 | |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 825 | final String[] thiskind = this.kind; |
| 826 | if (thiskind != null) { |
| 827 | final int N = thiskind.length; |
| 828 | final String[] thatkind = that.kind = new String[N]; |
| 829 | System.arraycopy(thiskind, 0, thatkind, 0, N); |
| 830 | } |
| 831 | |
| 832 | if (this.extras != null) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 833 | try { |
| 834 | that.extras = new Bundle(this.extras); |
| 835 | // will unparcel |
| 836 | that.extras.size(); |
| 837 | } catch (BadParcelableException e) { |
| 838 | Log.e(TAG, "could not unparcel extras from notification: " + this, e); |
| 839 | that.extras = null; |
| 840 | } |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 841 | } |
| 842 | |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 843 | if (this.actions != null) { |
| 844 | that.actions = new Action[this.actions.length]; |
| 845 | for(int i=0; i<this.actions.length; i++) { |
| 846 | that.actions[i] = this.actions[i].clone(); |
| 847 | } |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 848 | } |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 849 | |
Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 850 | if (heavy && this.bigContentView != null) { |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 851 | that.bigContentView = this.bigContentView.clone(); |
| 852 | } |
Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 853 | |
| 854 | if (!heavy) { |
| 855 | that.lightenPayload(); // will clean out extras |
| 856 | } |
| 857 | } |
| 858 | |
| 859 | /** |
| 860 | * Removes heavyweight parts of the Notification object for archival or for sending to |
| 861 | * listeners when the full contents are not necessary. |
| 862 | * @hide |
| 863 | */ |
| 864 | public final void lightenPayload() { |
| 865 | tickerView = null; |
| 866 | contentView = null; |
| 867 | bigContentView = null; |
| 868 | largeIcon = null; |
| 869 | if (extras != null) { |
| 870 | extras.remove(Notification.EXTRA_LARGE_ICON); |
| 871 | extras.remove(Notification.EXTRA_LARGE_ICON_BIG); |
| 872 | extras.remove(Notification.EXTRA_PICTURE); |
| 873 | } |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 874 | } |
| 875 | |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 876 | /** |
| 877 | * Make sure this CharSequence is safe to put into a bundle, which basically |
| 878 | * means it had better not be some custom Parcelable implementation. |
| 879 | * @hide |
| 880 | */ |
| 881 | public static CharSequence safeCharSequence(CharSequence cs) { |
| 882 | if (cs instanceof Parcelable) { |
| 883 | Log.e(TAG, "warning: " + cs.getClass().getCanonicalName() |
| 884 | + " instance is a custom Parcelable and not allowed in Notification"); |
| 885 | return cs.toString(); |
| 886 | } |
| 887 | |
| 888 | return cs; |
| 889 | } |
| 890 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 891 | public int describeContents() { |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | /** |
| 896 | * Flatten this notification from a parcel. |
| 897 | */ |
| 898 | public void writeToParcel(Parcel parcel, int flags) |
| 899 | { |
| 900 | parcel.writeInt(1); |
| 901 | |
| 902 | parcel.writeLong(when); |
| 903 | parcel.writeInt(icon); |
| 904 | parcel.writeInt(number); |
| 905 | if (contentIntent != null) { |
| 906 | parcel.writeInt(1); |
| 907 | contentIntent.writeToParcel(parcel, 0); |
| 908 | } else { |
| 909 | parcel.writeInt(0); |
| 910 | } |
| 911 | if (deleteIntent != null) { |
| 912 | parcel.writeInt(1); |
| 913 | deleteIntent.writeToParcel(parcel, 0); |
| 914 | } else { |
| 915 | parcel.writeInt(0); |
| 916 | } |
| 917 | if (tickerText != null) { |
| 918 | parcel.writeInt(1); |
| 919 | TextUtils.writeToParcel(tickerText, parcel, flags); |
| 920 | } else { |
| 921 | parcel.writeInt(0); |
| 922 | } |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 923 | if (tickerView != null) { |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 924 | parcel.writeInt(1); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 925 | tickerView.writeToParcel(parcel, 0); |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 926 | } else { |
| 927 | parcel.writeInt(0); |
| 928 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 929 | if (contentView != null) { |
| 930 | parcel.writeInt(1); |
| 931 | contentView.writeToParcel(parcel, 0); |
| 932 | } else { |
| 933 | parcel.writeInt(0); |
| 934 | } |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 935 | if (largeIcon != null) { |
| 936 | parcel.writeInt(1); |
| 937 | largeIcon.writeToParcel(parcel, 0); |
| 938 | } else { |
| 939 | parcel.writeInt(0); |
| 940 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 941 | |
| 942 | parcel.writeInt(defaults); |
| 943 | parcel.writeInt(this.flags); |
| 944 | |
| 945 | if (sound != null) { |
| 946 | parcel.writeInt(1); |
| 947 | sound.writeToParcel(parcel, 0); |
| 948 | } else { |
| 949 | parcel.writeInt(0); |
| 950 | } |
| 951 | parcel.writeInt(audioStreamType); |
| 952 | parcel.writeLongArray(vibrate); |
| 953 | parcel.writeInt(ledARGB); |
| 954 | parcel.writeInt(ledOnMS); |
| 955 | parcel.writeInt(ledOffMS); |
| 956 | parcel.writeInt(iconLevel); |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 957 | |
| 958 | if (fullScreenIntent != null) { |
| 959 | parcel.writeInt(1); |
| 960 | fullScreenIntent.writeToParcel(parcel, 0); |
| 961 | } else { |
| 962 | parcel.writeInt(0); |
| 963 | } |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 964 | |
| 965 | parcel.writeInt(priority); |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 966 | |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 967 | parcel.writeStringArray(kind); // ok for null |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 968 | |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 969 | parcel.writeBundle(extras); // null ok |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 970 | |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 971 | parcel.writeTypedArray(actions, 0); // null ok |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 972 | |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 973 | if (bigContentView != null) { |
| 974 | parcel.writeInt(1); |
| 975 | bigContentView.writeToParcel(parcel, 0); |
| 976 | } else { |
| 977 | parcel.writeInt(0); |
| 978 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | /** |
| 982 | * Parcelable.Creator that instantiates Notification objects |
| 983 | */ |
| 984 | public static final Parcelable.Creator<Notification> CREATOR |
| 985 | = new Parcelable.Creator<Notification>() |
| 986 | { |
| 987 | public Notification createFromParcel(Parcel parcel) |
| 988 | { |
| 989 | return new Notification(parcel); |
| 990 | } |
| 991 | |
| 992 | public Notification[] newArray(int size) |
| 993 | { |
| 994 | return new Notification[size]; |
| 995 | } |
| 996 | }; |
| 997 | |
| 998 | /** |
| 999 | * Sets the {@link #contentView} field to be a view with the standard "Latest Event" |
| 1000 | * layout. |
| 1001 | * |
| 1002 | * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields |
| 1003 | * in the view.</p> |
| 1004 | * @param context The context for your application / activity. |
| 1005 | * @param contentTitle The title that goes in the expanded entry. |
| 1006 | * @param contentText The text that goes in the expanded entry. |
| 1007 | * @param contentIntent The intent to launch when the user clicks the expanded notification. |
| 1008 | * If this is an activity, it must include the |
| 1009 | * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 1010 | * that you take care of task management as described in the |
| 1011 | * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back |
| 1012 | * Stack</a> document. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1013 | * |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1014 | * @deprecated Use {@link Builder} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1015 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1016 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1017 | public void setLatestEventInfo(Context context, |
| 1018 | CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) { |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 1019 | Notification.Builder builder = new Notification.Builder(context); |
| 1020 | |
| 1021 | // First, ensure that key pieces of information that may have been set directly |
| 1022 | // are preserved |
| 1023 | builder.setWhen(this.when); |
| 1024 | builder.setSmallIcon(this.icon); |
| 1025 | builder.setPriority(this.priority); |
| 1026 | builder.setTicker(this.tickerText); |
| 1027 | builder.setNumber(this.number); |
| 1028 | builder.mFlags = this.flags; |
| 1029 | builder.setSound(this.sound, this.audioStreamType); |
| 1030 | builder.setDefaults(this.defaults); |
| 1031 | builder.setVibrate(this.vibrate); |
| 1032 | |
| 1033 | // now apply the latestEventInfo fields |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1034 | if (contentTitle != null) { |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 1035 | builder.setContentTitle(contentTitle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1036 | } |
| 1037 | if (contentText != null) { |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 1038 | builder.setContentText(contentText); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1039 | } |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 1040 | builder.setContentIntent(contentIntent); |
| 1041 | builder.buildInto(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1042 | } |
| 1043 | |
| 1044 | @Override |
| 1045 | public String toString() { |
| 1046 | StringBuilder sb = new StringBuilder(); |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1047 | sb.append("Notification(pri="); |
| 1048 | sb.append(priority); |
| 1049 | sb.append(" contentView="); |
Joe Onorato | c9596d6 | 2011-01-12 17:03:11 -0800 | [diff] [blame] | 1050 | if (contentView != null) { |
| 1051 | sb.append(contentView.getPackage()); |
| 1052 | sb.append("/0x"); |
| 1053 | sb.append(Integer.toHexString(contentView.getLayoutId())); |
| 1054 | } else { |
| 1055 | sb.append("null"); |
| 1056 | } |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1057 | // 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] | 1058 | sb.append(" vibrate="); |
Daniel Sandler | 6738eee | 2012-11-16 12:03:32 -0500 | [diff] [blame] | 1059 | if ((this.defaults & DEFAULT_VIBRATE) != 0) { |
| 1060 | sb.append("default"); |
| 1061 | } else if (this.vibrate != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1062 | int N = this.vibrate.length-1; |
| 1063 | sb.append("["); |
| 1064 | for (int i=0; i<N; i++) { |
| 1065 | sb.append(this.vibrate[i]); |
| 1066 | sb.append(','); |
| 1067 | } |
Simon Schoar | 8cf97d9 | 2009-06-10 22:08:37 +0200 | [diff] [blame] | 1068 | if (N != -1) { |
| 1069 | sb.append(this.vibrate[N]); |
| 1070 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1071 | sb.append("]"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1072 | } else { |
| 1073 | sb.append("null"); |
| 1074 | } |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1075 | sb.append(" sound="); |
Daniel Sandler | 6738eee | 2012-11-16 12:03:32 -0500 | [diff] [blame] | 1076 | if ((this.defaults & DEFAULT_SOUND) != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1077 | sb.append("default"); |
Daniel Sandler | 6738eee | 2012-11-16 12:03:32 -0500 | [diff] [blame] | 1078 | } else if (this.sound != null) { |
| 1079 | sb.append(this.sound.toString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1080 | } else { |
| 1081 | sb.append("null"); |
| 1082 | } |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1083 | sb.append(" defaults=0x"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1084 | sb.append(Integer.toHexString(this.defaults)); |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1085 | sb.append(" flags=0x"); |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 1086 | sb.append(Integer.toHexString(this.flags)); |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1087 | sb.append(" kind=["); |
| 1088 | if (this.kind == null) { |
| 1089 | sb.append("null"); |
| 1090 | } else { |
| 1091 | for (int i=0; i<this.kind.length; i++) { |
| 1092 | if (i>0) sb.append(","); |
| 1093 | sb.append(this.kind[i]); |
| 1094 | } |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 1095 | } |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 1096 | sb.append("]"); |
| 1097 | if (actions != null) { |
| 1098 | sb.append(" "); |
| 1099 | sb.append(actions.length); |
| 1100 | sb.append(" action"); |
| 1101 | if (actions.length > 1) sb.append("s"); |
| 1102 | } |
| 1103 | sb.append(")"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1104 | return sb.toString(); |
| 1105 | } |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1106 | |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 1107 | /** {@hide} */ |
| 1108 | public void setUser(UserHandle user) { |
Amith Yamasani | ecbd68b | 2012-11-02 12:17:19 -0700 | [diff] [blame] | 1109 | if (user.getIdentifier() == UserHandle.USER_ALL) { |
| 1110 | user = UserHandle.OWNER; |
| 1111 | } |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 1112 | if (tickerView != null) { |
| 1113 | tickerView.setUser(user); |
| 1114 | } |
| 1115 | if (contentView != null) { |
| 1116 | contentView.setUser(user); |
| 1117 | } |
| 1118 | if (bigContentView != null) { |
| 1119 | bigContentView.setUser(user); |
| 1120 | } |
| 1121 | } |
| 1122 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1123 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1124 | * Builder class for {@link Notification} objects. |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 1125 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1126 | * Provides a convenient way to set the various fields of a {@link Notification} and generate |
Scott Main | 183bf11 | 2012-08-13 19:12:13 -0700 | [diff] [blame] | 1127 | * content views using the platform's notification layout template. If your app supports |
| 1128 | * versions of Android as old as API level 4, you can instead use |
| 1129 | * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder}, |
| 1130 | * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support |
| 1131 | * library</a>. |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 1132 | * |
Scott Main | 183bf11 | 2012-08-13 19:12:13 -0700 | [diff] [blame] | 1133 | * <p>Example: |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 1134 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1135 | * <pre class="prettyprint"> |
Scott Main | 183bf11 | 2012-08-13 19:12:13 -0700 | [diff] [blame] | 1136 | * Notification noti = new Notification.Builder(mContext) |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1137 | * .setContentTitle("New mail from " + sender.toString()) |
| 1138 | * .setContentText(subject) |
| 1139 | * .setSmallIcon(R.drawable.new_mail) |
| 1140 | * .setLargeIcon(aBitmap) |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 1141 | * .build(); |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1142 | * </pre> |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1143 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1144 | public static class Builder { |
Daniel Sandler | 602ad1c | 2012-06-12 16:06:27 -0400 | [diff] [blame] | 1145 | private static final int MAX_ACTION_BUTTONS = 3; |
Daniel Sandler | 8680bf8 | 2012-05-15 16:52:52 -0400 | [diff] [blame] | 1146 | |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1147 | private Context mContext; |
| 1148 | |
| 1149 | private long mWhen; |
| 1150 | private int mSmallIcon; |
| 1151 | private int mSmallIconLevel; |
Joe Onorato | 8595a3d | 2010-11-19 18:12:07 -0800 | [diff] [blame] | 1152 | private int mNumber; |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1153 | private CharSequence mContentTitle; |
| 1154 | private CharSequence mContentText; |
| 1155 | private CharSequence mContentInfo; |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 1156 | private CharSequence mSubText; |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1157 | private PendingIntent mContentIntent; |
| 1158 | private RemoteViews mContentView; |
| 1159 | private PendingIntent mDeleteIntent; |
| 1160 | private PendingIntent mFullScreenIntent; |
| 1161 | private CharSequence mTickerText; |
| 1162 | private RemoteViews mTickerView; |
| 1163 | private Bitmap mLargeIcon; |
| 1164 | private Uri mSound; |
| 1165 | private int mAudioStreamType; |
| 1166 | private long[] mVibrate; |
| 1167 | private int mLedArgb; |
| 1168 | private int mLedOnMs; |
| 1169 | private int mLedOffMs; |
| 1170 | private int mDefaults; |
| 1171 | private int mFlags; |
Jeff Sharkey | 1c40013 | 2011-08-05 14:50:13 -0700 | [diff] [blame] | 1172 | private int mProgressMax; |
| 1173 | private int mProgress; |
| 1174 | private boolean mProgressIndeterminate; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1175 | private ArrayList<String> mKindList = new ArrayList<String>(1); |
| 1176 | private Bundle mExtras; |
| 1177 | private int mPriority; |
Daniel Sandler | 8680bf8 | 2012-05-15 16:52:52 -0400 | [diff] [blame] | 1178 | private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS); |
Daniel Sandler | a2985ed | 2012-04-03 16:42:00 -0400 | [diff] [blame] | 1179 | private boolean mUseChronometer; |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 1180 | private Style mStyle; |
Daniel Sandler | 0c89049 | 2012-09-12 17:23:10 -0700 | [diff] [blame] | 1181 | private boolean mShowWhen = true; |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1182 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1183 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1184 | * Constructs a new Builder with the defaults: |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1185 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1186 | |
| 1187 | * <table> |
| 1188 | * <tr><th align=right>priority</th> |
| 1189 | * <td>{@link #PRIORITY_DEFAULT}</td></tr> |
| 1190 | * <tr><th align=right>when</th> |
| 1191 | * <td>now ({@link System#currentTimeMillis()})</td></tr> |
| 1192 | * <tr><th align=right>audio stream</th> |
| 1193 | * <td>{@link #STREAM_DEFAULT}</td></tr> |
| 1194 | * </table> |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1195 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1196 | |
| 1197 | * @param context |
| 1198 | * A {@link Context} that will be used by the Builder to construct the |
| 1199 | * RemoteViews. The Context will not be held past the lifetime of this Builder |
| 1200 | * object. |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1201 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1202 | public Builder(Context context) { |
| 1203 | mContext = context; |
Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 1204 | |
| 1205 | // Set defaults to match the defaults of a Notification |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1206 | mWhen = System.currentTimeMillis(); |
Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 1207 | mAudioStreamType = STREAM_DEFAULT; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1208 | mPriority = PRIORITY_DEFAULT; |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1209 | } |
| 1210 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1211 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1212 | * Add a timestamp pertaining to the notification (usually the time the event occurred). |
Daniel Sandler | 0c89049 | 2012-09-12 17:23:10 -0700 | [diff] [blame] | 1213 | * It will be shown in the notification content view by default; use |
| 1214 | * {@link Builder#setShowWhen(boolean) setShowWhen} to control this. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1215 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1216 | * @see Notification#when |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1217 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1218 | public Builder setWhen(long when) { |
| 1219 | mWhen = when; |
| 1220 | return this; |
| 1221 | } |
| 1222 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1223 | /** |
Daniel Sandler | 0c89049 | 2012-09-12 17:23:10 -0700 | [diff] [blame] | 1224 | * Control whether the timestamp set with {@link Builder#setWhen(long) setWhen} is shown |
| 1225 | * in the content view. |
| 1226 | */ |
| 1227 | public Builder setShowWhen(boolean show) { |
| 1228 | mShowWhen = show; |
| 1229 | return this; |
| 1230 | } |
| 1231 | |
| 1232 | /** |
Daniel Sandler | d33b803 | 2012-05-10 11:41:48 -0400 | [diff] [blame] | 1233 | * Show the {@link Notification#when} field as a stopwatch. |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 1234 | * |
| 1235 | * Instead of presenting <code>when</code> as a timestamp, the notification will show an |
Daniel Sandler | d33b803 | 2012-05-10 11:41:48 -0400 | [diff] [blame] | 1236 | * automatically updating display of the minutes and seconds since <code>when</code>. |
Daniel Sandler | a2985ed | 2012-04-03 16:42:00 -0400 | [diff] [blame] | 1237 | * |
Daniel Sandler | d33b803 | 2012-05-10 11:41:48 -0400 | [diff] [blame] | 1238 | * Useful when showing an elapsed time (like an ongoing phone call). |
| 1239 | * |
| 1240 | * @see android.widget.Chronometer |
Daniel Sandler | a2985ed | 2012-04-03 16:42:00 -0400 | [diff] [blame] | 1241 | * @see Notification#when |
| 1242 | */ |
| 1243 | public Builder setUsesChronometer(boolean b) { |
| 1244 | mUseChronometer = b; |
| 1245 | return this; |
| 1246 | } |
| 1247 | |
| 1248 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1249 | * Set the small icon resource, which will be used to represent the notification in the |
| 1250 | * status bar. |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1251 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1252 | |
| 1253 | * The platform template for the expanded view will draw this icon in the left, unless a |
| 1254 | * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small |
| 1255 | * icon will be moved to the right-hand side. |
| 1256 | * |
| 1257 | |
| 1258 | * @param icon |
| 1259 | * A resource ID in the application's package of the drawable to use. |
| 1260 | * @see Notification#icon |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1261 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1262 | public Builder setSmallIcon(int icon) { |
| 1263 | mSmallIcon = icon; |
| 1264 | return this; |
| 1265 | } |
| 1266 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1267 | /** |
| 1268 | * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional |
| 1269 | * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable |
| 1270 | * LevelListDrawable}. |
| 1271 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1272 | * @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] | 1273 | * @param level The level to use for the icon. |
| 1274 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1275 | * @see Notification#icon |
| 1276 | * @see Notification#iconLevel |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1277 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1278 | public Builder setSmallIcon(int icon, int level) { |
| 1279 | mSmallIcon = icon; |
| 1280 | mSmallIconLevel = level; |
| 1281 | return this; |
| 1282 | } |
| 1283 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1284 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1285 | * Set the first line of text in the platform notification template. |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1286 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1287 | public Builder setContentTitle(CharSequence title) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 1288 | mContentTitle = safeCharSequence(title); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1289 | return this; |
| 1290 | } |
| 1291 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1292 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1293 | * Set the second line of text in the platform notification template. |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1294 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1295 | public Builder setContentText(CharSequence text) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 1296 | mContentText = safeCharSequence(text); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1297 | return this; |
| 1298 | } |
| 1299 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1300 | /** |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 1301 | * Set the third line of text in the platform notification template. |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 1302 | * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the |
| 1303 | * same location in the standard template. |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 1304 | */ |
| 1305 | public Builder setSubText(CharSequence text) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 1306 | mSubText = safeCharSequence(text); |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 1307 | return this; |
| 1308 | } |
| 1309 | |
| 1310 | /** |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1311 | * Set the large number at the right-hand side of the notification. This is |
| 1312 | * equivalent to setContentInfo, although it might show the number in a different |
| 1313 | * font size for readability. |
| 1314 | */ |
Joe Onorato | 8595a3d | 2010-11-19 18:12:07 -0800 | [diff] [blame] | 1315 | public Builder setNumber(int number) { |
| 1316 | mNumber = number; |
| 1317 | return this; |
| 1318 | } |
| 1319 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1320 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1321 | * A small piece of additional information pertaining to this notification. |
| 1322 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1323 | * The platform template will draw this on the last line of the notification, at the far |
| 1324 | * 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] | 1325 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1326 | public Builder setContentInfo(CharSequence info) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 1327 | mContentInfo = safeCharSequence(info); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1328 | return this; |
| 1329 | } |
| 1330 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1331 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1332 | * Set the progress this notification represents. |
| 1333 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1334 | * The platform template will represent this using a {@link ProgressBar}. |
Jeff Sharkey | 1c40013 | 2011-08-05 14:50:13 -0700 | [diff] [blame] | 1335 | */ |
| 1336 | public Builder setProgress(int max, int progress, boolean indeterminate) { |
| 1337 | mProgressMax = max; |
| 1338 | mProgress = progress; |
| 1339 | mProgressIndeterminate = indeterminate; |
| 1340 | return this; |
| 1341 | } |
| 1342 | |
| 1343 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1344 | * Supply a custom RemoteViews to use instead of the platform template. |
| 1345 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1346 | * @see Notification#contentView |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1347 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1348 | public Builder setContent(RemoteViews views) { |
| 1349 | mContentView = views; |
| 1350 | return this; |
| 1351 | } |
| 1352 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1353 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1354 | * Supply a {@link PendingIntent} to be sent when the notification is clicked. |
| 1355 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1356 | * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you |
| 1357 | * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use |
| 1358 | * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)} |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1359 | * 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] | 1360 | * clickable buttons inside the notification view). |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1361 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1362 | * @see Notification#contentIntent Notification.contentIntent |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1363 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1364 | public Builder setContentIntent(PendingIntent intent) { |
| 1365 | mContentIntent = intent; |
| 1366 | return this; |
| 1367 | } |
| 1368 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1369 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1370 | * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user. |
| 1371 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1372 | * @see Notification#deleteIntent |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1373 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1374 | public Builder setDeleteIntent(PendingIntent intent) { |
| 1375 | mDeleteIntent = intent; |
| 1376 | return this; |
| 1377 | } |
| 1378 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1379 | /** |
| 1380 | * An intent to launch instead of posting the notification to the status bar. |
| 1381 | * Only for use with extremely high-priority notifications demanding the user's |
| 1382 | * <strong>immediate</strong> attention, such as an incoming phone call or |
| 1383 | * alarm clock that the user has explicitly set to a particular time. |
| 1384 | * If this facility is used for something else, please give the user an option |
| 1385 | * to turn it off and use a normal notification, as this can be extremely |
| 1386 | * disruptive. |
| 1387 | * |
| 1388 | * @param intent The pending intent to launch. |
| 1389 | * @param highPriority Passing true will cause this notification to be sent |
| 1390 | * even if other notifications are suppressed. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1391 | * |
| 1392 | * @see Notification#fullScreenIntent |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1393 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1394 | public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) { |
| 1395 | mFullScreenIntent = intent; |
| 1396 | setFlag(FLAG_HIGH_PRIORITY, highPriority); |
| 1397 | return this; |
| 1398 | } |
| 1399 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1400 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1401 | * 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] | 1402 | * arrives. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1403 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1404 | * @see Notification#tickerText |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1405 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1406 | public Builder setTicker(CharSequence tickerText) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 1407 | mTickerText = safeCharSequence(tickerText); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1408 | return this; |
| 1409 | } |
| 1410 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1411 | /** |
| 1412 | * Set the text that is displayed in the status bar when the notification first |
| 1413 | * arrives, and also a RemoteViews object that may be displayed instead on some |
| 1414 | * devices. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1415 | * |
| 1416 | * @see Notification#tickerText |
| 1417 | * @see Notification#tickerView |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1418 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1419 | public Builder setTicker(CharSequence tickerText, RemoteViews views) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 1420 | mTickerText = safeCharSequence(tickerText); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1421 | mTickerView = views; |
| 1422 | return this; |
| 1423 | } |
| 1424 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1425 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1426 | * Add a large icon to the notification (and the ticker on some devices). |
| 1427 | * |
| 1428 | * In the platform template, this image will be shown on the left of the notification view |
| 1429 | * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side). |
| 1430 | * |
| 1431 | * @see Notification#largeIcon |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1432 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1433 | public Builder setLargeIcon(Bitmap icon) { |
| 1434 | mLargeIcon = icon; |
| 1435 | return this; |
| 1436 | } |
| 1437 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1438 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1439 | * Set the sound to play. |
| 1440 | * |
| 1441 | * It will be played on the {@link #STREAM_DEFAULT default stream} for notifications. |
| 1442 | * |
| 1443 | * @see Notification#sound |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1444 | */ |
Joe Onorato | 52f80cd | 2010-11-21 15:34:48 -0800 | [diff] [blame] | 1445 | public Builder setSound(Uri sound) { |
| 1446 | mSound = sound; |
| 1447 | mAudioStreamType = STREAM_DEFAULT; |
| 1448 | return this; |
| 1449 | } |
| 1450 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1451 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1452 | * 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] | 1453 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1454 | * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants. |
| 1455 | * |
| 1456 | * @see Notification#sound |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1457 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1458 | public Builder setSound(Uri sound, int streamType) { |
| 1459 | mSound = sound; |
| 1460 | mAudioStreamType = streamType; |
| 1461 | return this; |
| 1462 | } |
| 1463 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1464 | /** |
| 1465 | * Set the vibration pattern to use. |
| 1466 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1467 | |
| 1468 | * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the |
| 1469 | * <code>pattern</code> parameter. |
| 1470 | * |
| 1471 | |
| 1472 | * @see Notification#vibrate |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1473 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1474 | public Builder setVibrate(long[] pattern) { |
| 1475 | mVibrate = pattern; |
| 1476 | return this; |
| 1477 | } |
| 1478 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1479 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1480 | * Set the desired color for the indicator LED on the device, as well as the |
| 1481 | * blink duty cycle (specified in milliseconds). |
| 1482 | * |
| 1483 | |
| 1484 | * Not all devices will honor all (or even any) of these values. |
| 1485 | * |
| 1486 | |
| 1487 | * @see Notification#ledARGB |
| 1488 | * @see Notification#ledOnMS |
| 1489 | * @see Notification#ledOffMS |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1490 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1491 | public Builder setLights(int argb, int onMs, int offMs) { |
| 1492 | mLedArgb = argb; |
| 1493 | mLedOnMs = onMs; |
| 1494 | mLedOffMs = offMs; |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1495 | return this; |
| 1496 | } |
| 1497 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1498 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1499 | * Set whether this is an "ongoing" notification. |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1500 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1501 | |
| 1502 | * Ongoing notifications cannot be dismissed by the user, so your application or service |
| 1503 | * must take care of canceling them. |
| 1504 | * |
| 1505 | |
| 1506 | * They are typically used to indicate a background task that the user is actively engaged |
| 1507 | * with (e.g., playing music) or is pending in some way and therefore occupying the device |
| 1508 | * (e.g., a file download, sync operation, active network connection). |
| 1509 | * |
| 1510 | |
| 1511 | * @see Notification#FLAG_ONGOING_EVENT |
| 1512 | * @see Service#setForeground(boolean) |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1513 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1514 | public Builder setOngoing(boolean ongoing) { |
| 1515 | setFlag(FLAG_ONGOING_EVENT, ongoing); |
| 1516 | return this; |
| 1517 | } |
| 1518 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1519 | /** |
| 1520 | * Set this flag if you would only like the sound, vibrate |
| 1521 | * and ticker to be played if the notification is not already showing. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1522 | * |
| 1523 | * @see Notification#FLAG_ONLY_ALERT_ONCE |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1524 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1525 | public Builder setOnlyAlertOnce(boolean onlyAlertOnce) { |
| 1526 | setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce); |
| 1527 | return this; |
| 1528 | } |
| 1529 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1530 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1531 | * Make this notification automatically dismissed when the user touches it. The |
| 1532 | * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens. |
| 1533 | * |
| 1534 | * @see Notification#FLAG_AUTO_CANCEL |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1535 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1536 | public Builder setAutoCancel(boolean autoCancel) { |
Joe Onorato | 281d83f | 2011-01-04 17:13:10 -0800 | [diff] [blame] | 1537 | setFlag(FLAG_AUTO_CANCEL, autoCancel); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1538 | return this; |
| 1539 | } |
| 1540 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1541 | /** |
Griff Hazen | dfcb080 | 2014-02-11 12:00:00 -0800 | [diff] [blame^] | 1542 | * Set whether or not this notification should not bridge to other devices. |
| 1543 | * |
| 1544 | * <p>Some notifications can be bridged to other devices for remote display. |
| 1545 | * This hint can be set to recommend this notification not be bridged. |
| 1546 | */ |
| 1547 | public Builder setLocalOnly(boolean localOnly) { |
| 1548 | setFlag(FLAG_LOCAL_ONLY, localOnly); |
| 1549 | return this; |
| 1550 | } |
| 1551 | |
| 1552 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1553 | * Set which notification properties will be inherited from system defaults. |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1554 | * <p> |
| 1555 | * The value should be one or more of the following fields combined with |
| 1556 | * bitwise-or: |
| 1557 | * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. |
| 1558 | * <p> |
| 1559 | * For all default values, use {@link #DEFAULT_ALL}. |
| 1560 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1561 | public Builder setDefaults(int defaults) { |
| 1562 | mDefaults = defaults; |
| 1563 | return this; |
| 1564 | } |
| 1565 | |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1566 | /** |
| 1567 | * Set the priority of this notification. |
| 1568 | * |
| 1569 | * @see Notification#priority |
| 1570 | */ |
| 1571 | public Builder setPriority(int pri) { |
| 1572 | mPriority = pri; |
| 1573 | return this; |
| 1574 | } |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 1575 | |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1576 | /** |
Daniel Sandler | d33b803 | 2012-05-10 11:41:48 -0400 | [diff] [blame] | 1577 | * @hide |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 1578 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1579 | * Add a kind (category) to this notification. Optional. |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 1580 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1581 | * @see Notification#kind |
| 1582 | */ |
| 1583 | public Builder addKind(String k) { |
| 1584 | mKindList.add(k); |
| 1585 | return this; |
| 1586 | } |
| 1587 | |
| 1588 | /** |
| 1589 | * Add metadata to this notification. |
| 1590 | * |
| 1591 | * 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] | 1592 | * current contents are copied into the Notification each time {@link #build()} is |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1593 | * called. |
| 1594 | * |
| 1595 | * @see Notification#extras |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1596 | */ |
| 1597 | public Builder setExtras(Bundle bag) { |
| 1598 | mExtras = bag; |
| 1599 | return this; |
| 1600 | } |
| 1601 | |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 1602 | /** |
| 1603 | * Add an action to this notification. Actions are typically displayed by |
| 1604 | * the system as a button adjacent to the notification content. |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 1605 | * <p> |
| 1606 | * Every action must have an icon (32dp square and matching the |
| 1607 | * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo |
| 1608 | * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}. |
| 1609 | * <p> |
| 1610 | * A notification in its expanded form can display up to 3 actions, from left to right in |
| 1611 | * the order they were added. Actions will not be displayed when the notification is |
| 1612 | * collapsed, however, so be sure that any essential functions may be accessed by the user |
| 1613 | * in some other way (for example, in the Activity pointed to by {@link #contentIntent}). |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 1614 | * |
| 1615 | * @param icon Resource ID of a drawable that represents the action. |
| 1616 | * @param title Text describing the action. |
| 1617 | * @param intent PendingIntent to be fired when the action is invoked. |
| 1618 | */ |
| 1619 | public Builder addAction(int icon, CharSequence title, PendingIntent intent) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 1620 | mActions.add(new Action(icon, safeCharSequence(title), intent)); |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 1621 | return this; |
| 1622 | } |
| 1623 | |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 1624 | /** |
| 1625 | * Add a rich notification style to be applied at build time. |
| 1626 | * |
| 1627 | * @param style Object responsible for modifying the notification style. |
| 1628 | */ |
| 1629 | public Builder setStyle(Style style) { |
| 1630 | if (mStyle != style) { |
| 1631 | mStyle = style; |
Daniel Sandler | c08dea2 | 2012-06-28 08:35:24 -0700 | [diff] [blame] | 1632 | if (mStyle != null) { |
| 1633 | mStyle.setBuilder(this); |
| 1634 | } |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 1635 | } |
| 1636 | return this; |
| 1637 | } |
| 1638 | |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1639 | private void setFlag(int mask, boolean value) { |
| 1640 | if (value) { |
| 1641 | mFlags |= mask; |
| 1642 | } else { |
| 1643 | mFlags &= ~mask; |
| 1644 | } |
| 1645 | } |
| 1646 | |
Daniel Sandler | 6387d2f | 2012-05-22 13:44:09 -0400 | [diff] [blame] | 1647 | private RemoteViews applyStandardTemplate(int resId, boolean fitIn1U) { |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 1648 | RemoteViews contentView = new RemoteViews(mContext.getPackageName(), resId); |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 1649 | boolean showLine3 = false; |
| 1650 | boolean showLine2 = false; |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 1651 | int smallIconImageViewId = R.id.icon; |
| 1652 | if (mLargeIcon != null) { |
| 1653 | contentView.setImageViewBitmap(R.id.icon, mLargeIcon); |
| 1654 | smallIconImageViewId = R.id.right_icon; |
| 1655 | } |
Daniel Sandler | e95658c | 2012-05-10 00:33:54 -0400 | [diff] [blame] | 1656 | if (mPriority < PRIORITY_LOW) { |
| 1657 | contentView.setInt(R.id.icon, |
| 1658 | "setBackgroundResource", R.drawable.notification_template_icon_low_bg); |
| 1659 | contentView.setInt(R.id.status_bar_latest_event_content, |
| 1660 | "setBackgroundResource", R.drawable.notification_bg_low); |
| 1661 | } |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 1662 | if (mSmallIcon != 0) { |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 1663 | contentView.setImageViewResource(smallIconImageViewId, mSmallIcon); |
| 1664 | contentView.setViewVisibility(smallIconImageViewId, View.VISIBLE); |
Jeff Sharkey | 1c40013 | 2011-08-05 14:50:13 -0700 | [diff] [blame] | 1665 | } else { |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 1666 | contentView.setViewVisibility(smallIconImageViewId, View.GONE); |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 1667 | } |
| 1668 | if (mContentTitle != null) { |
| 1669 | contentView.setTextViewText(R.id.title, mContentTitle); |
| 1670 | } |
| 1671 | if (mContentText != null) { |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 1672 | contentView.setTextViewText(R.id.text, mContentText); |
| 1673 | showLine3 = true; |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 1674 | } |
| 1675 | if (mContentInfo != null) { |
| 1676 | contentView.setTextViewText(R.id.info, mContentInfo); |
Jeff Sharkey | 1c40013 | 2011-08-05 14:50:13 -0700 | [diff] [blame] | 1677 | contentView.setViewVisibility(R.id.info, View.VISIBLE); |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 1678 | showLine3 = true; |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 1679 | } else if (mNumber > 0) { |
Daniel Sandler | ebce011 | 2011-06-16 16:44:51 -0400 | [diff] [blame] | 1680 | final int tooBig = mContext.getResources().getInteger( |
| 1681 | R.integer.status_bar_notification_info_maxnum); |
| 1682 | if (mNumber > tooBig) { |
| 1683 | contentView.setTextViewText(R.id.info, mContext.getResources().getString( |
| 1684 | R.string.status_bar_notification_info_overflow)); |
Joe Onorato | 059a2f8 | 2011-01-04 10:27:01 -0800 | [diff] [blame] | 1685 | } else { |
| 1686 | NumberFormat f = NumberFormat.getIntegerInstance(); |
| 1687 | contentView.setTextViewText(R.id.info, f.format(mNumber)); |
| 1688 | } |
Jeff Sharkey | 1c40013 | 2011-08-05 14:50:13 -0700 | [diff] [blame] | 1689 | contentView.setViewVisibility(R.id.info, View.VISIBLE); |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 1690 | showLine3 = true; |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 1691 | } else { |
| 1692 | contentView.setViewVisibility(R.id.info, View.GONE); |
| 1693 | } |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 1694 | |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 1695 | // Need to show three lines? |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 1696 | if (mSubText != null) { |
| 1697 | contentView.setTextViewText(R.id.text, mSubText); |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 1698 | if (mContentText != null) { |
| 1699 | contentView.setTextViewText(R.id.text2, mContentText); |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 1700 | contentView.setViewVisibility(R.id.text2, View.VISIBLE); |
| 1701 | showLine2 = true; |
| 1702 | } else { |
| 1703 | contentView.setViewVisibility(R.id.text2, View.GONE); |
| 1704 | } |
Jeff Sharkey | 1c40013 | 2011-08-05 14:50:13 -0700 | [diff] [blame] | 1705 | } else { |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 1706 | contentView.setViewVisibility(R.id.text2, View.GONE); |
| 1707 | if (mProgressMax != 0 || mProgressIndeterminate) { |
| 1708 | contentView.setProgressBar( |
| 1709 | R.id.progress, mProgressMax, mProgress, mProgressIndeterminate); |
| 1710 | contentView.setViewVisibility(R.id.progress, View.VISIBLE); |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 1711 | showLine2 = true; |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 1712 | } else { |
| 1713 | contentView.setViewVisibility(R.id.progress, View.GONE); |
| 1714 | } |
Jeff Sharkey | 1c40013 | 2011-08-05 14:50:13 -0700 | [diff] [blame] | 1715 | } |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 1716 | if (showLine2) { |
Daniel Sandler | 6387d2f | 2012-05-22 13:44:09 -0400 | [diff] [blame] | 1717 | if (fitIn1U) { |
| 1718 | // need to shrink all the type to make sure everything fits |
| 1719 | final Resources res = mContext.getResources(); |
| 1720 | final float subTextSize = res.getDimensionPixelSize( |
| 1721 | R.dimen.notification_subtext_size); |
| 1722 | contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize); |
| 1723 | } |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 1724 | // vertical centering |
| 1725 | contentView.setViewPadding(R.id.line1, 0, 0, 0, 0); |
| 1726 | } |
| 1727 | |
Daniel Sandler | 0c89049 | 2012-09-12 17:23:10 -0700 | [diff] [blame] | 1728 | if (mWhen != 0 && mShowWhen) { |
Daniel Sandler | a2985ed | 2012-04-03 16:42:00 -0400 | [diff] [blame] | 1729 | if (mUseChronometer) { |
| 1730 | contentView.setViewVisibility(R.id.chronometer, View.VISIBLE); |
| 1731 | contentView.setLong(R.id.chronometer, "setBase", |
| 1732 | mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis())); |
| 1733 | contentView.setBoolean(R.id.chronometer, "setStarted", true); |
| 1734 | } else { |
| 1735 | contentView.setViewVisibility(R.id.time, View.VISIBLE); |
| 1736 | contentView.setLong(R.id.time, "setTime", mWhen); |
| 1737 | } |
Daniel Sandler | 0c89049 | 2012-09-12 17:23:10 -0700 | [diff] [blame] | 1738 | } else { |
| 1739 | contentView.setViewVisibility(R.id.time, View.GONE); |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 1740 | } |
Daniel Sandler | 0c89049 | 2012-09-12 17:23:10 -0700 | [diff] [blame] | 1741 | |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 1742 | contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE); |
Daniel Sandler | 6387d2f | 2012-05-22 13:44:09 -0400 | [diff] [blame] | 1743 | contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE); |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 1744 | return contentView; |
| 1745 | } |
| 1746 | |
Daniel Sandler | 96fd7c1 | 2012-03-30 16:37:36 -0400 | [diff] [blame] | 1747 | private RemoteViews applyStandardTemplateWithActions(int layoutId) { |
Daniel Sandler | 6387d2f | 2012-05-22 13:44:09 -0400 | [diff] [blame] | 1748 | RemoteViews big = applyStandardTemplate(layoutId, false); |
Daniel Sandler | 96fd7c1 | 2012-03-30 16:37:36 -0400 | [diff] [blame] | 1749 | |
| 1750 | int N = mActions.size(); |
| 1751 | if (N > 0) { |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 1752 | // Log.d("Notification", "has actions: " + mContentText); |
Daniel Sandler | 96fd7c1 | 2012-03-30 16:37:36 -0400 | [diff] [blame] | 1753 | big.setViewVisibility(R.id.actions, View.VISIBLE); |
Daniel Sandler | 6387d2f | 2012-05-22 13:44:09 -0400 | [diff] [blame] | 1754 | big.setViewVisibility(R.id.action_divider, View.VISIBLE); |
Daniel Sandler | 8680bf8 | 2012-05-15 16:52:52 -0400 | [diff] [blame] | 1755 | if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS; |
Chris Wren | 2c22eb0 | 2012-05-08 09:49:13 -0400 | [diff] [blame] | 1756 | big.removeAllViews(R.id.actions); |
Daniel Sandler | 96fd7c1 | 2012-03-30 16:37:36 -0400 | [diff] [blame] | 1757 | for (int i=0; i<N; i++) { |
| 1758 | final RemoteViews button = generateActionButton(mActions.get(i)); |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 1759 | //Log.d("Notification", "adding action " + i + ": " + mActions.get(i).title); |
Daniel Sandler | 96fd7c1 | 2012-03-30 16:37:36 -0400 | [diff] [blame] | 1760 | big.addView(R.id.actions, button); |
| 1761 | } |
| 1762 | } |
| 1763 | return big; |
| 1764 | } |
| 1765 | |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1766 | private RemoteViews makeContentView() { |
| 1767 | if (mContentView != null) { |
| 1768 | return mContentView; |
| 1769 | } else { |
Daniel Sandler | 6387d2f | 2012-05-22 13:44:09 -0400 | [diff] [blame] | 1770 | return applyStandardTemplate(R.layout.notification_template_base, true); // no more special large_icon flavor |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1771 | } |
| 1772 | } |
| 1773 | |
| 1774 | private RemoteViews makeTickerView() { |
| 1775 | if (mTickerView != null) { |
| 1776 | return mTickerView; |
| 1777 | } else { |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 1778 | if (mContentView == null) { |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 1779 | return applyStandardTemplate(mLargeIcon == null |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 1780 | ? R.layout.status_bar_latest_event_ticker |
Daniel Sandler | 6387d2f | 2012-05-22 13:44:09 -0400 | [diff] [blame] | 1781 | : R.layout.status_bar_latest_event_ticker_large_icon, true); |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 1782 | } else { |
| 1783 | return null; |
| 1784 | } |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1785 | } |
| 1786 | } |
| 1787 | |
Daniel Sandler | 96fd7c1 | 2012-03-30 16:37:36 -0400 | [diff] [blame] | 1788 | private RemoteViews makeBigContentView() { |
| 1789 | if (mActions.size() == 0) return null; |
| 1790 | |
Chris Wren | b023bf8 | 2012-04-23 16:05:42 -0400 | [diff] [blame] | 1791 | return applyStandardTemplateWithActions(R.layout.notification_template_big_base); |
Daniel Sandler | 96fd7c1 | 2012-03-30 16:37:36 -0400 | [diff] [blame] | 1792 | } |
| 1793 | |
Daniel Sandler | 96fd7c1 | 2012-03-30 16:37:36 -0400 | [diff] [blame] | 1794 | private RemoteViews generateActionButton(Action action) { |
Daniel Sandler | 8680bf8 | 2012-05-15 16:52:52 -0400 | [diff] [blame] | 1795 | final boolean tombstone = (action.actionIntent == null); |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 1796 | RemoteViews button = new RemoteViews(mContext.getPackageName(), |
Daniel Sandler | 8680bf8 | 2012-05-15 16:52:52 -0400 | [diff] [blame] | 1797 | tombstone ? R.layout.notification_action_tombstone |
| 1798 | : R.layout.notification_action); |
Daniel Sandler | 96fd7c1 | 2012-03-30 16:37:36 -0400 | [diff] [blame] | 1799 | button.setTextViewCompoundDrawables(R.id.action0, action.icon, 0, 0, 0); |
| 1800 | button.setTextViewText(R.id.action0, action.title); |
Daniel Sandler | 8680bf8 | 2012-05-15 16:52:52 -0400 | [diff] [blame] | 1801 | if (!tombstone) { |
Daniel Sandler | e551884 | 2012-05-10 16:20:40 -0400 | [diff] [blame] | 1802 | button.setOnClickPendingIntent(R.id.action0, action.actionIntent); |
Daniel Sandler | e551884 | 2012-05-10 16:20:40 -0400 | [diff] [blame] | 1803 | } |
Daniel Sandler | 96fd7c1 | 2012-03-30 16:37:36 -0400 | [diff] [blame] | 1804 | button.setContentDescription(R.id.action0, action.title); |
| 1805 | return button; |
| 1806 | } |
| 1807 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1808 | /** |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 1809 | * Apply the unstyled operations and return a new {@link Notification} object. |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 1810 | * @hide |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1811 | */ |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 1812 | public Notification buildUnstyled() { |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1813 | Notification n = new Notification(); |
| 1814 | n.when = mWhen; |
| 1815 | n.icon = mSmallIcon; |
| 1816 | n.iconLevel = mSmallIconLevel; |
Joe Onorato | 8595a3d | 2010-11-19 18:12:07 -0800 | [diff] [blame] | 1817 | n.number = mNumber; |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1818 | n.contentView = makeContentView(); |
| 1819 | n.contentIntent = mContentIntent; |
| 1820 | n.deleteIntent = mDeleteIntent; |
| 1821 | n.fullScreenIntent = mFullScreenIntent; |
| 1822 | n.tickerText = mTickerText; |
| 1823 | n.tickerView = makeTickerView(); |
| 1824 | n.largeIcon = mLargeIcon; |
| 1825 | n.sound = mSound; |
| 1826 | n.audioStreamType = mAudioStreamType; |
| 1827 | n.vibrate = mVibrate; |
| 1828 | n.ledARGB = mLedArgb; |
| 1829 | n.ledOnMS = mLedOnMs; |
| 1830 | n.ledOffMS = mLedOffMs; |
| 1831 | n.defaults = mDefaults; |
| 1832 | n.flags = mFlags; |
Daniel Sandler | 96fd7c1 | 2012-03-30 16:37:36 -0400 | [diff] [blame] | 1833 | n.bigContentView = makeBigContentView(); |
Daniel Sandler | 26c1343 | 2013-04-04 11:01:04 -0400 | [diff] [blame] | 1834 | if (mLedOnMs != 0 || mLedOffMs != 0) { |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 1835 | n.flags |= FLAG_SHOW_LIGHTS; |
| 1836 | } |
| 1837 | if ((mDefaults & DEFAULT_LIGHTS) != 0) { |
| 1838 | n.flags |= FLAG_SHOW_LIGHTS; |
| 1839 | } |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1840 | if (mKindList.size() > 0) { |
| 1841 | n.kind = new String[mKindList.size()]; |
| 1842 | mKindList.toArray(n.kind); |
| 1843 | } else { |
| 1844 | n.kind = null; |
| 1845 | } |
| 1846 | n.priority = mPriority; |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 1847 | if (mActions.size() > 0) { |
| 1848 | n.actions = new Action[mActions.size()]; |
| 1849 | mActions.toArray(n.actions); |
| 1850 | } |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 1851 | |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1852 | return n; |
| 1853 | } |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 1854 | |
| 1855 | /** |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 1856 | * Capture, in the provided bundle, semantic information used in the construction of |
| 1857 | * this Notification object. |
| 1858 | * @hide |
| 1859 | */ |
| 1860 | public void addExtras(Bundle extras) { |
| 1861 | // Store original information used in the construction of this object |
| 1862 | extras.putCharSequence(EXTRA_TITLE, mContentTitle); |
| 1863 | extras.putCharSequence(EXTRA_TEXT, mContentText); |
| 1864 | extras.putCharSequence(EXTRA_SUB_TEXT, mSubText); |
| 1865 | extras.putCharSequence(EXTRA_INFO_TEXT, mContentInfo); |
| 1866 | extras.putInt(EXTRA_SMALL_ICON, mSmallIcon); |
| 1867 | extras.putInt(EXTRA_PROGRESS, mProgress); |
| 1868 | extras.putInt(EXTRA_PROGRESS_MAX, mProgressMax); |
| 1869 | extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mProgressIndeterminate); |
| 1870 | extras.putBoolean(EXTRA_SHOW_CHRONOMETER, mUseChronometer); |
| 1871 | extras.putBoolean(EXTRA_SHOW_WHEN, mShowWhen); |
John Spurlock | ac08a47 | 2013-06-10 11:37:08 -0400 | [diff] [blame] | 1872 | if (mLargeIcon != null) { |
| 1873 | extras.putParcelable(EXTRA_LARGE_ICON, mLargeIcon); |
| 1874 | } |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 1875 | } |
| 1876 | |
| 1877 | /** |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 1878 | * @deprecated Use {@link #build()} instead. |
| 1879 | */ |
| 1880 | @Deprecated |
| 1881 | public Notification getNotification() { |
| 1882 | return build(); |
| 1883 | } |
| 1884 | |
| 1885 | /** |
| 1886 | * Combine all of the options that have been set and return a new {@link Notification} |
| 1887 | * object. |
| 1888 | */ |
| 1889 | public Notification build() { |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 1890 | Notification n = buildUnstyled(); |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 1891 | |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 1892 | if (mStyle != null) { |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 1893 | n = mStyle.buildStyled(n); |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 1894 | } |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 1895 | |
| 1896 | n.extras = mExtras != null ? new Bundle(mExtras) : new Bundle(); |
| 1897 | |
| 1898 | addExtras(n.extras); |
| 1899 | if (mStyle != null) { |
| 1900 | mStyle.addExtras(n.extras); |
| 1901 | } |
| 1902 | |
| 1903 | return n; |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 1904 | } |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 1905 | |
| 1906 | /** |
| 1907 | * Apply this Builder to an existing {@link Notification} object. |
| 1908 | * |
| 1909 | * @hide |
| 1910 | */ |
| 1911 | public Notification buildInto(Notification n) { |
Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 1912 | build().cloneInto(n, true); |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 1913 | return n; |
| 1914 | } |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 1915 | } |
| 1916 | |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 1917 | /** |
| 1918 | * An object that can apply a rich notification style to a {@link Notification.Builder} |
| 1919 | * object. |
| 1920 | */ |
Griff Hazen | dfcb080 | 2014-02-11 12:00:00 -0800 | [diff] [blame^] | 1921 | public static abstract class Style { |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 1922 | private CharSequence mBigContentTitle; |
| 1923 | private CharSequence mSummaryText = null; |
Daniel Sandler | 619738c | 2012-06-07 16:33:08 -0400 | [diff] [blame] | 1924 | private boolean mSummaryTextSet = false; |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 1925 | |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 1926 | protected Builder mBuilder; |
| 1927 | |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 1928 | /** |
| 1929 | * Overrides ContentTitle in the big form of the template. |
| 1930 | * This defaults to the value passed to setContentTitle(). |
| 1931 | */ |
| 1932 | protected void internalSetBigContentTitle(CharSequence title) { |
| 1933 | mBigContentTitle = title; |
| 1934 | } |
| 1935 | |
| 1936 | /** |
| 1937 | * Set the first line of text after the detail section in the big form of the template. |
| 1938 | */ |
| 1939 | protected void internalSetSummaryText(CharSequence cs) { |
| 1940 | mSummaryText = cs; |
Daniel Sandler | 619738c | 2012-06-07 16:33:08 -0400 | [diff] [blame] | 1941 | mSummaryTextSet = true; |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 1942 | } |
| 1943 | |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 1944 | public void setBuilder(Builder builder) { |
| 1945 | if (mBuilder != builder) { |
| 1946 | mBuilder = builder; |
Daniel Sandler | c08dea2 | 2012-06-28 08:35:24 -0700 | [diff] [blame] | 1947 | if (mBuilder != null) { |
| 1948 | mBuilder.setStyle(this); |
| 1949 | } |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 1950 | } |
| 1951 | } |
| 1952 | |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 1953 | protected void checkBuilder() { |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 1954 | if (mBuilder == null) { |
| 1955 | throw new IllegalArgumentException("Style requires a valid Builder object"); |
| 1956 | } |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 1957 | } |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 1958 | |
| 1959 | protected RemoteViews getStandardView(int layoutId) { |
| 1960 | checkBuilder(); |
| 1961 | |
| 1962 | if (mBigContentTitle != null) { |
| 1963 | mBuilder.setContentTitle(mBigContentTitle); |
| 1964 | } |
| 1965 | |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 1966 | RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId); |
| 1967 | |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 1968 | if (mBigContentTitle != null && mBigContentTitle.equals("")) { |
| 1969 | contentView.setViewVisibility(R.id.line1, View.GONE); |
Chris Wren | 67dc9a0 | 2012-05-16 01:03:20 -0400 | [diff] [blame] | 1970 | } else { |
| 1971 | contentView.setViewVisibility(R.id.line1, View.VISIBLE); |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 1972 | } |
| 1973 | |
Daniel Sandler | 619738c | 2012-06-07 16:33:08 -0400 | [diff] [blame] | 1974 | // The last line defaults to the subtext, but can be replaced by mSummaryText |
| 1975 | final CharSequence overflowText = |
| 1976 | mSummaryTextSet ? mSummaryText |
| 1977 | : mBuilder.mSubText; |
| 1978 | if (overflowText != null) { |
| 1979 | contentView.setTextViewText(R.id.text, overflowText); |
| 1980 | contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE); |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 1981 | contentView.setViewVisibility(R.id.line3, View.VISIBLE); |
Daniel Sandler | 916ad91 | 2012-06-13 12:17:07 -0400 | [diff] [blame] | 1982 | } else { |
| 1983 | contentView.setViewVisibility(R.id.overflow_divider, View.GONE); |
| 1984 | contentView.setViewVisibility(R.id.line3, View.GONE); |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | return contentView; |
| 1988 | } |
| 1989 | |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 1990 | /** |
| 1991 | * @hide |
| 1992 | */ |
| 1993 | public void addExtras(Bundle extras) { |
| 1994 | if (mSummaryTextSet) { |
| 1995 | extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText); |
| 1996 | } |
| 1997 | if (mBigContentTitle != null) { |
| 1998 | extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle); |
| 1999 | } |
| 2000 | } |
| 2001 | |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 2002 | /** |
| 2003 | * @hide |
| 2004 | */ |
| 2005 | public abstract Notification buildStyled(Notification wip); |
| 2006 | |
| 2007 | /** |
| 2008 | * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is |
| 2009 | * attached to. |
| 2010 | * |
| 2011 | * @return the fully constructed Notification. |
| 2012 | */ |
| 2013 | public Notification build() { |
| 2014 | checkBuilder(); |
| 2015 | return mBuilder.build(); |
| 2016 | } |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2017 | } |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2018 | |
| 2019 | /** |
Daniel Sandler | 4dfbe83 | 2012-04-11 14:51:46 -0400 | [diff] [blame] | 2020 | * Helper class for generating large-format notifications that include a large image attachment. |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 2021 | * |
Daniel Sandler | 4dfbe83 | 2012-04-11 14:51:46 -0400 | [diff] [blame] | 2022 | * 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] | 2023 | * <pre class="prettyprint"> |
| 2024 | * Notification noti = new Notification.BigPictureStyle( |
| 2025 | * new Notification.Builder() |
Daniel Sandler | 4dfbe83 | 2012-04-11 14:51:46 -0400 | [diff] [blame] | 2026 | * .setContentTitle("New photo from " + sender.toString()) |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2027 | * .setContentText(subject) |
Daniel Sandler | 4dfbe83 | 2012-04-11 14:51:46 -0400 | [diff] [blame] | 2028 | * .setSmallIcon(R.drawable.new_post) |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2029 | * .setLargeIcon(aBitmap)) |
| 2030 | * .bigPicture(aBigBitmap) |
| 2031 | * .build(); |
| 2032 | * </pre> |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 2033 | * |
Daniel Sandler | 4dfbe83 | 2012-04-11 14:51:46 -0400 | [diff] [blame] | 2034 | * @see Notification#bigContentView |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2035 | */ |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 2036 | public static class BigPictureStyle extends Style { |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2037 | private Bitmap mPicture; |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 2038 | private Bitmap mBigLargeIcon; |
| 2039 | private boolean mBigLargeIconSet = false; |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2040 | |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 2041 | public BigPictureStyle() { |
| 2042 | } |
| 2043 | |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2044 | public BigPictureStyle(Builder builder) { |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 2045 | setBuilder(builder); |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2046 | } |
| 2047 | |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 2048 | /** |
| 2049 | * Overrides ContentTitle in the big form of the template. |
| 2050 | * This defaults to the value passed to setContentTitle(). |
| 2051 | */ |
| 2052 | public BigPictureStyle setBigContentTitle(CharSequence title) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 2053 | internalSetBigContentTitle(safeCharSequence(title)); |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 2054 | return this; |
| 2055 | } |
| 2056 | |
| 2057 | /** |
| 2058 | * Set the first line of text after the detail section in the big form of the template. |
| 2059 | */ |
| 2060 | public BigPictureStyle setSummaryText(CharSequence cs) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 2061 | internalSetSummaryText(safeCharSequence(cs)); |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 2062 | return this; |
| 2063 | } |
| 2064 | |
Chris Wren | 0bd664d | 2012-08-01 13:56:56 -0400 | [diff] [blame] | 2065 | /** |
| 2066 | * Provide the bitmap to be used as the payload for the BigPicture notification. |
| 2067 | */ |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2068 | public BigPictureStyle bigPicture(Bitmap b) { |
| 2069 | mPicture = b; |
| 2070 | return this; |
| 2071 | } |
| 2072 | |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 2073 | /** |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 2074 | * Override the large icon when the big notification is shown. |
| 2075 | */ |
| 2076 | public BigPictureStyle bigLargeIcon(Bitmap b) { |
| 2077 | mBigLargeIconSet = true; |
| 2078 | mBigLargeIcon = b; |
| 2079 | return this; |
| 2080 | } |
| 2081 | |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2082 | private RemoteViews makeBigContentView() { |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 2083 | RemoteViews contentView = getStandardView(R.layout.notification_template_big_picture); |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2084 | |
| 2085 | contentView.setImageViewBitmap(R.id.big_picture, mPicture); |
| 2086 | |
| 2087 | return contentView; |
| 2088 | } |
| 2089 | |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 2090 | /** |
| 2091 | * @hide |
| 2092 | */ |
| 2093 | public void addExtras(Bundle extras) { |
| 2094 | super.addExtras(extras); |
| 2095 | |
| 2096 | if (mBigLargeIconSet) { |
| 2097 | extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon); |
| 2098 | } |
| 2099 | extras.putParcelable(EXTRA_PICTURE, mPicture); |
| 2100 | } |
| 2101 | |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 2102 | /** |
| 2103 | * @hide |
| 2104 | */ |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 2105 | @Override |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 2106 | public Notification buildStyled(Notification wip) { |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 2107 | if (mBigLargeIconSet ) { |
| 2108 | mBuilder.mLargeIcon = mBigLargeIcon; |
| 2109 | } |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2110 | wip.bigContentView = makeBigContentView(); |
| 2111 | return wip; |
| 2112 | } |
| 2113 | } |
| 2114 | |
| 2115 | /** |
Daniel Sandler | 4dfbe83 | 2012-04-11 14:51:46 -0400 | [diff] [blame] | 2116 | * Helper class for generating large-format notifications that include a lot of text. |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 2117 | * |
Daniel Sandler | 4dfbe83 | 2012-04-11 14:51:46 -0400 | [diff] [blame] | 2118 | * 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] | 2119 | * <pre class="prettyprint"> |
Daniel Sandler | 8768278 | 2012-11-07 14:04:42 -0500 | [diff] [blame] | 2120 | * Notification noti = new Notification.BigTextStyle( |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2121 | * new Notification.Builder() |
| 2122 | * .setContentTitle("New mail from " + sender.toString()) |
| 2123 | * .setContentText(subject) |
| 2124 | * .setSmallIcon(R.drawable.new_mail) |
| 2125 | * .setLargeIcon(aBitmap)) |
| 2126 | * .bigText(aVeryLongString) |
| 2127 | * .build(); |
| 2128 | * </pre> |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 2129 | * |
Daniel Sandler | 4dfbe83 | 2012-04-11 14:51:46 -0400 | [diff] [blame] | 2130 | * @see Notification#bigContentView |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2131 | */ |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 2132 | public static class BigTextStyle extends Style { |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2133 | private CharSequence mBigText; |
| 2134 | |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 2135 | public BigTextStyle() { |
| 2136 | } |
| 2137 | |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2138 | public BigTextStyle(Builder builder) { |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 2139 | setBuilder(builder); |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2140 | } |
| 2141 | |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 2142 | /** |
| 2143 | * Overrides ContentTitle in the big form of the template. |
| 2144 | * This defaults to the value passed to setContentTitle(). |
| 2145 | */ |
| 2146 | public BigTextStyle setBigContentTitle(CharSequence title) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 2147 | internalSetBigContentTitle(safeCharSequence(title)); |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 2148 | return this; |
| 2149 | } |
| 2150 | |
| 2151 | /** |
| 2152 | * Set the first line of text after the detail section in the big form of the template. |
| 2153 | */ |
| 2154 | public BigTextStyle setSummaryText(CharSequence cs) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 2155 | internalSetSummaryText(safeCharSequence(cs)); |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 2156 | return this; |
| 2157 | } |
| 2158 | |
Chris Wren | 0bd664d | 2012-08-01 13:56:56 -0400 | [diff] [blame] | 2159 | /** |
| 2160 | * Provide the longer text to be displayed in the big form of the |
| 2161 | * template in place of the content text. |
| 2162 | */ |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2163 | public BigTextStyle bigText(CharSequence cs) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 2164 | mBigText = safeCharSequence(cs); |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2165 | return this; |
| 2166 | } |
| 2167 | |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 2168 | /** |
| 2169 | * @hide |
| 2170 | */ |
| 2171 | public void addExtras(Bundle extras) { |
| 2172 | super.addExtras(extras); |
| 2173 | |
| 2174 | extras.putCharSequence(EXTRA_TEXT, mBigText); |
| 2175 | } |
| 2176 | |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2177 | private RemoteViews makeBigContentView() { |
Daniel Sandler | 619738c | 2012-06-07 16:33:08 -0400 | [diff] [blame] | 2178 | // Remove the content text so line3 only shows if you have a summary |
| 2179 | final boolean hadThreeLines = (mBuilder.mContentText != null && mBuilder.mSubText != null); |
Daniel Sandler | 6387d2f | 2012-05-22 13:44:09 -0400 | [diff] [blame] | 2180 | mBuilder.mContentText = null; |
Daniel Sandler | 916ad91 | 2012-06-13 12:17:07 -0400 | [diff] [blame] | 2181 | |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 2182 | RemoteViews contentView = getStandardView(R.layout.notification_template_big_text); |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 2183 | |
Daniel Sandler | 619738c | 2012-06-07 16:33:08 -0400 | [diff] [blame] | 2184 | if (hadThreeLines) { |
| 2185 | // vertical centering |
| 2186 | contentView.setViewPadding(R.id.line1, 0, 0, 0, 0); |
| 2187 | } |
| 2188 | |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2189 | contentView.setTextViewText(R.id.big_text, mBigText); |
| 2190 | contentView.setViewVisibility(R.id.big_text, View.VISIBLE); |
Chris Wren | 3c5f9243 | 2012-05-04 16:31:17 -0400 | [diff] [blame] | 2191 | contentView.setViewVisibility(R.id.text2, View.GONE); |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2192 | |
| 2193 | return contentView; |
| 2194 | } |
| 2195 | |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 2196 | /** |
| 2197 | * @hide |
| 2198 | */ |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 2199 | @Override |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 2200 | public Notification buildStyled(Notification wip) { |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2201 | wip.bigContentView = makeBigContentView(); |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 2202 | |
| 2203 | wip.extras.putCharSequence(EXTRA_TEXT, mBigText); |
| 2204 | |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2205 | return wip; |
| 2206 | } |
| 2207 | } |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 2208 | |
| 2209 | /** |
| 2210 | * Helper class for generating large-format notifications that include a list of (up to 5) strings. |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 2211 | * |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 2212 | * This class is a "rebuilder": It consumes a Builder object and modifies its behavior, like so: |
| 2213 | * <pre class="prettyprint"> |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 2214 | * Notification noti = new Notification.InboxStyle( |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 2215 | * new Notification.Builder() |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 2216 | * .setContentTitle("5 New mails from " + sender.toString()) |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 2217 | * .setContentText(subject) |
| 2218 | * .setSmallIcon(R.drawable.new_mail) |
| 2219 | * .setLargeIcon(aBitmap)) |
| 2220 | * .addLine(str1) |
| 2221 | * .addLine(str2) |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 2222 | * .setContentTitle("") |
| 2223 | * .setSummaryText("+3 more") |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 2224 | * .build(); |
| 2225 | * </pre> |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 2226 | * |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 2227 | * @see Notification#bigContentView |
| 2228 | */ |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 2229 | public static class InboxStyle extends Style { |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 2230 | private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5); |
| 2231 | |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 2232 | public InboxStyle() { |
| 2233 | } |
| 2234 | |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 2235 | public InboxStyle(Builder builder) { |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 2236 | setBuilder(builder); |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 2237 | } |
| 2238 | |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 2239 | /** |
| 2240 | * Overrides ContentTitle in the big form of the template. |
| 2241 | * This defaults to the value passed to setContentTitle(). |
| 2242 | */ |
| 2243 | public InboxStyle setBigContentTitle(CharSequence title) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 2244 | internalSetBigContentTitle(safeCharSequence(title)); |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 2245 | return this; |
| 2246 | } |
| 2247 | |
| 2248 | /** |
| 2249 | * Set the first line of text after the detail section in the big form of the template. |
| 2250 | */ |
| 2251 | public InboxStyle setSummaryText(CharSequence cs) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 2252 | internalSetSummaryText(safeCharSequence(cs)); |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 2253 | return this; |
| 2254 | } |
| 2255 | |
Chris Wren | 0bd664d | 2012-08-01 13:56:56 -0400 | [diff] [blame] | 2256 | /** |
| 2257 | * Append a line to the digest section of the Inbox notification. |
| 2258 | */ |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 2259 | public InboxStyle addLine(CharSequence cs) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 2260 | mTexts.add(safeCharSequence(cs)); |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 2261 | return this; |
| 2262 | } |
| 2263 | |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 2264 | /** |
| 2265 | * @hide |
| 2266 | */ |
| 2267 | public void addExtras(Bundle extras) { |
| 2268 | super.addExtras(extras); |
| 2269 | CharSequence[] a = new CharSequence[mTexts.size()]; |
| 2270 | extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a)); |
| 2271 | } |
| 2272 | |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 2273 | private RemoteViews makeBigContentView() { |
Daniel Sandler | 619738c | 2012-06-07 16:33:08 -0400 | [diff] [blame] | 2274 | // Remove the content text so line3 disappears unless you have a summary |
| 2275 | mBuilder.mContentText = null; |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 2276 | RemoteViews contentView = getStandardView(R.layout.notification_template_inbox); |
Daniel Sandler | 619738c | 2012-06-07 16:33:08 -0400 | [diff] [blame] | 2277 | |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 2278 | contentView.setViewVisibility(R.id.text2, View.GONE); |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 2279 | |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 2280 | int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3, |
Chris Wren | 29bb6d9 | 2012-05-17 18:09:42 -0400 | [diff] [blame] | 2281 | R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6}; |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 2282 | |
Chris Wren | 4ed80d5 | 2012-05-17 09:30:03 -0400 | [diff] [blame] | 2283 | // Make sure all rows are gone in case we reuse a view. |
| 2284 | for (int rowId : rowIds) { |
| 2285 | contentView.setViewVisibility(rowId, View.GONE); |
| 2286 | } |
| 2287 | |
Chris Wren | 683ab00 | 2012-09-20 10:35:54 -0400 | [diff] [blame] | 2288 | |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 2289 | int i=0; |
| 2290 | while (i < mTexts.size() && i < rowIds.length) { |
| 2291 | CharSequence str = mTexts.get(i); |
| 2292 | if (str != null && !str.equals("")) { |
| 2293 | contentView.setViewVisibility(rowIds[i], View.VISIBLE); |
| 2294 | contentView.setTextViewText(rowIds[i], str); |
| 2295 | } |
| 2296 | i++; |
| 2297 | } |
| 2298 | |
Chris Wren | 683ab00 | 2012-09-20 10:35:54 -0400 | [diff] [blame] | 2299 | contentView.setViewVisibility(R.id.inbox_end_pad, |
| 2300 | mTexts.size() > 0 ? View.VISIBLE : View.GONE); |
| 2301 | |
| 2302 | contentView.setViewVisibility(R.id.inbox_more, |
| 2303 | mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE); |
Chris Wren | 29bb6d9 | 2012-05-17 18:09:42 -0400 | [diff] [blame] | 2304 | |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 2305 | return contentView; |
| 2306 | } |
| 2307 | |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 2308 | /** |
| 2309 | * @hide |
| 2310 | */ |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 2311 | @Override |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 2312 | public Notification buildStyled(Notification wip) { |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 2313 | wip.bigContentView = makeBigContentView(); |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 2314 | |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 2315 | return wip; |
| 2316 | } |
| 2317 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2318 | } |