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