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 | |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 19 | import android.annotation.IntDef; |
Daniel Sandler | 01df1c6 | 2014-06-09 10:54:01 -0400 | [diff] [blame] | 20 | import android.annotation.SdkConstant; |
| 21 | import android.annotation.SdkConstant.SdkConstantType; |
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; |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 24 | import android.content.pm.PackageManager.NameNotFoundException; |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 25 | import android.content.res.Resources; |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 26 | import android.graphics.Bitmap; |
Kenny Guy | 8a0101b | 2014-05-08 23:34:12 +0100 | [diff] [blame] | 27 | import android.graphics.Canvas; |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 28 | import android.graphics.PorterDuff; |
Kenny Guy | 8a0101b | 2014-05-08 23:34:12 +0100 | [diff] [blame] | 29 | import android.graphics.drawable.Drawable; |
John Spurlock | c0650f02 | 2014-07-19 13:22:39 -0400 | [diff] [blame] | 30 | import android.media.AudioAttributes; |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 31 | import android.media.AudioManager; |
Jeff Brown | dba34ba | 2014-06-24 20:46:03 -0700 | [diff] [blame] | 32 | import android.media.session.MediaSession; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.net.Uri; |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 34 | import android.os.BadParcelableException; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 35 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.os.Parcel; |
| 37 | import android.os.Parcelable; |
Daniel Sandler | a2985ed | 2012-04-03 16:42:00 -0400 | [diff] [blame] | 38 | import android.os.SystemClock; |
Jeff Sharkey | 6d51571 | 2012-09-20 16:06:08 -0700 | [diff] [blame] | 39 | import android.os.UserHandle; |
Kenny Guy | 8a0101b | 2014-05-08 23:34:12 +0100 | [diff] [blame] | 40 | import android.os.UserManager; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 41 | import android.text.TextUtils; |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 42 | import android.util.Log; |
Jorim Jaggi | 445d3c0 | 2014-08-19 22:33:42 +0200 | [diff] [blame] | 43 | import android.util.MathUtils; |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 44 | import android.util.TypedValue; |
Griff Hazen | 61a9e86 | 2014-05-22 16:05:19 -0700 | [diff] [blame] | 45 | import android.view.Gravity; |
Joe Onorato | 8595a3d | 2010-11-19 18:12:07 -0800 | [diff] [blame] | 46 | import android.view.View; |
Jeff Sharkey | 1c40013 | 2011-08-05 14:50:13 -0700 | [diff] [blame] | 47 | import android.widget.ProgressBar; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 48 | import android.widget.RemoteViews; |
| 49 | |
Griff Hazen | 959591e | 2014-05-15 22:26:18 -0700 | [diff] [blame] | 50 | import com.android.internal.R; |
Griff Hazen | c091ba8 | 2014-05-16 10:13:26 -0700 | [diff] [blame] | 51 | import com.android.internal.util.NotificationColorUtil; |
Griff Hazen | 959591e | 2014-05-15 22:26:18 -0700 | [diff] [blame] | 52 | |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 53 | import java.lang.annotation.Retention; |
| 54 | import java.lang.annotation.RetentionPolicy; |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 55 | import java.lang.reflect.Constructor; |
Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 56 | import java.text.NumberFormat; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 57 | import java.util.ArrayList; |
Griff Hazen | 61a9e86 | 2014-05-22 16:05:19 -0700 | [diff] [blame] | 58 | import java.util.Arrays; |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 59 | import java.util.Collections; |
Griff Hazen | 61a9e86 | 2014-05-22 16:05:19 -0700 | [diff] [blame] | 60 | import java.util.List; |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 61 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 62 | /** |
| 63 | * A class that represents how a persistent notification is to be presented to |
| 64 | * the user using the {@link android.app.NotificationManager}. |
| 65 | * |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 66 | * <p>The {@link Notification.Builder Notification.Builder} has been added to make it |
| 67 | * easier to construct Notifications.</p> |
| 68 | * |
Joe Fernandez | 558459f | 2011-10-13 16:47:36 -0700 | [diff] [blame] | 69 | * <div class="special reference"> |
| 70 | * <h3>Developer Guides</h3> |
| 71 | * <p>For a guide to creating notifications, read the |
| 72 | * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a> |
| 73 | * developer guide.</p> |
| 74 | * </div> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 75 | */ |
| 76 | public class Notification implements Parcelable |
| 77 | { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 78 | private static final String TAG = "Notification"; |
| 79 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 80 | /** |
Daniel Sandler | 01df1c6 | 2014-06-09 10:54:01 -0400 | [diff] [blame] | 81 | * An activity that provides a user interface for adjusting notification preferences for its |
| 82 | * containing application. Optional but recommended for apps that post |
| 83 | * {@link android.app.Notification Notifications}. |
| 84 | */ |
| 85 | @SdkConstant(SdkConstantType.INTENT_CATEGORY) |
| 86 | public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES |
| 87 | = "android.intent.category.NOTIFICATION_PREFERENCES"; |
| 88 | |
| 89 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | * Use all default values (where applicable). |
| 91 | */ |
| 92 | public static final int DEFAULT_ALL = ~0; |
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 | /** |
| 95 | * Use the default notification sound. This will ignore any given |
| 96 | * {@link #sound}. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 97 | * |
Chris Wren | 47c20a1 | 2014-06-18 17:27:29 -0400 | [diff] [blame] | 98 | * <p> |
| 99 | * A notification that is noisy is more likely to be presented as a heads-up notification. |
| 100 | * </p> |
| 101 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | * @see #defaults |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 103 | */ |
| 104 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | public static final int DEFAULT_SOUND = 1; |
| 106 | |
| 107 | /** |
| 108 | * Use the default notification vibrate. This will ignore any given |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 109 | * {@link #vibrate}. Using phone vibration requires the |
Scott Main | b8b3645 | 2009-04-26 15:50:49 -0700 | [diff] [blame] | 110 | * {@link android.Manifest.permission#VIBRATE VIBRATE} permission. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 111 | * |
Chris Wren | 47c20a1 | 2014-06-18 17:27:29 -0400 | [diff] [blame] | 112 | * <p> |
| 113 | * A notification that vibrates is more likely to be presented as a heads-up notification. |
| 114 | * </p> |
| 115 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | * @see #defaults |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 117 | */ |
| 118 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | public static final int DEFAULT_VIBRATE = 2; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 120 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | /** |
| 122 | * Use the default notification lights. This will ignore the |
| 123 | * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or |
| 124 | * {@link #ledOnMS}. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 125 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 126 | * @see #defaults |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 127 | */ |
| 128 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 129 | public static final int DEFAULT_LIGHTS = 4; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 130 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 132 | * A timestamp related to this notification, in milliseconds since the epoch. |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 133 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 134 | * Default value: {@link System#currentTimeMillis() Now}. |
| 135 | * |
| 136 | * Choose a timestamp that will be most relevant to the user. For most finite events, this |
| 137 | * corresponds to the time the event happened (or will happen, in the case of events that have |
| 138 | * 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] | 139 | * timestamped according to when the activity began. |
| 140 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 141 | * Some examples: |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 142 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 143 | * <ul> |
| 144 | * <li>Notification of a new chat message should be stamped when the message was received.</li> |
| 145 | * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li> |
| 146 | * <li>Notification of a completed file download should be stamped when the download finished.</li> |
| 147 | * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li> |
| 148 | * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time. |
| 149 | * <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] | 150 | * </ul> |
| 151 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | */ |
| 153 | public long when; |
| 154 | |
| 155 | /** |
| 156 | * 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] | 157 | * 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] | 158 | */ |
| 159 | public int icon; |
| 160 | |
| 161 | /** |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 162 | * If the icon in the status bar is to have more than one level, you can set this. Otherwise, |
| 163 | * leave it at its default value of 0. |
| 164 | * |
| 165 | * @see android.widget.ImageView#setImageLevel |
Griff Hazen | 959591e | 2014-05-15 22:26:18 -0700 | [diff] [blame] | 166 | * @see android.graphics.drawable.Drawable#setLevel |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 167 | */ |
| 168 | public int iconLevel; |
| 169 | |
| 170 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 171 | * The number of events that this notification represents. For example, in a new mail |
| 172 | * notification, this could be the number of unread messages. |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 173 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 174 | * The system may or may not use this field to modify the appearance of the notification. For |
| 175 | * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was |
| 176 | * superimposed over the icon in the status bar. Starting with |
| 177 | * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by |
| 178 | * {@link Notification.Builder} has displayed the number in the expanded notification view. |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 179 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 180 | * 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] | 181 | */ |
| 182 | public int number; |
| 183 | |
| 184 | /** |
| 185 | * The intent to execute when the expanded status entry is clicked. If |
| 186 | * this is an activity, it must include the |
| 187 | * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 188 | * that you take care of task management as described in the |
| 189 | * <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] | 190 | * Stack</a> document. In particular, make sure to read the notification section |
| 191 | * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling |
| 192 | * Notifications</a> for the correct ways to launch an application from a |
| 193 | * notification. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | */ |
| 195 | public PendingIntent contentIntent; |
| 196 | |
| 197 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 198 | * The intent to execute when the notification is explicitly dismissed by the user, either with |
| 199 | * the "Clear All" button or by swiping it away individually. |
| 200 | * |
| 201 | * This probably shouldn't be launching an activity since several of those will be sent |
| 202 | * at the same time. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | */ |
| 204 | public PendingIntent deleteIntent; |
| 205 | |
| 206 | /** |
Dianne Hackborn | 170bae7 | 2010-09-03 15:14:28 -0700 | [diff] [blame] | 207 | * 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] | 208 | * |
Chris Wren | 47c20a1 | 2014-06-18 17:27:29 -0400 | [diff] [blame] | 209 | * <p> |
| 210 | * The system UI may choose to display a heads-up notification, instead of |
| 211 | * launching this intent, while the user is using the device. |
| 212 | * </p> |
| 213 | * |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 214 | * @see Notification.Builder#setFullScreenIntent |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 215 | */ |
| 216 | public PendingIntent fullScreenIntent; |
| 217 | |
| 218 | /** |
Dan Sandler | 5fcdf6e | 2014-07-18 11:31:15 -0400 | [diff] [blame] | 219 | * Text that summarizes this notification for accessibility services. |
| 220 | * |
| 221 | * As of the L release, this text is no longer shown on screen, but it is still useful to |
| 222 | * accessibility services (where it serves as an audible announcement of the notification's |
| 223 | * appearance). |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 224 | * |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 225 | * @see #tickerView |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | */ |
| 227 | public CharSequence tickerText; |
| 228 | |
| 229 | /** |
Dan Sandler | 5fcdf6e | 2014-07-18 11:31:15 -0400 | [diff] [blame] | 230 | * Formerly, a view showing the {@link #tickerText}. |
| 231 | * |
| 232 | * No longer displayed in the status bar as of API 21. |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 233 | */ |
Dan Sandler | 5fcdf6e | 2014-07-18 11:31:15 -0400 | [diff] [blame] | 234 | @Deprecated |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 235 | public RemoteViews tickerView; |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 236 | |
| 237 | /** |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 238 | * 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] | 239 | */ |
| 240 | public RemoteViews contentView; |
| 241 | |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 242 | /** |
Daniel Sandler | 4dfbe83 | 2012-04-11 14:51:46 -0400 | [diff] [blame] | 243 | * A large-format version of {@link #contentView}, giving the Notification an |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 244 | * opportunity to show more detail. The system UI may choose to show this |
| 245 | * instead of the normal content view at its discretion. |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 246 | */ |
| 247 | public RemoteViews bigContentView; |
| 248 | |
Chris Wren | 8fd39ec | 2014-02-27 17:43:26 -0500 | [diff] [blame] | 249 | |
| 250 | /** |
Chris Wren | 47c20a1 | 2014-06-18 17:27:29 -0400 | [diff] [blame] | 251 | * A medium-format version of {@link #contentView}, providing the Notification an |
| 252 | * opportunity to add action buttons to contentView. At its discretion, the system UI may |
| 253 | * choose to show this as a heads-up notification, which will pop up so the user can see |
| 254 | * it without leaving their current activity. |
Chris Wren | 8fd39ec | 2014-02-27 17:43:26 -0500 | [diff] [blame] | 255 | */ |
| 256 | public RemoteViews headsUpContentView; |
| 257 | |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 258 | /** |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 259 | * 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] | 260 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 261 | public Bitmap largeIcon; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | |
| 263 | /** |
| 264 | * The sound to play. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 265 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | * <p> |
Chris Wren | 47c20a1 | 2014-06-18 17:27:29 -0400 | [diff] [blame] | 267 | * A notification that is noisy is more likely to be presented as a heads-up notification. |
| 268 | * </p> |
| 269 | * |
| 270 | * <p> |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 271 | * To play the default notification sound, see {@link #defaults}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | * </p> |
| 273 | */ |
| 274 | public Uri sound; |
| 275 | |
| 276 | /** |
| 277 | * Use this constant as the value for audioStreamType to request that |
| 278 | * the default stream type for notifications be used. Currently the |
Jeff Sharkey | 098d580 | 2012-04-26 17:30:34 -0700 | [diff] [blame] | 279 | * default stream type is {@link AudioManager#STREAM_NOTIFICATION}. |
John Spurlock | c0650f02 | 2014-07-19 13:22:39 -0400 | [diff] [blame] | 280 | * |
| 281 | * @deprecated Use {@link #audioAttributes} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 282 | */ |
Jean-Michel Trivi | 81f871e | 2014-08-06 16:32:38 -0700 | [diff] [blame] | 283 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | public static final int STREAM_DEFAULT = -1; |
| 285 | |
| 286 | /** |
| 287 | * The audio stream type to use when playing the sound. |
| 288 | * Should be one of the STREAM_ constants from |
| 289 | * {@link android.media.AudioManager}. |
John Spurlock | c0650f02 | 2014-07-19 13:22:39 -0400 | [diff] [blame] | 290 | * |
| 291 | * @deprecated Use {@link #audioAttributes} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 292 | */ |
Jean-Michel Trivi | 81f871e | 2014-08-06 16:32:38 -0700 | [diff] [blame] | 293 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | public int audioStreamType = STREAM_DEFAULT; |
| 295 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 296 | /** |
John Spurlock | c0650f02 | 2014-07-19 13:22:39 -0400 | [diff] [blame] | 297 | * The default value of {@link #audioAttributes}. |
| 298 | */ |
| 299 | public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder() |
| 300 | .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION) |
| 301 | .setUsage(AudioAttributes.USAGE_NOTIFICATION) |
| 302 | .build(); |
| 303 | |
| 304 | /** |
| 305 | * The {@link AudioAttributes audio attributes} to use when playing the sound. |
| 306 | */ |
| 307 | public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT; |
| 308 | |
| 309 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 310 | * The pattern with which to vibrate. |
| 311 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 312 | * <p> |
| 313 | * To vibrate the default pattern, see {@link #defaults}. |
| 314 | * </p> |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 315 | * |
Chris Wren | 47c20a1 | 2014-06-18 17:27:29 -0400 | [diff] [blame] | 316 | * <p> |
| 317 | * A notification that vibrates is more likely to be presented as a heads-up notification. |
| 318 | * </p> |
| 319 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 320 | * @see android.os.Vibrator#vibrate(long[],int) |
| 321 | */ |
| 322 | public long[] vibrate; |
| 323 | |
| 324 | /** |
| 325 | * The color of the led. The hardware will do its best approximation. |
| 326 | * |
| 327 | * @see #FLAG_SHOW_LIGHTS |
| 328 | * @see #flags |
| 329 | */ |
| 330 | public int ledARGB; |
| 331 | |
| 332 | /** |
| 333 | * The number of milliseconds for the LED to be on while it's flashing. |
| 334 | * The hardware will do its best approximation. |
| 335 | * |
| 336 | * @see #FLAG_SHOW_LIGHTS |
| 337 | * @see #flags |
| 338 | */ |
| 339 | public int ledOnMS; |
| 340 | |
| 341 | /** |
| 342 | * The number of milliseconds for the LED to be off while it's flashing. |
| 343 | * The hardware will do its best approximation. |
| 344 | * |
| 345 | * @see #FLAG_SHOW_LIGHTS |
| 346 | * @see #flags |
| 347 | */ |
| 348 | public int ledOffMS; |
| 349 | |
| 350 | /** |
| 351 | * Specifies which values should be taken from the defaults. |
| 352 | * <p> |
| 353 | * To set, OR the desired from {@link #DEFAULT_SOUND}, |
| 354 | * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default |
| 355 | * values, use {@link #DEFAULT_ALL}. |
| 356 | * </p> |
| 357 | */ |
| 358 | public int defaults; |
| 359 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 360 | /** |
| 361 | * Bit to be bitwise-ored into the {@link #flags} field that should be |
| 362 | * set if you want the LED on for this notification. |
| 363 | * <ul> |
| 364 | * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB |
| 365 | * or 0 for both ledOnMS and ledOffMS.</li> |
| 366 | * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li> |
| 367 | * <li>To flash the LED, pass the number of milliseconds that it should |
| 368 | * be on and off to ledOnMS and ledOffMS.</li> |
| 369 | * </ul> |
| 370 | * <p> |
| 371 | * Since hardware varies, you are not guaranteed that any of the values |
| 372 | * you pass are honored exactly. Use the system defaults (TODO) if possible |
| 373 | * because they will be set to values that work on any given hardware. |
| 374 | * <p> |
| 375 | * The alpha channel must be set for forward compatibility. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 376 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 377 | */ |
| 378 | public static final int FLAG_SHOW_LIGHTS = 0x00000001; |
| 379 | |
| 380 | /** |
| 381 | * Bit to be bitwise-ored into the {@link #flags} field that should be |
| 382 | * set if this notification is in reference to something that is ongoing, |
| 383 | * like a phone call. It should not be set if this notification is in |
| 384 | * reference to something that happened at a particular point in time, |
| 385 | * like a missed phone call. |
| 386 | */ |
| 387 | public static final int FLAG_ONGOING_EVENT = 0x00000002; |
| 388 | |
| 389 | /** |
| 390 | * 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] | 391 | * the audio will be repeated until the notification is |
| 392 | * cancelled or the notification window is opened. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 393 | */ |
| 394 | public static final int FLAG_INSISTENT = 0x00000004; |
| 395 | |
| 396 | /** |
| 397 | * Bit to be bitwise-ored into the {@link #flags} field that should be |
Griff Hazen | 293977b | 2014-04-28 08:37:20 -0700 | [diff] [blame] | 398 | * set if you would only like the sound, vibrate and ticker to be played |
| 399 | * if the notification was not already showing. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 400 | */ |
| 401 | public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008; |
| 402 | |
| 403 | /** |
| 404 | * Bit to be bitwise-ored into the {@link #flags} field that should be |
| 405 | * 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] | 406 | * user. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 407 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 408 | */ |
| 409 | public static final int FLAG_AUTO_CANCEL = 0x00000010; |
| 410 | |
| 411 | /** |
| 412 | * Bit to be bitwise-ored into the {@link #flags} field that should be |
| 413 | * set if the notification should not be canceled when the user clicks |
| 414 | * the Clear all button. |
| 415 | */ |
| 416 | public static final int FLAG_NO_CLEAR = 0x00000020; |
| 417 | |
Dianne Hackborn | d8a43f6 | 2009-08-17 23:33:56 -0700 | [diff] [blame] | 418 | /** |
| 419 | * Bit to be bitwise-ored into the {@link #flags} field that should be |
| 420 | * set if this notification represents a currently running service. This |
| 421 | * will normally be set for you by {@link Service#startForeground}. |
| 422 | */ |
| 423 | public static final int FLAG_FOREGROUND_SERVICE = 0x00000040; |
| 424 | |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 425 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 426 | * Obsolete flag indicating high-priority notifications; use the priority field instead. |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 427 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 428 | * @deprecated Use {@link #priority} with a positive value. |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 429 | */ |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 430 | public static final int FLAG_HIGH_PRIORITY = 0x00000080; |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 431 | |
Griff Hazen | dfcb080 | 2014-02-11 12:00:00 -0800 | [diff] [blame] | 432 | /** |
| 433 | * Bit to be bitswise-ored into the {@link #flags} field that should be |
| 434 | * set if this notification is relevant to the current device only |
| 435 | * and it is not recommended that it bridge to other devices. |
| 436 | */ |
| 437 | public static final int FLAG_LOCAL_ONLY = 0x00000100; |
| 438 | |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 439 | /** |
| 440 | * Bit to be bitswise-ored into the {@link #flags} field that should be |
| 441 | * set if this notification is the group summary for a group of notifications. |
| 442 | * Grouped notifications may display in a cluster or stack on devices which |
| 443 | * support such rendering. Requires a group key also be set using {@link Builder#setGroup}. |
| 444 | */ |
| 445 | public static final int FLAG_GROUP_SUMMARY = 0x00000200; |
| 446 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 447 | public int flags; |
| 448 | |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 449 | /** @hide */ |
| 450 | @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX}) |
| 451 | @Retention(RetentionPolicy.SOURCE) |
| 452 | public @interface Priority {} |
| 453 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 455 | * Default notification {@link #priority}. If your application does not prioritize its own |
| 456 | * notifications, use this value for all notifications. |
| 457 | */ |
| 458 | public static final int PRIORITY_DEFAULT = 0; |
| 459 | |
| 460 | /** |
| 461 | * Lower {@link #priority}, for items that are less important. The UI may choose to show these |
| 462 | * items smaller, or at a different position in the list, compared with your app's |
| 463 | * {@link #PRIORITY_DEFAULT} items. |
| 464 | */ |
| 465 | public static final int PRIORITY_LOW = -1; |
| 466 | |
| 467 | /** |
| 468 | * Lowest {@link #priority}; these items might not be shown to the user except under special |
| 469 | * circumstances, such as detailed notification logs. |
| 470 | */ |
| 471 | public static final int PRIORITY_MIN = -2; |
| 472 | |
| 473 | /** |
| 474 | * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to |
| 475 | * show these items larger, or at a different position in notification lists, compared with |
| 476 | * your app's {@link #PRIORITY_DEFAULT} items. |
| 477 | */ |
| 478 | public static final int PRIORITY_HIGH = 1; |
| 479 | |
| 480 | /** |
| 481 | * Highest {@link #priority}, for your application's most important items that require the |
| 482 | * user's prompt attention or input. |
| 483 | */ |
| 484 | public static final int PRIORITY_MAX = 2; |
| 485 | |
| 486 | /** |
| 487 | * Relative priority for this notification. |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 488 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 489 | * Priority is an indication of how much of the user's valuable attention should be consumed by |
| 490 | * this notification. Low-priority notifications may be hidden from the user in certain |
| 491 | * 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] | 492 | * system will make a determination about how to interpret this priority when presenting |
| 493 | * the notification. |
Chris Wren | 47c20a1 | 2014-06-18 17:27:29 -0400 | [diff] [blame] | 494 | * |
| 495 | * <p> |
| 496 | * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented |
| 497 | * as a heads-up notification. |
| 498 | * </p> |
| 499 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 500 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 501 | @Priority |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 502 | public int priority; |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 503 | |
Dan Sandler | 26e81cf | 2014-05-06 10:01:27 -0400 | [diff] [blame] | 504 | /** |
| 505 | * Accent color (an ARGB integer like the constants in {@link android.graphics.Color}) |
| 506 | * to be applied by the standard Style templates when presenting this notification. |
| 507 | * |
| 508 | * The current template design constructs a colorful header image by overlaying the |
| 509 | * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are |
| 510 | * ignored. |
| 511 | */ |
| 512 | public int color = COLOR_DEFAULT; |
| 513 | |
| 514 | /** |
| 515 | * Special value of {@link #color} telling the system not to decorate this notification with |
| 516 | * any special color but instead use default colors when presenting this notification. |
| 517 | */ |
| 518 | public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT |
Dan Sandler | 0bf2ed8 | 2013-12-21 23:33:41 -0600 | [diff] [blame] | 519 | |
| 520 | /** |
| 521 | * Sphere of visibility of this notification, which affects how and when the SystemUI reveals |
| 522 | * the notification's presence and contents in untrusted situations (namely, on the secure |
| 523 | * lockscreen). |
| 524 | * |
| 525 | * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always |
| 526 | * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are |
| 527 | * shown in all situations, but the contents are only available if the device is unlocked for |
| 528 | * the appropriate user. |
| 529 | * |
| 530 | * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification |
| 531 | * can be read even in an "insecure" context (that is, above a secure lockscreen). |
| 532 | * To modify the public version of this notification—for example, to redact some portions—see |
| 533 | * {@link Builder#setPublicVersion(Notification)}. |
| 534 | * |
| 535 | * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon |
| 536 | * and ticker until the user has bypassed the lockscreen. |
| 537 | */ |
| 538 | public int visibility; |
| 539 | |
| 540 | public static final int VISIBILITY_PUBLIC = 1; |
| 541 | public static final int VISIBILITY_PRIVATE = 0; |
| 542 | public static final int VISIBILITY_SECRET = -1; |
| 543 | |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 544 | /** |
John Spurlock | fd7f1e0 | 2014-03-18 16:41:57 -0400 | [diff] [blame] | 545 | * Notification category: incoming call (voice or video) or similar synchronous communication request. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 546 | */ |
John Spurlock | fd7f1e0 | 2014-03-18 16:41:57 -0400 | [diff] [blame] | 547 | public static final String CATEGORY_CALL = "call"; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 548 | |
| 549 | /** |
John Spurlock | fd7f1e0 | 2014-03-18 16:41:57 -0400 | [diff] [blame] | 550 | * Notification category: incoming direct message (SMS, instant message, etc.). |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 551 | */ |
John Spurlock | fd7f1e0 | 2014-03-18 16:41:57 -0400 | [diff] [blame] | 552 | public static final String CATEGORY_MESSAGE = "msg"; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 553 | |
| 554 | /** |
John Spurlock | fd7f1e0 | 2014-03-18 16:41:57 -0400 | [diff] [blame] | 555 | * Notification category: asynchronous bulk message (email). |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 556 | */ |
John Spurlock | fd7f1e0 | 2014-03-18 16:41:57 -0400 | [diff] [blame] | 557 | public static final String CATEGORY_EMAIL = "email"; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 558 | |
| 559 | /** |
John Spurlock | fd7f1e0 | 2014-03-18 16:41:57 -0400 | [diff] [blame] | 560 | * Notification category: calendar event. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 561 | */ |
John Spurlock | fd7f1e0 | 2014-03-18 16:41:57 -0400 | [diff] [blame] | 562 | public static final String CATEGORY_EVENT = "event"; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 563 | |
| 564 | /** |
John Spurlock | fd7f1e0 | 2014-03-18 16:41:57 -0400 | [diff] [blame] | 565 | * Notification category: promotion or advertisement. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 566 | */ |
John Spurlock | fd7f1e0 | 2014-03-18 16:41:57 -0400 | [diff] [blame] | 567 | public static final String CATEGORY_PROMO = "promo"; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 568 | |
| 569 | /** |
John Spurlock | fd7f1e0 | 2014-03-18 16:41:57 -0400 | [diff] [blame] | 570 | * Notification category: alarm or timer. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 571 | */ |
John Spurlock | fd7f1e0 | 2014-03-18 16:41:57 -0400 | [diff] [blame] | 572 | public static final String CATEGORY_ALARM = "alarm"; |
| 573 | |
| 574 | /** |
| 575 | * Notification category: progress of a long-running background operation. |
| 576 | */ |
| 577 | public static final String CATEGORY_PROGRESS = "progress"; |
| 578 | |
| 579 | /** |
| 580 | * Notification category: social network or sharing update. |
| 581 | */ |
| 582 | public static final String CATEGORY_SOCIAL = "social"; |
| 583 | |
| 584 | /** |
| 585 | * Notification category: error in background operation or authentication status. |
| 586 | */ |
| 587 | public static final String CATEGORY_ERROR = "err"; |
| 588 | |
| 589 | /** |
| 590 | * Notification category: media transport control for playback. |
| 591 | */ |
| 592 | public static final String CATEGORY_TRANSPORT = "transport"; |
| 593 | |
| 594 | /** |
| 595 | * Notification category: system or device status update. Reserved for system use. |
| 596 | */ |
| 597 | public static final String CATEGORY_SYSTEM = "sys"; |
| 598 | |
| 599 | /** |
| 600 | * Notification category: indication of running background service. |
| 601 | */ |
| 602 | public static final String CATEGORY_SERVICE = "service"; |
| 603 | |
| 604 | /** |
John Spurlock | 0a69c8c | 2014-03-21 13:30:57 -0400 | [diff] [blame] | 605 | * Notification category: a specific, timely recommendation for a single thing. |
| 606 | * For example, a news app might want to recommend a news story it believes the user will |
| 607 | * want to read next. |
| 608 | */ |
| 609 | public static final String CATEGORY_RECOMMENDATION = "recommendation"; |
| 610 | |
| 611 | /** |
John Spurlock | fd7f1e0 | 2014-03-18 16:41:57 -0400 | [diff] [blame] | 612 | * Notification category: ongoing information about device or contextual status. |
| 613 | */ |
| 614 | public static final String CATEGORY_STATUS = "status"; |
| 615 | |
| 616 | /** |
| 617 | * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants) |
| 618 | * that best describes this Notification. May be used by the system for ranking and filtering. |
| 619 | */ |
| 620 | public String category; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 621 | |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 622 | private String mGroupKey; |
| 623 | |
| 624 | /** |
| 625 | * Get the key used to group this notification into a cluster or stack |
| 626 | * with other notifications on devices which support such rendering. |
| 627 | */ |
| 628 | public String getGroup() { |
| 629 | return mGroupKey; |
| 630 | } |
| 631 | |
| 632 | private String mSortKey; |
| 633 | |
| 634 | /** |
| 635 | * Get a sort key that orders this notification among other notifications from the |
| 636 | * same package. This can be useful if an external sort was already applied and an app |
| 637 | * would like to preserve this. Notifications will be sorted lexicographically using this |
| 638 | * value, although providing different priorities in addition to providing sort key may |
| 639 | * cause this value to be ignored. |
| 640 | * |
| 641 | * <p>This sort key can also be used to order members of a notification group. See |
| 642 | * {@link Builder#setGroup}. |
| 643 | * |
| 644 | * @see String#compareTo(String) |
| 645 | */ |
| 646 | public String getSortKey() { |
| 647 | return mSortKey; |
| 648 | } |
| 649 | |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 650 | /** |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 651 | * Additional semantic data to be carried around with this Notification. |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 652 | * <p> |
| 653 | * The extras keys defined here are intended to capture the original inputs to {@link Builder} |
| 654 | * APIs, and are intended to be used by |
| 655 | * {@link android.service.notification.NotificationListenerService} implementations to extract |
| 656 | * detailed information from notification objects. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 657 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 658 | public Bundle extras = new Bundle(); |
| 659 | |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 660 | /** |
| 661 | * {@link #extras} key: this is the title of the notification, |
| 662 | * as supplied to {@link Builder#setContentTitle(CharSequence)}. |
| 663 | */ |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 664 | public static final String EXTRA_TITLE = "android.title"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 665 | |
| 666 | /** |
| 667 | * {@link #extras} key: this is the title of the notification when shown in expanded form, |
| 668 | * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}. |
| 669 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 670 | public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 671 | |
| 672 | /** |
| 673 | * {@link #extras} key: this is the main text payload, as supplied to |
| 674 | * {@link Builder#setContentText(CharSequence)}. |
| 675 | */ |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 676 | public static final String EXTRA_TEXT = "android.text"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 677 | |
| 678 | /** |
| 679 | * {@link #extras} key: this is a third line of text, as supplied to |
| 680 | * {@link Builder#setSubText(CharSequence)}. |
| 681 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 682 | public static final String EXTRA_SUB_TEXT = "android.subText"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 683 | |
| 684 | /** |
| 685 | * {@link #extras} key: this is a small piece of additional text as supplied to |
| 686 | * {@link Builder#setContentInfo(CharSequence)}. |
| 687 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 688 | public static final String EXTRA_INFO_TEXT = "android.infoText"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 689 | |
| 690 | /** |
| 691 | * {@link #extras} key: this is a line of summary information intended to be shown |
| 692 | * alongside expanded notifications, as supplied to (e.g.) |
| 693 | * {@link BigTextStyle#setSummaryText(CharSequence)}. |
| 694 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 695 | public static final String EXTRA_SUMMARY_TEXT = "android.summaryText"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 696 | |
| 697 | /** |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 698 | * {@link #extras} key: this is the longer text shown in the big form of a |
| 699 | * {@link BigTextStyle} notification, as supplied to |
| 700 | * {@link BigTextStyle#bigText(CharSequence)}. |
| 701 | */ |
| 702 | public static final String EXTRA_BIG_TEXT = "android.bigText"; |
| 703 | |
| 704 | /** |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 705 | * {@link #extras} key: this is the resource ID of the notification's main small icon, as |
| 706 | * supplied to {@link Builder#setSmallIcon(int)}. |
| 707 | */ |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 708 | public static final String EXTRA_SMALL_ICON = "android.icon"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 709 | |
| 710 | /** |
| 711 | * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the |
| 712 | * notification payload, as |
| 713 | * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}. |
| 714 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 715 | public static final String EXTRA_LARGE_ICON = "android.largeIcon"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 716 | |
| 717 | /** |
| 718 | * {@link #extras} key: this is a bitmap to be used instead of the one from |
| 719 | * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is |
| 720 | * shown in its expanded form, as supplied to |
| 721 | * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}. |
| 722 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 723 | public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 724 | |
| 725 | /** |
| 726 | * {@link #extras} key: this is the progress value supplied to |
| 727 | * {@link Builder#setProgress(int, int, boolean)}. |
| 728 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 729 | public static final String EXTRA_PROGRESS = "android.progress"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 730 | |
| 731 | /** |
| 732 | * {@link #extras} key: this is the maximum value supplied to |
| 733 | * {@link Builder#setProgress(int, int, boolean)}. |
| 734 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 735 | public static final String EXTRA_PROGRESS_MAX = "android.progressMax"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 736 | |
| 737 | /** |
| 738 | * {@link #extras} key: whether the progress bar is indeterminate, supplied to |
| 739 | * {@link Builder#setProgress(int, int, boolean)}. |
| 740 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 741 | public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 742 | |
| 743 | /** |
| 744 | * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically |
| 745 | * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to |
| 746 | * {@link Builder#setUsesChronometer(boolean)}. |
| 747 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 748 | public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 749 | |
| 750 | /** |
| 751 | * {@link #extras} key: whether {@link #when} should be shown, |
| 752 | * as supplied to {@link Builder#setShowWhen(boolean)}. |
| 753 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 754 | public static final String EXTRA_SHOW_WHEN = "android.showWhen"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 755 | |
| 756 | /** |
| 757 | * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded |
| 758 | * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}. |
| 759 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 760 | public static final String EXTRA_PICTURE = "android.picture"; |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 761 | |
| 762 | /** |
| 763 | * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded |
| 764 | * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}. |
| 765 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 766 | public static final String EXTRA_TEXT_LINES = "android.textLines"; |
Dan Sandler | 842dd77 | 2014-05-15 09:36:47 -0400 | [diff] [blame] | 767 | |
| 768 | /** |
| 769 | * {@link #extras} key: A string representing the name of the specific |
| 770 | * {@link android.app.Notification.Style} used to create this notification. |
| 771 | */ |
Chris Wren | 91ad563 | 2013-06-05 15:05:57 -0400 | [diff] [blame] | 772 | public static final String EXTRA_TEMPLATE = "android.template"; |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 773 | |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 774 | /** |
| 775 | * {@link #extras} key: An array of people that this notification relates to, specified |
| 776 | * by contacts provider contact URI. |
| 777 | */ |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 778 | public static final String EXTRA_PEOPLE = "android.people"; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 779 | |
| 780 | /** |
Chris Wren | 47c20a1 | 2014-06-18 17:27:29 -0400 | [diff] [blame] | 781 | * {@link #extras} key: used to provide hints about the appropriateness of |
| 782 | * displaying this notification as a heads-up notification. |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 783 | * @hide |
| 784 | */ |
| 785 | public static final String EXTRA_AS_HEADS_UP = "headsup"; |
| 786 | |
| 787 | /** |
John Spurlock | fd7f1e0 | 2014-03-18 16:41:57 -0400 | [diff] [blame] | 788 | * Allow certain system-generated notifications to appear before the device is provisioned. |
| 789 | * Only available to notifications coming from the android package. |
| 790 | * @hide |
| 791 | */ |
| 792 | public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup"; |
| 793 | |
| 794 | /** |
Jose Lima | e9e3b3b | 2014-05-18 23:44:50 -0700 | [diff] [blame] | 795 | * {@link #extras} key: A |
| 796 | * {@link android.content.ContentUris content URI} pointing to an image that can be displayed |
| 797 | * in the background when the notification is selected. The URI must point to an image stream |
| 798 | * suitable for passing into |
| 799 | * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream) |
| 800 | * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider |
| 801 | * URI used for this purpose must require no permissions to read the image data. |
| 802 | */ |
| 803 | public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri"; |
| 804 | |
| 805 | /** |
Dan Sandler | 842dd77 | 2014-05-15 09:36:47 -0400 | [diff] [blame] | 806 | * {@link #extras} key: A |
Jeff Brown | dba34ba | 2014-06-24 20:46:03 -0700 | [diff] [blame] | 807 | * {@link android.media.session.MediaSession.Token} associated with a |
Dan Sandler | 842dd77 | 2014-05-15 09:36:47 -0400 | [diff] [blame] | 808 | * {@link android.app.Notification.MediaStyle} notification. |
| 809 | */ |
| 810 | public static final String EXTRA_MEDIA_SESSION = "android.mediaSession"; |
| 811 | |
| 812 | /** |
Bryan Mawhinney | e191f90 | 2014-07-22 12:50:09 +0100 | [diff] [blame] | 813 | * {@link #extras} key: the indices of actions to be shown in the compact view, |
| 814 | * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}. |
| 815 | */ |
| 816 | public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions"; |
| 817 | |
Christoph Studer | 943aa67 | 2014-08-03 20:31:16 +0200 | [diff] [blame] | 818 | /** |
Christoph Studer | 7ac80e6 | 2014-08-04 16:01:57 +0200 | [diff] [blame] | 819 | * {@link #extras} key: the user that built the notification. |
Christoph Studer | 943aa67 | 2014-08-03 20:31:16 +0200 | [diff] [blame] | 820 | * |
| 821 | * @hide |
| 822 | */ |
Christoph Studer | 7ac80e6 | 2014-08-04 16:01:57 +0200 | [diff] [blame] | 823 | public static final String EXTRA_ORIGINATING_USERID = "android.originatingUserId"; |
Christoph Studer | 943aa67 | 2014-08-03 20:31:16 +0200 | [diff] [blame] | 824 | |
Bryan Mawhinney | e191f90 | 2014-07-22 12:50:09 +0100 | [diff] [blame] | 825 | /** |
Chris Wren | 47c20a1 | 2014-06-18 17:27:29 -0400 | [diff] [blame] | 826 | * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification should not be |
| 827 | * displayed in the heads up space. |
| 828 | * |
| 829 | * <p> |
| 830 | * If this notification has a {@link #fullScreenIntent}, then it will always launch the |
| 831 | * full-screen intent when posted. |
| 832 | * </p> |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 833 | * @hide |
| 834 | */ |
| 835 | public static final int HEADS_UP_NEVER = 0; |
| 836 | |
| 837 | /** |
Chris Wren | 47c20a1 | 2014-06-18 17:27:29 -0400 | [diff] [blame] | 838 | * Default value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification may be |
| 839 | * displayed as a heads up. |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 840 | * @hide |
| 841 | */ |
| 842 | public static final int HEADS_UP_ALLOWED = 1; |
| 843 | |
| 844 | /** |
Chris Wren | 47c20a1 | 2014-06-18 17:27:29 -0400 | [diff] [blame] | 845 | * Value for {@link #EXTRA_AS_HEADS_UP} that indicates this notification is a |
| 846 | * good candidate for display as a heads up. |
Chris Wren | 51c7510 | 2013-07-16 20:49:17 -0400 | [diff] [blame] | 847 | * @hide |
| 848 | */ |
| 849 | public static final int HEADS_UP_REQUESTED = 2; |
| 850 | |
| 851 | /** |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 852 | * Structure to encapsulate a named action that can be shown as part of this notification. |
| 853 | * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is |
| 854 | * selected by the user. |
| 855 | * <p> |
Griff Hazen | 959591e | 2014-05-15 22:26:18 -0700 | [diff] [blame] | 856 | * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)} |
| 857 | * or {@link Notification.Builder#addAction(Notification.Action)} |
| 858 | * to attach actions. |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 859 | */ |
Daniel Sandler | ea2a317 | 2013-02-20 22:24:20 -0500 | [diff] [blame] | 860 | public static class Action implements Parcelable { |
Griff Hazen | 959591e | 2014-05-15 22:26:18 -0700 | [diff] [blame] | 861 | private final Bundle mExtras; |
Griff Hazen | 61a9e86 | 2014-05-22 16:05:19 -0700 | [diff] [blame] | 862 | private final RemoteInput[] mRemoteInputs; |
Griff Hazen | 959591e | 2014-05-15 22:26:18 -0700 | [diff] [blame] | 863 | |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 864 | /** |
| 865 | * Small icon representing the action. |
| 866 | */ |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 867 | public int icon; |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 868 | |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 869 | /** |
| 870 | * Title of the action. |
| 871 | */ |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 872 | public CharSequence title; |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 873 | |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 874 | /** |
| 875 | * Intent to send when the user invokes this action. May be null, in which case the action |
| 876 | * may be rendered in a disabled presentation by the system UI. |
| 877 | */ |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 878 | public PendingIntent actionIntent; |
Griff Hazen | 959591e | 2014-05-15 22:26:18 -0700 | [diff] [blame] | 879 | |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 880 | private Action(Parcel in) { |
| 881 | icon = in.readInt(); |
| 882 | title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in); |
| 883 | if (in.readInt() == 1) { |
| 884 | actionIntent = PendingIntent.CREATOR.createFromParcel(in); |
| 885 | } |
Griff Hazen | 959591e | 2014-05-15 22:26:18 -0700 | [diff] [blame] | 886 | mExtras = in.readBundle(); |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 887 | mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR); |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 888 | } |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 889 | |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 890 | /** |
Griff Hazen | 959591e | 2014-05-15 22:26:18 -0700 | [diff] [blame] | 891 | * Use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}. |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 892 | */ |
| 893 | public Action(int icon, CharSequence title, PendingIntent intent) { |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 894 | this(icon, title, intent, new Bundle(), null); |
Griff Hazen | 959591e | 2014-05-15 22:26:18 -0700 | [diff] [blame] | 895 | } |
| 896 | |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 897 | private Action(int icon, CharSequence title, PendingIntent intent, Bundle extras, |
| 898 | RemoteInput[] remoteInputs) { |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 899 | this.icon = icon; |
| 900 | this.title = title; |
| 901 | this.actionIntent = intent; |
Griff Hazen | 959591e | 2014-05-15 22:26:18 -0700 | [diff] [blame] | 902 | this.mExtras = extras != null ? extras : new Bundle(); |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 903 | this.mRemoteInputs = remoteInputs; |
Griff Hazen | 959591e | 2014-05-15 22:26:18 -0700 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | /** |
| 907 | * Get additional metadata carried around with this Action. |
| 908 | */ |
| 909 | public Bundle getExtras() { |
| 910 | return mExtras; |
| 911 | } |
| 912 | |
| 913 | /** |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 914 | * Get the list of inputs to be collected from the user when this action is sent. |
| 915 | * May return null if no remote inputs were added. |
| 916 | */ |
| 917 | public RemoteInput[] getRemoteInputs() { |
| 918 | return mRemoteInputs; |
| 919 | } |
| 920 | |
| 921 | /** |
Griff Hazen | 959591e | 2014-05-15 22:26:18 -0700 | [diff] [blame] | 922 | * Builder class for {@link Action} objects. |
| 923 | */ |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 924 | public static final class Builder { |
Griff Hazen | 959591e | 2014-05-15 22:26:18 -0700 | [diff] [blame] | 925 | private final int mIcon; |
| 926 | private final CharSequence mTitle; |
| 927 | private final PendingIntent mIntent; |
| 928 | private final Bundle mExtras; |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 929 | private ArrayList<RemoteInput> mRemoteInputs; |
Griff Hazen | 959591e | 2014-05-15 22:26:18 -0700 | [diff] [blame] | 930 | |
| 931 | /** |
| 932 | * Construct a new builder for {@link Action} object. |
| 933 | * @param icon icon to show for this action |
| 934 | * @param title the title of the action |
| 935 | * @param intent the {@link PendingIntent} to fire when users trigger this action |
| 936 | */ |
| 937 | public Builder(int icon, CharSequence title, PendingIntent intent) { |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 938 | this(icon, title, intent, new Bundle(), null); |
Griff Hazen | 959591e | 2014-05-15 22:26:18 -0700 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | /** |
| 942 | * Construct a new builder for {@link Action} object using the fields from an |
| 943 | * {@link Action}. |
| 944 | * @param action the action to read fields from. |
| 945 | */ |
| 946 | public Builder(Action action) { |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 947 | this(action.icon, action.title, action.actionIntent, new Bundle(action.mExtras), |
| 948 | action.getRemoteInputs()); |
Griff Hazen | 959591e | 2014-05-15 22:26:18 -0700 | [diff] [blame] | 949 | } |
| 950 | |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 951 | private Builder(int icon, CharSequence title, PendingIntent intent, Bundle extras, |
| 952 | RemoteInput[] remoteInputs) { |
Griff Hazen | 959591e | 2014-05-15 22:26:18 -0700 | [diff] [blame] | 953 | mIcon = icon; |
| 954 | mTitle = title; |
| 955 | mIntent = intent; |
| 956 | mExtras = extras; |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 957 | if (remoteInputs != null) { |
| 958 | mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length); |
| 959 | Collections.addAll(mRemoteInputs, remoteInputs); |
| 960 | } |
Griff Hazen | 959591e | 2014-05-15 22:26:18 -0700 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | /** |
| 964 | * Merge additional metadata into this builder. |
| 965 | * |
| 966 | * <p>Values within the Bundle will replace existing extras values in this Builder. |
| 967 | * |
| 968 | * @see Notification.Action#extras |
| 969 | */ |
| 970 | public Builder addExtras(Bundle extras) { |
| 971 | if (extras != null) { |
| 972 | mExtras.putAll(extras); |
| 973 | } |
| 974 | return this; |
| 975 | } |
| 976 | |
| 977 | /** |
| 978 | * Get the metadata Bundle used by this Builder. |
| 979 | * |
| 980 | * <p>The returned Bundle is shared with this Builder. |
| 981 | */ |
| 982 | public Bundle getExtras() { |
| 983 | return mExtras; |
| 984 | } |
| 985 | |
| 986 | /** |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 987 | * Add an input to be collected from the user when this action is sent. |
| 988 | * Response values can be retrieved from the fired intent by using the |
| 989 | * {@link RemoteInput#getResultsFromIntent} function. |
| 990 | * @param remoteInput a {@link RemoteInput} to add to the action |
| 991 | * @return this object for method chaining |
| 992 | */ |
| 993 | public Builder addRemoteInput(RemoteInput remoteInput) { |
| 994 | if (mRemoteInputs == null) { |
| 995 | mRemoteInputs = new ArrayList<RemoteInput>(); |
| 996 | } |
| 997 | mRemoteInputs.add(remoteInput); |
| 998 | return this; |
| 999 | } |
| 1000 | |
| 1001 | /** |
| 1002 | * Apply an extender to this action builder. Extenders may be used to add |
| 1003 | * metadata or change options on this builder. |
| 1004 | */ |
Griff Hazen | 61a9e86 | 2014-05-22 16:05:19 -0700 | [diff] [blame] | 1005 | public Builder extend(Extender extender) { |
| 1006 | extender.extend(this); |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 1007 | return this; |
| 1008 | } |
| 1009 | |
| 1010 | /** |
Griff Hazen | 959591e | 2014-05-15 22:26:18 -0700 | [diff] [blame] | 1011 | * Combine all of the options that have been set and return a new {@link Action} |
| 1012 | * object. |
| 1013 | * @return the built action |
| 1014 | */ |
| 1015 | public Action build() { |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 1016 | RemoteInput[] remoteInputs = mRemoteInputs != null |
| 1017 | ? mRemoteInputs.toArray(new RemoteInput[mRemoteInputs.size()]) : null; |
| 1018 | return new Action(mIcon, mTitle, mIntent, mExtras, remoteInputs); |
Griff Hazen | 959591e | 2014-05-15 22:26:18 -0700 | [diff] [blame] | 1019 | } |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 1020 | } |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 1021 | |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 1022 | @Override |
| 1023 | public Action clone() { |
| 1024 | return new Action( |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 1025 | icon, |
| 1026 | title, |
| 1027 | actionIntent, // safe to alias |
| 1028 | new Bundle(mExtras), |
| 1029 | getRemoteInputs()); |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 1030 | } |
| 1031 | @Override |
| 1032 | public int describeContents() { |
| 1033 | return 0; |
| 1034 | } |
| 1035 | @Override |
| 1036 | public void writeToParcel(Parcel out, int flags) { |
| 1037 | out.writeInt(icon); |
| 1038 | TextUtils.writeToParcel(title, out, flags); |
| 1039 | if (actionIntent != null) { |
| 1040 | out.writeInt(1); |
| 1041 | actionIntent.writeToParcel(out, flags); |
| 1042 | } else { |
| 1043 | out.writeInt(0); |
| 1044 | } |
Griff Hazen | 959591e | 2014-05-15 22:26:18 -0700 | [diff] [blame] | 1045 | out.writeBundle(mExtras); |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 1046 | out.writeTypedArray(mRemoteInputs, flags); |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 1047 | } |
Griff Hazen | 959591e | 2014-05-15 22:26:18 -0700 | [diff] [blame] | 1048 | public static final Parcelable.Creator<Action> CREATOR = |
| 1049 | new Parcelable.Creator<Action>() { |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 1050 | public Action createFromParcel(Parcel in) { |
| 1051 | return new Action(in); |
| 1052 | } |
| 1053 | public Action[] newArray(int size) { |
| 1054 | return new Action[size]; |
| 1055 | } |
| 1056 | }; |
Griff Hazen | 61a9e86 | 2014-05-22 16:05:19 -0700 | [diff] [blame] | 1057 | |
| 1058 | /** |
| 1059 | * Extender interface for use with {@link Builder#extend}. Extenders may be used to add |
| 1060 | * metadata or change options on an action builder. |
| 1061 | */ |
| 1062 | public interface Extender { |
| 1063 | /** |
| 1064 | * Apply this extender to a notification action builder. |
| 1065 | * @param builder the builder to be modified. |
| 1066 | * @return the build object for chaining. |
| 1067 | */ |
| 1068 | public Builder extend(Builder builder); |
| 1069 | } |
| 1070 | |
| 1071 | /** |
| 1072 | * Wearable extender for notification actions. To add extensions to an action, |
| 1073 | * create a new {@link android.app.Notification.Action.WearableExtender} object using |
| 1074 | * the {@code WearableExtender()} constructor and apply it to a |
| 1075 | * {@link android.app.Notification.Action.Builder} using |
| 1076 | * {@link android.app.Notification.Action.Builder#extend}. |
| 1077 | * |
| 1078 | * <pre class="prettyprint"> |
| 1079 | * Notification.Action action = new Notification.Action.Builder( |
| 1080 | * R.drawable.archive_all, "Archive all", actionIntent) |
Griff Hazen | 14f5799 | 2014-05-26 09:07:14 -0700 | [diff] [blame] | 1081 | * .extend(new Notification.Action.WearableExtender() |
Griff Hazen | 61a9e86 | 2014-05-22 16:05:19 -0700 | [diff] [blame] | 1082 | * .setAvailableOffline(false)) |
Griff Hazen | 14f5799 | 2014-05-26 09:07:14 -0700 | [diff] [blame] | 1083 | * .build();</pre> |
Griff Hazen | 61a9e86 | 2014-05-22 16:05:19 -0700 | [diff] [blame] | 1084 | */ |
| 1085 | public static final class WearableExtender implements Extender { |
| 1086 | /** Notification action extra which contains wearable extensions */ |
| 1087 | private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS"; |
| 1088 | |
| 1089 | private static final String KEY_FLAGS = "flags"; |
| 1090 | |
| 1091 | // Flags bitwise-ored to mFlags |
| 1092 | private static final int FLAG_AVAILABLE_OFFLINE = 0x1; |
| 1093 | |
| 1094 | // Default value for flags integer |
| 1095 | private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE; |
| 1096 | |
| 1097 | private int mFlags = DEFAULT_FLAGS; |
| 1098 | |
| 1099 | /** |
| 1100 | * Create a {@link android.app.Notification.Action.WearableExtender} with default |
| 1101 | * options. |
| 1102 | */ |
| 1103 | public WearableExtender() { |
| 1104 | } |
| 1105 | |
| 1106 | /** |
| 1107 | * Create a {@link android.app.Notification.Action.WearableExtender} by reading |
| 1108 | * wearable options present in an existing notification action. |
| 1109 | * @param action the notification action to inspect. |
| 1110 | */ |
| 1111 | public WearableExtender(Action action) { |
| 1112 | Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS); |
| 1113 | if (wearableBundle != null) { |
| 1114 | mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS); |
| 1115 | } |
| 1116 | } |
| 1117 | |
| 1118 | /** |
| 1119 | * Apply wearable extensions to a notification action that is being built. This is |
| 1120 | * typically called by the {@link android.app.Notification.Action.Builder#extend} |
| 1121 | * method of {@link android.app.Notification.Action.Builder}. |
| 1122 | */ |
| 1123 | @Override |
| 1124 | public Action.Builder extend(Action.Builder builder) { |
| 1125 | Bundle wearableBundle = new Bundle(); |
| 1126 | |
| 1127 | if (mFlags != DEFAULT_FLAGS) { |
| 1128 | wearableBundle.putInt(KEY_FLAGS, mFlags); |
| 1129 | } |
| 1130 | |
| 1131 | builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle); |
| 1132 | return builder; |
| 1133 | } |
| 1134 | |
| 1135 | @Override |
| 1136 | public WearableExtender clone() { |
| 1137 | WearableExtender that = new WearableExtender(); |
| 1138 | that.mFlags = this.mFlags; |
| 1139 | return that; |
| 1140 | } |
| 1141 | |
| 1142 | /** |
| 1143 | * Set whether this action is available when the wearable device is not connected to |
| 1144 | * a companion device. The user can still trigger this action when the wearable device is |
| 1145 | * offline, but a visual hint will indicate that the action may not be available. |
| 1146 | * Defaults to true. |
| 1147 | */ |
| 1148 | public WearableExtender setAvailableOffline(boolean availableOffline) { |
| 1149 | setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline); |
| 1150 | return this; |
| 1151 | } |
| 1152 | |
| 1153 | /** |
| 1154 | * Get whether this action is available when the wearable device is not connected to |
| 1155 | * a companion device. The user can still trigger this action when the wearable device is |
| 1156 | * offline, but a visual hint will indicate that the action may not be available. |
| 1157 | * Defaults to true. |
| 1158 | */ |
| 1159 | public boolean isAvailableOffline() { |
| 1160 | return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0; |
| 1161 | } |
| 1162 | |
| 1163 | private void setFlag(int mask, boolean value) { |
| 1164 | if (value) { |
| 1165 | mFlags |= mask; |
| 1166 | } else { |
| 1167 | mFlags &= ~mask; |
| 1168 | } |
| 1169 | } |
| 1170 | } |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 1171 | } |
| 1172 | |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 1173 | /** |
| 1174 | * Array of all {@link Action} structures attached to this notification by |
| 1175 | * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of |
| 1176 | * {@link android.service.notification.NotificationListenerService} that provide an alternative |
| 1177 | * interface for invoking actions. |
| 1178 | */ |
Daniel Sandler | ea2a317 | 2013-02-20 22:24:20 -0500 | [diff] [blame] | 1179 | public Action[] actions; |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 1180 | |
| 1181 | /** |
Dan Sandler | 0bf2ed8 | 2013-12-21 23:33:41 -0600 | [diff] [blame] | 1182 | * Replacement version of this notification whose content will be shown |
| 1183 | * in an insecure context such as atop a secure keyguard. See {@link #visibility} |
| 1184 | * and {@link #VISIBILITY_PUBLIC}. |
| 1185 | */ |
| 1186 | public Notification publicVersion; |
| 1187 | |
| 1188 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1189 | * Constructs a Notification object with default values. |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1190 | * You might want to consider using {@link Builder} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1191 | */ |
| 1192 | public Notification() |
| 1193 | { |
| 1194 | this.when = System.currentTimeMillis(); |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1195 | this.priority = PRIORITY_DEFAULT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1199 | * @hide |
| 1200 | */ |
| 1201 | public Notification(Context context, int icon, CharSequence tickerText, long when, |
| 1202 | CharSequence contentTitle, CharSequence contentText, Intent contentIntent) |
| 1203 | { |
| 1204 | this.when = when; |
| 1205 | this.icon = icon; |
| 1206 | this.tickerText = tickerText; |
| 1207 | setLatestEventInfo(context, contentTitle, contentText, |
| 1208 | PendingIntent.getActivity(context, 0, contentIntent, 0)); |
| 1209 | } |
| 1210 | |
| 1211 | /** |
| 1212 | * Constructs a Notification object with the information needed to |
| 1213 | * have a status bar icon without the standard expanded view. |
| 1214 | * |
| 1215 | * @param icon The resource id of the icon to put in the status bar. |
| 1216 | * @param tickerText The text that flows by in the status bar when the notification first |
| 1217 | * activates. |
| 1218 | * @param when The time to show in the time field. In the System.currentTimeMillis |
| 1219 | * timebase. |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1220 | * |
| 1221 | * @deprecated Use {@link Builder} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1222 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1223 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1224 | public Notification(int icon, CharSequence tickerText, long when) |
| 1225 | { |
| 1226 | this.icon = icon; |
| 1227 | this.tickerText = tickerText; |
| 1228 | this.when = when; |
| 1229 | } |
| 1230 | |
| 1231 | /** |
| 1232 | * Unflatten the notification from a parcel. |
| 1233 | */ |
| 1234 | public Notification(Parcel parcel) |
| 1235 | { |
| 1236 | int version = parcel.readInt(); |
| 1237 | |
| 1238 | when = parcel.readLong(); |
| 1239 | icon = parcel.readInt(); |
| 1240 | number = parcel.readInt(); |
| 1241 | if (parcel.readInt() != 0) { |
| 1242 | contentIntent = PendingIntent.CREATOR.createFromParcel(parcel); |
| 1243 | } |
| 1244 | if (parcel.readInt() != 0) { |
| 1245 | deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel); |
| 1246 | } |
| 1247 | if (parcel.readInt() != 0) { |
| 1248 | tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel); |
| 1249 | } |
| 1250 | if (parcel.readInt() != 0) { |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1251 | tickerView = RemoteViews.CREATOR.createFromParcel(parcel); |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 1252 | } |
| 1253 | if (parcel.readInt() != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1254 | contentView = RemoteViews.CREATOR.createFromParcel(parcel); |
| 1255 | } |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 1256 | if (parcel.readInt() != 0) { |
| 1257 | largeIcon = Bitmap.CREATOR.createFromParcel(parcel); |
| 1258 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1259 | defaults = parcel.readInt(); |
| 1260 | flags = parcel.readInt(); |
| 1261 | if (parcel.readInt() != 0) { |
| 1262 | sound = Uri.CREATOR.createFromParcel(parcel); |
| 1263 | } |
| 1264 | |
| 1265 | audioStreamType = parcel.readInt(); |
John Spurlock | c0650f02 | 2014-07-19 13:22:39 -0400 | [diff] [blame] | 1266 | if (parcel.readInt() != 0) { |
| 1267 | audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel); |
| 1268 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1269 | vibrate = parcel.createLongArray(); |
| 1270 | ledARGB = parcel.readInt(); |
| 1271 | ledOnMS = parcel.readInt(); |
| 1272 | ledOffMS = parcel.readInt(); |
| 1273 | iconLevel = parcel.readInt(); |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 1274 | |
| 1275 | if (parcel.readInt() != 0) { |
| 1276 | fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel); |
| 1277 | } |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1278 | |
| 1279 | priority = parcel.readInt(); |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 1280 | |
John Spurlock | fd7f1e0 | 2014-03-18 16:41:57 -0400 | [diff] [blame] | 1281 | category = parcel.readString(); |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1282 | |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 1283 | mGroupKey = parcel.readString(); |
| 1284 | |
| 1285 | mSortKey = parcel.readString(); |
| 1286 | |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 1287 | extras = parcel.readBundle(); // may be null |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 1288 | |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 1289 | actions = parcel.createTypedArray(Action.CREATOR); // may be null |
| 1290 | |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 1291 | if (parcel.readInt() != 0) { |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 1292 | bigContentView = RemoteViews.CREATOR.createFromParcel(parcel); |
| 1293 | } |
Dan Sandler | 0bf2ed8 | 2013-12-21 23:33:41 -0600 | [diff] [blame] | 1294 | |
Chris Wren | 8fd39ec | 2014-02-27 17:43:26 -0500 | [diff] [blame] | 1295 | if (parcel.readInt() != 0) { |
| 1296 | headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel); |
| 1297 | } |
| 1298 | |
Dan Sandler | 0bf2ed8 | 2013-12-21 23:33:41 -0600 | [diff] [blame] | 1299 | visibility = parcel.readInt(); |
| 1300 | |
| 1301 | if (parcel.readInt() != 0) { |
| 1302 | publicVersion = Notification.CREATOR.createFromParcel(parcel); |
| 1303 | } |
Dan Sandler | 26e81cf | 2014-05-06 10:01:27 -0400 | [diff] [blame] | 1304 | |
| 1305 | color = parcel.readInt(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1306 | } |
| 1307 | |
Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 1308 | @Override |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 1309 | public Notification clone() { |
| 1310 | Notification that = new Notification(); |
Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 1311 | cloneInto(that, true); |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 1312 | return that; |
| 1313 | } |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 1314 | |
Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 1315 | /** |
| 1316 | * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members |
| 1317 | * of this into that. |
| 1318 | * @hide |
| 1319 | */ |
| 1320 | public void cloneInto(Notification that, boolean heavy) { |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 1321 | that.when = this.when; |
| 1322 | that.icon = this.icon; |
| 1323 | that.number = this.number; |
| 1324 | |
| 1325 | // PendingIntents are global, so there's no reason (or way) to clone them. |
| 1326 | that.contentIntent = this.contentIntent; |
| 1327 | that.deleteIntent = this.deleteIntent; |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 1328 | that.fullScreenIntent = this.fullScreenIntent; |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 1329 | |
| 1330 | if (this.tickerText != null) { |
| 1331 | that.tickerText = this.tickerText.toString(); |
| 1332 | } |
Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 1333 | if (heavy && this.tickerView != null) { |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1334 | that.tickerView = this.tickerView.clone(); |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 1335 | } |
Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 1336 | if (heavy && this.contentView != null) { |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 1337 | that.contentView = this.contentView.clone(); |
| 1338 | } |
Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 1339 | if (heavy && this.largeIcon != null) { |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 1340 | that.largeIcon = Bitmap.createBitmap(this.largeIcon); |
| 1341 | } |
Jozef BABJAK | a8b9183 | 2011-02-22 08:05:08 +0100 | [diff] [blame] | 1342 | that.iconLevel = this.iconLevel; |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 1343 | that.sound = this.sound; // android.net.Uri is immutable |
| 1344 | that.audioStreamType = this.audioStreamType; |
John Spurlock | c0650f02 | 2014-07-19 13:22:39 -0400 | [diff] [blame] | 1345 | if (this.audioAttributes != null) { |
| 1346 | that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build(); |
| 1347 | } |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 1348 | |
| 1349 | final long[] vibrate = this.vibrate; |
| 1350 | if (vibrate != null) { |
| 1351 | final int N = vibrate.length; |
| 1352 | final long[] vib = that.vibrate = new long[N]; |
| 1353 | System.arraycopy(vibrate, 0, vib, 0, N); |
| 1354 | } |
| 1355 | |
| 1356 | that.ledARGB = this.ledARGB; |
| 1357 | that.ledOnMS = this.ledOnMS; |
| 1358 | that.ledOffMS = this.ledOffMS; |
| 1359 | that.defaults = this.defaults; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1360 | |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 1361 | that.flags = this.flags; |
| 1362 | |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1363 | that.priority = this.priority; |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 1364 | |
John Spurlock | fd7f1e0 | 2014-03-18 16:41:57 -0400 | [diff] [blame] | 1365 | that.category = this.category; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1366 | |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 1367 | that.mGroupKey = this.mGroupKey; |
| 1368 | |
| 1369 | that.mSortKey = this.mSortKey; |
| 1370 | |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1371 | if (this.extras != null) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 1372 | try { |
| 1373 | that.extras = new Bundle(this.extras); |
| 1374 | // will unparcel |
| 1375 | that.extras.size(); |
| 1376 | } catch (BadParcelableException e) { |
| 1377 | Log.e(TAG, "could not unparcel extras from notification: " + this, e); |
| 1378 | that.extras = null; |
| 1379 | } |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1380 | } |
| 1381 | |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 1382 | if (this.actions != null) { |
| 1383 | that.actions = new Action[this.actions.length]; |
| 1384 | for(int i=0; i<this.actions.length; i++) { |
| 1385 | that.actions[i] = this.actions[i].clone(); |
| 1386 | } |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 1387 | } |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 1388 | |
Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 1389 | if (heavy && this.bigContentView != null) { |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 1390 | that.bigContentView = this.bigContentView.clone(); |
| 1391 | } |
Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 1392 | |
Chris Wren | 8fd39ec | 2014-02-27 17:43:26 -0500 | [diff] [blame] | 1393 | if (heavy && this.headsUpContentView != null) { |
| 1394 | that.headsUpContentView = this.headsUpContentView.clone(); |
| 1395 | } |
| 1396 | |
Dan Sandler | 0bf2ed8 | 2013-12-21 23:33:41 -0600 | [diff] [blame] | 1397 | that.visibility = this.visibility; |
| 1398 | |
| 1399 | if (this.publicVersion != null) { |
| 1400 | that.publicVersion = new Notification(); |
| 1401 | this.publicVersion.cloneInto(that.publicVersion, heavy); |
| 1402 | } |
| 1403 | |
Dan Sandler | 26e81cf | 2014-05-06 10:01:27 -0400 | [diff] [blame] | 1404 | that.color = this.color; |
| 1405 | |
Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 1406 | if (!heavy) { |
| 1407 | that.lightenPayload(); // will clean out extras |
| 1408 | } |
| 1409 | } |
| 1410 | |
| 1411 | /** |
| 1412 | * Removes heavyweight parts of the Notification object for archival or for sending to |
| 1413 | * listeners when the full contents are not necessary. |
| 1414 | * @hide |
| 1415 | */ |
| 1416 | public final void lightenPayload() { |
| 1417 | tickerView = null; |
| 1418 | contentView = null; |
| 1419 | bigContentView = null; |
Chris Wren | 8fd39ec | 2014-02-27 17:43:26 -0500 | [diff] [blame] | 1420 | headsUpContentView = null; |
Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 1421 | largeIcon = null; |
| 1422 | if (extras != null) { |
| 1423 | extras.remove(Notification.EXTRA_LARGE_ICON); |
| 1424 | extras.remove(Notification.EXTRA_LARGE_ICON_BIG); |
| 1425 | extras.remove(Notification.EXTRA_PICTURE); |
| 1426 | } |
Joe Onorato | 18e69df | 2010-05-17 22:26:12 -0700 | [diff] [blame] | 1427 | } |
| 1428 | |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 1429 | /** |
| 1430 | * Make sure this CharSequence is safe to put into a bundle, which basically |
| 1431 | * means it had better not be some custom Parcelable implementation. |
| 1432 | * @hide |
| 1433 | */ |
| 1434 | public static CharSequence safeCharSequence(CharSequence cs) { |
| 1435 | if (cs instanceof Parcelable) { |
| 1436 | Log.e(TAG, "warning: " + cs.getClass().getCanonicalName() |
| 1437 | + " instance is a custom Parcelable and not allowed in Notification"); |
| 1438 | return cs.toString(); |
| 1439 | } |
| 1440 | |
| 1441 | return cs; |
| 1442 | } |
| 1443 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1444 | public int describeContents() { |
| 1445 | return 0; |
| 1446 | } |
| 1447 | |
| 1448 | /** |
| 1449 | * Flatten this notification from a parcel. |
| 1450 | */ |
| 1451 | public void writeToParcel(Parcel parcel, int flags) |
| 1452 | { |
| 1453 | parcel.writeInt(1); |
| 1454 | |
| 1455 | parcel.writeLong(when); |
| 1456 | parcel.writeInt(icon); |
| 1457 | parcel.writeInt(number); |
| 1458 | if (contentIntent != null) { |
| 1459 | parcel.writeInt(1); |
| 1460 | contentIntent.writeToParcel(parcel, 0); |
| 1461 | } else { |
| 1462 | parcel.writeInt(0); |
| 1463 | } |
| 1464 | if (deleteIntent != null) { |
| 1465 | parcel.writeInt(1); |
| 1466 | deleteIntent.writeToParcel(parcel, 0); |
| 1467 | } else { |
| 1468 | parcel.writeInt(0); |
| 1469 | } |
| 1470 | if (tickerText != null) { |
| 1471 | parcel.writeInt(1); |
| 1472 | TextUtils.writeToParcel(tickerText, parcel, flags); |
| 1473 | } else { |
| 1474 | parcel.writeInt(0); |
| 1475 | } |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1476 | if (tickerView != null) { |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 1477 | parcel.writeInt(1); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1478 | tickerView.writeToParcel(parcel, 0); |
Joe Onorato | ef1e776 | 2010-09-17 18:38:38 -0400 | [diff] [blame] | 1479 | } else { |
| 1480 | parcel.writeInt(0); |
| 1481 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1482 | if (contentView != null) { |
| 1483 | parcel.writeInt(1); |
| 1484 | contentView.writeToParcel(parcel, 0); |
| 1485 | } else { |
| 1486 | parcel.writeInt(0); |
| 1487 | } |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 1488 | if (largeIcon != null) { |
| 1489 | parcel.writeInt(1); |
| 1490 | largeIcon.writeToParcel(parcel, 0); |
| 1491 | } else { |
| 1492 | parcel.writeInt(0); |
| 1493 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1494 | |
| 1495 | parcel.writeInt(defaults); |
| 1496 | parcel.writeInt(this.flags); |
| 1497 | |
| 1498 | if (sound != null) { |
| 1499 | parcel.writeInt(1); |
| 1500 | sound.writeToParcel(parcel, 0); |
| 1501 | } else { |
| 1502 | parcel.writeInt(0); |
| 1503 | } |
| 1504 | parcel.writeInt(audioStreamType); |
John Spurlock | c0650f02 | 2014-07-19 13:22:39 -0400 | [diff] [blame] | 1505 | |
| 1506 | if (audioAttributes != null) { |
| 1507 | parcel.writeInt(1); |
| 1508 | audioAttributes.writeToParcel(parcel, 0); |
| 1509 | } else { |
| 1510 | parcel.writeInt(0); |
| 1511 | } |
| 1512 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1513 | parcel.writeLongArray(vibrate); |
| 1514 | parcel.writeInt(ledARGB); |
| 1515 | parcel.writeInt(ledOnMS); |
| 1516 | parcel.writeInt(ledOffMS); |
| 1517 | parcel.writeInt(iconLevel); |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 1518 | |
| 1519 | if (fullScreenIntent != null) { |
| 1520 | parcel.writeInt(1); |
| 1521 | fullScreenIntent.writeToParcel(parcel, 0); |
| 1522 | } else { |
| 1523 | parcel.writeInt(0); |
| 1524 | } |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1525 | |
| 1526 | parcel.writeInt(priority); |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 1527 | |
John Spurlock | fd7f1e0 | 2014-03-18 16:41:57 -0400 | [diff] [blame] | 1528 | parcel.writeString(category); |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 1529 | |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 1530 | parcel.writeString(mGroupKey); |
| 1531 | |
| 1532 | parcel.writeString(mSortKey); |
| 1533 | |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 1534 | parcel.writeBundle(extras); // null ok |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 1535 | |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 1536 | parcel.writeTypedArray(actions, 0); // null ok |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 1537 | |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 1538 | if (bigContentView != null) { |
| 1539 | parcel.writeInt(1); |
| 1540 | bigContentView.writeToParcel(parcel, 0); |
| 1541 | } else { |
| 1542 | parcel.writeInt(0); |
| 1543 | } |
Dan Sandler | 0bf2ed8 | 2013-12-21 23:33:41 -0600 | [diff] [blame] | 1544 | |
Chris Wren | 8fd39ec | 2014-02-27 17:43:26 -0500 | [diff] [blame] | 1545 | if (headsUpContentView != null) { |
| 1546 | parcel.writeInt(1); |
| 1547 | headsUpContentView.writeToParcel(parcel, 0); |
| 1548 | } else { |
| 1549 | parcel.writeInt(0); |
| 1550 | } |
| 1551 | |
Dan Sandler | 0bf2ed8 | 2013-12-21 23:33:41 -0600 | [diff] [blame] | 1552 | parcel.writeInt(visibility); |
| 1553 | |
| 1554 | if (publicVersion != null) { |
| 1555 | parcel.writeInt(1); |
| 1556 | publicVersion.writeToParcel(parcel, 0); |
| 1557 | } else { |
| 1558 | parcel.writeInt(0); |
| 1559 | } |
Dan Sandler | 26e81cf | 2014-05-06 10:01:27 -0400 | [diff] [blame] | 1560 | |
| 1561 | parcel.writeInt(color); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | /** |
| 1565 | * Parcelable.Creator that instantiates Notification objects |
| 1566 | */ |
| 1567 | public static final Parcelable.Creator<Notification> CREATOR |
| 1568 | = new Parcelable.Creator<Notification>() |
| 1569 | { |
| 1570 | public Notification createFromParcel(Parcel parcel) |
| 1571 | { |
| 1572 | return new Notification(parcel); |
| 1573 | } |
| 1574 | |
| 1575 | public Notification[] newArray(int size) |
| 1576 | { |
| 1577 | return new Notification[size]; |
| 1578 | } |
| 1579 | }; |
| 1580 | |
| 1581 | /** |
| 1582 | * Sets the {@link #contentView} field to be a view with the standard "Latest Event" |
| 1583 | * layout. |
| 1584 | * |
| 1585 | * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields |
| 1586 | * in the view.</p> |
| 1587 | * @param context The context for your application / activity. |
| 1588 | * @param contentTitle The title that goes in the expanded entry. |
| 1589 | * @param contentText The text that goes in the expanded entry. |
| 1590 | * @param contentIntent The intent to launch when the user clicks the expanded notification. |
| 1591 | * If this is an activity, it must include the |
| 1592 | * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires |
Scott Main | 7aee61f | 2011-02-08 11:25:01 -0800 | [diff] [blame] | 1593 | * that you take care of task management as described in the |
| 1594 | * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back |
| 1595 | * Stack</a> document. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1596 | * |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1597 | * @deprecated Use {@link Builder} instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1598 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1599 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1600 | public void setLatestEventInfo(Context context, |
| 1601 | CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) { |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 1602 | Notification.Builder builder = new Notification.Builder(context); |
| 1603 | |
| 1604 | // First, ensure that key pieces of information that may have been set directly |
| 1605 | // are preserved |
| 1606 | builder.setWhen(this.when); |
| 1607 | builder.setSmallIcon(this.icon); |
| 1608 | builder.setPriority(this.priority); |
| 1609 | builder.setTicker(this.tickerText); |
| 1610 | builder.setNumber(this.number); |
Selim Cinek | 255dd04 | 2014-08-19 22:29:02 +0200 | [diff] [blame] | 1611 | builder.setColor(this.color); |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 1612 | builder.mFlags = this.flags; |
| 1613 | builder.setSound(this.sound, this.audioStreamType); |
| 1614 | builder.setDefaults(this.defaults); |
| 1615 | builder.setVibrate(this.vibrate); |
| 1616 | |
| 1617 | // now apply the latestEventInfo fields |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1618 | if (contentTitle != null) { |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 1619 | builder.setContentTitle(contentTitle); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1620 | } |
| 1621 | if (contentText != null) { |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 1622 | builder.setContentText(contentText); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1623 | } |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 1624 | builder.setContentIntent(contentIntent); |
| 1625 | builder.buildInto(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | @Override |
| 1629 | public String toString() { |
| 1630 | StringBuilder sb = new StringBuilder(); |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1631 | sb.append("Notification(pri="); |
| 1632 | sb.append(priority); |
| 1633 | sb.append(" contentView="); |
Joe Onorato | c9596d6 | 2011-01-12 17:03:11 -0800 | [diff] [blame] | 1634 | if (contentView != null) { |
| 1635 | sb.append(contentView.getPackage()); |
| 1636 | sb.append("/0x"); |
| 1637 | sb.append(Integer.toHexString(contentView.getLayoutId())); |
| 1638 | } else { |
| 1639 | sb.append("null"); |
| 1640 | } |
| 1641 | sb.append(" vibrate="); |
Daniel Sandler | 6738eee | 2012-11-16 12:03:32 -0500 | [diff] [blame] | 1642 | if ((this.defaults & DEFAULT_VIBRATE) != 0) { |
| 1643 | sb.append("default"); |
| 1644 | } else if (this.vibrate != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1645 | int N = this.vibrate.length-1; |
| 1646 | sb.append("["); |
| 1647 | for (int i=0; i<N; i++) { |
| 1648 | sb.append(this.vibrate[i]); |
| 1649 | sb.append(','); |
| 1650 | } |
Simon Schoar | 8cf97d9 | 2009-06-10 22:08:37 +0200 | [diff] [blame] | 1651 | if (N != -1) { |
| 1652 | sb.append(this.vibrate[N]); |
| 1653 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1654 | sb.append("]"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1655 | } else { |
| 1656 | sb.append("null"); |
| 1657 | } |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1658 | sb.append(" sound="); |
Daniel Sandler | 6738eee | 2012-11-16 12:03:32 -0500 | [diff] [blame] | 1659 | if ((this.defaults & DEFAULT_SOUND) != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1660 | sb.append("default"); |
Daniel Sandler | 6738eee | 2012-11-16 12:03:32 -0500 | [diff] [blame] | 1661 | } else if (this.sound != null) { |
| 1662 | sb.append(this.sound.toString()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1663 | } else { |
| 1664 | sb.append("null"); |
| 1665 | } |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1666 | sb.append(" defaults=0x"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1667 | sb.append(Integer.toHexString(this.defaults)); |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1668 | sb.append(" flags=0x"); |
Daniel Sandler | e46cbd3 | 2010-06-17 10:35:26 -0400 | [diff] [blame] | 1669 | sb.append(Integer.toHexString(this.flags)); |
Dan Sandler | 26e81cf | 2014-05-06 10:01:27 -0400 | [diff] [blame] | 1670 | sb.append(String.format(" color=0x%08x", this.color)); |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 1671 | if (this.category != null) { |
| 1672 | sb.append(" category="); |
| 1673 | sb.append(this.category); |
| 1674 | } |
| 1675 | if (this.mGroupKey != null) { |
| 1676 | sb.append(" groupKey="); |
| 1677 | sb.append(this.mGroupKey); |
| 1678 | } |
| 1679 | if (this.mSortKey != null) { |
| 1680 | sb.append(" sortKey="); |
| 1681 | sb.append(this.mSortKey); |
| 1682 | } |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 1683 | if (actions != null) { |
Dan Sandler | 1b71878 | 2014-07-18 12:43:45 -0400 | [diff] [blame] | 1684 | sb.append(" actions="); |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 1685 | sb.append(actions.length); |
Dan Sandler | 1b71878 | 2014-07-18 12:43:45 -0400 | [diff] [blame] | 1686 | } |
| 1687 | sb.append(" vis="); |
| 1688 | sb.append(visibilityToString(this.visibility)); |
| 1689 | if (this.publicVersion != null) { |
| 1690 | sb.append(" publicVersion="); |
| 1691 | sb.append(publicVersion.toString()); |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 1692 | } |
| 1693 | sb.append(")"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1694 | return sb.toString(); |
| 1695 | } |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1696 | |
Dan Sandler | 1b71878 | 2014-07-18 12:43:45 -0400 | [diff] [blame] | 1697 | /** |
| 1698 | * {@hide} |
| 1699 | */ |
| 1700 | public static String visibilityToString(int vis) { |
| 1701 | switch (vis) { |
| 1702 | case VISIBILITY_PRIVATE: |
| 1703 | return "PRIVATE"; |
| 1704 | case VISIBILITY_PUBLIC: |
| 1705 | return "PUBLIC"; |
| 1706 | case VISIBILITY_SECRET: |
| 1707 | return "SECRET"; |
| 1708 | default: |
| 1709 | return "UNKNOWN(" + String.valueOf(vis) + ")"; |
| 1710 | } |
| 1711 | } |
| 1712 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1713 | /** |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 1714 | * @hide |
| 1715 | */ |
| 1716 | public boolean isValid() { |
| 1717 | // Would like to check for icon!=0 here, too, but NotificationManagerService accepts that |
| 1718 | // for legacy reasons. |
| 1719 | return contentView != null || extras.getBoolean(Builder.EXTRA_REBUILD_CONTENT_VIEW); |
| 1720 | } |
| 1721 | |
| 1722 | /** |
Christoph Studer | c8db24b | 2014-07-25 17:50:30 +0200 | [diff] [blame] | 1723 | * @hide |
| 1724 | */ |
| 1725 | public boolean isGroupSummary() { |
| 1726 | return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0; |
| 1727 | } |
| 1728 | |
| 1729 | /** |
| 1730 | * @hide |
| 1731 | */ |
| 1732 | public boolean isGroupChild() { |
| 1733 | return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0; |
| 1734 | } |
| 1735 | |
| 1736 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1737 | * Builder class for {@link Notification} objects. |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 1738 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1739 | * 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] | 1740 | * content views using the platform's notification layout template. If your app supports |
| 1741 | * versions of Android as old as API level 4, you can instead use |
| 1742 | * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder}, |
| 1743 | * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support |
| 1744 | * library</a>. |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 1745 | * |
Scott Main | 183bf11 | 2012-08-13 19:12:13 -0700 | [diff] [blame] | 1746 | * <p>Example: |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 1747 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1748 | * <pre class="prettyprint"> |
Scott Main | 183bf11 | 2012-08-13 19:12:13 -0700 | [diff] [blame] | 1749 | * Notification noti = new Notification.Builder(mContext) |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1750 | * .setContentTitle("New mail from " + sender.toString()) |
| 1751 | * .setContentText(subject) |
| 1752 | * .setSmallIcon(R.drawable.new_mail) |
| 1753 | * .setLargeIcon(aBitmap) |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 1754 | * .build(); |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1755 | * </pre> |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1756 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1757 | public static class Builder { |
Daniel Sandler | 602ad1c | 2012-06-12 16:06:27 -0400 | [diff] [blame] | 1758 | private static final int MAX_ACTION_BUTTONS = 3; |
Jorim Jaggi | 445d3c0 | 2014-08-19 22:33:42 +0200 | [diff] [blame] | 1759 | private static final float LARGE_TEXT_SCALE = 1.3f; |
Daniel Sandler | 8680bf8 | 2012-05-15 16:52:52 -0400 | [diff] [blame] | 1760 | |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 1761 | /** |
| 1762 | * @hide |
| 1763 | */ |
| 1764 | public static final String EXTRA_NEEDS_REBUILD = "android.rebuild"; |
| 1765 | |
| 1766 | /** |
| 1767 | * @hide |
| 1768 | */ |
| 1769 | public static final String EXTRA_REBUILD_LARGE_ICON = "android.rebuild.largeIcon"; |
| 1770 | /** |
| 1771 | * @hide |
| 1772 | */ |
| 1773 | public static final String EXTRA_REBUILD_CONTENT_VIEW = "android.rebuild.contentView"; |
| 1774 | /** |
| 1775 | * @hide |
| 1776 | */ |
| 1777 | public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT = |
| 1778 | "android.rebuild.contentViewActionCount"; |
| 1779 | /** |
| 1780 | * @hide |
| 1781 | */ |
| 1782 | public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW |
| 1783 | = "android.rebuild.bigView"; |
| 1784 | /** |
| 1785 | * @hide |
| 1786 | */ |
| 1787 | public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT |
| 1788 | = "android.rebuild.bigViewActionCount"; |
| 1789 | /** |
| 1790 | * @hide |
| 1791 | */ |
| 1792 | public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW |
| 1793 | = "android.rebuild.hudView"; |
| 1794 | /** |
| 1795 | * @hide |
| 1796 | */ |
| 1797 | public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT |
| 1798 | = "android.rebuild.hudViewActionCount"; |
| 1799 | |
| 1800 | /** |
| 1801 | * The package name of the context used to create the notification via a Builder. |
| 1802 | */ |
| 1803 | private static final String EXTRA_REBUILD_CONTEXT_PACKAGE = |
| 1804 | "android.rebuild.contextPackage"; |
| 1805 | |
| 1806 | // Whether to enable stripping (at post time) & rebuilding (at listener receive time) of |
| 1807 | // memory intensive resources. |
| 1808 | private static final boolean STRIP_AND_REBUILD = true; |
| 1809 | |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1810 | private Context mContext; |
| 1811 | |
| 1812 | private long mWhen; |
| 1813 | private int mSmallIcon; |
| 1814 | private int mSmallIconLevel; |
Joe Onorato | 8595a3d | 2010-11-19 18:12:07 -0800 | [diff] [blame] | 1815 | private int mNumber; |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1816 | private CharSequence mContentTitle; |
| 1817 | private CharSequence mContentText; |
| 1818 | private CharSequence mContentInfo; |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 1819 | private CharSequence mSubText; |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1820 | private PendingIntent mContentIntent; |
| 1821 | private RemoteViews mContentView; |
| 1822 | private PendingIntent mDeleteIntent; |
| 1823 | private PendingIntent mFullScreenIntent; |
| 1824 | private CharSequence mTickerText; |
| 1825 | private RemoteViews mTickerView; |
| 1826 | private Bitmap mLargeIcon; |
| 1827 | private Uri mSound; |
| 1828 | private int mAudioStreamType; |
John Spurlock | c0650f02 | 2014-07-19 13:22:39 -0400 | [diff] [blame] | 1829 | private AudioAttributes mAudioAttributes; |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1830 | private long[] mVibrate; |
| 1831 | private int mLedArgb; |
| 1832 | private int mLedOnMs; |
| 1833 | private int mLedOffMs; |
| 1834 | private int mDefaults; |
| 1835 | private int mFlags; |
Jeff Sharkey | 1c40013 | 2011-08-05 14:50:13 -0700 | [diff] [blame] | 1836 | private int mProgressMax; |
| 1837 | private int mProgress; |
| 1838 | private boolean mProgressIndeterminate; |
John Spurlock | fd7f1e0 | 2014-03-18 16:41:57 -0400 | [diff] [blame] | 1839 | private String mCategory; |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 1840 | private String mGroupKey; |
| 1841 | private String mSortKey; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1842 | private Bundle mExtras; |
| 1843 | private int mPriority; |
Daniel Sandler | 8680bf8 | 2012-05-15 16:52:52 -0400 | [diff] [blame] | 1844 | private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS); |
Daniel Sandler | a2985ed | 2012-04-03 16:42:00 -0400 | [diff] [blame] | 1845 | private boolean mUseChronometer; |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 1846 | private Style mStyle; |
Daniel Sandler | 0c89049 | 2012-09-12 17:23:10 -0700 | [diff] [blame] | 1847 | private boolean mShowWhen = true; |
Dan Sandler | 0bf2ed8 | 2013-12-21 23:33:41 -0600 | [diff] [blame] | 1848 | private int mVisibility = VISIBILITY_PRIVATE; |
| 1849 | private Notification mPublicVersion = null; |
Dan Sandler | 26e81cf | 2014-05-06 10:01:27 -0400 | [diff] [blame] | 1850 | private final NotificationColorUtil mColorUtil; |
Chris Wren | dde7530 | 2014-03-26 17:24:15 -0400 | [diff] [blame] | 1851 | private ArrayList<String> mPeople; |
Dan Sandler | 26e81cf | 2014-05-06 10:01:27 -0400 | [diff] [blame] | 1852 | private int mColor = COLOR_DEFAULT; |
Christoph Studer | 7ac80e6 | 2014-08-04 16:01:57 +0200 | [diff] [blame] | 1853 | |
| 1854 | /** |
| 1855 | * The user that built the notification originally. |
| 1856 | */ |
| 1857 | private int mOriginatingUserId; |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 1858 | |
| 1859 | /** |
| 1860 | * Contains extras related to rebuilding during the build phase. |
| 1861 | */ |
| 1862 | private Bundle mRebuildBundle = new Bundle(); |
| 1863 | /** |
| 1864 | * Contains the notification to rebuild when this Builder is in "rebuild" mode. |
| 1865 | * Null otherwise. |
| 1866 | */ |
| 1867 | private Notification mRebuildNotification = null; |
| 1868 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1869 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1870 | * Constructs a new Builder with the defaults: |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1871 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1872 | |
| 1873 | * <table> |
| 1874 | * <tr><th align=right>priority</th> |
| 1875 | * <td>{@link #PRIORITY_DEFAULT}</td></tr> |
| 1876 | * <tr><th align=right>when</th> |
| 1877 | * <td>now ({@link System#currentTimeMillis()})</td></tr> |
| 1878 | * <tr><th align=right>audio stream</th> |
| 1879 | * <td>{@link #STREAM_DEFAULT}</td></tr> |
| 1880 | * </table> |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1881 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1882 | |
| 1883 | * @param context |
| 1884 | * A {@link Context} that will be used by the Builder to construct the |
| 1885 | * RemoteViews. The Context will not be held past the lifetime of this Builder |
| 1886 | * object. |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1887 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1888 | public Builder(Context context) { |
Adam Powell | bf06fa0 | 2014-05-30 12:32:18 -0700 | [diff] [blame] | 1889 | /* |
| 1890 | * Important compatibility note! |
| 1891 | * Some apps out in the wild create a Notification.Builder in their Activity subclass |
| 1892 | * constructor for later use. At this point Activities - themselves subclasses of |
| 1893 | * ContextWrapper - do not have their inner Context populated yet. This means that |
| 1894 | * any calls to Context methods from within this constructor can cause NPEs in existing |
| 1895 | * apps. Any data populated from mContext should therefore be populated lazily to |
| 1896 | * preserve compatibility. |
| 1897 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1898 | mContext = context; |
Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 1899 | |
| 1900 | // Set defaults to match the defaults of a Notification |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1901 | mWhen = System.currentTimeMillis(); |
Andy Stadler | 110988c | 2010-12-03 14:29:16 -0800 | [diff] [blame] | 1902 | mAudioStreamType = STREAM_DEFAULT; |
John Spurlock | c0650f02 | 2014-07-19 13:22:39 -0400 | [diff] [blame] | 1903 | mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1904 | mPriority = PRIORITY_DEFAULT; |
Chris Wren | dde7530 | 2014-03-26 17:24:15 -0400 | [diff] [blame] | 1905 | mPeople = new ArrayList<String>(); |
Jorim Jaggi | 39fa59f | 2014-02-25 15:38:45 +0100 | [diff] [blame] | 1906 | |
Dan Sandler | aa21e39 | 2014-08-06 19:26:11 +0000 | [diff] [blame] | 1907 | mColorUtil = NotificationColorUtil.getInstance(); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1908 | } |
| 1909 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1910 | /** |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 1911 | * Creates a Builder for rebuilding the given Notification. |
| 1912 | * <p> |
| 1913 | * Call {@link #rebuild()} to retrieve the rebuilt version of 'n'. |
| 1914 | */ |
| 1915 | private Builder(Context context, Notification n) { |
| 1916 | this(context); |
| 1917 | mRebuildNotification = n; |
| 1918 | restoreFromNotification(n); |
| 1919 | |
| 1920 | Style style = null; |
| 1921 | Bundle extras = n.extras; |
| 1922 | String templateClass = extras.getString(EXTRA_TEMPLATE); |
| 1923 | if (!TextUtils.isEmpty(templateClass)) { |
| 1924 | Class<? extends Style> styleClass = getNotificationStyleClass(templateClass); |
| 1925 | if (styleClass == null) { |
| 1926 | Log.d(TAG, "Unknown style class: " + styleClass); |
| 1927 | return; |
| 1928 | } |
| 1929 | |
| 1930 | try { |
| 1931 | Constructor<? extends Style> constructor = styleClass.getConstructor(); |
| 1932 | style = constructor.newInstance(); |
| 1933 | style.restoreFromExtras(extras); |
| 1934 | } catch (Throwable t) { |
| 1935 | Log.e(TAG, "Could not create Style", t); |
| 1936 | return; |
| 1937 | } |
| 1938 | } |
| 1939 | if (style != null) { |
| 1940 | setStyle(style); |
| 1941 | } |
| 1942 | } |
| 1943 | |
| 1944 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1945 | * 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] | 1946 | * It will be shown in the notification content view by default; use |
Griff Hazen | 50c1165 | 2014-05-16 09:46:31 -0700 | [diff] [blame] | 1947 | * {@link #setShowWhen(boolean) setShowWhen} to control this. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1948 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1949 | * @see Notification#when |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1950 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1951 | public Builder setWhen(long when) { |
| 1952 | mWhen = when; |
| 1953 | return this; |
| 1954 | } |
| 1955 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1956 | /** |
Griff Hazen | 50c1165 | 2014-05-16 09:46:31 -0700 | [diff] [blame] | 1957 | * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown |
Daniel Sandler | 0c89049 | 2012-09-12 17:23:10 -0700 | [diff] [blame] | 1958 | * in the content view. |
| 1959 | */ |
| 1960 | public Builder setShowWhen(boolean show) { |
| 1961 | mShowWhen = show; |
| 1962 | return this; |
| 1963 | } |
| 1964 | |
| 1965 | /** |
Daniel Sandler | d33b803 | 2012-05-10 11:41:48 -0400 | [diff] [blame] | 1966 | * Show the {@link Notification#when} field as a stopwatch. |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 1967 | * |
| 1968 | * 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] | 1969 | * automatically updating display of the minutes and seconds since <code>when</code>. |
Daniel Sandler | a2985ed | 2012-04-03 16:42:00 -0400 | [diff] [blame] | 1970 | * |
Daniel Sandler | d33b803 | 2012-05-10 11:41:48 -0400 | [diff] [blame] | 1971 | * Useful when showing an elapsed time (like an ongoing phone call). |
| 1972 | * |
| 1973 | * @see android.widget.Chronometer |
Daniel Sandler | a2985ed | 2012-04-03 16:42:00 -0400 | [diff] [blame] | 1974 | * @see Notification#when |
| 1975 | */ |
| 1976 | public Builder setUsesChronometer(boolean b) { |
| 1977 | mUseChronometer = b; |
| 1978 | return this; |
| 1979 | } |
| 1980 | |
| 1981 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1982 | * Set the small icon resource, which will be used to represent the notification in the |
| 1983 | * status bar. |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1984 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 1985 | |
| 1986 | * The platform template for the expanded view will draw this icon in the left, unless a |
| 1987 | * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small |
| 1988 | * icon will be moved to the right-hand side. |
| 1989 | * |
| 1990 | |
| 1991 | * @param icon |
| 1992 | * A resource ID in the application's package of the drawable to use. |
| 1993 | * @see Notification#icon |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 1994 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 1995 | public Builder setSmallIcon(int icon) { |
| 1996 | mSmallIcon = icon; |
| 1997 | return this; |
| 1998 | } |
| 1999 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2000 | /** |
| 2001 | * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional |
| 2002 | * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable |
| 2003 | * LevelListDrawable}. |
| 2004 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2005 | * @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] | 2006 | * @param level The level to use for the icon. |
| 2007 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2008 | * @see Notification#icon |
| 2009 | * @see Notification#iconLevel |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2010 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2011 | public Builder setSmallIcon(int icon, int level) { |
| 2012 | mSmallIcon = icon; |
| 2013 | mSmallIconLevel = level; |
| 2014 | return this; |
| 2015 | } |
| 2016 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2017 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2018 | * Set the first line of text in the platform notification template. |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2019 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2020 | public Builder setContentTitle(CharSequence title) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 2021 | mContentTitle = safeCharSequence(title); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2022 | return this; |
| 2023 | } |
| 2024 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2025 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2026 | * Set the second line of text in the platform notification template. |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2027 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2028 | public Builder setContentText(CharSequence text) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 2029 | mContentText = safeCharSequence(text); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2030 | return this; |
| 2031 | } |
| 2032 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2033 | /** |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 2034 | * Set the third line of text in the platform notification template. |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 2035 | * Don't use if you're also using {@link #setProgress(int, int, boolean)}; they occupy the |
| 2036 | * same location in the standard template. |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2037 | */ |
| 2038 | public Builder setSubText(CharSequence text) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 2039 | mSubText = safeCharSequence(text); |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2040 | return this; |
| 2041 | } |
| 2042 | |
| 2043 | /** |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2044 | * Set the large number at the right-hand side of the notification. This is |
| 2045 | * equivalent to setContentInfo, although it might show the number in a different |
| 2046 | * font size for readability. |
| 2047 | */ |
Joe Onorato | 8595a3d | 2010-11-19 18:12:07 -0800 | [diff] [blame] | 2048 | public Builder setNumber(int number) { |
| 2049 | mNumber = number; |
| 2050 | return this; |
| 2051 | } |
| 2052 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2053 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2054 | * A small piece of additional information pertaining to this notification. |
| 2055 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2056 | * The platform template will draw this on the last line of the notification, at the far |
| 2057 | * 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] | 2058 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2059 | public Builder setContentInfo(CharSequence info) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 2060 | mContentInfo = safeCharSequence(info); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2061 | return this; |
| 2062 | } |
| 2063 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2064 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2065 | * Set the progress this notification represents. |
| 2066 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2067 | * The platform template will represent this using a {@link ProgressBar}. |
Jeff Sharkey | 1c40013 | 2011-08-05 14:50:13 -0700 | [diff] [blame] | 2068 | */ |
| 2069 | public Builder setProgress(int max, int progress, boolean indeterminate) { |
| 2070 | mProgressMax = max; |
| 2071 | mProgress = progress; |
| 2072 | mProgressIndeterminate = indeterminate; |
| 2073 | return this; |
| 2074 | } |
| 2075 | |
| 2076 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2077 | * Supply a custom RemoteViews to use instead of the platform template. |
| 2078 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2079 | * @see Notification#contentView |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2080 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2081 | public Builder setContent(RemoteViews views) { |
| 2082 | mContentView = views; |
| 2083 | return this; |
| 2084 | } |
| 2085 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2086 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2087 | * Supply a {@link PendingIntent} to be sent when the notification is clicked. |
| 2088 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2089 | * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you |
| 2090 | * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use |
| 2091 | * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)} |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2092 | * 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] | 2093 | * clickable buttons inside the notification view). |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2094 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2095 | * @see Notification#contentIntent Notification.contentIntent |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2096 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2097 | public Builder setContentIntent(PendingIntent intent) { |
| 2098 | mContentIntent = intent; |
| 2099 | return this; |
| 2100 | } |
| 2101 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2102 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2103 | * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user. |
| 2104 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2105 | * @see Notification#deleteIntent |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2106 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2107 | public Builder setDeleteIntent(PendingIntent intent) { |
| 2108 | mDeleteIntent = intent; |
| 2109 | return this; |
| 2110 | } |
| 2111 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2112 | /** |
| 2113 | * An intent to launch instead of posting the notification to the status bar. |
| 2114 | * Only for use with extremely high-priority notifications demanding the user's |
| 2115 | * <strong>immediate</strong> attention, such as an incoming phone call or |
| 2116 | * alarm clock that the user has explicitly set to a particular time. |
| 2117 | * If this facility is used for something else, please give the user an option |
| 2118 | * to turn it off and use a normal notification, as this can be extremely |
| 2119 | * disruptive. |
| 2120 | * |
Chris Wren | 47c20a1 | 2014-06-18 17:27:29 -0400 | [diff] [blame] | 2121 | * <p> |
| 2122 | * The system UI may choose to display a heads-up notification, instead of |
| 2123 | * launching this intent, while the user is using the device. |
| 2124 | * </p> |
| 2125 | * |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2126 | * @param intent The pending intent to launch. |
| 2127 | * @param highPriority Passing true will cause this notification to be sent |
| 2128 | * even if other notifications are suppressed. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2129 | * |
| 2130 | * @see Notification#fullScreenIntent |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2131 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2132 | public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) { |
| 2133 | mFullScreenIntent = intent; |
| 2134 | setFlag(FLAG_HIGH_PRIORITY, highPriority); |
| 2135 | return this; |
| 2136 | } |
| 2137 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2138 | /** |
Dan Sandler | 5fcdf6e | 2014-07-18 11:31:15 -0400 | [diff] [blame] | 2139 | * Set the "ticker" text which is sent to accessibility services. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2140 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2141 | * @see Notification#tickerText |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2142 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2143 | public Builder setTicker(CharSequence tickerText) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 2144 | mTickerText = safeCharSequence(tickerText); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2145 | return this; |
| 2146 | } |
| 2147 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2148 | /** |
Dan Sandler | 5fcdf6e | 2014-07-18 11:31:15 -0400 | [diff] [blame] | 2149 | * Obsolete version of {@link #setTicker(CharSequence)}. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2150 | * |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2151 | */ |
Dan Sandler | 5fcdf6e | 2014-07-18 11:31:15 -0400 | [diff] [blame] | 2152 | @Deprecated |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2153 | public Builder setTicker(CharSequence tickerText, RemoteViews views) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 2154 | mTickerText = safeCharSequence(tickerText); |
Dan Sandler | 5fcdf6e | 2014-07-18 11:31:15 -0400 | [diff] [blame] | 2155 | mTickerView = views; // we'll save it for you anyway |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2156 | return this; |
| 2157 | } |
| 2158 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2159 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2160 | * Add a large icon to the notification (and the ticker on some devices). |
| 2161 | * |
| 2162 | * In the platform template, this image will be shown on the left of the notification view |
| 2163 | * in place of the {@link #setSmallIcon(int) small icon} (which will move to the right side). |
| 2164 | * |
| 2165 | * @see Notification#largeIcon |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2166 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2167 | public Builder setLargeIcon(Bitmap icon) { |
| 2168 | mLargeIcon = icon; |
| 2169 | return this; |
| 2170 | } |
| 2171 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2172 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2173 | * Set the sound to play. |
| 2174 | * |
John Spurlock | c0650f02 | 2014-07-19 13:22:39 -0400 | [diff] [blame] | 2175 | * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes} |
| 2176 | * for notifications. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2177 | * |
Chris Wren | 47c20a1 | 2014-06-18 17:27:29 -0400 | [diff] [blame] | 2178 | * <p> |
| 2179 | * A notification that is noisy is more likely to be presented as a heads-up notification. |
| 2180 | * </p> |
| 2181 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2182 | * @see Notification#sound |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2183 | */ |
Joe Onorato | 52f80cd | 2010-11-21 15:34:48 -0800 | [diff] [blame] | 2184 | public Builder setSound(Uri sound) { |
| 2185 | mSound = sound; |
John Spurlock | c0650f02 | 2014-07-19 13:22:39 -0400 | [diff] [blame] | 2186 | mAudioAttributes = AUDIO_ATTRIBUTES_DEFAULT; |
Joe Onorato | 52f80cd | 2010-11-21 15:34:48 -0800 | [diff] [blame] | 2187 | return this; |
| 2188 | } |
| 2189 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2190 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2191 | * 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] | 2192 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2193 | * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants. |
| 2194 | * |
Chris Wren | 47c20a1 | 2014-06-18 17:27:29 -0400 | [diff] [blame] | 2195 | * <p> |
| 2196 | * A notification that is noisy is more likely to be presented as a heads-up notification. |
| 2197 | * </p> |
John Spurlock | c0650f02 | 2014-07-19 13:22:39 -0400 | [diff] [blame] | 2198 | * @deprecated use {@link #setSound(Uri, AudioAttributes)} instead. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2199 | * @see Notification#sound |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2200 | */ |
Jean-Michel Trivi | 81f871e | 2014-08-06 16:32:38 -0700 | [diff] [blame] | 2201 | @Deprecated |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2202 | public Builder setSound(Uri sound, int streamType) { |
| 2203 | mSound = sound; |
| 2204 | mAudioStreamType = streamType; |
| 2205 | return this; |
| 2206 | } |
| 2207 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2208 | /** |
John Spurlock | c0650f02 | 2014-07-19 13:22:39 -0400 | [diff] [blame] | 2209 | * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to |
| 2210 | * use during playback. |
| 2211 | * |
| 2212 | * <p> |
| 2213 | * A notification that is noisy is more likely to be presented as a heads-up notification. |
| 2214 | * </p> |
| 2215 | * |
| 2216 | * @see Notification#sound |
| 2217 | */ |
| 2218 | public Builder setSound(Uri sound, AudioAttributes audioAttributes) { |
| 2219 | mSound = sound; |
| 2220 | mAudioAttributes = audioAttributes; |
| 2221 | return this; |
| 2222 | } |
| 2223 | |
| 2224 | /** |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2225 | * Set the vibration pattern to use. |
| 2226 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2227 | * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the |
| 2228 | * <code>pattern</code> parameter. |
| 2229 | * |
Chris Wren | 47c20a1 | 2014-06-18 17:27:29 -0400 | [diff] [blame] | 2230 | * <p> |
| 2231 | * A notification that vibrates is more likely to be presented as a heads-up notification. |
| 2232 | * </p> |
| 2233 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2234 | * @see Notification#vibrate |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2235 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2236 | public Builder setVibrate(long[] pattern) { |
| 2237 | mVibrate = pattern; |
| 2238 | return this; |
| 2239 | } |
| 2240 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2241 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2242 | * Set the desired color for the indicator LED on the device, as well as the |
| 2243 | * blink duty cycle (specified in milliseconds). |
| 2244 | * |
| 2245 | |
| 2246 | * Not all devices will honor all (or even any) of these values. |
| 2247 | * |
| 2248 | |
| 2249 | * @see Notification#ledARGB |
| 2250 | * @see Notification#ledOnMS |
| 2251 | * @see Notification#ledOffMS |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2252 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2253 | public Builder setLights(int argb, int onMs, int offMs) { |
| 2254 | mLedArgb = argb; |
| 2255 | mLedOnMs = onMs; |
| 2256 | mLedOffMs = offMs; |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2257 | return this; |
| 2258 | } |
| 2259 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2260 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2261 | * Set whether this is an "ongoing" notification. |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2262 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2263 | |
| 2264 | * Ongoing notifications cannot be dismissed by the user, so your application or service |
| 2265 | * must take care of canceling them. |
| 2266 | * |
| 2267 | |
| 2268 | * They are typically used to indicate a background task that the user is actively engaged |
| 2269 | * with (e.g., playing music) or is pending in some way and therefore occupying the device |
| 2270 | * (e.g., a file download, sync operation, active network connection). |
| 2271 | * |
| 2272 | |
| 2273 | * @see Notification#FLAG_ONGOING_EVENT |
| 2274 | * @see Service#setForeground(boolean) |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2275 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2276 | public Builder setOngoing(boolean ongoing) { |
| 2277 | setFlag(FLAG_ONGOING_EVENT, ongoing); |
| 2278 | return this; |
| 2279 | } |
| 2280 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2281 | /** |
| 2282 | * Set this flag if you would only like the sound, vibrate |
| 2283 | * and ticker to be played if the notification is not already showing. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2284 | * |
| 2285 | * @see Notification#FLAG_ONLY_ALERT_ONCE |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2286 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2287 | public Builder setOnlyAlertOnce(boolean onlyAlertOnce) { |
| 2288 | setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce); |
| 2289 | return this; |
| 2290 | } |
| 2291 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2292 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2293 | * Make this notification automatically dismissed when the user touches it. The |
| 2294 | * PendingIntent set with {@link #setDeleteIntent} will be sent when this happens. |
| 2295 | * |
| 2296 | * @see Notification#FLAG_AUTO_CANCEL |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2297 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2298 | public Builder setAutoCancel(boolean autoCancel) { |
Joe Onorato | 281d83f | 2011-01-04 17:13:10 -0800 | [diff] [blame] | 2299 | setFlag(FLAG_AUTO_CANCEL, autoCancel); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2300 | return this; |
| 2301 | } |
| 2302 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2303 | /** |
Griff Hazen | dfcb080 | 2014-02-11 12:00:00 -0800 | [diff] [blame] | 2304 | * Set whether or not this notification should not bridge to other devices. |
| 2305 | * |
| 2306 | * <p>Some notifications can be bridged to other devices for remote display. |
| 2307 | * This hint can be set to recommend this notification not be bridged. |
| 2308 | */ |
| 2309 | public Builder setLocalOnly(boolean localOnly) { |
| 2310 | setFlag(FLAG_LOCAL_ONLY, localOnly); |
| 2311 | return this; |
| 2312 | } |
| 2313 | |
| 2314 | /** |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2315 | * Set which notification properties will be inherited from system defaults. |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2316 | * <p> |
| 2317 | * The value should be one or more of the following fields combined with |
| 2318 | * bitwise-or: |
| 2319 | * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. |
| 2320 | * <p> |
| 2321 | * For all default values, use {@link #DEFAULT_ALL}. |
| 2322 | */ |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2323 | public Builder setDefaults(int defaults) { |
| 2324 | mDefaults = defaults; |
| 2325 | return this; |
| 2326 | } |
| 2327 | |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2328 | /** |
| 2329 | * Set the priority of this notification. |
| 2330 | * |
| 2331 | * @see Notification#priority |
| 2332 | */ |
Tor Norbye | d9273d6 | 2013-05-30 15:59:53 -0700 | [diff] [blame] | 2333 | public Builder setPriority(@Priority int pri) { |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2334 | mPriority = pri; |
| 2335 | return this; |
| 2336 | } |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 2337 | |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2338 | /** |
John Spurlock | fd7f1e0 | 2014-03-18 16:41:57 -0400 | [diff] [blame] | 2339 | * Set the notification category. |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 2340 | * |
John Spurlock | fd7f1e0 | 2014-03-18 16:41:57 -0400 | [diff] [blame] | 2341 | * @see Notification#category |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2342 | */ |
John Spurlock | fd7f1e0 | 2014-03-18 16:41:57 -0400 | [diff] [blame] | 2343 | public Builder setCategory(String category) { |
| 2344 | mCategory = category; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2345 | return this; |
| 2346 | } |
| 2347 | |
| 2348 | /** |
Chris Wren | dde7530 | 2014-03-26 17:24:15 -0400 | [diff] [blame] | 2349 | * Add a person that is relevant to this notification. |
| 2350 | * |
| 2351 | * @see Notification#EXTRA_PEOPLE |
| 2352 | */ |
| 2353 | public Builder addPerson(String handle) { |
| 2354 | mPeople.add(handle); |
| 2355 | return this; |
| 2356 | } |
| 2357 | |
| 2358 | /** |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 2359 | * Set this notification to be part of a group of notifications sharing the same key. |
| 2360 | * Grouped notifications may display in a cluster or stack on devices which |
| 2361 | * support such rendering. |
| 2362 | * |
| 2363 | * <p>To make this notification the summary for its group, also call |
| 2364 | * {@link #setGroupSummary}. A sort order can be specified for group members by using |
| 2365 | * {@link #setSortKey}. |
| 2366 | * @param groupKey The group key of the group. |
| 2367 | * @return this object for method chaining |
| 2368 | */ |
| 2369 | public Builder setGroup(String groupKey) { |
| 2370 | mGroupKey = groupKey; |
| 2371 | return this; |
| 2372 | } |
| 2373 | |
| 2374 | /** |
| 2375 | * Set this notification to be the group summary for a group of notifications. |
| 2376 | * Grouped notifications may display in a cluster or stack on devices which |
| 2377 | * support such rendering. Requires a group key also be set using {@link #setGroup}. |
| 2378 | * @param isGroupSummary Whether this notification should be a group summary. |
| 2379 | * @return this object for method chaining |
| 2380 | */ |
| 2381 | public Builder setGroupSummary(boolean isGroupSummary) { |
| 2382 | setFlag(FLAG_GROUP_SUMMARY, isGroupSummary); |
| 2383 | return this; |
| 2384 | } |
| 2385 | |
| 2386 | /** |
| 2387 | * Set a sort key that orders this notification among other notifications from the |
| 2388 | * same package. This can be useful if an external sort was already applied and an app |
| 2389 | * would like to preserve this. Notifications will be sorted lexicographically using this |
| 2390 | * value, although providing different priorities in addition to providing sort key may |
| 2391 | * cause this value to be ignored. |
| 2392 | * |
| 2393 | * <p>This sort key can also be used to order members of a notification group. See |
Griff Hazen | 9e1379f | 2014-05-20 12:50:51 -0700 | [diff] [blame] | 2394 | * {@link #setGroup}. |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 2395 | * |
| 2396 | * @see String#compareTo(String) |
| 2397 | */ |
| 2398 | public Builder setSortKey(String sortKey) { |
| 2399 | mSortKey = sortKey; |
| 2400 | return this; |
| 2401 | } |
| 2402 | |
| 2403 | /** |
Griff Hazen | 720042b | 2014-02-24 15:46:56 -0800 | [diff] [blame] | 2404 | * Merge additional metadata into this notification. |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2405 | * |
Griff Hazen | 720042b | 2014-02-24 15:46:56 -0800 | [diff] [blame] | 2406 | * <p>Values within the Bundle will replace existing extras values in this Builder. |
| 2407 | * |
| 2408 | * @see Notification#extras |
| 2409 | */ |
Griff Hazen | 959591e | 2014-05-15 22:26:18 -0700 | [diff] [blame] | 2410 | public Builder addExtras(Bundle extras) { |
| 2411 | if (extras != null) { |
| 2412 | if (mExtras == null) { |
| 2413 | mExtras = new Bundle(extras); |
| 2414 | } else { |
| 2415 | mExtras.putAll(extras); |
| 2416 | } |
Griff Hazen | 720042b | 2014-02-24 15:46:56 -0800 | [diff] [blame] | 2417 | } |
| 2418 | return this; |
| 2419 | } |
| 2420 | |
| 2421 | /** |
| 2422 | * Set metadata for this notification. |
| 2423 | * |
| 2424 | * <p>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] | 2425 | * current contents are copied into the Notification each time {@link #build()} is |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2426 | * called. |
| 2427 | * |
Griff Hazen | 720042b | 2014-02-24 15:46:56 -0800 | [diff] [blame] | 2428 | * <p>Replaces any existing extras values with those from the provided Bundle. |
| 2429 | * Use {@link #addExtras} to merge in metadata instead. |
| 2430 | * |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2431 | * @see Notification#extras |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2432 | */ |
Griff Hazen | 959591e | 2014-05-15 22:26:18 -0700 | [diff] [blame] | 2433 | public Builder setExtras(Bundle extras) { |
| 2434 | mExtras = extras; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2435 | return this; |
| 2436 | } |
| 2437 | |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 2438 | /** |
Griff Hazen | 720042b | 2014-02-24 15:46:56 -0800 | [diff] [blame] | 2439 | * Get the current metadata Bundle used by this notification Builder. |
| 2440 | * |
| 2441 | * <p>The returned Bundle is shared with this Builder. |
| 2442 | * |
| 2443 | * <p>The current contents of this Bundle are copied into the Notification each time |
| 2444 | * {@link #build()} is called. |
| 2445 | * |
| 2446 | * @see Notification#extras |
| 2447 | */ |
| 2448 | public Bundle getExtras() { |
| 2449 | if (mExtras == null) { |
| 2450 | mExtras = new Bundle(); |
| 2451 | } |
| 2452 | return mExtras; |
| 2453 | } |
| 2454 | |
| 2455 | /** |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 2456 | * Add an action to this notification. Actions are typically displayed by |
| 2457 | * the system as a button adjacent to the notification content. |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 2458 | * <p> |
| 2459 | * Every action must have an icon (32dp square and matching the |
| 2460 | * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo |
| 2461 | * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}. |
| 2462 | * <p> |
| 2463 | * A notification in its expanded form can display up to 3 actions, from left to right in |
| 2464 | * the order they were added. Actions will not be displayed when the notification is |
| 2465 | * collapsed, however, so be sure that any essential functions may be accessed by the user |
| 2466 | * in some other way (for example, in the Activity pointed to by {@link #contentIntent}). |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 2467 | * |
| 2468 | * @param icon Resource ID of a drawable that represents the action. |
| 2469 | * @param title Text describing the action. |
| 2470 | * @param intent PendingIntent to be fired when the action is invoked. |
| 2471 | */ |
| 2472 | public Builder addAction(int icon, CharSequence title, PendingIntent intent) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 2473 | mActions.add(new Action(icon, safeCharSequence(title), intent)); |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 2474 | return this; |
| 2475 | } |
| 2476 | |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 2477 | /** |
Griff Hazen | 959591e | 2014-05-15 22:26:18 -0700 | [diff] [blame] | 2478 | * Add an action to this notification. Actions are typically displayed by |
| 2479 | * the system as a button adjacent to the notification content. |
| 2480 | * <p> |
| 2481 | * Every action must have an icon (32dp square and matching the |
| 2482 | * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo |
| 2483 | * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}. |
| 2484 | * <p> |
| 2485 | * A notification in its expanded form can display up to 3 actions, from left to right in |
| 2486 | * the order they were added. Actions will not be displayed when the notification is |
| 2487 | * collapsed, however, so be sure that any essential functions may be accessed by the user |
| 2488 | * in some other way (for example, in the Activity pointed to by {@link #contentIntent}). |
| 2489 | * |
| 2490 | * @param action The action to add. |
| 2491 | */ |
| 2492 | public Builder addAction(Action action) { |
| 2493 | mActions.add(action); |
| 2494 | return this; |
| 2495 | } |
| 2496 | |
| 2497 | /** |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 2498 | * Add a rich notification style to be applied at build time. |
| 2499 | * |
| 2500 | * @param style Object responsible for modifying the notification style. |
| 2501 | */ |
| 2502 | public Builder setStyle(Style style) { |
| 2503 | if (mStyle != style) { |
| 2504 | mStyle = style; |
Daniel Sandler | c08dea2 | 2012-06-28 08:35:24 -0700 | [diff] [blame] | 2505 | if (mStyle != null) { |
| 2506 | mStyle.setBuilder(this); |
| 2507 | } |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 2508 | } |
| 2509 | return this; |
| 2510 | } |
| 2511 | |
Dan Sandler | 0bf2ed8 | 2013-12-21 23:33:41 -0600 | [diff] [blame] | 2512 | /** |
| 2513 | * Specify the value of {@link #visibility}. |
Griff Hazen | b720abe | 2014-05-20 13:15:30 -0700 | [diff] [blame] | 2514 | * |
Dan Sandler | 0bf2ed8 | 2013-12-21 23:33:41 -0600 | [diff] [blame] | 2515 | * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default), |
| 2516 | * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}. |
| 2517 | * |
| 2518 | * @return The same Builder. |
| 2519 | */ |
| 2520 | public Builder setVisibility(int visibility) { |
| 2521 | mVisibility = visibility; |
| 2522 | return this; |
| 2523 | } |
| 2524 | |
| 2525 | /** |
| 2526 | * Supply a replacement Notification whose contents should be shown in insecure contexts |
| 2527 | * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}. |
| 2528 | * @param n A replacement notification, presumably with some or all info redacted. |
| 2529 | * @return The same Builder. |
| 2530 | */ |
| 2531 | public Builder setPublicVersion(Notification n) { |
| 2532 | mPublicVersion = n; |
| 2533 | return this; |
| 2534 | } |
| 2535 | |
Griff Hazen | b720abe | 2014-05-20 13:15:30 -0700 | [diff] [blame] | 2536 | /** |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 2537 | * Apply an extender to this notification builder. Extenders may be used to add |
| 2538 | * metadata or change options on this builder. |
| 2539 | */ |
Griff Hazen | 61a9e86 | 2014-05-22 16:05:19 -0700 | [diff] [blame] | 2540 | public Builder extend(Extender extender) { |
| 2541 | extender.extend(this); |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 2542 | return this; |
| 2543 | } |
| 2544 | |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2545 | private void setFlag(int mask, boolean value) { |
| 2546 | if (value) { |
| 2547 | mFlags |= mask; |
| 2548 | } else { |
| 2549 | mFlags &= ~mask; |
| 2550 | } |
| 2551 | } |
| 2552 | |
Dan Sandler | 26e81cf | 2014-05-06 10:01:27 -0400 | [diff] [blame] | 2553 | /** |
| 2554 | * Sets {@link Notification#color}. |
| 2555 | * |
| 2556 | * @param argb The accent color to use |
| 2557 | * |
| 2558 | * @return The same Builder. |
| 2559 | */ |
| 2560 | public Builder setColor(int argb) { |
| 2561 | mColor = argb; |
| 2562 | return this; |
| 2563 | } |
| 2564 | |
Kenny Guy | 8a0101b | 2014-05-08 23:34:12 +0100 | [diff] [blame] | 2565 | private Bitmap getProfileBadge() { |
Christoph Studer | 7ac80e6 | 2014-08-04 16:01:57 +0200 | [diff] [blame] | 2566 | // Note: This assumes that the current user can read the profile badge of the |
| 2567 | // originating user. |
Kenny Guy | 8a0101b | 2014-05-08 23:34:12 +0100 | [diff] [blame] | 2568 | UserManager userManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
Svetoslav | c71c42f | 2014-08-05 18:57:05 -0700 | [diff] [blame] | 2569 | Drawable badge = userManager.getBadgeForUser(new UserHandle(mOriginatingUserId), 0); |
Kenny Guy | 8a0101b | 2014-05-08 23:34:12 +0100 | [diff] [blame] | 2570 | if (badge == null) { |
| 2571 | return null; |
| 2572 | } |
| 2573 | final int width = badge.getIntrinsicWidth(); |
| 2574 | final int height = badge.getIntrinsicHeight(); |
| 2575 | Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); |
| 2576 | Canvas canvas = new Canvas(bitmap); |
| 2577 | badge.setBounds(0, 0, width, height); |
| 2578 | badge.draw(canvas); |
| 2579 | return bitmap; |
| 2580 | } |
| 2581 | |
Kenny Guy | 98193ea | 2014-07-24 19:54:37 +0100 | [diff] [blame^] | 2582 | private boolean addProfileBadge(RemoteViews contentView, int resId) { |
| 2583 | Bitmap profileBadge = getProfileBadge(); |
| 2584 | |
| 2585 | contentView.setViewVisibility(R.id.profile_badge_large_template, View.GONE); |
| 2586 | contentView.setViewVisibility(R.id.profile_badge_line2, View.GONE); |
| 2587 | contentView.setViewVisibility(R.id.profile_badge_line3, View.GONE); |
| 2588 | |
| 2589 | if (profileBadge != null) { |
| 2590 | contentView.setImageViewBitmap(resId, profileBadge); |
| 2591 | contentView.setViewVisibility(resId, View.VISIBLE); |
| 2592 | |
| 2593 | // Make sure Line 3 is visible. As badge will be here if there |
| 2594 | // is no text to display. |
| 2595 | if (resId == R.id.profile_badge_line3) { |
| 2596 | contentView.setViewVisibility(R.id.line3, View.VISIBLE); |
| 2597 | } |
| 2598 | return true; |
| 2599 | } |
| 2600 | return false; |
| 2601 | } |
| 2602 | |
Jorim Jaggi | 445d3c0 | 2014-08-19 22:33:42 +0200 | [diff] [blame] | 2603 | private RemoteViews applyStandardTemplate(int resId) { |
Svet Ganov | 0da85b6 | 2014-08-06 14:11:37 -0700 | [diff] [blame] | 2604 | RemoteViews contentView = new BuilderRemoteViews(mContext.getPackageName(), |
| 2605 | mOriginatingUserId, resId); |
Dan Sandler | 539aad4 | 2014-08-04 00:43:39 -0400 | [diff] [blame] | 2606 | |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 2607 | boolean showLine3 = false; |
| 2608 | boolean showLine2 = false; |
Kenny Guy | 98193ea | 2014-07-24 19:54:37 +0100 | [diff] [blame^] | 2609 | boolean contentTextInLine2 = false; |
Dan Sandler | 190d58d | 2014-05-15 09:33:39 -0400 | [diff] [blame] | 2610 | |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 2611 | if (mLargeIcon != null) { |
| 2612 | contentView.setImageViewBitmap(R.id.icon, mLargeIcon); |
Dan Sandler | 26e81cf | 2014-05-06 10:01:27 -0400 | [diff] [blame] | 2613 | processLargeIcon(mLargeIcon, contentView); |
Dan Sandler | 190d58d | 2014-05-15 09:33:39 -0400 | [diff] [blame] | 2614 | contentView.setImageViewResource(R.id.right_icon, mSmallIcon); |
| 2615 | contentView.setViewVisibility(R.id.right_icon, View.VISIBLE); |
| 2616 | processSmallRightIcon(mSmallIcon, contentView); |
| 2617 | } else { // small icon at left |
| 2618 | contentView.setImageViewResource(R.id.icon, mSmallIcon); |
| 2619 | contentView.setViewVisibility(R.id.icon, View.VISIBLE); |
| 2620 | processSmallIconAsLarge(mSmallIcon, contentView); |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 2621 | } |
| 2622 | if (mContentTitle != null) { |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 2623 | contentView.setTextViewText(R.id.title, processLegacyText(mContentTitle)); |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 2624 | } |
| 2625 | if (mContentText != null) { |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 2626 | contentView.setTextViewText(R.id.text, processLegacyText(mContentText)); |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 2627 | showLine3 = true; |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 2628 | } |
| 2629 | if (mContentInfo != null) { |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 2630 | contentView.setTextViewText(R.id.info, processLegacyText(mContentInfo)); |
Jeff Sharkey | 1c40013 | 2011-08-05 14:50:13 -0700 | [diff] [blame] | 2631 | contentView.setViewVisibility(R.id.info, View.VISIBLE); |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 2632 | showLine3 = true; |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 2633 | } else if (mNumber > 0) { |
Daniel Sandler | ebce011 | 2011-06-16 16:44:51 -0400 | [diff] [blame] | 2634 | final int tooBig = mContext.getResources().getInteger( |
| 2635 | R.integer.status_bar_notification_info_maxnum); |
| 2636 | if (mNumber > tooBig) { |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 2637 | contentView.setTextViewText(R.id.info, processLegacyText( |
| 2638 | mContext.getResources().getString( |
| 2639 | R.string.status_bar_notification_info_overflow))); |
Joe Onorato | 059a2f8 | 2011-01-04 10:27:01 -0800 | [diff] [blame] | 2640 | } else { |
| 2641 | NumberFormat f = NumberFormat.getIntegerInstance(); |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 2642 | contentView.setTextViewText(R.id.info, processLegacyText(f.format(mNumber))); |
Joe Onorato | 059a2f8 | 2011-01-04 10:27:01 -0800 | [diff] [blame] | 2643 | } |
Jeff Sharkey | 1c40013 | 2011-08-05 14:50:13 -0700 | [diff] [blame] | 2644 | contentView.setViewVisibility(R.id.info, View.VISIBLE); |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 2645 | showLine3 = true; |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 2646 | } else { |
| 2647 | contentView.setViewVisibility(R.id.info, View.GONE); |
| 2648 | } |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2649 | |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 2650 | // Need to show three lines? |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2651 | if (mSubText != null) { |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 2652 | contentView.setTextViewText(R.id.text, processLegacyText(mSubText)); |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 2653 | if (mContentText != null) { |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 2654 | contentView.setTextViewText(R.id.text2, processLegacyText(mContentText)); |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 2655 | contentView.setViewVisibility(R.id.text2, View.VISIBLE); |
| 2656 | showLine2 = true; |
Kenny Guy | 98193ea | 2014-07-24 19:54:37 +0100 | [diff] [blame^] | 2657 | contentTextInLine2 = true; |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 2658 | } else { |
| 2659 | contentView.setViewVisibility(R.id.text2, View.GONE); |
| 2660 | } |
Jeff Sharkey | 1c40013 | 2011-08-05 14:50:13 -0700 | [diff] [blame] | 2661 | } else { |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2662 | contentView.setViewVisibility(R.id.text2, View.GONE); |
| 2663 | if (mProgressMax != 0 || mProgressIndeterminate) { |
| 2664 | contentView.setProgressBar( |
| 2665 | R.id.progress, mProgressMax, mProgress, mProgressIndeterminate); |
| 2666 | contentView.setViewVisibility(R.id.progress, View.VISIBLE); |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 2667 | showLine2 = true; |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 2668 | } else { |
| 2669 | contentView.setViewVisibility(R.id.progress, View.GONE); |
| 2670 | } |
Jeff Sharkey | 1c40013 | 2011-08-05 14:50:13 -0700 | [diff] [blame] | 2671 | } |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 2672 | if (showLine2) { |
Jorim Jaggi | 445d3c0 | 2014-08-19 22:33:42 +0200 | [diff] [blame] | 2673 | |
| 2674 | // need to shrink all the type to make sure everything fits |
| 2675 | final Resources res = mContext.getResources(); |
| 2676 | final float subTextSize = res.getDimensionPixelSize( |
| 2677 | R.dimen.notification_subtext_size); |
| 2678 | contentView.setTextViewTextSize(R.id.text, TypedValue.COMPLEX_UNIT_PX, subTextSize); |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 2679 | } |
| 2680 | |
Daniel Sandler | 0c89049 | 2012-09-12 17:23:10 -0700 | [diff] [blame] | 2681 | if (mWhen != 0 && mShowWhen) { |
Daniel Sandler | a2985ed | 2012-04-03 16:42:00 -0400 | [diff] [blame] | 2682 | if (mUseChronometer) { |
| 2683 | contentView.setViewVisibility(R.id.chronometer, View.VISIBLE); |
| 2684 | contentView.setLong(R.id.chronometer, "setBase", |
| 2685 | mWhen + (SystemClock.elapsedRealtime() - System.currentTimeMillis())); |
| 2686 | contentView.setBoolean(R.id.chronometer, "setStarted", true); |
| 2687 | } else { |
| 2688 | contentView.setViewVisibility(R.id.time, View.VISIBLE); |
| 2689 | contentView.setLong(R.id.time, "setTime", mWhen); |
| 2690 | } |
Daniel Sandler | 0c89049 | 2012-09-12 17:23:10 -0700 | [diff] [blame] | 2691 | } else { |
| 2692 | contentView.setViewVisibility(R.id.time, View.GONE); |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 2693 | } |
Daniel Sandler | 0c89049 | 2012-09-12 17:23:10 -0700 | [diff] [blame] | 2694 | |
Jorim Jaggi | 445d3c0 | 2014-08-19 22:33:42 +0200 | [diff] [blame] | 2695 | // Adjust padding depending on line count and font size. |
| 2696 | contentView.setViewPadding(R.id.line1, 0, calculateTopPadding(mContext, |
| 2697 | hasThreeLines(), mContext.getResources().getConfiguration().fontScale), |
| 2698 | 0, 0); |
| 2699 | |
Kenny Guy | 98193ea | 2014-07-24 19:54:37 +0100 | [diff] [blame^] | 2700 | // We want to add badge to first line of text. |
| 2701 | boolean addedBadge = addProfileBadge(contentView, |
| 2702 | contentTextInLine2 ? R.id.profile_badge_line2 : R.id.profile_badge_line3); |
| 2703 | // If we added the badge to line 3 then we should show line 3. |
| 2704 | if (addedBadge && !contentTextInLine2) { |
| 2705 | showLine3 = true; |
| 2706 | } |
| 2707 | |
| 2708 | // Note getStandardView may hide line 3 again. |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 2709 | contentView.setViewVisibility(R.id.line3, showLine3 ? View.VISIBLE : View.GONE); |
Daniel Sandler | 6387d2f | 2012-05-22 13:44:09 -0400 | [diff] [blame] | 2710 | contentView.setViewVisibility(R.id.overflow_divider, showLine3 ? View.VISIBLE : View.GONE); |
Joe Onorato | 561d385 | 2010-11-20 18:09:34 -0800 | [diff] [blame] | 2711 | return contentView; |
| 2712 | } |
| 2713 | |
Jorim Jaggi | 445d3c0 | 2014-08-19 22:33:42 +0200 | [diff] [blame] | 2714 | /** |
| 2715 | * Logic to find out whether the notification is going to have three lines in the contracted |
| 2716 | * layout. This is used to adjust the top padding. |
| 2717 | * |
| 2718 | * @return true if the notification is going to have three lines; false if the notification |
| 2719 | * is going to have one or two lines |
| 2720 | */ |
| 2721 | private boolean hasThreeLines() { |
| 2722 | boolean hasLine3 = mContentText != null || mContentInfo != null || mNumber > 0; |
| 2723 | boolean hasLine2 = (mSubText != null && mContentText != null) || |
| 2724 | (mSubText == null && (mProgressMax != 0 || mProgressIndeterminate)); |
| 2725 | return hasLine2 && hasLine3; |
| 2726 | } |
| 2727 | |
| 2728 | /** |
| 2729 | * @hide |
| 2730 | */ |
| 2731 | public static int calculateTopPadding(Context ctx, boolean hasThreeLines, |
| 2732 | float fontScale) { |
| 2733 | int padding = ctx.getResources().getDimensionPixelSize(hasThreeLines |
| 2734 | ? R.dimen.notification_top_pad_narrow |
| 2735 | : R.dimen.notification_top_pad); |
| 2736 | int largePadding = ctx.getResources().getDimensionPixelSize(hasThreeLines |
| 2737 | ? R.dimen.notification_top_pad_large_text_narrow |
| 2738 | : R.dimen.notification_top_pad_large_text); |
| 2739 | float largeFactor = (MathUtils.constrain(fontScale, 1.0f, LARGE_TEXT_SCALE) - 1f) |
| 2740 | / (LARGE_TEXT_SCALE - 1f); |
| 2741 | |
| 2742 | // Linearly interpolate the padding between large and normal with the font scale ranging |
| 2743 | // from 1f to LARGE_TEXT_SCALE |
| 2744 | return Math.round((1 - largeFactor) * padding + largeFactor * largePadding); |
| 2745 | } |
| 2746 | |
Daniel Sandler | 96fd7c1 | 2012-03-30 16:37:36 -0400 | [diff] [blame] | 2747 | private RemoteViews applyStandardTemplateWithActions(int layoutId) { |
Jorim Jaggi | 445d3c0 | 2014-08-19 22:33:42 +0200 | [diff] [blame] | 2748 | RemoteViews big = applyStandardTemplate(layoutId); |
Daniel Sandler | 96fd7c1 | 2012-03-30 16:37:36 -0400 | [diff] [blame] | 2749 | |
| 2750 | int N = mActions.size(); |
| 2751 | if (N > 0) { |
Daniel Sandler | 96fd7c1 | 2012-03-30 16:37:36 -0400 | [diff] [blame] | 2752 | big.setViewVisibility(R.id.actions, View.VISIBLE); |
Daniel Sandler | 6387d2f | 2012-05-22 13:44:09 -0400 | [diff] [blame] | 2753 | big.setViewVisibility(R.id.action_divider, View.VISIBLE); |
Daniel Sandler | 8680bf8 | 2012-05-15 16:52:52 -0400 | [diff] [blame] | 2754 | if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS; |
Chris Wren | 2c22eb0 | 2012-05-08 09:49:13 -0400 | [diff] [blame] | 2755 | big.removeAllViews(R.id.actions); |
Daniel Sandler | 96fd7c1 | 2012-03-30 16:37:36 -0400 | [diff] [blame] | 2756 | for (int i=0; i<N; i++) { |
| 2757 | final RemoteViews button = generateActionButton(mActions.get(i)); |
Daniel Sandler | 96fd7c1 | 2012-03-30 16:37:36 -0400 | [diff] [blame] | 2758 | big.addView(R.id.actions, button); |
| 2759 | } |
| 2760 | } |
| 2761 | return big; |
| 2762 | } |
| 2763 | |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2764 | private RemoteViews makeContentView() { |
| 2765 | if (mContentView != null) { |
| 2766 | return mContentView; |
| 2767 | } else { |
Jorim Jaggi | 445d3c0 | 2014-08-19 22:33:42 +0200 | [diff] [blame] | 2768 | return applyStandardTemplate(getBaseLayoutResource()); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2769 | } |
| 2770 | } |
| 2771 | |
| 2772 | private RemoteViews makeTickerView() { |
| 2773 | if (mTickerView != null) { |
| 2774 | return mTickerView; |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2775 | } |
Dan Sandler | 5fcdf6e | 2014-07-18 11:31:15 -0400 | [diff] [blame] | 2776 | return null; // tickers are not created by default anymore |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2777 | } |
| 2778 | |
Daniel Sandler | 96fd7c1 | 2012-03-30 16:37:36 -0400 | [diff] [blame] | 2779 | private RemoteViews makeBigContentView() { |
| 2780 | if (mActions.size() == 0) return null; |
| 2781 | |
Jorim Jaggi | 39fa59f | 2014-02-25 15:38:45 +0100 | [diff] [blame] | 2782 | return applyStandardTemplateWithActions(getBigBaseLayoutResource()); |
Daniel Sandler | 96fd7c1 | 2012-03-30 16:37:36 -0400 | [diff] [blame] | 2783 | } |
| 2784 | |
Jorim Jaggi | 39fa59f | 2014-02-25 15:38:45 +0100 | [diff] [blame] | 2785 | private RemoteViews makeHeadsUpContentView() { |
Chris Wren | 8fd39ec | 2014-02-27 17:43:26 -0500 | [diff] [blame] | 2786 | if (mActions.size() == 0) return null; |
| 2787 | |
Jorim Jaggi | 39fa59f | 2014-02-25 15:38:45 +0100 | [diff] [blame] | 2788 | return applyStandardTemplateWithActions(getBigBaseLayoutResource()); |
Chris Wren | 8fd39ec | 2014-02-27 17:43:26 -0500 | [diff] [blame] | 2789 | } |
| 2790 | |
| 2791 | |
Daniel Sandler | 96fd7c1 | 2012-03-30 16:37:36 -0400 | [diff] [blame] | 2792 | private RemoteViews generateActionButton(Action action) { |
Daniel Sandler | 8680bf8 | 2012-05-15 16:52:52 -0400 | [diff] [blame] | 2793 | final boolean tombstone = (action.actionIntent == null); |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 2794 | RemoteViews button = new RemoteViews(mContext.getPackageName(), |
Jorim Jaggi | 39fa59f | 2014-02-25 15:38:45 +0100 | [diff] [blame] | 2795 | tombstone ? getActionTombstoneLayoutResource() |
| 2796 | : getActionLayoutResource()); |
Chris Wren | 8749ac8a | 2013-12-03 14:31:01 -0500 | [diff] [blame] | 2797 | button.setTextViewCompoundDrawablesRelative(R.id.action0, action.icon, 0, 0, 0); |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 2798 | button.setTextViewText(R.id.action0, processLegacyText(action.title)); |
Daniel Sandler | 8680bf8 | 2012-05-15 16:52:52 -0400 | [diff] [blame] | 2799 | if (!tombstone) { |
Daniel Sandler | e551884 | 2012-05-10 16:20:40 -0400 | [diff] [blame] | 2800 | button.setOnClickPendingIntent(R.id.action0, action.actionIntent); |
Daniel Sandler | e551884 | 2012-05-10 16:20:40 -0400 | [diff] [blame] | 2801 | } |
Daniel Sandler | 96fd7c1 | 2012-03-30 16:37:36 -0400 | [diff] [blame] | 2802 | button.setContentDescription(R.id.action0, action.title); |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 2803 | processLegacyAction(action, button); |
Daniel Sandler | 96fd7c1 | 2012-03-30 16:37:36 -0400 | [diff] [blame] | 2804 | return button; |
| 2805 | } |
| 2806 | |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2807 | /** |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 2808 | * @return Whether we are currently building a notification from a legacy (an app that |
Alan Viverette | 3cb07a46 | 2014-06-06 14:19:53 -0700 | [diff] [blame] | 2809 | * doesn't create material notifications by itself) app. |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 2810 | */ |
| 2811 | private boolean isLegacy() { |
Dan Sandler | 26e81cf | 2014-05-06 10:01:27 -0400 | [diff] [blame] | 2812 | return mColorUtil != null; |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 2813 | } |
| 2814 | |
| 2815 | private void processLegacyAction(Action action, RemoteViews button) { |
| 2816 | if (isLegacy()) { |
Dan Sandler | 26e81cf | 2014-05-06 10:01:27 -0400 | [diff] [blame] | 2817 | if (mColorUtil.isGrayscale(mContext, action.icon)) { |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 2818 | button.setTextViewCompoundDrawablesRelativeColorFilter(R.id.action0, 0, |
| 2819 | mContext.getResources().getColor( |
| 2820 | R.color.notification_action_legacy_color_filter), |
| 2821 | PorterDuff.Mode.MULTIPLY); |
| 2822 | } |
| 2823 | } |
| 2824 | } |
| 2825 | |
| 2826 | private CharSequence processLegacyText(CharSequence charSequence) { |
| 2827 | if (isLegacy()) { |
Dan Sandler | 26e81cf | 2014-05-06 10:01:27 -0400 | [diff] [blame] | 2828 | return mColorUtil.invertCharSequenceColors(charSequence); |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 2829 | } else { |
| 2830 | return charSequence; |
| 2831 | } |
| 2832 | } |
| 2833 | |
Dan Sandler | 26e81cf | 2014-05-06 10:01:27 -0400 | [diff] [blame] | 2834 | /** |
| 2835 | * Apply any necessary background to smallIcons being used in the largeIcon spot. |
| 2836 | */ |
| 2837 | private void processSmallIconAsLarge(int largeIconId, RemoteViews contentView) { |
| 2838 | if (!isLegacy() || mColorUtil.isGrayscale(mContext, largeIconId)) { |
| 2839 | applyLargeIconBackground(contentView); |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 2840 | } |
| 2841 | } |
| 2842 | |
Dan Sandler | 26e81cf | 2014-05-06 10:01:27 -0400 | [diff] [blame] | 2843 | /** |
| 2844 | * Apply any necessary background to a largeIcon if it's a fake smallIcon (that is, |
| 2845 | * if it's grayscale). |
| 2846 | */ |
| 2847 | // TODO: also check bounds, transparency, that sort of thing. |
| 2848 | private void processLargeIcon(Bitmap largeIcon, RemoteViews contentView) { |
| 2849 | if (!isLegacy() || mColorUtil.isGrayscale(largeIcon)) { |
| 2850 | applyLargeIconBackground(contentView); |
Dan Sandler | 190d58d | 2014-05-15 09:33:39 -0400 | [diff] [blame] | 2851 | } else { |
| 2852 | removeLargeIconBackground(contentView); |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 2853 | } |
| 2854 | } |
| 2855 | |
Dan Sandler | 26e81cf | 2014-05-06 10:01:27 -0400 | [diff] [blame] | 2856 | /** |
| 2857 | * Add a colored circle behind the largeIcon slot. |
| 2858 | */ |
| 2859 | private void applyLargeIconBackground(RemoteViews contentView) { |
| 2860 | contentView.setInt(R.id.icon, "setBackgroundResource", |
Jorim Jaggi | 445d3c0 | 2014-08-19 22:33:42 +0200 | [diff] [blame] | 2861 | R.drawable.notification_icon_legacy_bg); |
Dan Sandler | 26e81cf | 2014-05-06 10:01:27 -0400 | [diff] [blame] | 2862 | |
| 2863 | contentView.setDrawableParameters( |
| 2864 | R.id.icon, |
| 2865 | true, |
| 2866 | -1, |
Jorim Jaggi | 7441931 | 2014-06-10 20:57:21 +0200 | [diff] [blame] | 2867 | resolveColor(), |
Dan Sandler | 26e81cf | 2014-05-06 10:01:27 -0400 | [diff] [blame] | 2868 | PorterDuff.Mode.SRC_ATOP, |
| 2869 | -1); |
Jorim Jaggi | 445d3c0 | 2014-08-19 22:33:42 +0200 | [diff] [blame] | 2870 | |
| 2871 | int padding = mContext.getResources().getDimensionPixelSize( |
| 2872 | R.dimen.notification_large_icon_circle_padding); |
| 2873 | contentView.setViewPadding(R.id.icon, padding, padding, padding, padding); |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 2874 | } |
| 2875 | |
Dan Sandler | 190d58d | 2014-05-15 09:33:39 -0400 | [diff] [blame] | 2876 | private void removeLargeIconBackground(RemoteViews contentView) { |
| 2877 | contentView.setInt(R.id.icon, "setBackgroundResource", 0); |
| 2878 | } |
| 2879 | |
Dan Sandler | 26e81cf | 2014-05-06 10:01:27 -0400 | [diff] [blame] | 2880 | /** |
| 2881 | * Recolor small icons when used in the R.id.right_icon slot. |
| 2882 | */ |
Dan Sandler | 190d58d | 2014-05-15 09:33:39 -0400 | [diff] [blame] | 2883 | private void processSmallRightIcon(int smallIconDrawableId, |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 2884 | RemoteViews contentView) { |
Dan Sandler | 26e81cf | 2014-05-06 10:01:27 -0400 | [diff] [blame] | 2885 | if (!isLegacy() || mColorUtil.isGrayscale(mContext, smallIconDrawableId)) { |
Dan Sandler | 190d58d | 2014-05-15 09:33:39 -0400 | [diff] [blame] | 2886 | contentView.setDrawableParameters(R.id.right_icon, false, -1, |
| 2887 | 0xFFFFFFFF, |
| 2888 | PorterDuff.Mode.SRC_ATOP, -1); |
| 2889 | |
| 2890 | contentView.setInt(R.id.right_icon, |
| 2891 | "setBackgroundResource", |
| 2892 | R.drawable.notification_icon_legacy_bg); |
| 2893 | |
| 2894 | contentView.setDrawableParameters( |
| 2895 | R.id.right_icon, |
| 2896 | true, |
| 2897 | -1, |
Jorim Jaggi | 7441931 | 2014-06-10 20:57:21 +0200 | [diff] [blame] | 2898 | resolveColor(), |
Dan Sandler | 190d58d | 2014-05-15 09:33:39 -0400 | [diff] [blame] | 2899 | PorterDuff.Mode.SRC_ATOP, |
| 2900 | -1); |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 2901 | } |
| 2902 | } |
| 2903 | |
Jorim Jaggi | 7441931 | 2014-06-10 20:57:21 +0200 | [diff] [blame] | 2904 | private int sanitizeColor() { |
| 2905 | if (mColor != COLOR_DEFAULT) { |
| 2906 | mColor |= 0xFF000000; // no alpha for custom colors |
| 2907 | } |
| 2908 | return mColor; |
| 2909 | } |
| 2910 | |
Dan Sandler | 26e81cf | 2014-05-06 10:01:27 -0400 | [diff] [blame] | 2911 | private int resolveColor() { |
| 2912 | if (mColor == COLOR_DEFAULT) { |
Jorim Jaggi | 7441931 | 2014-06-10 20:57:21 +0200 | [diff] [blame] | 2913 | return mContext.getResources().getColor(R.color.notification_icon_bg_color); |
Dan Sandler | 26e81cf | 2014-05-06 10:01:27 -0400 | [diff] [blame] | 2914 | } |
| 2915 | return mColor; |
| 2916 | } |
| 2917 | |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 2918 | /** |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 2919 | * Apply the unstyled operations and return a new {@link Notification} object. |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 2920 | * @hide |
Joe Onorato | cb109a0 | 2011-01-18 17:57:41 -0800 | [diff] [blame] | 2921 | */ |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 2922 | public Notification buildUnstyled() { |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2923 | Notification n = new Notification(); |
| 2924 | n.when = mWhen; |
| 2925 | n.icon = mSmallIcon; |
| 2926 | n.iconLevel = mSmallIconLevel; |
Joe Onorato | 8595a3d | 2010-11-19 18:12:07 -0800 | [diff] [blame] | 2927 | n.number = mNumber; |
Dan Sandler | 26e81cf | 2014-05-06 10:01:27 -0400 | [diff] [blame] | 2928 | |
Jorim Jaggi | 7441931 | 2014-06-10 20:57:21 +0200 | [diff] [blame] | 2929 | n.color = sanitizeColor(); |
Dan Sandler | 26e81cf | 2014-05-06 10:01:27 -0400 | [diff] [blame] | 2930 | |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 2931 | setBuilderContentView(n, makeContentView()); |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2932 | n.contentIntent = mContentIntent; |
| 2933 | n.deleteIntent = mDeleteIntent; |
| 2934 | n.fullScreenIntent = mFullScreenIntent; |
| 2935 | n.tickerText = mTickerText; |
| 2936 | n.tickerView = makeTickerView(); |
| 2937 | n.largeIcon = mLargeIcon; |
| 2938 | n.sound = mSound; |
| 2939 | n.audioStreamType = mAudioStreamType; |
John Spurlock | c0650f02 | 2014-07-19 13:22:39 -0400 | [diff] [blame] | 2940 | n.audioAttributes = mAudioAttributes; |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2941 | n.vibrate = mVibrate; |
| 2942 | n.ledARGB = mLedArgb; |
| 2943 | n.ledOnMS = mLedOnMs; |
| 2944 | n.ledOffMS = mLedOffMs; |
| 2945 | n.defaults = mDefaults; |
| 2946 | n.flags = mFlags; |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 2947 | setBuilderBigContentView(n, makeBigContentView()); |
| 2948 | setBuilderHeadsUpContentView(n, makeHeadsUpContentView()); |
Daniel Sandler | 26c1343 | 2013-04-04 11:01:04 -0400 | [diff] [blame] | 2949 | if (mLedOnMs != 0 || mLedOffMs != 0) { |
Joe Onorato | 8d0b655 | 2010-11-22 16:09:29 -0800 | [diff] [blame] | 2950 | n.flags |= FLAG_SHOW_LIGHTS; |
| 2951 | } |
| 2952 | if ((mDefaults & DEFAULT_LIGHTS) != 0) { |
| 2953 | n.flags |= FLAG_SHOW_LIGHTS; |
| 2954 | } |
John Spurlock | fd7f1e0 | 2014-03-18 16:41:57 -0400 | [diff] [blame] | 2955 | n.category = mCategory; |
Griff Hazen | 5cadc3b | 2014-05-20 09:55:39 -0700 | [diff] [blame] | 2956 | n.mGroupKey = mGroupKey; |
| 2957 | n.mSortKey = mSortKey; |
Daniel Sandler | 2561b0b | 2012-02-13 21:04:12 -0500 | [diff] [blame] | 2958 | n.priority = mPriority; |
Daniel Sandler | a0a938c | 2012-03-15 08:42:37 -0400 | [diff] [blame] | 2959 | if (mActions.size() > 0) { |
| 2960 | n.actions = new Action[mActions.size()]; |
| 2961 | mActions.toArray(n.actions); |
| 2962 | } |
Dan Sandler | 0bf2ed8 | 2013-12-21 23:33:41 -0600 | [diff] [blame] | 2963 | n.visibility = mVisibility; |
| 2964 | |
| 2965 | if (mPublicVersion != null) { |
| 2966 | n.publicVersion = new Notification(); |
| 2967 | mPublicVersion.cloneInto(n.publicVersion, true); |
| 2968 | } |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 2969 | // Note: If you're adding new fields, also update restoreFromNotitification(). |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 2970 | return n; |
| 2971 | } |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 2972 | |
| 2973 | /** |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 2974 | * Capture, in the provided bundle, semantic information used in the construction of |
| 2975 | * this Notification object. |
| 2976 | * @hide |
| 2977 | */ |
Griff Hazen | 720042b | 2014-02-24 15:46:56 -0800 | [diff] [blame] | 2978 | public void populateExtras(Bundle extras) { |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 2979 | // Store original information used in the construction of this object |
Christoph Studer | 7ac80e6 | 2014-08-04 16:01:57 +0200 | [diff] [blame] | 2980 | extras.putInt(EXTRA_ORIGINATING_USERID, mOriginatingUserId); |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 2981 | extras.putString(EXTRA_REBUILD_CONTEXT_PACKAGE, mContext.getPackageName()); |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 2982 | extras.putCharSequence(EXTRA_TITLE, mContentTitle); |
| 2983 | extras.putCharSequence(EXTRA_TEXT, mContentText); |
| 2984 | extras.putCharSequence(EXTRA_SUB_TEXT, mSubText); |
| 2985 | extras.putCharSequence(EXTRA_INFO_TEXT, mContentInfo); |
| 2986 | extras.putInt(EXTRA_SMALL_ICON, mSmallIcon); |
| 2987 | extras.putInt(EXTRA_PROGRESS, mProgress); |
| 2988 | extras.putInt(EXTRA_PROGRESS_MAX, mProgressMax); |
| 2989 | extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, mProgressIndeterminate); |
| 2990 | extras.putBoolean(EXTRA_SHOW_CHRONOMETER, mUseChronometer); |
| 2991 | extras.putBoolean(EXTRA_SHOW_WHEN, mShowWhen); |
John Spurlock | ac08a47 | 2013-06-10 11:37:08 -0400 | [diff] [blame] | 2992 | if (mLargeIcon != null) { |
| 2993 | extras.putParcelable(EXTRA_LARGE_ICON, mLargeIcon); |
| 2994 | } |
Chris Wren | dde7530 | 2014-03-26 17:24:15 -0400 | [diff] [blame] | 2995 | if (!mPeople.isEmpty()) { |
| 2996 | extras.putStringArray(EXTRA_PEOPLE, mPeople.toArray(new String[mPeople.size()])); |
| 2997 | } |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 2998 | // NOTE: If you're adding new extras also update restoreFromNotification(). |
| 2999 | } |
| 3000 | |
| 3001 | |
| 3002 | /** |
| 3003 | * @hide |
| 3004 | */ |
| 3005 | public static void stripForDelivery(Notification n) { |
| 3006 | if (!STRIP_AND_REBUILD) { |
| 3007 | return; |
| 3008 | } |
| 3009 | |
| 3010 | String templateClass = n.extras.getString(EXTRA_TEMPLATE); |
| 3011 | // Only strip views for known Styles because we won't know how to |
| 3012 | // re-create them otherwise. |
| 3013 | boolean stripViews = TextUtils.isEmpty(templateClass) || |
| 3014 | getNotificationStyleClass(templateClass) != null; |
| 3015 | |
| 3016 | boolean isStripped = false; |
| 3017 | |
| 3018 | if (n.largeIcon != null && n.extras.containsKey(EXTRA_LARGE_ICON)) { |
| 3019 | // TODO: Would like to check for equality here, but if the notification |
| 3020 | // has been cloned, we can't. |
| 3021 | n.largeIcon = null; |
| 3022 | n.extras.putBoolean(EXTRA_REBUILD_LARGE_ICON, true); |
| 3023 | isStripped = true; |
| 3024 | } |
| 3025 | // Get rid of unmodified BuilderRemoteViews. |
| 3026 | |
| 3027 | if (stripViews && |
| 3028 | n.contentView instanceof BuilderRemoteViews && |
| 3029 | n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) == |
| 3030 | n.contentView.getSequenceNumber()) { |
| 3031 | n.contentView = null; |
| 3032 | n.extras.putBoolean(EXTRA_REBUILD_CONTENT_VIEW, true); |
| 3033 | n.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT); |
| 3034 | isStripped = true; |
| 3035 | } |
| 3036 | if (stripViews && |
| 3037 | n.bigContentView instanceof BuilderRemoteViews && |
| 3038 | n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) == |
| 3039 | n.bigContentView.getSequenceNumber()) { |
| 3040 | n.bigContentView = null; |
| 3041 | n.extras.putBoolean(EXTRA_REBUILD_BIG_CONTENT_VIEW, true); |
| 3042 | n.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT); |
| 3043 | isStripped = true; |
| 3044 | } |
| 3045 | if (stripViews && |
| 3046 | n.headsUpContentView instanceof BuilderRemoteViews && |
| 3047 | n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) == |
| 3048 | n.headsUpContentView.getSequenceNumber()) { |
| 3049 | n.headsUpContentView = null; |
| 3050 | n.extras.putBoolean(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW, true); |
| 3051 | n.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT); |
| 3052 | isStripped = true; |
| 3053 | } |
| 3054 | |
| 3055 | if (isStripped) { |
| 3056 | n.extras.putBoolean(EXTRA_NEEDS_REBUILD, true); |
| 3057 | } |
| 3058 | } |
| 3059 | |
| 3060 | /** |
| 3061 | * @hide |
| 3062 | */ |
| 3063 | public static Notification rebuild(Context context, Notification n) { |
| 3064 | Bundle extras = n.extras; |
| 3065 | if (!extras.getBoolean(EXTRA_NEEDS_REBUILD)) return n; |
| 3066 | extras.remove(EXTRA_NEEDS_REBUILD); |
| 3067 | |
| 3068 | // Re-create notification context so we can access app resources. |
| 3069 | String packageName = extras.getString(EXTRA_REBUILD_CONTEXT_PACKAGE); |
| 3070 | Context builderContext; |
| 3071 | try { |
| 3072 | builderContext = context.createPackageContext(packageName, |
| 3073 | Context.CONTEXT_RESTRICTED); |
| 3074 | } catch (NameNotFoundException e) { |
| 3075 | Log.e(TAG, "Package name " + packageName + " not found"); |
| 3076 | builderContext = context; // try with our context |
| 3077 | } |
| 3078 | |
| 3079 | Builder b = new Builder(builderContext, n); |
| 3080 | return b.rebuild(); |
| 3081 | } |
| 3082 | |
| 3083 | /** |
| 3084 | * Rebuilds the notification passed in to the rebuild-constructor |
| 3085 | * {@link #Builder(Context, Notification)}. |
| 3086 | * |
| 3087 | * <p> |
| 3088 | * Throws IllegalStateException when invoked on a Builder that isn't in rebuild mode. |
| 3089 | * |
| 3090 | * @hide |
| 3091 | */ |
| 3092 | private Notification rebuild() { |
| 3093 | if (mRebuildNotification == null) { |
| 3094 | throw new IllegalStateException("rebuild() only valid when in 'rebuild' mode."); |
| 3095 | } |
| 3096 | |
| 3097 | Bundle extras = mRebuildNotification.extras; |
| 3098 | |
| 3099 | if (extras.getBoolean(EXTRA_REBUILD_LARGE_ICON)) { |
| 3100 | mRebuildNotification.largeIcon = extras.getParcelable(EXTRA_LARGE_ICON); |
| 3101 | } |
| 3102 | extras.remove(EXTRA_REBUILD_LARGE_ICON); |
| 3103 | |
| 3104 | if (extras.getBoolean(EXTRA_REBUILD_CONTENT_VIEW)) { |
| 3105 | setBuilderContentView(mRebuildNotification, makeContentView()); |
| 3106 | if (mStyle != null) { |
| 3107 | mStyle.populateContentView(mRebuildNotification); |
| 3108 | } |
| 3109 | } |
| 3110 | extras.remove(EXTRA_REBUILD_CONTENT_VIEW); |
| 3111 | |
| 3112 | if (extras.getBoolean(EXTRA_REBUILD_BIG_CONTENT_VIEW)) { |
| 3113 | setBuilderBigContentView(mRebuildNotification, makeBigContentView()); |
| 3114 | if (mStyle != null) { |
| 3115 | mStyle.populateBigContentView(mRebuildNotification); |
| 3116 | } |
| 3117 | } |
| 3118 | extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW); |
| 3119 | |
| 3120 | if (extras.getBoolean(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW)) { |
| 3121 | setBuilderHeadsUpContentView(mRebuildNotification, makeHeadsUpContentView()); |
| 3122 | if (mStyle != null) { |
| 3123 | mStyle.populateHeadsUpContentView(mRebuildNotification); |
| 3124 | } |
| 3125 | } |
| 3126 | extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW); |
| 3127 | |
| 3128 | return mRebuildNotification; |
| 3129 | } |
| 3130 | |
| 3131 | private static Class<? extends Style> getNotificationStyleClass(String templateClass) { |
| 3132 | Class<? extends Style>[] classes = new Class[]{ |
| 3133 | BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class}; |
| 3134 | for (Class<? extends Style> innerClass : classes) { |
| 3135 | if (templateClass.equals(innerClass.getName())) { |
| 3136 | return innerClass; |
| 3137 | } |
| 3138 | } |
| 3139 | return null; |
| 3140 | } |
| 3141 | |
| 3142 | private void setBuilderContentView(Notification n, RemoteViews contentView) { |
| 3143 | n.contentView = contentView; |
| 3144 | if (contentView instanceof BuilderRemoteViews) { |
| 3145 | mRebuildBundle.putInt(Builder.EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, |
| 3146 | contentView.getSequenceNumber()); |
| 3147 | } |
| 3148 | } |
| 3149 | |
| 3150 | private void setBuilderBigContentView(Notification n, RemoteViews bigContentView) { |
| 3151 | n.bigContentView = bigContentView; |
| 3152 | if (bigContentView instanceof BuilderRemoteViews) { |
| 3153 | mRebuildBundle.putInt(Builder.EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, |
| 3154 | bigContentView.getSequenceNumber()); |
| 3155 | } |
| 3156 | } |
| 3157 | |
| 3158 | private void setBuilderHeadsUpContentView(Notification n, |
| 3159 | RemoteViews headsUpContentView) { |
| 3160 | n.headsUpContentView = headsUpContentView; |
| 3161 | if (headsUpContentView instanceof BuilderRemoteViews) { |
| 3162 | mRebuildBundle.putInt(Builder.EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, |
| 3163 | headsUpContentView.getSequenceNumber()); |
| 3164 | } |
| 3165 | } |
| 3166 | |
| 3167 | private void restoreFromNotification(Notification n) { |
| 3168 | |
| 3169 | // Notification fields. |
| 3170 | mWhen = n.when; |
| 3171 | mSmallIcon = n.icon; |
| 3172 | mSmallIconLevel = n.iconLevel; |
| 3173 | mNumber = n.number; |
| 3174 | |
| 3175 | mColor = n.color; |
| 3176 | |
| 3177 | mContentView = n.contentView; |
| 3178 | mDeleteIntent = n.deleteIntent; |
| 3179 | mFullScreenIntent = n.fullScreenIntent; |
| 3180 | mTickerText = n.tickerText; |
| 3181 | mTickerView = n.tickerView; |
| 3182 | mLargeIcon = n.largeIcon; |
| 3183 | mSound = n.sound; |
| 3184 | mAudioStreamType = n.audioStreamType; |
| 3185 | mAudioAttributes = n.audioAttributes; |
| 3186 | |
| 3187 | mVibrate = n.vibrate; |
| 3188 | mLedArgb = n.ledARGB; |
| 3189 | mLedOnMs = n.ledOnMS; |
| 3190 | mLedOffMs = n.ledOffMS; |
| 3191 | mDefaults = n.defaults; |
| 3192 | mFlags = n.flags; |
| 3193 | |
| 3194 | mCategory = n.category; |
| 3195 | mGroupKey = n.mGroupKey; |
| 3196 | mSortKey = n.mSortKey; |
| 3197 | mPriority = n.priority; |
| 3198 | mActions.clear(); |
| 3199 | if (n.actions != null) { |
| 3200 | Collections.addAll(mActions, n.actions); |
| 3201 | } |
| 3202 | mVisibility = n.visibility; |
| 3203 | |
| 3204 | mPublicVersion = n.publicVersion; |
| 3205 | |
| 3206 | // Extras. |
| 3207 | Bundle extras = n.extras; |
Christoph Studer | 7ac80e6 | 2014-08-04 16:01:57 +0200 | [diff] [blame] | 3208 | mOriginatingUserId = extras.getInt(EXTRA_ORIGINATING_USERID); |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 3209 | mContentTitle = extras.getCharSequence(EXTRA_TITLE); |
| 3210 | mContentText = extras.getCharSequence(EXTRA_TEXT); |
| 3211 | mSubText = extras.getCharSequence(EXTRA_SUB_TEXT); |
| 3212 | mContentInfo = extras.getCharSequence(EXTRA_INFO_TEXT); |
| 3213 | mSmallIcon = extras.getInt(EXTRA_SMALL_ICON); |
| 3214 | mProgress = extras.getInt(EXTRA_PROGRESS); |
| 3215 | mProgressMax = extras.getInt(EXTRA_PROGRESS_MAX); |
| 3216 | mProgressIndeterminate = extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE); |
| 3217 | mUseChronometer = extras.getBoolean(EXTRA_SHOW_CHRONOMETER); |
| 3218 | mShowWhen = extras.getBoolean(EXTRA_SHOW_WHEN); |
| 3219 | if (extras.containsKey(EXTRA_LARGE_ICON)) { |
| 3220 | mLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON); |
| 3221 | } |
| 3222 | if (extras.containsKey(EXTRA_PEOPLE)) { |
| 3223 | mPeople.clear(); |
| 3224 | Collections.addAll(mPeople, extras.getStringArray(EXTRA_PEOPLE)); |
| 3225 | } |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 3226 | } |
| 3227 | |
| 3228 | /** |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 3229 | * @deprecated Use {@link #build()} instead. |
| 3230 | */ |
| 3231 | @Deprecated |
| 3232 | public Notification getNotification() { |
| 3233 | return build(); |
| 3234 | } |
| 3235 | |
| 3236 | /** |
| 3237 | * Combine all of the options that have been set and return a new {@link Notification} |
| 3238 | * object. |
| 3239 | */ |
| 3240 | public Notification build() { |
Christoph Studer | 7ac80e6 | 2014-08-04 16:01:57 +0200 | [diff] [blame] | 3241 | mOriginatingUserId = mContext.getUserId(); |
Christoph Studer | 943aa67 | 2014-08-03 20:31:16 +0200 | [diff] [blame] | 3242 | |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 3243 | Notification n = buildUnstyled(); |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 3244 | |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 3245 | if (mStyle != null) { |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 3246 | n = mStyle.buildStyled(n); |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 3247 | } |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 3248 | |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 3249 | if (mExtras != null) { |
| 3250 | n.extras.putAll(mExtras); |
| 3251 | } |
| 3252 | |
| 3253 | if (mRebuildBundle.size() > 0) { |
| 3254 | n.extras.putAll(mRebuildBundle); |
| 3255 | mRebuildBundle.clear(); |
| 3256 | } |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 3257 | |
Griff Hazen | 720042b | 2014-02-24 15:46:56 -0800 | [diff] [blame] | 3258 | populateExtras(n.extras); |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 3259 | if (mStyle != null) { |
| 3260 | mStyle.addExtras(n.extras); |
| 3261 | } |
| 3262 | |
| 3263 | return n; |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 3264 | } |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 3265 | |
| 3266 | /** |
| 3267 | * Apply this Builder to an existing {@link Notification} object. |
| 3268 | * |
| 3269 | * @hide |
| 3270 | */ |
| 3271 | public Notification buildInto(Notification n) { |
Daniel Sandler | 1a497d3 | 2013-04-18 14:52:45 -0400 | [diff] [blame] | 3272 | build().cloneInto(n, true); |
Daniel Sandler | be6e7e0 | 2013-02-01 17:49:11 -0500 | [diff] [blame] | 3273 | return n; |
| 3274 | } |
Jorim Jaggi | 39fa59f | 2014-02-25 15:38:45 +0100 | [diff] [blame] | 3275 | |
Jorim Jaggi | 39fa59f | 2014-02-25 15:38:45 +0100 | [diff] [blame] | 3276 | private int getBaseLayoutResource() { |
Alan Viverette | 3cb07a46 | 2014-06-06 14:19:53 -0700 | [diff] [blame] | 3277 | return R.layout.notification_template_material_base; |
Jorim Jaggi | 39fa59f | 2014-02-25 15:38:45 +0100 | [diff] [blame] | 3278 | } |
| 3279 | |
| 3280 | private int getBigBaseLayoutResource() { |
Alan Viverette | 3cb07a46 | 2014-06-06 14:19:53 -0700 | [diff] [blame] | 3281 | return R.layout.notification_template_material_big_base; |
Jorim Jaggi | 39fa59f | 2014-02-25 15:38:45 +0100 | [diff] [blame] | 3282 | } |
| 3283 | |
| 3284 | private int getBigPictureLayoutResource() { |
Alan Viverette | 3cb07a46 | 2014-06-06 14:19:53 -0700 | [diff] [blame] | 3285 | return R.layout.notification_template_material_big_picture; |
Jorim Jaggi | 39fa59f | 2014-02-25 15:38:45 +0100 | [diff] [blame] | 3286 | } |
| 3287 | |
| 3288 | private int getBigTextLayoutResource() { |
Jorim Jaggi | 445d3c0 | 2014-08-19 22:33:42 +0200 | [diff] [blame] | 3289 | return R.layout.notification_template_material_big_text; |
Jorim Jaggi | 39fa59f | 2014-02-25 15:38:45 +0100 | [diff] [blame] | 3290 | } |
| 3291 | |
| 3292 | private int getInboxLayoutResource() { |
Alan Viverette | 3cb07a46 | 2014-06-06 14:19:53 -0700 | [diff] [blame] | 3293 | return R.layout.notification_template_material_inbox; |
Jorim Jaggi | 39fa59f | 2014-02-25 15:38:45 +0100 | [diff] [blame] | 3294 | } |
| 3295 | |
| 3296 | private int getActionLayoutResource() { |
Alan Viverette | 3cb07a46 | 2014-06-06 14:19:53 -0700 | [diff] [blame] | 3297 | return R.layout.notification_material_action; |
Jorim Jaggi | 39fa59f | 2014-02-25 15:38:45 +0100 | [diff] [blame] | 3298 | } |
| 3299 | |
| 3300 | private int getActionTombstoneLayoutResource() { |
Alan Viverette | 3cb07a46 | 2014-06-06 14:19:53 -0700 | [diff] [blame] | 3301 | return R.layout.notification_material_action_tombstone; |
Jorim Jaggi | 39fa59f | 2014-02-25 15:38:45 +0100 | [diff] [blame] | 3302 | } |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 3303 | } |
| 3304 | |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 3305 | /** |
| 3306 | * An object that can apply a rich notification style to a {@link Notification.Builder} |
| 3307 | * object. |
| 3308 | */ |
Griff Hazen | dfcb080 | 2014-02-11 12:00:00 -0800 | [diff] [blame] | 3309 | public static abstract class Style { |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 3310 | private CharSequence mBigContentTitle; |
| 3311 | private CharSequence mSummaryText = null; |
Daniel Sandler | 619738c | 2012-06-07 16:33:08 -0400 | [diff] [blame] | 3312 | private boolean mSummaryTextSet = false; |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 3313 | |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 3314 | protected Builder mBuilder; |
| 3315 | |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 3316 | /** |
| 3317 | * Overrides ContentTitle in the big form of the template. |
| 3318 | * This defaults to the value passed to setContentTitle(). |
| 3319 | */ |
| 3320 | protected void internalSetBigContentTitle(CharSequence title) { |
| 3321 | mBigContentTitle = title; |
| 3322 | } |
| 3323 | |
| 3324 | /** |
| 3325 | * Set the first line of text after the detail section in the big form of the template. |
| 3326 | */ |
| 3327 | protected void internalSetSummaryText(CharSequence cs) { |
| 3328 | mSummaryText = cs; |
Daniel Sandler | 619738c | 2012-06-07 16:33:08 -0400 | [diff] [blame] | 3329 | mSummaryTextSet = true; |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 3330 | } |
| 3331 | |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 3332 | public void setBuilder(Builder builder) { |
| 3333 | if (mBuilder != builder) { |
| 3334 | mBuilder = builder; |
Daniel Sandler | c08dea2 | 2012-06-28 08:35:24 -0700 | [diff] [blame] | 3335 | if (mBuilder != null) { |
| 3336 | mBuilder.setStyle(this); |
| 3337 | } |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 3338 | } |
| 3339 | } |
| 3340 | |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 3341 | protected void checkBuilder() { |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 3342 | if (mBuilder == null) { |
| 3343 | throw new IllegalArgumentException("Style requires a valid Builder object"); |
| 3344 | } |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 3345 | } |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 3346 | |
| 3347 | protected RemoteViews getStandardView(int layoutId) { |
| 3348 | checkBuilder(); |
| 3349 | |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 3350 | // Nasty. |
| 3351 | CharSequence oldBuilderContentTitle = mBuilder.mContentTitle; |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 3352 | if (mBigContentTitle != null) { |
| 3353 | mBuilder.setContentTitle(mBigContentTitle); |
| 3354 | } |
| 3355 | |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 3356 | RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId); |
| 3357 | |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 3358 | mBuilder.mContentTitle = oldBuilderContentTitle; |
| 3359 | |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 3360 | if (mBigContentTitle != null && mBigContentTitle.equals("")) { |
| 3361 | contentView.setViewVisibility(R.id.line1, View.GONE); |
Chris Wren | 67dc9a0 | 2012-05-16 01:03:20 -0400 | [diff] [blame] | 3362 | } else { |
| 3363 | contentView.setViewVisibility(R.id.line1, View.VISIBLE); |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 3364 | } |
| 3365 | |
Daniel Sandler | 619738c | 2012-06-07 16:33:08 -0400 | [diff] [blame] | 3366 | // The last line defaults to the subtext, but can be replaced by mSummaryText |
| 3367 | final CharSequence overflowText = |
| 3368 | mSummaryTextSet ? mSummaryText |
| 3369 | : mBuilder.mSubText; |
| 3370 | if (overflowText != null) { |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 3371 | contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(overflowText)); |
Daniel Sandler | 619738c | 2012-06-07 16:33:08 -0400 | [diff] [blame] | 3372 | contentView.setViewVisibility(R.id.overflow_divider, View.VISIBLE); |
Daniel Sandler | 9f7936a | 2012-05-21 16:14:28 -0400 | [diff] [blame] | 3373 | contentView.setViewVisibility(R.id.line3, View.VISIBLE); |
Daniel Sandler | 916ad91 | 2012-06-13 12:17:07 -0400 | [diff] [blame] | 3374 | } else { |
Kenny Guy | 98193ea | 2014-07-24 19:54:37 +0100 | [diff] [blame^] | 3375 | // Clear text in case we use the line to show the profile badge. |
| 3376 | contentView.setTextViewText(R.id.text, ""); |
Daniel Sandler | 916ad91 | 2012-06-13 12:17:07 -0400 | [diff] [blame] | 3377 | contentView.setViewVisibility(R.id.overflow_divider, View.GONE); |
| 3378 | contentView.setViewVisibility(R.id.line3, View.GONE); |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 3379 | } |
| 3380 | |
| 3381 | return contentView; |
| 3382 | } |
| 3383 | |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 3384 | /** |
Jorim Jaggi | 445d3c0 | 2014-08-19 22:33:42 +0200 | [diff] [blame] | 3385 | * Changes the padding of the first line such that the big and small content view have the |
| 3386 | * same top padding. |
| 3387 | * |
| 3388 | * @hide |
| 3389 | */ |
| 3390 | protected void applyTopPadding(RemoteViews contentView) { |
| 3391 | int topPadding = Builder.calculateTopPadding(mBuilder.mContext, |
| 3392 | mBuilder.hasThreeLines(), |
| 3393 | mBuilder.mContext.getResources().getConfiguration().fontScale); |
| 3394 | contentView.setViewPadding(R.id.line1, 0, topPadding, 0, 0); |
| 3395 | } |
| 3396 | |
| 3397 | /** |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 3398 | * @hide |
| 3399 | */ |
| 3400 | public void addExtras(Bundle extras) { |
| 3401 | if (mSummaryTextSet) { |
| 3402 | extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText); |
| 3403 | } |
| 3404 | if (mBigContentTitle != null) { |
| 3405 | extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle); |
| 3406 | } |
Chris Wren | 91ad563 | 2013-06-05 15:05:57 -0400 | [diff] [blame] | 3407 | extras.putString(EXTRA_TEMPLATE, this.getClass().getName()); |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 3408 | } |
| 3409 | |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 3410 | /** |
| 3411 | * @hide |
| 3412 | */ |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 3413 | protected void restoreFromExtras(Bundle extras) { |
| 3414 | if (extras.containsKey(EXTRA_SUMMARY_TEXT)) { |
| 3415 | mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT); |
| 3416 | mSummaryTextSet = true; |
| 3417 | } |
| 3418 | if (extras.containsKey(EXTRA_TITLE_BIG)) { |
| 3419 | mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG); |
| 3420 | } |
| 3421 | } |
| 3422 | |
| 3423 | |
| 3424 | /** |
| 3425 | * @hide |
| 3426 | */ |
| 3427 | public Notification buildStyled(Notification wip) { |
| 3428 | populateTickerView(wip); |
| 3429 | populateContentView(wip); |
| 3430 | populateBigContentView(wip); |
| 3431 | populateHeadsUpContentView(wip); |
| 3432 | return wip; |
| 3433 | } |
| 3434 | |
| 3435 | // The following methods are split out so we can re-create notification partially. |
| 3436 | /** |
| 3437 | * @hide |
| 3438 | */ |
| 3439 | protected void populateTickerView(Notification wip) {} |
| 3440 | /** |
| 3441 | * @hide |
| 3442 | */ |
| 3443 | protected void populateContentView(Notification wip) {} |
| 3444 | |
| 3445 | /** |
| 3446 | * @hide |
| 3447 | */ |
| 3448 | protected void populateBigContentView(Notification wip) {} |
| 3449 | |
| 3450 | /** |
| 3451 | * @hide |
| 3452 | */ |
| 3453 | protected void populateHeadsUpContentView(Notification wip) {} |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 3454 | |
| 3455 | /** |
| 3456 | * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is |
| 3457 | * attached to. |
| 3458 | * |
| 3459 | * @return the fully constructed Notification. |
| 3460 | */ |
| 3461 | public Notification build() { |
| 3462 | checkBuilder(); |
| 3463 | return mBuilder.build(); |
| 3464 | } |
Joe Onorato | 46439ce | 2010-11-19 13:56:21 -0800 | [diff] [blame] | 3465 | } |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 3466 | |
| 3467 | /** |
Daniel Sandler | 4dfbe83 | 2012-04-11 14:51:46 -0400 | [diff] [blame] | 3468 | * 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] | 3469 | * |
Robert Ly | 91c5ce3 | 2014-06-08 15:37:00 -0700 | [diff] [blame] | 3470 | * Here's how you'd set the <code>BigPictureStyle</code> on a notification: |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 3471 | * <pre class="prettyprint"> |
Robert Ly | 91c5ce3 | 2014-06-08 15:37:00 -0700 | [diff] [blame] | 3472 | * Notification notif = new Notification.Builder(mContext) |
| 3473 | * .setContentTitle("New photo from " + sender.toString()) |
| 3474 | * .setContentText(subject) |
| 3475 | * .setSmallIcon(R.drawable.new_post) |
| 3476 | * .setLargeIcon(aBitmap) |
| 3477 | * .setStyle(new Notification.BigPictureStyle() |
| 3478 | * .bigPicture(aBigBitmap)) |
| 3479 | * .build(); |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 3480 | * </pre> |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 3481 | * |
Daniel Sandler | 4dfbe83 | 2012-04-11 14:51:46 -0400 | [diff] [blame] | 3482 | * @see Notification#bigContentView |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 3483 | */ |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 3484 | public static class BigPictureStyle extends Style { |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 3485 | private Bitmap mPicture; |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 3486 | private Bitmap mBigLargeIcon; |
| 3487 | private boolean mBigLargeIconSet = false; |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 3488 | |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 3489 | public BigPictureStyle() { |
| 3490 | } |
| 3491 | |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 3492 | public BigPictureStyle(Builder builder) { |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 3493 | setBuilder(builder); |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 3494 | } |
| 3495 | |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 3496 | /** |
| 3497 | * Overrides ContentTitle in the big form of the template. |
| 3498 | * This defaults to the value passed to setContentTitle(). |
| 3499 | */ |
| 3500 | public BigPictureStyle setBigContentTitle(CharSequence title) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 3501 | internalSetBigContentTitle(safeCharSequence(title)); |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 3502 | return this; |
| 3503 | } |
| 3504 | |
| 3505 | /** |
| 3506 | * Set the first line of text after the detail section in the big form of the template. |
| 3507 | */ |
| 3508 | public BigPictureStyle setSummaryText(CharSequence cs) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 3509 | internalSetSummaryText(safeCharSequence(cs)); |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 3510 | return this; |
| 3511 | } |
| 3512 | |
Chris Wren | 0bd664d | 2012-08-01 13:56:56 -0400 | [diff] [blame] | 3513 | /** |
| 3514 | * Provide the bitmap to be used as the payload for the BigPicture notification. |
| 3515 | */ |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 3516 | public BigPictureStyle bigPicture(Bitmap b) { |
| 3517 | mPicture = b; |
| 3518 | return this; |
| 3519 | } |
| 3520 | |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 3521 | /** |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 3522 | * Override the large icon when the big notification is shown. |
| 3523 | */ |
| 3524 | public BigPictureStyle bigLargeIcon(Bitmap b) { |
| 3525 | mBigLargeIconSet = true; |
| 3526 | mBigLargeIcon = b; |
| 3527 | return this; |
| 3528 | } |
| 3529 | |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 3530 | private RemoteViews makeBigContentView() { |
Jorim Jaggi | 39fa59f | 2014-02-25 15:38:45 +0100 | [diff] [blame] | 3531 | RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource()); |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 3532 | |
| 3533 | contentView.setImageViewBitmap(R.id.big_picture, mPicture); |
| 3534 | |
Jorim Jaggi | 445d3c0 | 2014-08-19 22:33:42 +0200 | [diff] [blame] | 3535 | applyTopPadding(contentView); |
| 3536 | |
Kenny Guy | 98193ea | 2014-07-24 19:54:37 +0100 | [diff] [blame^] | 3537 | boolean twoTextLines = mBuilder.mSubText != null && mBuilder.mContentText != null; |
| 3538 | mBuilder.addProfileBadge(contentView, |
| 3539 | twoTextLines ? R.id.profile_badge_line2 : R.id.profile_badge_line3); |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 3540 | return contentView; |
| 3541 | } |
| 3542 | |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 3543 | /** |
| 3544 | * @hide |
| 3545 | */ |
| 3546 | public void addExtras(Bundle extras) { |
| 3547 | super.addExtras(extras); |
| 3548 | |
| 3549 | if (mBigLargeIconSet) { |
| 3550 | extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon); |
| 3551 | } |
| 3552 | extras.putParcelable(EXTRA_PICTURE, mPicture); |
| 3553 | } |
| 3554 | |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 3555 | /** |
| 3556 | * @hide |
| 3557 | */ |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 3558 | @Override |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 3559 | protected void restoreFromExtras(Bundle extras) { |
| 3560 | super.restoreFromExtras(extras); |
| 3561 | |
| 3562 | if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) { |
| 3563 | mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG); |
Chris Wren | 3745a3d | 2012-05-22 15:11:52 -0400 | [diff] [blame] | 3564 | } |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 3565 | mPicture = extras.getParcelable(EXTRA_PICTURE); |
| 3566 | } |
| 3567 | |
| 3568 | /** |
| 3569 | * @hide |
| 3570 | */ |
| 3571 | @Override |
| 3572 | public void populateBigContentView(Notification wip) { |
| 3573 | mBuilder.setBuilderBigContentView(wip, makeBigContentView()); |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 3574 | } |
| 3575 | } |
| 3576 | |
| 3577 | /** |
Daniel Sandler | 4dfbe83 | 2012-04-11 14:51:46 -0400 | [diff] [blame] | 3578 | * 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] | 3579 | * |
Robert Ly | 91c5ce3 | 2014-06-08 15:37:00 -0700 | [diff] [blame] | 3580 | * Here's how you'd set the <code>BigTextStyle</code> on a notification: |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 3581 | * <pre class="prettyprint"> |
Robert Ly | 91c5ce3 | 2014-06-08 15:37:00 -0700 | [diff] [blame] | 3582 | * Notification notif = new Notification.Builder(mContext) |
| 3583 | * .setContentTitle("New mail from " + sender.toString()) |
| 3584 | * .setContentText(subject) |
| 3585 | * .setSmallIcon(R.drawable.new_mail) |
| 3586 | * .setLargeIcon(aBitmap) |
| 3587 | * .setStyle(new Notification.BigTextStyle() |
| 3588 | * .bigText(aVeryLongString)) |
| 3589 | * .build(); |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 3590 | * </pre> |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 3591 | * |
Daniel Sandler | 4dfbe83 | 2012-04-11 14:51:46 -0400 | [diff] [blame] | 3592 | * @see Notification#bigContentView |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 3593 | */ |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 3594 | public static class BigTextStyle extends Style { |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 3595 | private CharSequence mBigText; |
| 3596 | |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 3597 | public BigTextStyle() { |
| 3598 | } |
| 3599 | |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 3600 | public BigTextStyle(Builder builder) { |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 3601 | setBuilder(builder); |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 3602 | } |
| 3603 | |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 3604 | /** |
| 3605 | * Overrides ContentTitle in the big form of the template. |
| 3606 | * This defaults to the value passed to setContentTitle(). |
| 3607 | */ |
| 3608 | public BigTextStyle setBigContentTitle(CharSequence title) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 3609 | internalSetBigContentTitle(safeCharSequence(title)); |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 3610 | return this; |
| 3611 | } |
| 3612 | |
| 3613 | /** |
| 3614 | * Set the first line of text after the detail section in the big form of the template. |
| 3615 | */ |
| 3616 | public BigTextStyle setSummaryText(CharSequence cs) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 3617 | internalSetSummaryText(safeCharSequence(cs)); |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 3618 | return this; |
| 3619 | } |
| 3620 | |
Chris Wren | 0bd664d | 2012-08-01 13:56:56 -0400 | [diff] [blame] | 3621 | /** |
| 3622 | * Provide the longer text to be displayed in the big form of the |
| 3623 | * template in place of the content text. |
| 3624 | */ |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 3625 | public BigTextStyle bigText(CharSequence cs) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 3626 | mBigText = safeCharSequence(cs); |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 3627 | return this; |
| 3628 | } |
| 3629 | |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 3630 | /** |
| 3631 | * @hide |
| 3632 | */ |
| 3633 | public void addExtras(Bundle extras) { |
| 3634 | super.addExtras(extras); |
| 3635 | |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 3636 | extras.putCharSequence(EXTRA_BIG_TEXT, mBigText); |
| 3637 | } |
| 3638 | |
| 3639 | /** |
| 3640 | * @hide |
| 3641 | */ |
| 3642 | @Override |
| 3643 | protected void restoreFromExtras(Bundle extras) { |
| 3644 | super.restoreFromExtras(extras); |
| 3645 | |
| 3646 | mBigText = extras.getCharSequence(EXTRA_BIG_TEXT); |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 3647 | } |
| 3648 | |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 3649 | private RemoteViews makeBigContentView() { |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 3650 | |
| 3651 | // Nasty |
| 3652 | CharSequence oldBuilderContentText = mBuilder.mContentText; |
Daniel Sandler | 6387d2f | 2012-05-22 13:44:09 -0400 | [diff] [blame] | 3653 | mBuilder.mContentText = null; |
Daniel Sandler | 916ad91 | 2012-06-13 12:17:07 -0400 | [diff] [blame] | 3654 | |
Jorim Jaggi | 39fa59f | 2014-02-25 15:38:45 +0100 | [diff] [blame] | 3655 | RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource()); |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 3656 | |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 3657 | mBuilder.mContentText = oldBuilderContentText; |
| 3658 | |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 3659 | contentView.setTextViewText(R.id.big_text, mBuilder.processLegacyText(mBigText)); |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 3660 | contentView.setViewVisibility(R.id.big_text, View.VISIBLE); |
Chris Wren | 3c5f9243 | 2012-05-04 16:31:17 -0400 | [diff] [blame] | 3661 | contentView.setViewVisibility(R.id.text2, View.GONE); |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 3662 | |
Jorim Jaggi | 445d3c0 | 2014-08-19 22:33:42 +0200 | [diff] [blame] | 3663 | applyTopPadding(contentView); |
| 3664 | |
Kenny Guy | 98193ea | 2014-07-24 19:54:37 +0100 | [diff] [blame^] | 3665 | mBuilder.addProfileBadge(contentView, R.id.profile_badge_large_template); |
| 3666 | |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 3667 | return contentView; |
| 3668 | } |
| 3669 | |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 3670 | /** |
| 3671 | * @hide |
| 3672 | */ |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 3673 | @Override |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 3674 | public void populateBigContentView(Notification wip) { |
| 3675 | mBuilder.setBuilderBigContentView(wip, makeBigContentView()); |
Daniel Sandler | f3b7343 | 2012-03-27 15:01:25 -0400 | [diff] [blame] | 3676 | } |
| 3677 | } |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 3678 | |
| 3679 | /** |
| 3680 | * 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] | 3681 | * |
Robert Ly | 91c5ce3 | 2014-06-08 15:37:00 -0700 | [diff] [blame] | 3682 | * Here's how you'd set the <code>InboxStyle</code> on a notification: |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 3683 | * <pre class="prettyprint"> |
Robert Ly | 91c5ce3 | 2014-06-08 15:37:00 -0700 | [diff] [blame] | 3684 | * Notification notif = new Notification.Builder(mContext) |
| 3685 | * .setContentTitle("5 New mails from " + sender.toString()) |
| 3686 | * .setContentText(subject) |
| 3687 | * .setSmallIcon(R.drawable.new_mail) |
| 3688 | * .setLargeIcon(aBitmap) |
| 3689 | * .setStyle(new Notification.InboxStyle() |
| 3690 | * .addLine(str1) |
| 3691 | * .addLine(str2) |
| 3692 | * .setContentTitle("") |
| 3693 | * .setSummaryText("+3 more")) |
| 3694 | * .build(); |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 3695 | * </pre> |
Joe Malin | 8d40d04 | 2012-11-05 11:36:40 -0800 | [diff] [blame] | 3696 | * |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 3697 | * @see Notification#bigContentView |
| 3698 | */ |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 3699 | public static class InboxStyle extends Style { |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 3700 | private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5); |
| 3701 | |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 3702 | public InboxStyle() { |
| 3703 | } |
| 3704 | |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 3705 | public InboxStyle(Builder builder) { |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 3706 | setBuilder(builder); |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 3707 | } |
| 3708 | |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 3709 | /** |
| 3710 | * Overrides ContentTitle in the big form of the template. |
| 3711 | * This defaults to the value passed to setContentTitle(). |
| 3712 | */ |
| 3713 | public InboxStyle setBigContentTitle(CharSequence title) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 3714 | internalSetBigContentTitle(safeCharSequence(title)); |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 3715 | return this; |
| 3716 | } |
| 3717 | |
| 3718 | /** |
| 3719 | * Set the first line of text after the detail section in the big form of the template. |
| 3720 | */ |
| 3721 | public InboxStyle setSummaryText(CharSequence cs) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 3722 | internalSetSummaryText(safeCharSequence(cs)); |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 3723 | return this; |
| 3724 | } |
| 3725 | |
Chris Wren | 0bd664d | 2012-08-01 13:56:56 -0400 | [diff] [blame] | 3726 | /** |
| 3727 | * Append a line to the digest section of the Inbox notification. |
| 3728 | */ |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 3729 | public InboxStyle addLine(CharSequence cs) { |
Daniel Sandler | dcbaf66 | 2013-04-26 16:23:09 -0400 | [diff] [blame] | 3730 | mTexts.add(safeCharSequence(cs)); |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 3731 | return this; |
| 3732 | } |
| 3733 | |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 3734 | /** |
| 3735 | * @hide |
| 3736 | */ |
| 3737 | public void addExtras(Bundle extras) { |
| 3738 | super.addExtras(extras); |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 3739 | |
Daniel Sandler | f45564e | 2013-04-15 15:05:08 -0400 | [diff] [blame] | 3740 | CharSequence[] a = new CharSequence[mTexts.size()]; |
| 3741 | extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a)); |
| 3742 | } |
| 3743 | |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 3744 | /** |
| 3745 | * @hide |
| 3746 | */ |
| 3747 | @Override |
| 3748 | protected void restoreFromExtras(Bundle extras) { |
| 3749 | super.restoreFromExtras(extras); |
| 3750 | |
| 3751 | mTexts.clear(); |
| 3752 | if (extras.containsKey(EXTRA_TEXT_LINES)) { |
| 3753 | Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES)); |
| 3754 | } |
| 3755 | } |
| 3756 | |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 3757 | private RemoteViews makeBigContentView() { |
Daniel Sandler | 619738c | 2012-06-07 16:33:08 -0400 | [diff] [blame] | 3758 | // Remove the content text so line3 disappears unless you have a summary |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 3759 | |
| 3760 | // Nasty |
| 3761 | CharSequence oldBuilderContentText = mBuilder.mContentText; |
Daniel Sandler | 619738c | 2012-06-07 16:33:08 -0400 | [diff] [blame] | 3762 | mBuilder.mContentText = null; |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 3763 | |
Jorim Jaggi | 39fa59f | 2014-02-25 15:38:45 +0100 | [diff] [blame] | 3764 | RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource()); |
Daniel Sandler | 619738c | 2012-06-07 16:33:08 -0400 | [diff] [blame] | 3765 | |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 3766 | mBuilder.mContentText = oldBuilderContentText; |
| 3767 | |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 3768 | contentView.setViewVisibility(R.id.text2, View.GONE); |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 3769 | |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 3770 | 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] | 3771 | R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6}; |
Chris Wren | d6297db | 2012-05-03 16:20:13 -0400 | [diff] [blame] | 3772 | |
Chris Wren | 4ed80d5 | 2012-05-17 09:30:03 -0400 | [diff] [blame] | 3773 | // Make sure all rows are gone in case we reuse a view. |
| 3774 | for (int rowId : rowIds) { |
| 3775 | contentView.setViewVisibility(rowId, View.GONE); |
| 3776 | } |
| 3777 | |
Jorim Jaggi | 445d3c0 | 2014-08-19 22:33:42 +0200 | [diff] [blame] | 3778 | final boolean largeText = |
| 3779 | mBuilder.mContext.getResources().getConfiguration().fontScale > 1f; |
| 3780 | final float subTextSize = mBuilder.mContext.getResources().getDimensionPixelSize( |
| 3781 | R.dimen.notification_subtext_size); |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 3782 | int i=0; |
| 3783 | while (i < mTexts.size() && i < rowIds.length) { |
| 3784 | CharSequence str = mTexts.get(i); |
| 3785 | if (str != null && !str.equals("")) { |
| 3786 | contentView.setViewVisibility(rowIds[i], View.VISIBLE); |
Jorim Jaggi | 5c2d846 | 2014-03-21 17:37:00 +0100 | [diff] [blame] | 3787 | contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str)); |
Jorim Jaggi | 445d3c0 | 2014-08-19 22:33:42 +0200 | [diff] [blame] | 3788 | if (largeText) { |
| 3789 | contentView.setTextViewTextSize(rowIds[i], TypedValue.COMPLEX_UNIT_PX, |
| 3790 | subTextSize); |
| 3791 | } |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 3792 | } |
| 3793 | i++; |
| 3794 | } |
| 3795 | |
Chris Wren | 683ab00 | 2012-09-20 10:35:54 -0400 | [diff] [blame] | 3796 | contentView.setViewVisibility(R.id.inbox_end_pad, |
| 3797 | mTexts.size() > 0 ? View.VISIBLE : View.GONE); |
| 3798 | |
| 3799 | contentView.setViewVisibility(R.id.inbox_more, |
| 3800 | mTexts.size() > rowIds.length ? View.VISIBLE : View.GONE); |
Chris Wren | 29bb6d9 | 2012-05-17 18:09:42 -0400 | [diff] [blame] | 3801 | |
Jorim Jaggi | 445d3c0 | 2014-08-19 22:33:42 +0200 | [diff] [blame] | 3802 | applyTopPadding(contentView); |
| 3803 | |
Kenny Guy | 98193ea | 2014-07-24 19:54:37 +0100 | [diff] [blame^] | 3804 | mBuilder.addProfileBadge(contentView, R.id.profile_badge_large_template); |
| 3805 | |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 3806 | return contentView; |
| 3807 | } |
| 3808 | |
Daniel Sandler | cf1d39b | 2013-09-23 13:35:35 -0400 | [diff] [blame] | 3809 | /** |
| 3810 | * @hide |
| 3811 | */ |
Chris Wren | fbd96ba | 2012-05-01 12:03:58 -0400 | [diff] [blame] | 3812 | @Override |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 3813 | public void populateBigContentView(Notification wip) { |
| 3814 | mBuilder.setBuilderBigContentView(wip, makeBigContentView()); |
Daniel Sandler | 879c5e0 | 2012-04-17 16:46:51 -0400 | [diff] [blame] | 3815 | } |
| 3816 | } |
Dan Sandler | 842dd77 | 2014-05-15 09:36:47 -0400 | [diff] [blame] | 3817 | |
| 3818 | /** |
| 3819 | * Notification style for media playback notifications. |
| 3820 | * |
| 3821 | * In the expanded form, {@link Notification#bigContentView}, up to 5 |
| 3822 | * {@link Notification.Action}s specified with |
| 3823 | * {@link Notification.Builder#addAction(int, CharSequence, PendingIntent) addAction} will be |
| 3824 | * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to |
| 3825 | * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be |
| 3826 | * treated as album artwork. |
| 3827 | * |
| 3828 | * Unlike the other styles provided here, MediaStyle can also modify the standard-size |
| 3829 | * {@link Notification#contentView}; by providing action indices to |
| 3830 | * {@link #setShowActionsInCompactView(int...)} you can promote up to 2 actions to be displayed |
| 3831 | * in the standard view alongside the usual content. |
| 3832 | * |
Bryan Mawhinney | 6be8de3 | 2014-07-18 10:35:12 +0100 | [diff] [blame] | 3833 | * Notifications created with MediaStyle will have their category set to |
| 3834 | * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different |
| 3835 | * category using {@link Notification.Builder#setCategory(String) setCategory()}. |
| 3836 | * |
Jeff Brown | dba34ba | 2014-06-24 20:46:03 -0700 | [diff] [blame] | 3837 | * Finally, if you attach a {@link android.media.session.MediaSession.Token} using |
| 3838 | * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)}, |
Dan Sandler | 842dd77 | 2014-05-15 09:36:47 -0400 | [diff] [blame] | 3839 | * the System UI can identify this as a notification representing an active media session |
| 3840 | * and respond accordingly (by showing album artwork in the lockscreen, for example). |
| 3841 | * |
| 3842 | * To use this style with your Notification, feed it to |
| 3843 | * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so: |
| 3844 | * <pre class="prettyprint"> |
| 3845 | * Notification noti = new Notification.Builder() |
| 3846 | * .setSmallIcon(R.drawable.ic_stat_player) |
| 3847 | * .setContentTitle("Track title") // these three lines are optional |
| 3848 | * .setContentText("Artist - Album") // if you use |
Bryan Mawhinney | 6be8de3 | 2014-07-18 10:35:12 +0100 | [diff] [blame] | 3849 | * .setLargeIcon(albumArtBitmap)) // setMediaSession(token) |
| 3850 | * .setStyle(<b>new Notification.MediaStyle()</b> |
| 3851 | * .setMediaSession(mySession)) |
Dan Sandler | 842dd77 | 2014-05-15 09:36:47 -0400 | [diff] [blame] | 3852 | * .build(); |
| 3853 | * </pre> |
| 3854 | * |
| 3855 | * @see Notification#bigContentView |
| 3856 | */ |
| 3857 | public static class MediaStyle extends Style { |
| 3858 | static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 2; |
| 3859 | static final int MAX_MEDIA_BUTTONS = 5; |
| 3860 | |
| 3861 | private int[] mActionsToShowInCompact = null; |
Jeff Brown | dba34ba | 2014-06-24 20:46:03 -0700 | [diff] [blame] | 3862 | private MediaSession.Token mToken; |
Dan Sandler | 842dd77 | 2014-05-15 09:36:47 -0400 | [diff] [blame] | 3863 | |
| 3864 | public MediaStyle() { |
| 3865 | } |
| 3866 | |
| 3867 | public MediaStyle(Builder builder) { |
| 3868 | setBuilder(builder); |
| 3869 | } |
| 3870 | |
| 3871 | /** |
| 3872 | * Request up to 2 actions (by index in the order of addition) to be shown in the compact |
| 3873 | * notification view. |
| 3874 | */ |
| 3875 | public MediaStyle setShowActionsInCompactView(int...actions) { |
| 3876 | mActionsToShowInCompact = actions; |
| 3877 | return this; |
| 3878 | } |
| 3879 | |
| 3880 | /** |
Jeff Brown | dba34ba | 2014-06-24 20:46:03 -0700 | [diff] [blame] | 3881 | * Attach a {@link android.media.session.MediaSession.Token} to this Notification |
| 3882 | * to provide additional playback information and control to the SystemUI. |
Dan Sandler | 842dd77 | 2014-05-15 09:36:47 -0400 | [diff] [blame] | 3883 | */ |
Jeff Brown | dba34ba | 2014-06-24 20:46:03 -0700 | [diff] [blame] | 3884 | public MediaStyle setMediaSession(MediaSession.Token token) { |
Dan Sandler | 842dd77 | 2014-05-15 09:36:47 -0400 | [diff] [blame] | 3885 | mToken = token; |
| 3886 | return this; |
| 3887 | } |
| 3888 | |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 3889 | /** |
| 3890 | * @hide |
| 3891 | */ |
Dan Sandler | 842dd77 | 2014-05-15 09:36:47 -0400 | [diff] [blame] | 3892 | @Override |
| 3893 | public Notification buildStyled(Notification wip) { |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 3894 | super.buildStyled(wip); |
Bryan Mawhinney | 6be8de3 | 2014-07-18 10:35:12 +0100 | [diff] [blame] | 3895 | if (wip.category == null) { |
| 3896 | wip.category = Notification.CATEGORY_TRANSPORT; |
| 3897 | } |
Dan Sandler | 842dd77 | 2014-05-15 09:36:47 -0400 | [diff] [blame] | 3898 | return wip; |
| 3899 | } |
| 3900 | |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 3901 | /** |
| 3902 | * @hide |
| 3903 | */ |
| 3904 | @Override |
| 3905 | public void populateContentView(Notification wip) { |
| 3906 | mBuilder.setBuilderContentView(wip, makeMediaContentView()); |
| 3907 | } |
| 3908 | |
| 3909 | /** |
| 3910 | * @hide |
| 3911 | */ |
| 3912 | @Override |
| 3913 | public void populateBigContentView(Notification wip) { |
| 3914 | mBuilder.setBuilderBigContentView(wip, makeMediaBigContentView()); |
| 3915 | } |
| 3916 | |
Dan Sandler | 842dd77 | 2014-05-15 09:36:47 -0400 | [diff] [blame] | 3917 | /** @hide */ |
| 3918 | @Override |
| 3919 | public void addExtras(Bundle extras) { |
| 3920 | super.addExtras(extras); |
| 3921 | |
| 3922 | if (mToken != null) { |
| 3923 | extras.putParcelable(EXTRA_MEDIA_SESSION, mToken); |
| 3924 | } |
Bryan Mawhinney | e191f90 | 2014-07-22 12:50:09 +0100 | [diff] [blame] | 3925 | if (mActionsToShowInCompact != null) { |
| 3926 | extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact); |
| 3927 | } |
Dan Sandler | 842dd77 | 2014-05-15 09:36:47 -0400 | [diff] [blame] | 3928 | } |
| 3929 | |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 3930 | /** |
| 3931 | * @hide |
| 3932 | */ |
| 3933 | @Override |
| 3934 | protected void restoreFromExtras(Bundle extras) { |
| 3935 | super.restoreFromExtras(extras); |
| 3936 | |
| 3937 | if (extras.containsKey(EXTRA_MEDIA_SESSION)) { |
| 3938 | mToken = extras.getParcelable(EXTRA_MEDIA_SESSION); |
| 3939 | } |
| 3940 | if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) { |
| 3941 | mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS); |
| 3942 | } |
| 3943 | } |
| 3944 | |
Dan Sandler | 842dd77 | 2014-05-15 09:36:47 -0400 | [diff] [blame] | 3945 | private RemoteViews generateMediaActionButton(Action action) { |
| 3946 | final boolean tombstone = (action.actionIntent == null); |
| 3947 | RemoteViews button = new RemoteViews(mBuilder.mContext.getPackageName(), |
Alan Viverette | 3cb07a46 | 2014-06-06 14:19:53 -0700 | [diff] [blame] | 3948 | R.layout.notification_material_media_action); |
Dan Sandler | 842dd77 | 2014-05-15 09:36:47 -0400 | [diff] [blame] | 3949 | button.setImageViewResource(R.id.action0, action.icon); |
| 3950 | if (!tombstone) { |
| 3951 | button.setOnClickPendingIntent(R.id.action0, action.actionIntent); |
| 3952 | } |
| 3953 | button.setContentDescription(R.id.action0, action.title); |
| 3954 | return button; |
| 3955 | } |
| 3956 | |
| 3957 | private RemoteViews makeMediaContentView() { |
| 3958 | RemoteViews view = mBuilder.applyStandardTemplate( |
Jorim Jaggi | 445d3c0 | 2014-08-19 22:33:42 +0200 | [diff] [blame] | 3959 | R.layout.notification_template_material_media); |
Dan Sandler | 842dd77 | 2014-05-15 09:36:47 -0400 | [diff] [blame] | 3960 | |
| 3961 | final int numActions = mBuilder.mActions.size(); |
| 3962 | final int N = mActionsToShowInCompact == null |
| 3963 | ? 0 |
| 3964 | : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT); |
| 3965 | if (N > 0) { |
| 3966 | view.removeAllViews(R.id.actions); |
| 3967 | for (int i = 0; i < N; i++) { |
| 3968 | if (i >= numActions) { |
| 3969 | throw new IllegalArgumentException(String.format( |
| 3970 | "setShowActionsInCompactView: action %d out of bounds (max %d)", |
| 3971 | i, numActions - 1)); |
| 3972 | } |
| 3973 | |
| 3974 | final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]); |
| 3975 | final RemoteViews button = generateMediaActionButton(action); |
| 3976 | view.addView(R.id.actions, button); |
| 3977 | } |
| 3978 | } |
| 3979 | return view; |
| 3980 | } |
| 3981 | |
| 3982 | private RemoteViews makeMediaBigContentView() { |
| 3983 | RemoteViews big = mBuilder.applyStandardTemplate( |
Jorim Jaggi | 445d3c0 | 2014-08-19 22:33:42 +0200 | [diff] [blame] | 3984 | R.layout.notification_template_material_big_media); |
Dan Sandler | 842dd77 | 2014-05-15 09:36:47 -0400 | [diff] [blame] | 3985 | |
| 3986 | final int N = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS); |
| 3987 | if (N > 0) { |
| 3988 | big.removeAllViews(R.id.actions); |
| 3989 | for (int i=0; i<N; i++) { |
| 3990 | final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i)); |
| 3991 | big.addView(R.id.actions, button); |
| 3992 | } |
| 3993 | } |
| 3994 | return big; |
| 3995 | } |
| 3996 | } |
Griff Hazen | 61a9e86 | 2014-05-22 16:05:19 -0700 | [diff] [blame] | 3997 | |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 3998 | // When adding a new Style subclass here, don't forget to update |
| 3999 | // Builder.getNotificationStyleClass. |
| 4000 | |
Griff Hazen | 61a9e86 | 2014-05-22 16:05:19 -0700 | [diff] [blame] | 4001 | /** |
| 4002 | * Extender interface for use with {@link Builder#extend}. Extenders may be used to add |
| 4003 | * metadata or change options on a notification builder. |
| 4004 | */ |
| 4005 | public interface Extender { |
| 4006 | /** |
| 4007 | * Apply this extender to a notification builder. |
| 4008 | * @param builder the builder to be modified. |
| 4009 | * @return the build object for chaining. |
| 4010 | */ |
| 4011 | public Builder extend(Builder builder); |
| 4012 | } |
| 4013 | |
| 4014 | /** |
| 4015 | * Helper class to add wearable extensions to notifications. |
| 4016 | * <p class="note"> See |
| 4017 | * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications |
| 4018 | * for Android Wear</a> for more information on how to use this class. |
| 4019 | * <p> |
| 4020 | * To create a notification with wearable extensions: |
| 4021 | * <ol> |
| 4022 | * <li>Create a {@link android.app.Notification.Builder}, setting any desired |
| 4023 | * properties. |
| 4024 | * <li>Create a {@link android.app.Notification.WearableExtender}. |
| 4025 | * <li>Set wearable-specific properties using the |
| 4026 | * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}. |
| 4027 | * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a |
| 4028 | * notification. |
| 4029 | * <li>Post the notification to the notification system with the |
| 4030 | * {@code NotificationManager.notify(...)} methods. |
| 4031 | * </ol> |
| 4032 | * |
| 4033 | * <pre class="prettyprint"> |
| 4034 | * Notification notif = new Notification.Builder(mContext) |
| 4035 | * .setContentTitle("New mail from " + sender.toString()) |
| 4036 | * .setContentText(subject) |
| 4037 | * .setSmallIcon(R.drawable.new_mail) |
| 4038 | * .extend(new Notification.WearableExtender() |
| 4039 | * .setContentIcon(R.drawable.new_mail)) |
| 4040 | * .build(); |
| 4041 | * NotificationManager notificationManger = |
| 4042 | * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); |
| 4043 | * notificationManger.notify(0, notif);</pre> |
| 4044 | * |
| 4045 | * <p>Wearable extensions can be accessed on an existing notification by using the |
| 4046 | * {@code WearableExtender(Notification)} constructor, |
| 4047 | * and then using the {@code get} methods to access values. |
| 4048 | * |
| 4049 | * <pre class="prettyprint"> |
| 4050 | * Notification.WearableExtender wearableExtender = new Notification.WearableExtender( |
| 4051 | * notification); |
Griff Hazen | 14f5799 | 2014-05-26 09:07:14 -0700 | [diff] [blame] | 4052 | * List<Notification> pages = wearableExtender.getPages();</pre> |
Griff Hazen | 61a9e86 | 2014-05-22 16:05:19 -0700 | [diff] [blame] | 4053 | */ |
| 4054 | public static final class WearableExtender implements Extender { |
| 4055 | /** |
| 4056 | * Sentinel value for an action index that is unset. |
| 4057 | */ |
| 4058 | public static final int UNSET_ACTION_INDEX = -1; |
| 4059 | |
| 4060 | /** |
| 4061 | * Size value for use with {@link #setCustomSizePreset} to show this notification with |
| 4062 | * default sizing. |
| 4063 | * <p>For custom display notifications created using {@link #setDisplayIntent}, |
| 4064 | * the default is {@link #SIZE_LARGE}. All other notifications size automatically based |
| 4065 | * on their content. |
| 4066 | */ |
| 4067 | public static final int SIZE_DEFAULT = 0; |
| 4068 | |
| 4069 | /** |
| 4070 | * Size value for use with {@link #setCustomSizePreset} to show this notification |
| 4071 | * with an extra small size. |
| 4072 | * <p>This value is only applicable for custom display notifications created using |
| 4073 | * {@link #setDisplayIntent}. |
| 4074 | */ |
| 4075 | public static final int SIZE_XSMALL = 1; |
| 4076 | |
| 4077 | /** |
| 4078 | * Size value for use with {@link #setCustomSizePreset} to show this notification |
| 4079 | * with a small size. |
| 4080 | * <p>This value is only applicable for custom display notifications created using |
| 4081 | * {@link #setDisplayIntent}. |
| 4082 | */ |
| 4083 | public static final int SIZE_SMALL = 2; |
| 4084 | |
| 4085 | /** |
| 4086 | * Size value for use with {@link #setCustomSizePreset} to show this notification |
| 4087 | * with a medium size. |
| 4088 | * <p>This value is only applicable for custom display notifications created using |
| 4089 | * {@link #setDisplayIntent}. |
| 4090 | */ |
| 4091 | public static final int SIZE_MEDIUM = 3; |
| 4092 | |
| 4093 | /** |
| 4094 | * Size value for use with {@link #setCustomSizePreset} to show this notification |
| 4095 | * with a large size. |
| 4096 | * <p>This value is only applicable for custom display notifications created using |
| 4097 | * {@link #setDisplayIntent}. |
| 4098 | */ |
| 4099 | public static final int SIZE_LARGE = 4; |
| 4100 | |
Griff Hazen | d5f11f9 | 2014-05-27 15:40:09 -0700 | [diff] [blame] | 4101 | /** |
| 4102 | * Size value for use with {@link #setCustomSizePreset} to show this notification |
| 4103 | * full screen. |
| 4104 | * <p>This value is only applicable for custom display notifications created using |
| 4105 | * {@link #setDisplayIntent}. |
| 4106 | */ |
| 4107 | public static final int SIZE_FULL_SCREEN = 5; |
| 4108 | |
Griff Hazen | 61a9e86 | 2014-05-22 16:05:19 -0700 | [diff] [blame] | 4109 | /** Notification extra which contains wearable extensions */ |
| 4110 | private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS"; |
| 4111 | |
| 4112 | // Keys within EXTRA_WEARABLE_OPTIONS for wearable options. |
| 4113 | private static final String KEY_ACTIONS = "actions"; |
| 4114 | private static final String KEY_FLAGS = "flags"; |
| 4115 | private static final String KEY_DISPLAY_INTENT = "displayIntent"; |
| 4116 | private static final String KEY_PAGES = "pages"; |
| 4117 | private static final String KEY_BACKGROUND = "background"; |
| 4118 | private static final String KEY_CONTENT_ICON = "contentIcon"; |
| 4119 | private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity"; |
| 4120 | private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex"; |
| 4121 | private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset"; |
| 4122 | private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight"; |
| 4123 | private static final String KEY_GRAVITY = "gravity"; |
| 4124 | |
| 4125 | // Flags bitwise-ored to mFlags |
| 4126 | private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1; |
| 4127 | private static final int FLAG_HINT_HIDE_ICON = 1 << 1; |
| 4128 | private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2; |
| 4129 | private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3; |
| 4130 | |
| 4131 | // Default value for flags integer |
| 4132 | private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE; |
| 4133 | |
| 4134 | private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END; |
| 4135 | private static final int DEFAULT_GRAVITY = Gravity.BOTTOM; |
| 4136 | |
| 4137 | private ArrayList<Action> mActions = new ArrayList<Action>(); |
| 4138 | private int mFlags = DEFAULT_FLAGS; |
| 4139 | private PendingIntent mDisplayIntent; |
| 4140 | private ArrayList<Notification> mPages = new ArrayList<Notification>(); |
| 4141 | private Bitmap mBackground; |
| 4142 | private int mContentIcon; |
| 4143 | private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY; |
| 4144 | private int mContentActionIndex = UNSET_ACTION_INDEX; |
| 4145 | private int mCustomSizePreset = SIZE_DEFAULT; |
| 4146 | private int mCustomContentHeight; |
| 4147 | private int mGravity = DEFAULT_GRAVITY; |
| 4148 | |
| 4149 | /** |
| 4150 | * Create a {@link android.app.Notification.WearableExtender} with default |
| 4151 | * options. |
| 4152 | */ |
| 4153 | public WearableExtender() { |
| 4154 | } |
| 4155 | |
| 4156 | public WearableExtender(Notification notif) { |
| 4157 | Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS); |
| 4158 | if (wearableBundle != null) { |
| 4159 | List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS); |
| 4160 | if (actions != null) { |
| 4161 | mActions.addAll(actions); |
| 4162 | } |
| 4163 | |
| 4164 | mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS); |
| 4165 | mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT); |
| 4166 | |
| 4167 | Notification[] pages = getNotificationArrayFromBundle( |
| 4168 | wearableBundle, KEY_PAGES); |
| 4169 | if (pages != null) { |
| 4170 | Collections.addAll(mPages, pages); |
| 4171 | } |
| 4172 | |
| 4173 | mBackground = wearableBundle.getParcelable(KEY_BACKGROUND); |
| 4174 | mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON); |
| 4175 | mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY, |
| 4176 | DEFAULT_CONTENT_ICON_GRAVITY); |
| 4177 | mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX, |
| 4178 | UNSET_ACTION_INDEX); |
| 4179 | mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET, |
| 4180 | SIZE_DEFAULT); |
| 4181 | mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT); |
| 4182 | mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY); |
| 4183 | } |
| 4184 | } |
| 4185 | |
| 4186 | /** |
| 4187 | * Apply wearable extensions to a notification that is being built. This is typically |
| 4188 | * called by the {@link android.app.Notification.Builder#extend} method of |
| 4189 | * {@link android.app.Notification.Builder}. |
| 4190 | */ |
| 4191 | @Override |
| 4192 | public Notification.Builder extend(Notification.Builder builder) { |
| 4193 | Bundle wearableBundle = new Bundle(); |
| 4194 | |
| 4195 | if (!mActions.isEmpty()) { |
| 4196 | wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions); |
| 4197 | } |
| 4198 | if (mFlags != DEFAULT_FLAGS) { |
| 4199 | wearableBundle.putInt(KEY_FLAGS, mFlags); |
| 4200 | } |
| 4201 | if (mDisplayIntent != null) { |
| 4202 | wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent); |
| 4203 | } |
| 4204 | if (!mPages.isEmpty()) { |
| 4205 | wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray( |
| 4206 | new Notification[mPages.size()])); |
| 4207 | } |
| 4208 | if (mBackground != null) { |
| 4209 | wearableBundle.putParcelable(KEY_BACKGROUND, mBackground); |
| 4210 | } |
| 4211 | if (mContentIcon != 0) { |
| 4212 | wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon); |
| 4213 | } |
| 4214 | if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) { |
| 4215 | wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity); |
| 4216 | } |
| 4217 | if (mContentActionIndex != UNSET_ACTION_INDEX) { |
| 4218 | wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX, |
| 4219 | mContentActionIndex); |
| 4220 | } |
| 4221 | if (mCustomSizePreset != SIZE_DEFAULT) { |
| 4222 | wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset); |
| 4223 | } |
| 4224 | if (mCustomContentHeight != 0) { |
| 4225 | wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight); |
| 4226 | } |
| 4227 | if (mGravity != DEFAULT_GRAVITY) { |
| 4228 | wearableBundle.putInt(KEY_GRAVITY, mGravity); |
| 4229 | } |
| 4230 | |
| 4231 | builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle); |
| 4232 | return builder; |
| 4233 | } |
| 4234 | |
| 4235 | @Override |
| 4236 | public WearableExtender clone() { |
| 4237 | WearableExtender that = new WearableExtender(); |
| 4238 | that.mActions = new ArrayList<Action>(this.mActions); |
| 4239 | that.mFlags = this.mFlags; |
| 4240 | that.mDisplayIntent = this.mDisplayIntent; |
| 4241 | that.mPages = new ArrayList<Notification>(this.mPages); |
| 4242 | that.mBackground = this.mBackground; |
| 4243 | that.mContentIcon = this.mContentIcon; |
| 4244 | that.mContentIconGravity = this.mContentIconGravity; |
| 4245 | that.mContentActionIndex = this.mContentActionIndex; |
| 4246 | that.mCustomSizePreset = this.mCustomSizePreset; |
| 4247 | that.mCustomContentHeight = this.mCustomContentHeight; |
| 4248 | that.mGravity = this.mGravity; |
| 4249 | return that; |
| 4250 | } |
| 4251 | |
| 4252 | /** |
| 4253 | * Add a wearable action to this notification. |
| 4254 | * |
| 4255 | * <p>When wearable actions are added using this method, the set of actions that |
| 4256 | * show on a wearable device splits from devices that only show actions added |
| 4257 | * using {@link android.app.Notification.Builder#addAction}. This allows for customization |
| 4258 | * of which actions display on different devices. |
| 4259 | * |
| 4260 | * @param action the action to add to this notification |
| 4261 | * @return this object for method chaining |
| 4262 | * @see android.app.Notification.Action |
| 4263 | */ |
| 4264 | public WearableExtender addAction(Action action) { |
| 4265 | mActions.add(action); |
| 4266 | return this; |
| 4267 | } |
| 4268 | |
| 4269 | /** |
| 4270 | * Adds wearable actions to this notification. |
| 4271 | * |
| 4272 | * <p>When wearable actions are added using this method, the set of actions that |
| 4273 | * show on a wearable device splits from devices that only show actions added |
| 4274 | * using {@link android.app.Notification.Builder#addAction}. This allows for customization |
| 4275 | * of which actions display on different devices. |
| 4276 | * |
| 4277 | * @param actions the actions to add to this notification |
| 4278 | * @return this object for method chaining |
| 4279 | * @see android.app.Notification.Action |
| 4280 | */ |
| 4281 | public WearableExtender addActions(List<Action> actions) { |
| 4282 | mActions.addAll(actions); |
| 4283 | return this; |
| 4284 | } |
| 4285 | |
| 4286 | /** |
| 4287 | * Clear all wearable actions present on this builder. |
| 4288 | * @return this object for method chaining. |
| 4289 | * @see #addAction |
| 4290 | */ |
| 4291 | public WearableExtender clearActions() { |
| 4292 | mActions.clear(); |
| 4293 | return this; |
| 4294 | } |
| 4295 | |
| 4296 | /** |
| 4297 | * Get the wearable actions present on this notification. |
| 4298 | */ |
| 4299 | public List<Action> getActions() { |
| 4300 | return mActions; |
| 4301 | } |
| 4302 | |
| 4303 | /** |
| 4304 | * Set an intent to launch inside of an activity view when displaying |
Griff Hazen | 14f5799 | 2014-05-26 09:07:14 -0700 | [diff] [blame] | 4305 | * this notification. The {@link PendingIntent} provided should be for an activity. |
| 4306 | * |
| 4307 | * <pre class="prettyprint"> |
| 4308 | * Intent displayIntent = new Intent(context, MyDisplayActivity.class); |
| 4309 | * PendingIntent displayPendingIntent = PendingIntent.getActivity(context, |
| 4310 | * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT); |
| 4311 | * Notification notif = new Notification.Builder(context) |
| 4312 | * .extend(new Notification.WearableExtender() |
| 4313 | * .setDisplayIntent(displayPendingIntent) |
| 4314 | * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM)) |
| 4315 | * .build();</pre> |
| 4316 | * |
| 4317 | * <p>The activity to launch needs to allow embedding, must be exported, and |
Griff Hazen | 831ca9d | 2014-06-17 00:38:38 -0700 | [diff] [blame] | 4318 | * should have an empty task affinity. It is also recommended to use the device |
| 4319 | * default light theme. |
Griff Hazen | 14f5799 | 2014-05-26 09:07:14 -0700 | [diff] [blame] | 4320 | * |
| 4321 | * <p>Example AndroidManifest.xml entry: |
| 4322 | * <pre class="prettyprint"> |
| 4323 | * <activity android:name="com.example.MyDisplayActivity" |
| 4324 | * android:exported="true" |
| 4325 | * android:allowEmbedded="true" |
Griff Hazen | 831ca9d | 2014-06-17 00:38:38 -0700 | [diff] [blame] | 4326 | * android:taskAffinity="" |
| 4327 | * android:theme="@android:style/Theme.DeviceDefault.Light" /></pre> |
Griff Hazen | 61a9e86 | 2014-05-22 16:05:19 -0700 | [diff] [blame] | 4328 | * |
| 4329 | * @param intent the {@link PendingIntent} for an activity |
| 4330 | * @return this object for method chaining |
| 4331 | * @see android.app.Notification.WearableExtender#getDisplayIntent |
| 4332 | */ |
| 4333 | public WearableExtender setDisplayIntent(PendingIntent intent) { |
| 4334 | mDisplayIntent = intent; |
| 4335 | return this; |
| 4336 | } |
| 4337 | |
| 4338 | /** |
| 4339 | * Get the intent to launch inside of an activity view when displaying this |
| 4340 | * notification. This {@code PendingIntent} should be for an activity. |
| 4341 | */ |
| 4342 | public PendingIntent getDisplayIntent() { |
| 4343 | return mDisplayIntent; |
| 4344 | } |
| 4345 | |
| 4346 | /** |
| 4347 | * Add an additional page of content to display with this notification. The current |
| 4348 | * notification forms the first page, and pages added using this function form |
| 4349 | * subsequent pages. This field can be used to separate a notification into multiple |
| 4350 | * sections. |
| 4351 | * |
| 4352 | * @param page the notification to add as another page |
| 4353 | * @return this object for method chaining |
| 4354 | * @see android.app.Notification.WearableExtender#getPages |
| 4355 | */ |
| 4356 | public WearableExtender addPage(Notification page) { |
| 4357 | mPages.add(page); |
| 4358 | return this; |
| 4359 | } |
| 4360 | |
| 4361 | /** |
| 4362 | * Add additional pages of content to display with this notification. The current |
| 4363 | * notification forms the first page, and pages added using this function form |
| 4364 | * subsequent pages. This field can be used to separate a notification into multiple |
| 4365 | * sections. |
| 4366 | * |
| 4367 | * @param pages a list of notifications |
| 4368 | * @return this object for method chaining |
| 4369 | * @see android.app.Notification.WearableExtender#getPages |
| 4370 | */ |
| 4371 | public WearableExtender addPages(List<Notification> pages) { |
| 4372 | mPages.addAll(pages); |
| 4373 | return this; |
| 4374 | } |
| 4375 | |
| 4376 | /** |
| 4377 | * Clear all additional pages present on this builder. |
| 4378 | * @return this object for method chaining. |
| 4379 | * @see #addPage |
| 4380 | */ |
| 4381 | public WearableExtender clearPages() { |
| 4382 | mPages.clear(); |
| 4383 | return this; |
| 4384 | } |
| 4385 | |
| 4386 | /** |
| 4387 | * Get the array of additional pages of content for displaying this notification. The |
| 4388 | * current notification forms the first page, and elements within this array form |
| 4389 | * subsequent pages. This field can be used to separate a notification into multiple |
| 4390 | * sections. |
| 4391 | * @return the pages for this notification |
| 4392 | */ |
| 4393 | public List<Notification> getPages() { |
| 4394 | return mPages; |
| 4395 | } |
| 4396 | |
| 4397 | /** |
| 4398 | * Set a background image to be displayed behind the notification content. |
| 4399 | * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background |
| 4400 | * will work with any notification style. |
| 4401 | * |
| 4402 | * @param background the background bitmap |
| 4403 | * @return this object for method chaining |
| 4404 | * @see android.app.Notification.WearableExtender#getBackground |
| 4405 | */ |
| 4406 | public WearableExtender setBackground(Bitmap background) { |
| 4407 | mBackground = background; |
| 4408 | return this; |
| 4409 | } |
| 4410 | |
| 4411 | /** |
| 4412 | * Get a background image to be displayed behind the notification content. |
| 4413 | * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background |
| 4414 | * will work with any notification style. |
| 4415 | * |
| 4416 | * @return the background image |
| 4417 | * @see android.app.Notification.WearableExtender#setBackground |
| 4418 | */ |
| 4419 | public Bitmap getBackground() { |
| 4420 | return mBackground; |
| 4421 | } |
| 4422 | |
| 4423 | /** |
| 4424 | * Set an icon that goes with the content of this notification. |
| 4425 | */ |
| 4426 | public WearableExtender setContentIcon(int icon) { |
| 4427 | mContentIcon = icon; |
| 4428 | return this; |
| 4429 | } |
| 4430 | |
| 4431 | /** |
| 4432 | * Get an icon that goes with the content of this notification. |
| 4433 | */ |
| 4434 | public int getContentIcon() { |
| 4435 | return mContentIcon; |
| 4436 | } |
| 4437 | |
| 4438 | /** |
| 4439 | * Set the gravity that the content icon should have within the notification display. |
| 4440 | * Supported values include {@link android.view.Gravity#START} and |
| 4441 | * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}. |
| 4442 | * @see #setContentIcon |
| 4443 | */ |
| 4444 | public WearableExtender setContentIconGravity(int contentIconGravity) { |
| 4445 | mContentIconGravity = contentIconGravity; |
| 4446 | return this; |
| 4447 | } |
| 4448 | |
| 4449 | /** |
| 4450 | * Get the gravity that the content icon should have within the notification display. |
| 4451 | * Supported values include {@link android.view.Gravity#START} and |
| 4452 | * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}. |
| 4453 | * @see #getContentIcon |
| 4454 | */ |
| 4455 | public int getContentIconGravity() { |
| 4456 | return mContentIconGravity; |
| 4457 | } |
| 4458 | |
| 4459 | /** |
| 4460 | * Set an action from this notification's actions to be clickable with the content of |
Griff Hazen | 14f5799 | 2014-05-26 09:07:14 -0700 | [diff] [blame] | 4461 | * this notification. This action will no longer display separately from the |
| 4462 | * notification's content. |
| 4463 | * |
Griff Hazen | ca48d35 | 2014-05-28 22:37:13 -0700 | [diff] [blame] | 4464 | * <p>For notifications with multiple pages, child pages can also have content actions |
Griff Hazen | 14f5799 | 2014-05-26 09:07:14 -0700 | [diff] [blame] | 4465 | * set, although the list of available actions comes from the main notification and not |
| 4466 | * from the child page's notification. |
| 4467 | * |
| 4468 | * @param actionIndex The index of the action to hoist onto the current notification page. |
| 4469 | * If wearable actions were added to the main notification, this index |
| 4470 | * will apply to that list, otherwise it will apply to the regular |
| 4471 | * actions list. |
Griff Hazen | 61a9e86 | 2014-05-22 16:05:19 -0700 | [diff] [blame] | 4472 | */ |
| 4473 | public WearableExtender setContentAction(int actionIndex) { |
| 4474 | mContentActionIndex = actionIndex; |
| 4475 | return this; |
| 4476 | } |
| 4477 | |
| 4478 | /** |
Griff Hazen | ca48d35 | 2014-05-28 22:37:13 -0700 | [diff] [blame] | 4479 | * Get the index of the notification action, if any, that was specified as being clickable |
| 4480 | * with the content of this notification. This action will no longer display separately |
Griff Hazen | 14f5799 | 2014-05-26 09:07:14 -0700 | [diff] [blame] | 4481 | * from the notification's content. |
Griff Hazen | 61a9e86 | 2014-05-22 16:05:19 -0700 | [diff] [blame] | 4482 | * |
Griff Hazen | ca48d35 | 2014-05-28 22:37:13 -0700 | [diff] [blame] | 4483 | * <p>For notifications with multiple pages, child pages can also have content actions |
Griff Hazen | 14f5799 | 2014-05-26 09:07:14 -0700 | [diff] [blame] | 4484 | * set, although the list of available actions comes from the main notification and not |
| 4485 | * from the child page's notification. |
| 4486 | * |
| 4487 | * <p>If wearable specific actions were added to the main notification, this index will |
| 4488 | * apply to that list, otherwise it will apply to the regular actions list. |
Griff Hazen | ca48d35 | 2014-05-28 22:37:13 -0700 | [diff] [blame] | 4489 | * |
| 4490 | * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected. |
Griff Hazen | 61a9e86 | 2014-05-22 16:05:19 -0700 | [diff] [blame] | 4491 | */ |
| 4492 | public int getContentAction() { |
| 4493 | return mContentActionIndex; |
| 4494 | } |
| 4495 | |
| 4496 | /** |
| 4497 | * Set the gravity that this notification should have within the available viewport space. |
| 4498 | * Supported values include {@link android.view.Gravity#TOP}, |
| 4499 | * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}. |
| 4500 | * The default value is {@link android.view.Gravity#BOTTOM}. |
| 4501 | */ |
| 4502 | public WearableExtender setGravity(int gravity) { |
| 4503 | mGravity = gravity; |
| 4504 | return this; |
| 4505 | } |
| 4506 | |
| 4507 | /** |
| 4508 | * Get the gravity that this notification should have within the available viewport space. |
| 4509 | * Supported values include {@link android.view.Gravity#TOP}, |
| 4510 | * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}. |
| 4511 | * The default value is {@link android.view.Gravity#BOTTOM}. |
| 4512 | */ |
| 4513 | public int getGravity() { |
| 4514 | return mGravity; |
| 4515 | } |
| 4516 | |
| 4517 | /** |
| 4518 | * Set the custom size preset for the display of this notification out of the available |
| 4519 | * presets found in {@link android.app.Notification.WearableExtender}, e.g. |
| 4520 | * {@link #SIZE_LARGE}. |
| 4521 | * <p>Some custom size presets are only applicable for custom display notifications created |
| 4522 | * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the |
| 4523 | * documentation for the preset in question. See also |
| 4524 | * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}. |
| 4525 | */ |
| 4526 | public WearableExtender setCustomSizePreset(int sizePreset) { |
| 4527 | mCustomSizePreset = sizePreset; |
| 4528 | return this; |
| 4529 | } |
| 4530 | |
| 4531 | /** |
| 4532 | * Get the custom size preset for the display of this notification out of the available |
| 4533 | * presets found in {@link android.app.Notification.WearableExtender}, e.g. |
| 4534 | * {@link #SIZE_LARGE}. |
| 4535 | * <p>Some custom size presets are only applicable for custom display notifications created |
| 4536 | * using {@link #setDisplayIntent}. Check the documentation for the preset in question. |
| 4537 | * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}. |
| 4538 | */ |
| 4539 | public int getCustomSizePreset() { |
| 4540 | return mCustomSizePreset; |
| 4541 | } |
| 4542 | |
| 4543 | /** |
| 4544 | * Set the custom height in pixels for the display of this notification's content. |
| 4545 | * <p>This option is only available for custom display notifications created |
| 4546 | * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also |
| 4547 | * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and |
| 4548 | * {@link #getCustomContentHeight}. |
| 4549 | */ |
| 4550 | public WearableExtender setCustomContentHeight(int height) { |
| 4551 | mCustomContentHeight = height; |
| 4552 | return this; |
| 4553 | } |
| 4554 | |
| 4555 | /** |
| 4556 | * Get the custom height in pixels for the display of this notification's content. |
| 4557 | * <p>This option is only available for custom display notifications created |
| 4558 | * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and |
| 4559 | * {@link #setCustomContentHeight}. |
| 4560 | */ |
| 4561 | public int getCustomContentHeight() { |
| 4562 | return mCustomContentHeight; |
| 4563 | } |
| 4564 | |
| 4565 | /** |
| 4566 | * Set whether the scrolling position for the contents of this notification should start |
| 4567 | * at the bottom of the contents instead of the top when the contents are too long to |
| 4568 | * display within the screen. Default is false (start scroll at the top). |
| 4569 | */ |
| 4570 | public WearableExtender setStartScrollBottom(boolean startScrollBottom) { |
| 4571 | setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom); |
| 4572 | return this; |
| 4573 | } |
| 4574 | |
| 4575 | /** |
| 4576 | * Get whether the scrolling position for the contents of this notification should start |
| 4577 | * at the bottom of the contents instead of the top when the contents are too long to |
| 4578 | * display within the screen. Default is false (start scroll at the top). |
| 4579 | */ |
| 4580 | public boolean getStartScrollBottom() { |
| 4581 | return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0; |
| 4582 | } |
| 4583 | |
| 4584 | /** |
| 4585 | * Set whether the content intent is available when the wearable device is not connected |
| 4586 | * to a companion device. The user can still trigger this intent when the wearable device |
| 4587 | * is offline, but a visual hint will indicate that the content intent may not be available. |
| 4588 | * Defaults to true. |
| 4589 | */ |
| 4590 | public WearableExtender setContentIntentAvailableOffline( |
| 4591 | boolean contentIntentAvailableOffline) { |
| 4592 | setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline); |
| 4593 | return this; |
| 4594 | } |
| 4595 | |
| 4596 | /** |
| 4597 | * Get whether the content intent is available when the wearable device is not connected |
| 4598 | * to a companion device. The user can still trigger this intent when the wearable device |
| 4599 | * is offline, but a visual hint will indicate that the content intent may not be available. |
| 4600 | * Defaults to true. |
| 4601 | */ |
| 4602 | public boolean getContentIntentAvailableOffline() { |
| 4603 | return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0; |
| 4604 | } |
| 4605 | |
| 4606 | /** |
| 4607 | * Set a hint that this notification's icon should not be displayed. |
| 4608 | * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise. |
| 4609 | * @return this object for method chaining |
| 4610 | */ |
| 4611 | public WearableExtender setHintHideIcon(boolean hintHideIcon) { |
| 4612 | setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon); |
| 4613 | return this; |
| 4614 | } |
| 4615 | |
| 4616 | /** |
| 4617 | * Get a hint that this notification's icon should not be displayed. |
| 4618 | * @return {@code true} if this icon should not be displayed, false otherwise. |
| 4619 | * The default value is {@code false} if this was never set. |
| 4620 | */ |
| 4621 | public boolean getHintHideIcon() { |
| 4622 | return (mFlags & FLAG_HINT_HIDE_ICON) != 0; |
| 4623 | } |
| 4624 | |
| 4625 | /** |
| 4626 | * Set a visual hint that only the background image of this notification should be |
| 4627 | * displayed, and other semantic content should be hidden. This hint is only applicable |
| 4628 | * to sub-pages added using {@link #addPage}. |
| 4629 | */ |
| 4630 | public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) { |
| 4631 | setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly); |
| 4632 | return this; |
| 4633 | } |
| 4634 | |
| 4635 | /** |
| 4636 | * Get a visual hint that only the background image of this notification should be |
| 4637 | * displayed, and other semantic content should be hidden. This hint is only applicable |
| 4638 | * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}. |
| 4639 | */ |
| 4640 | public boolean getHintShowBackgroundOnly() { |
| 4641 | return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0; |
| 4642 | } |
| 4643 | |
| 4644 | private void setFlag(int mask, boolean value) { |
| 4645 | if (value) { |
| 4646 | mFlags |= mask; |
| 4647 | } else { |
| 4648 | mFlags &= ~mask; |
| 4649 | } |
| 4650 | } |
| 4651 | } |
| 4652 | |
| 4653 | /** |
| 4654 | * Get an array of Notification objects from a parcelable array bundle field. |
| 4655 | * Update the bundle to have a typed array so fetches in the future don't need |
| 4656 | * to do an array copy. |
| 4657 | */ |
| 4658 | private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) { |
| 4659 | Parcelable[] array = bundle.getParcelableArray(key); |
| 4660 | if (array instanceof Notification[] || array == null) { |
| 4661 | return (Notification[]) array; |
| 4662 | } |
| 4663 | Notification[] typedArray = Arrays.copyOf(array, array.length, |
| 4664 | Notification[].class); |
| 4665 | bundle.putParcelableArray(key, typedArray); |
| 4666 | return typedArray; |
| 4667 | } |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 4668 | |
| 4669 | private static class BuilderRemoteViews extends RemoteViews { |
| 4670 | public BuilderRemoteViews(Parcel parcel) { |
| 4671 | super(parcel); |
| 4672 | } |
| 4673 | |
Svet Ganov | 0da85b6 | 2014-08-06 14:11:37 -0700 | [diff] [blame] | 4674 | public BuilderRemoteViews(String packageName, int userId, int layoutId) { |
| 4675 | super(packageName, userId, layoutId); |
Christoph Studer | 4600f9b | 2014-07-22 22:44:43 +0200 | [diff] [blame] | 4676 | } |
| 4677 | |
| 4678 | @Override |
| 4679 | public BuilderRemoteViews clone() { |
| 4680 | Parcel p = Parcel.obtain(); |
| 4681 | writeToParcel(p, 0); |
| 4682 | p.setDataPosition(0); |
| 4683 | BuilderRemoteViews brv = new BuilderRemoteViews(p); |
| 4684 | p.recycle(); |
| 4685 | return brv; |
| 4686 | } |
| 4687 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4688 | } |