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