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