]>
| Commit | Line | Data |
|---|---|---|
| 1 | /* | |
| 2 | * Copyright (C) 2012 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.support.v4.app; | |
| 18 | ||
| 19 | import android.app.Notification; | |
| 20 | import android.app.NotificationManager; | |
| 21 | import android.app.PendingIntent; | |
| 22 | import android.content.Context; | |
| 23 | import android.graphics.Bitmap; | |
| 24 | import android.media.AudioManager; | |
| 25 | import android.net.Uri; | |
| 26 | import android.os.Build; | |
| 27 | import android.widget.RemoteViews; | |
| 28 | import java.util.ArrayList; | |
| 29 | ||
| 30 | /** | |
| 31 | * Helper for accessing features in {@link android.app.Notification} | |
| 32 | * introduced after API level 4 in a backwards compatible fashion. | |
| 33 | */ | |
| 34 | public class NotificationCompat { | |
| 35 | /** | |
| 36 | * Obsolete flag indicating high-priority notifications; use the priority field instead. | |
| 37 | * | |
| 38 | * @deprecated Use {@link NotificationCompat.Builder#setPriority(int)} with a positive value. | |
| 39 | */ | |
| 40 | @Deprecated | |
| 41 | public static final int FLAG_HIGH_PRIORITY = 0x00000080; | |
| 42 | ||
| 43 | /** | |
| 44 | * Default notification priority for {@link NotificationCompat.Builder#setPriority(int)}. | |
| 45 | * If your application does not prioritize its own notifications, | |
| 46 | * use this value for all notifications. | |
| 47 | */ | |
| 48 | public static final int PRIORITY_DEFAULT = 0; | |
| 49 | ||
| 50 | /** | |
| 51 | * Lower notification priority for {@link NotificationCompat.Builder#setPriority(int)}, | |
| 52 | * for items that are less important. The UI may choose to show | |
| 53 | * these items smaller, or at a different position in the list, | |
| 54 | * compared with your app's {@link #PRIORITY_DEFAULT} items. | |
| 55 | */ | |
| 56 | public static final int PRIORITY_LOW = -1; | |
| 57 | ||
| 58 | /** | |
| 59 | * Lowest notification priority for {@link NotificationCompat.Builder#setPriority(int)}; | |
| 60 | * these items might not be shown to the user except under | |
| 61 | * special circumstances, such as detailed notification logs. | |
| 62 | */ | |
| 63 | public static final int PRIORITY_MIN = -2; | |
| 64 | ||
| 65 | /** | |
| 66 | * Higher notification priority for {@link NotificationCompat.Builder#setPriority(int)}, | |
| 67 | * for more important notifications or alerts. The UI may choose | |
| 68 | * to show these items larger, or at a different position in | |
| 69 | * notification lists, compared with your app's {@link #PRIORITY_DEFAULT} items. | |
| 70 | */ | |
| 71 | public static final int PRIORITY_HIGH = 1; | |
| 72 | ||
| 73 | /** | |
| 74 | * Highest notification priority for {@link NotificationCompat.Builder#setPriority(int)}, | |
| 75 | * for your application's most important items that require the user's | |
| 76 | * prompt attention or input. | |
| 77 | */ | |
| 78 | public static final int PRIORITY_MAX = 2; | |
| 79 | ||
| 80 | private static final NotificationCompatImpl IMPL; | |
| 81 | ||
| 82 | interface NotificationCompatImpl { | |
| 83 | public Notification build(Builder b); | |
| 84 | } | |
| 85 | ||
| 86 | static class NotificationCompatImplBase implements NotificationCompatImpl { | |
| 87 | @SuppressWarnings("deprecation") | |
| 88 | @Override | |
| 89 | public Notification build(Builder b) { | |
| 90 | Notification result = b.mNotification; | |
| 91 | result.setLatestEventInfo(b.mContext, b.mContentTitle, | |
| 92 | b.mContentText, b.mContentIntent); | |
| 93 | // translate high priority requests into legacy flag | |
| 94 | if (b.mPriority > PRIORITY_DEFAULT) { | |
| 95 | result.flags |= FLAG_HIGH_PRIORITY; | |
| 96 | } | |
| 97 | return result; | |
| 98 | } | |
| 99 | } | |
| 100 | ||
| 101 | static class NotificationCompatImplHoneycomb implements NotificationCompatImpl { | |
| 102 | @Override | |
| 103 | public Notification build(Builder b) { | |
| 104 | return NotificationCompatHoneycomb.add(b.mContext, b.mNotification, | |
| 105 | b.mContentTitle, b.mContentText, b.mContentInfo, b.mTickerView, | |
| 106 | b.mNumber, b.mContentIntent, b.mFullScreenIntent, b.mLargeIcon); | |
| 107 | } | |
| 108 | } | |
| 109 | ||
| 110 | static class NotificationCompatImplIceCreamSandwich implements NotificationCompatImpl { | |
| 111 | @Override | |
| 112 | public Notification build(Builder b) { | |
| 113 | return NotificationCompatIceCreamSandwich.add(b.mContext, b.mNotification, | |
| 114 | b.mContentTitle, b.mContentText, b.mContentInfo, b.mTickerView, | |
| 115 | b.mNumber, b.mContentIntent, b.mFullScreenIntent, b.mLargeIcon, | |
| 116 | b.mProgressMax, b.mProgress, b.mProgressIndeterminate); | |
| 117 | } | |
| 118 | } | |
| 119 | ||
| 120 | static class NotificationCompatImplJellybean implements NotificationCompatImpl { | |
| 121 | @Override | |
| 122 | public Notification build(Builder b) { | |
| 123 | NotificationCompatJellybean jbBuilder = new NotificationCompatJellybean( | |
| 124 | b.mContext, b.mNotification, b.mContentTitle, b.mContentText, b.mContentInfo, | |
| 125 | b.mTickerView, b.mNumber, b.mContentIntent, b.mFullScreenIntent, b.mLargeIcon, | |
| 126 | b.mProgressMax, b.mProgress, b.mProgressIndeterminate, | |
| 127 | b.mUseChronometer, b.mPriority, b.mSubText); | |
| 128 | for (Action action: b.mActions) { | |
| 129 | jbBuilder.addAction(action.icon, action.title, action.actionIntent); | |
| 130 | } | |
| 131 | if (b.mStyle != null) { | |
| 132 | if (b.mStyle instanceof BigTextStyle) { | |
| 133 | BigTextStyle style = (BigTextStyle) b.mStyle; | |
| 134 | jbBuilder.addBigTextStyle(style.mBigContentTitle, | |
| 135 | style.mSummaryTextSet, | |
| 136 | style.mSummaryText, | |
| 137 | style.mBigText); | |
| 138 | } else if (b.mStyle instanceof InboxStyle) { | |
| 139 | InboxStyle style = (InboxStyle) b.mStyle; | |
| 140 | jbBuilder.addInboxStyle(style.mBigContentTitle, | |
| 141 | style.mSummaryTextSet, | |
| 142 | style.mSummaryText, | |
| 143 | style.mTexts); | |
| 144 | } else if (b.mStyle instanceof BigPictureStyle) { | |
| 145 | BigPictureStyle style = (BigPictureStyle) b.mStyle; | |
| 146 | jbBuilder.addBigPictureStyle(style.mBigContentTitle, | |
| 147 | style.mSummaryTextSet, | |
| 148 | style.mSummaryText, | |
| 149 | style.mPicture); | |
| 150 | } | |
| 151 | } | |
| 152 | return(jbBuilder.build()); | |
| 153 | } | |
| 154 | } | |
| 155 | ||
| 156 | static { | |
| 157 | if (Build.VERSION.SDK_INT >= 16) { | |
| 158 | IMPL = new NotificationCompatImplJellybean(); | |
| 159 | } else if (Build.VERSION.SDK_INT >= 14) { | |
| 160 | IMPL = new NotificationCompatImplIceCreamSandwich(); | |
| 161 | } else if (Build.VERSION.SDK_INT >= 11) { | |
| 162 | IMPL = new NotificationCompatImplHoneycomb(); | |
| 163 | } else { | |
| 164 | IMPL = new NotificationCompatImplBase(); | |
| 165 | } | |
| 166 | } | |
| 167 | ||
| 168 | /** | |
| 169 | * Builder class for {@link NotificationCompat} objects. Allows easier control over | |
| 170 | * all the flags, as well as help constructing the typical notification layouts. | |
| 171 | * <p> | |
| 172 | * On platform versions that don't offer expanded notifications, methods that depend on | |
| 173 | * expanded notifications have no effect. | |
| 174 | * </p> | |
| 175 | * <p> | |
| 176 | * For example, action buttons won't appear on platforms prior to Android 4.1. Action | |
| 177 | * buttons depend on expanded notifications, which are only available in Android 4.1 | |
| 178 | * and later. | |
| 179 | * <p> | |
| 180 | * For this reason, you should always ensure that UI controls in a notification are also | |
| 181 | * available in an {@link android.app.Activity} in your app, and you should always start that | |
| 182 | * {@link android.app.Activity} when users click the notification. To do this, use the | |
| 183 | * {@link NotificationCompat.Builder#setContentIntent setContentIntent()} | |
| 184 | * method. | |
| 185 | * </p> | |
| 186 | * | |
| 187 | */ | |
| 188 | public static class Builder { | |
| 189 | Context mContext; | |
| 190 | ||
| 191 | CharSequence mContentTitle; | |
| 192 | CharSequence mContentText; | |
| 193 | PendingIntent mContentIntent; | |
| 194 | PendingIntent mFullScreenIntent; | |
| 195 | RemoteViews mTickerView; | |
| 196 | Bitmap mLargeIcon; | |
| 197 | CharSequence mContentInfo; | |
| 198 | int mNumber; | |
| 199 | int mPriority; | |
| 200 | boolean mUseChronometer; | |
| 201 | Style mStyle; | |
| 202 | CharSequence mSubText; | |
| 203 | int mProgressMax; | |
| 204 | int mProgress; | |
| 205 | boolean mProgressIndeterminate; | |
| 206 | ArrayList<Action> mActions = new ArrayList<Action>(); | |
| 207 | ||
| 208 | Notification mNotification = new Notification(); | |
| 209 | ||
| 210 | /** | |
| 211 | * Constructor. | |
| 212 | * | |
| 213 | * Automatically sets the when field to {@link System#currentTimeMillis() | |
| 214 | * System.currentTimeMillis()} and the audio stream to the | |
| 215 | * {@link Notification#STREAM_DEFAULT}. | |
| 216 | * | |
| 217 | * @param context A {@link Context} that will be used to construct the | |
| 218 | * RemoteViews. The Context will not be held past the lifetime of this | |
| 219 | * Builder object. | |
| 220 | */ | |
| 221 | public Builder(Context context) { | |
| 222 | mContext = context; | |
| 223 | ||
| 224 | // Set defaults to match the defaults of a Notification | |
| 225 | mNotification.when = System.currentTimeMillis(); | |
| 226 | mNotification.audioStreamType = Notification.STREAM_DEFAULT; | |
| 227 | mPriority = PRIORITY_DEFAULT; | |
| 228 | } | |
| 229 | ||
| 230 | /** | |
| 231 | * Set the time that the event occurred. Notifications in the panel are | |
| 232 | * sorted by this time. | |
| 233 | */ | |
| 234 | public Builder setWhen(long when) { | |
| 235 | mNotification.when = when; | |
| 236 | return this; | |
| 237 | } | |
| 238 | ||
| 239 | /** | |
| 240 | * Show the {@link Notification#when} field as a stopwatch. | |
| 241 | * | |
| 242 | * Instead of presenting <code>when</code> as a timestamp, the notification will show an | |
| 243 | * automatically updating display of the minutes and seconds since <code>when</code>. | |
| 244 | * | |
| 245 | * Useful when showing an elapsed time (like an ongoing phone call). | |
| 246 | * | |
| 247 | * @see android.widget.Chronometer | |
| 248 | * @see Notification#when | |
| 249 | */ | |
| 250 | public Builder setUsesChronometer(boolean b) { | |
| 251 | mUseChronometer = b; | |
| 252 | return this; | |
| 253 | } | |
| 254 | ||
| 255 | /** | |
| 256 | * Set the small icon to use in the notification layouts. Different classes of devices | |
| 257 | * may return different sizes. See the UX guidelines for more information on how to | |
| 258 | * design these icons. | |
| 259 | * | |
| 260 | * @param icon A resource ID in the application's package of the drawble to use. | |
| 261 | */ | |
| 262 | public Builder setSmallIcon(int icon) { | |
| 263 | mNotification.icon = icon; | |
| 264 | return this; | |
| 265 | } | |
| 266 | ||
| 267 | /** | |
| 268 | * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional | |
| 269 | * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable | |
| 270 | * LevelListDrawable}. | |
| 271 | * | |
| 272 | * @param icon A resource ID in the application's package of the drawble to use. | |
| 273 | * @param level The level to use for the icon. | |
| 274 | * | |
| 275 | * @see android.graphics.drawable.LevelListDrawable | |
| 276 | */ | |
| 277 | public Builder setSmallIcon(int icon, int level) { | |
| 278 | mNotification.icon = icon; | |
| 279 | mNotification.iconLevel = level; | |
| 280 | return this; | |
| 281 | } | |
| 282 | ||
| 283 | /** | |
| 284 | * Set the title (first row) of the notification, in a standard notification. | |
| 285 | */ | |
| 286 | public Builder setContentTitle(CharSequence title) { | |
| 287 | mContentTitle = title; | |
| 288 | return this; | |
| 289 | } | |
| 290 | ||
| 291 | /** | |
| 292 | * Set the text (second row) of the notification, in a standard notification. | |
| 293 | */ | |
| 294 | public Builder setContentText(CharSequence text) { | |
| 295 | mContentText = text; | |
| 296 | return this; | |
| 297 | } | |
| 298 | ||
| 299 | /** | |
| 300 | * Set the third line of text in the platform notification template. | |
| 301 | * Don't use if you're also using {@link #setProgress(int, int, boolean)}; | |
| 302 | * they occupy the same location in the standard template. | |
| 303 | * <br> | |
| 304 | * If the platform does not provide large-format notifications, this method has no effect. | |
| 305 | * The third line of text only appears in expanded view. | |
| 306 | * <br> | |
| 307 | */ | |
| 308 | public Builder setSubText(CharSequence text) { | |
| 309 | mSubText = text; | |
| 310 | return this; | |
| 311 | } | |
| 312 | ||
| 313 | /** | |
| 314 | * Set the large number at the right-hand side of the notification. This is | |
| 315 | * equivalent to setContentInfo, although it might show the number in a different | |
| 316 | * font size for readability. | |
| 317 | */ | |
| 318 | public Builder setNumber(int number) { | |
| 319 | mNumber = number; | |
| 320 | return this; | |
| 321 | } | |
| 322 | ||
| 323 | /** | |
| 324 | * Set the large text at the right-hand side of the notification. | |
| 325 | */ | |
| 326 | public Builder setContentInfo(CharSequence info) { | |
| 327 | mContentInfo = info; | |
| 328 | return this; | |
| 329 | } | |
| 330 | ||
| 331 | /** | |
| 332 | * Set the progress this notification represents, which may be | |
| 333 | * represented as a {@link android.widget.ProgressBar}. | |
| 334 | */ | |
| 335 | public Builder setProgress(int max, int progress, boolean indeterminate) { | |
| 336 | mProgressMax = max; | |
| 337 | mProgress = progress; | |
| 338 | mProgressIndeterminate = indeterminate; | |
| 339 | return this; | |
| 340 | } | |
| 341 | ||
| 342 | /** | |
| 343 | * Supply a custom RemoteViews to use instead of the standard one. | |
| 344 | */ | |
| 345 | public Builder setContent(RemoteViews views) { | |
| 346 | mNotification.contentView = views; | |
| 347 | return this; | |
| 348 | } | |
| 349 | ||
| 350 | /** | |
| 351 | * Supply a {@link PendingIntent} to send when the notification is clicked. | |
| 352 | * If you do not supply an intent, you can now add PendingIntents to individual | |
| 353 | * views to be launched when clicked by calling {@link RemoteViews#setOnClickPendingIntent | |
| 354 | * RemoteViews.setOnClickPendingIntent(int,PendingIntent)}. Be sure to | |
| 355 | * read {@link Notification#contentIntent Notification.contentIntent} for | |
| 356 | * how to correctly use this. | |
| 357 | */ | |
| 358 | public Builder setContentIntent(PendingIntent intent) { | |
| 359 | mContentIntent = intent; | |
| 360 | return this; | |
| 361 | } | |
| 362 | ||
| 363 | /** | |
| 364 | * Supply a {@link PendingIntent} to send when the notification is cleared by the user | |
| 365 | * directly from the notification panel. For example, this intent is sent when the user | |
| 366 | * clicks the "Clear all" button, or the individual "X" buttons on notifications. This | |
| 367 | * intent is not sent when the application calls {@link NotificationManager#cancel | |
| 368 | * NotificationManager.cancel(int)}. | |
| 369 | */ | |
| 370 | public Builder setDeleteIntent(PendingIntent intent) { | |
| 371 | mNotification.deleteIntent = intent; | |
| 372 | return this; | |
| 373 | } | |
| 374 | ||
| 375 | /** | |
| 376 | * An intent to launch instead of posting the notification to the status bar. | |
| 377 | * Only for use with extremely high-priority notifications demanding the user's | |
| 378 | * <strong>immediate</strong> attention, such as an incoming phone call or | |
| 379 | * alarm clock that the user has explicitly set to a particular time. | |
| 380 | * If this facility is used for something else, please give the user an option | |
| 381 | * to turn it off and use a normal notification, as this can be extremely | |
| 382 | * disruptive. | |
| 383 | * | |
| 384 | * @param intent The pending intent to launch. | |
| 385 | * @param highPriority Passing true will cause this notification to be sent | |
| 386 | * even if other notifications are suppressed. | |
| 387 | */ | |
| 388 | public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) { | |
| 389 | mFullScreenIntent = intent; | |
| 390 | setFlag(FLAG_HIGH_PRIORITY, highPriority); | |
| 391 | return this; | |
| 392 | } | |
| 393 | ||
| 394 | /** | |
| 395 | * Set the text that is displayed in the status bar when the notification first | |
| 396 | * arrives. | |
| 397 | */ | |
| 398 | public Builder setTicker(CharSequence tickerText) { | |
| 399 | mNotification.tickerText = tickerText; | |
| 400 | return this; | |
| 401 | } | |
| 402 | ||
| 403 | /** | |
| 404 | * Set the text that is displayed in the status bar when the notification first | |
| 405 | * arrives, and also a RemoteViews object that may be displayed instead on some | |
| 406 | * devices. | |
| 407 | */ | |
| 408 | public Builder setTicker(CharSequence tickerText, RemoteViews views) { | |
| 409 | mNotification.tickerText = tickerText; | |
| 410 | mTickerView = views; | |
| 411 | return this; | |
| 412 | } | |
| 413 | ||
| 414 | /** | |
| 415 | * Set the large icon that is shown in the ticker and notification. | |
| 416 | */ | |
| 417 | public Builder setLargeIcon(Bitmap icon) { | |
| 418 | mLargeIcon = icon; | |
| 419 | return this; | |
| 420 | } | |
| 421 | ||
| 422 | /** | |
| 423 | * Set the sound to play. It will play on the default stream. | |
| 424 | */ | |
| 425 | public Builder setSound(Uri sound) { | |
| 426 | mNotification.sound = sound; | |
| 427 | mNotification.audioStreamType = Notification.STREAM_DEFAULT; | |
| 428 | return this; | |
| 429 | } | |
| 430 | ||
| 431 | /** | |
| 432 | * Set the sound to play. It will play on the stream you supply. | |
| 433 | * | |
| 434 | * @see #STREAM_DEFAULT | |
| 435 | * @see AudioManager for the <code>STREAM_</code> constants. | |
| 436 | */ | |
| 437 | public Builder setSound(Uri sound, int streamType) { | |
| 438 | mNotification.sound = sound; | |
| 439 | mNotification.audioStreamType = streamType; | |
| 440 | return this; | |
| 441 | } | |
| 442 | ||
| 443 | /** | |
| 444 | * Set the vibration pattern to use. | |
| 445 | * | |
| 446 | * @see android.os.Vibrator for a discussion of the <code>pattern</code> | |
| 447 | * parameter. | |
| 448 | */ | |
| 449 | public Builder setVibrate(long[] pattern) { | |
| 450 | mNotification.vibrate = pattern; | |
| 451 | return this; | |
| 452 | } | |
| 453 | ||
| 454 | /** | |
| 455 | * Set the argb value that you would like the LED on the device to blnk, as well as the | |
| 456 | * rate. The rate is specified in terms of the number of milliseconds to be on | |
| 457 | * and then the number of milliseconds to be off. | |
| 458 | */ | |
| 459 | public Builder setLights(int argb, int onMs, int offMs) { | |
| 460 | mNotification.ledARGB = argb; | |
| 461 | mNotification.ledOnMS = onMs; | |
| 462 | mNotification.ledOffMS = offMs; | |
| 463 | boolean showLights = mNotification.ledOnMS != 0 && mNotification.ledOffMS != 0; | |
| 464 | mNotification.flags = (mNotification.flags & ~Notification.FLAG_SHOW_LIGHTS) | | |
| 465 | (showLights ? Notification.FLAG_SHOW_LIGHTS : 0); | |
| 466 | return this; | |
| 467 | } | |
| 468 | ||
| 469 | /** | |
| 470 | * Set whether this is an ongoing notification. | |
| 471 | * | |
| 472 | * <p>Ongoing notifications differ from regular notifications in the following ways: | |
| 473 | * <ul> | |
| 474 | * <li>Ongoing notifications are sorted above the regular notifications in the | |
| 475 | * notification panel.</li> | |
| 476 | * <li>Ongoing notifications do not have an 'X' close button, and are not affected | |
| 477 | * by the "Clear all" button. | |
| 478 | * </ul> | |
| 479 | */ | |
| 480 | public Builder setOngoing(boolean ongoing) { | |
| 481 | setFlag(Notification.FLAG_ONGOING_EVENT, ongoing); | |
| 482 | return this; | |
| 483 | } | |
| 484 | ||
| 485 | /** | |
| 486 | * Set this flag if you would only like the sound, vibrate | |
| 487 | * and ticker to be played if the notification is not already showing. | |
| 488 | */ | |
| 489 | public Builder setOnlyAlertOnce(boolean onlyAlertOnce) { | |
| 490 | setFlag(Notification.FLAG_ONLY_ALERT_ONCE, onlyAlertOnce); | |
| 491 | return this; | |
| 492 | } | |
| 493 | ||
| 494 | /** | |
| 495 | * Setting this flag will make it so the notification is automatically | |
| 496 | * canceled when the user clicks it in the panel. The PendingIntent | |
| 497 | * set with {@link #setDeleteIntent} will be broadcast when the notification | |
| 498 | * is canceled. | |
| 499 | */ | |
| 500 | public Builder setAutoCancel(boolean autoCancel) { | |
| 501 | setFlag(Notification.FLAG_AUTO_CANCEL, autoCancel); | |
| 502 | return this; | |
| 503 | } | |
| 504 | ||
| 505 | /** | |
| 506 | * Set the default notification options that will be used. | |
| 507 | * <p> | |
| 508 | * The value should be one or more of the following fields combined with | |
| 509 | * bitwise-or: | |
| 510 | * {@link Notification#DEFAULT_SOUND}, {@link Notification#DEFAULT_VIBRATE}, | |
| 511 | * {@link Notification#DEFAULT_LIGHTS}. | |
| 512 | * <p> | |
| 513 | * For all default values, use {@link Notification#DEFAULT_ALL}. | |
| 514 | */ | |
| 515 | public Builder setDefaults(int defaults) { | |
| 516 | mNotification.defaults = defaults; | |
| 517 | if ((defaults & Notification.DEFAULT_LIGHTS) != 0) { | |
| 518 | mNotification.flags |= Notification.FLAG_SHOW_LIGHTS; | |
| 519 | } | |
| 520 | return this; | |
| 521 | } | |
| 522 | ||
| 523 | private void setFlag(int mask, boolean value) { | |
| 524 | if (value) { | |
| 525 | mNotification.flags |= mask; | |
| 526 | } else { | |
| 527 | mNotification.flags &= ~mask; | |
| 528 | } | |
| 529 | } | |
| 530 | ||
| 531 | /** | |
| 532 | * Set the relative priority for this notification. | |
| 533 | * | |
| 534 | * Priority is an indication of how much of the user's | |
| 535 | * valuable attention should be consumed by this | |
| 536 | * notification. Low-priority notifications may be hidden from | |
| 537 | * the user in certain situations, while the user might be | |
| 538 | * interrupted for a higher-priority notification. | |
| 539 | * The system sets a notification's priority based on various factors including the | |
| 540 | * setPriority value. The effect may differ slightly on different platforms. | |
| 541 | */ | |
| 542 | public Builder setPriority(int pri) { | |
| 543 | mPriority = pri; | |
| 544 | return this; | |
| 545 | } | |
| 546 | ||
| 547 | /** | |
| 548 | * Add an action to this notification. Actions are typically displayed by | |
| 549 | * the system as a button adjacent to the notification content. | |
| 550 | * <br> | |
| 551 | * Action buttons won't appear on platforms prior to Android 4.1. Action | |
| 552 | * buttons depend on expanded notifications, which are only available in Android 4.1 | |
| 553 | * and later. To ensure that an action button's functionality is always available, first | |
| 554 | * implement the functionality in the {@link android.app.Activity} that starts when a user | |
| 555 | * clicks the notification (see {@link #setContentIntent setContentIntent()}), and then | |
| 556 | * enhance the notification by implementing the same functionality with | |
| 557 | * {@link #addAction addAction()}. | |
| 558 | * | |
| 559 | * @param icon Resource ID of a drawable that represents the action. | |
| 560 | * @param title Text describing the action. | |
| 561 | * @param intent {@link android.app.PendingIntent} to be fired when the action is invoked. | |
| 562 | */ | |
| 563 | public Builder addAction(int icon, CharSequence title, PendingIntent intent) { | |
| 564 | mActions.add(new Action(icon, title, intent)); | |
| 565 | return this; | |
| 566 | } | |
| 567 | ||
| 568 | /** | |
| 569 | * Add a rich notification style to be applied at build time. | |
| 570 | * <br> | |
| 571 | * If the platform does not provide rich notification styles, this method has no effect. The | |
| 572 | * user will always see the normal notification style. | |
| 573 | * | |
| 574 | * @param style Object responsible for modifying the notification style. | |
| 575 | */ | |
| 576 | public Builder setStyle(Style style) { | |
| 577 | if (mStyle != style) { | |
| 578 | mStyle = style; | |
| 579 | if (mStyle != null) { | |
| 580 | mStyle.setBuilder(this); | |
| 581 | } | |
| 582 | } | |
| 583 | return this; | |
| 584 | } | |
| 585 | ||
| 586 | /** | |
| 587 | * @deprecated Use {@link #build()} instead. | |
| 588 | */ | |
| 589 | @Deprecated | |
| 590 | public Notification getNotification() { | |
| 591 | return IMPL.build(this); | |
| 592 | } | |
| 593 | ||
| 594 | /** | |
| 595 | * Combine all of the options that have been set and return a new {@link Notification} | |
| 596 | * object. | |
| 597 | */ | |
| 598 | public Notification build() { | |
| 599 | return IMPL.build(this); | |
| 600 | } | |
| 601 | } | |
| 602 | ||
| 603 | /** | |
| 604 | * An object that can apply a rich notification style to a {@link Notification.Builder} | |
| 605 | * object. | |
| 606 | * <br> | |
| 607 | * If the platform does not provide rich notification styles, methods in this class have no | |
| 608 | * effect. | |
| 609 | */ | |
| 610 | public static abstract class Style | |
| 611 | { | |
| 612 | Builder mBuilder; | |
| 613 | CharSequence mBigContentTitle; | |
| 614 | CharSequence mSummaryText; | |
| 615 | boolean mSummaryTextSet = false; | |
| 616 | ||
| 617 | public void setBuilder(Builder builder) { | |
| 618 | if (mBuilder != builder) { | |
| 619 | mBuilder = builder; | |
| 620 | if (mBuilder != null) { | |
| 621 | mBuilder.setStyle(this); | |
| 622 | } | |
| 623 | } | |
| 624 | } | |
| 625 | ||
| 626 | public Notification build() { | |
| 627 | Notification notification = null; | |
| 628 | if (mBuilder != null) { | |
| 629 | notification = mBuilder.build(); | |
| 630 | } | |
| 631 | return notification; | |
| 632 | } | |
| 633 | } | |
| 634 | ||
| 635 | /** | |
| 636 | * Helper class for generating large-format notifications that include a large image attachment. | |
| 637 | * <br> | |
| 638 | * If the platform does not provide large-format notifications, this method has no effect. The | |
| 639 | * user will always see the normal notification view. | |
| 640 | * <br> | |
| 641 | * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like so: | |
| 642 | * <pre class="prettyprint"> | |
| 643 | * Notification noti = new Notification.Builder() | |
| 644 | * .setContentTitle("New photo from " + sender.toString()) | |
| 645 | * .setContentText(subject) | |
| 646 | * .setSmallIcon(R.drawable.new_post) | |
| 647 | * .setLargeIcon(aBitmap) | |
| 648 | * .setStyle(new Notification.BigPictureStyle() | |
| 649 | * .bigPicture(aBigBitmap)) | |
| 650 | * .build(); | |
| 651 | * </pre> | |
| 652 | * | |
| 653 | * @see Notification#bigContentView | |
| 654 | */ | |
| 655 | public static class BigPictureStyle extends Style { | |
| 656 | Bitmap mPicture; | |
| 657 | ||
| 658 | public BigPictureStyle() { | |
| 659 | } | |
| 660 | ||
| 661 | public BigPictureStyle(Builder builder) { | |
| 662 | setBuilder(builder); | |
| 663 | } | |
| 664 | ||
| 665 | /** | |
| 666 | * Overrides ContentTitle in the big form of the template. | |
| 667 | * This defaults to the value passed to setContentTitle(). | |
| 668 | */ | |
| 669 | public BigPictureStyle setBigContentTitle(CharSequence title) { | |
| 670 | mBigContentTitle = title; | |
| 671 | return this; | |
| 672 | } | |
| 673 | ||
| 674 | /** | |
| 675 | * Set the first line of text after the detail section in the big form of the template. | |
| 676 | */ | |
| 677 | public BigPictureStyle setSummaryText(CharSequence cs) { | |
| 678 | mSummaryText = cs; | |
| 679 | mSummaryTextSet = true; | |
| 680 | return this; | |
| 681 | } | |
| 682 | ||
| 683 | /** | |
| 684 | * Provide the bitmap to be used as the payload for the BigPicture notification. | |
| 685 | */ | |
| 686 | public BigPictureStyle bigPicture(Bitmap b) { | |
| 687 | mPicture = b; | |
| 688 | return this; | |
| 689 | } | |
| 690 | } | |
| 691 | ||
| 692 | /** | |
| 693 | * Helper class for generating large-format notifications that include a lot of text. | |
| 694 | * | |
| 695 | * <br> | |
| 696 | * If the platform does not provide large-format notifications, this method has no effect. The | |
| 697 | * user will always see the normal notification view. | |
| 698 | * <br> | |
| 699 | * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like so: | |
| 700 | * <pre class="prettyprint"> | |
| 701 | * Notification noti = new Notification.Builder() | |
| 702 | * .setContentTitle("New mail from " + sender.toString()) | |
| 703 | * .setContentText(subject) | |
| 704 | * .setSmallIcon(R.drawable.new_mail) | |
| 705 | * .setLargeIcon(aBitmap) | |
| 706 | * .setStyle(new Notification.BigTextStyle() | |
| 707 | * .bigText(aVeryLongString)) | |
| 708 | * .build(); | |
| 709 | * </pre> | |
| 710 | * | |
| 711 | * @see Notification#bigContentView | |
| 712 | */ | |
| 713 | public static class BigTextStyle extends Style { | |
| 714 | CharSequence mBigText; | |
| 715 | ||
| 716 | public BigTextStyle() { | |
| 717 | } | |
| 718 | ||
| 719 | public BigTextStyle(Builder builder) { | |
| 720 | setBuilder(builder); | |
| 721 | } | |
| 722 | ||
| 723 | /** | |
| 724 | * Overrides ContentTitle in the big form of the template. | |
| 725 | * This defaults to the value passed to setContentTitle(). | |
| 726 | */ | |
| 727 | public BigTextStyle setBigContentTitle(CharSequence title) { | |
| 728 | mBigContentTitle = title; | |
| 729 | return this; | |
| 730 | } | |
| 731 | ||
| 732 | /** | |
| 733 | * Set the first line of text after the detail section in the big form of the template. | |
| 734 | */ | |
| 735 | public BigTextStyle setSummaryText(CharSequence cs) { | |
| 736 | mSummaryText = cs; | |
| 737 | mSummaryTextSet = true; | |
| 738 | return this; | |
| 739 | } | |
| 740 | ||
| 741 | /** | |
| 742 | * Provide the longer text to be displayed in the big form of the | |
| 743 | * template in place of the content text. | |
| 744 | */ | |
| 745 | public BigTextStyle bigText(CharSequence cs) { | |
| 746 | mBigText = cs; | |
| 747 | return this; | |
| 748 | } | |
| 749 | } | |
| 750 | ||
| 751 | /** | |
| 752 | * Helper class for generating large-format notifications that include a list of (up to 5) strings. | |
| 753 | * | |
| 754 | * <br> | |
| 755 | * If the platform does not provide large-format notifications, this method has no effect. The | |
| 756 | * user will always see the normal notification view. | |
| 757 | * <br> | |
| 758 | * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like so: | |
| 759 | * <pre class="prettyprint"> | |
| 760 | * Notification noti = new Notification.Builder() | |
| 761 | * .setContentTitle("5 New mails from " + sender.toString()) | |
| 762 | * .setContentText(subject) | |
| 763 | * .setSmallIcon(R.drawable.new_mail) | |
| 764 | * .setLargeIcon(aBitmap) | |
| 765 | * .setStyle(new Notification.InboxStyle() | |
| 766 | * .addLine(str1) | |
| 767 | * .addLine(str2) | |
| 768 | * .setContentTitle("") | |
| 769 | * .setSummaryText("+3 more")) | |
| 770 | * .build(); | |
| 771 | * </pre> | |
| 772 | * | |
| 773 | * @see Notification#bigContentView | |
| 774 | */ | |
| 775 | public static class InboxStyle extends Style { | |
| 776 | ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(); | |
| 777 | ||
| 778 | public InboxStyle() { | |
| 779 | } | |
| 780 | ||
| 781 | public InboxStyle(Builder builder) { | |
| 782 | setBuilder(builder); | |
| 783 | } | |
| 784 | ||
| 785 | /** | |
| 786 | * Overrides ContentTitle in the big form of the template. | |
| 787 | * This defaults to the value passed to setContentTitle(). | |
| 788 | */ | |
| 789 | public InboxStyle setBigContentTitle(CharSequence title) { | |
| 790 | mBigContentTitle = title; | |
| 791 | return this; | |
| 792 | } | |
| 793 | ||
| 794 | /** | |
| 795 | * Set the first line of text after the detail section in the big form of the template. | |
| 796 | */ | |
| 797 | public InboxStyle setSummaryText(CharSequence cs) { | |
| 798 | mSummaryText = cs; | |
| 799 | mSummaryTextSet = true; | |
| 800 | return this; | |
| 801 | } | |
| 802 | ||
| 803 | /** | |
| 804 | * Append a line to the digest section of the Inbox notification. | |
| 805 | */ | |
| 806 | public InboxStyle addLine(CharSequence cs) { | |
| 807 | mTexts.add(cs); | |
| 808 | return this; | |
| 809 | } | |
| 810 | } | |
| 811 | ||
| 812 | public static class Action { | |
| 813 | public int icon; | |
| 814 | public CharSequence title; | |
| 815 | public PendingIntent actionIntent; | |
| 816 | ||
| 817 | public Action(int icon_, CharSequence title_, PendingIntent intent_) { | |
| 818 | this.icon = icon_; | |
| 819 | this.title = title_; | |
| 820 | this.actionIntent = intent_; | |
| 821 | } | |
| 822 | } | |
| 823 | } |