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