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