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