blob: 6d7486b5311e9056652685d78b6941aabc0a2dfe [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
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
17package android.app;
18
Tor Norbye80756e32015-03-02 09:39:27 -080019import android.annotation.ColorInt;
Tor Norbye7b9c9122013-05-30 16:48:33 -070020import android.annotation.DrawableRes;
Tor Norbyed9273d62013-05-30 15:59:53 -070021import android.annotation.IntDef;
Alex Hillsfd590442016-10-07 09:52:44 -040022import android.annotation.NonNull;
Daniel Sandler01df1c62014-06-09 10:54:01 -040023import android.annotation.SdkConstant;
24import android.annotation.SdkConstant.SdkConstantType;
Julia Reynoldse46bb372016-03-17 11:05:58 -040025import android.annotation.SystemApi;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080026import android.content.Context;
27import android.content.Intent;
Kenny Guy77320062014-08-27 21:37:15 +010028import android.content.pm.ApplicationInfo;
Dan Sandler732bd6c2016-04-12 14:20:32 -040029import android.content.pm.PackageManager;
Christoph Studer4600f9b2014-07-22 22:44:43 +020030import android.content.pm.PackageManager.NameNotFoundException;
Julia Reynolds13d898c2017-02-02 12:22:05 -050031import android.content.pm.ShortcutInfo;
Jorim Jaggief72a192014-08-26 21:57:46 +020032import android.content.res.ColorStateList;
Joe Onoratoef1e7762010-09-17 18:38:38 -040033import android.graphics.Bitmap;
Kenny Guy8a0101b2014-05-08 23:34:12 +010034import android.graphics.Canvas;
Adrian Roosc1a80b02016-04-05 14:54:55 -070035import android.graphics.Color;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +010036import android.graphics.PorterDuff;
Kenny Guy8a0101b2014-05-08 23:34:12 +010037import android.graphics.drawable.Drawable;
Dan Sandlerd63f9322015-05-06 15:18:49 -040038import android.graphics.drawable.Icon;
John Spurlockc0650f022014-07-19 13:22:39 -040039import android.media.AudioAttributes;
Jeff Sharkey098d5802012-04-26 17:30:34 -070040import android.media.AudioManager;
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -080041import android.media.PlayerBase;
Jeff Browndba34ba2014-06-24 20:46:03 -070042import android.media.session.MediaSession;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.net.Uri;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040044import android.os.BadParcelableException;
Christoph Studer239f8352014-08-25 15:13:18 +020045import android.os.Build;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050046import android.os.Bundle;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.Parcel;
48import android.os.Parcelable;
Daniel Sandlera2985ed2012-04-03 16:42:00 -040049import android.os.SystemClock;
Selim Cinek6743c0b2017-01-18 18:24:01 -080050import android.os.SystemProperties;
Jeff Sharkey6d515712012-09-20 16:06:08 -070051import android.os.UserHandle;
Adrian Roosc1a80b02016-04-05 14:54:55 -070052import android.text.BidiFormatter;
Selim Cinek60a54252016-02-26 17:03:25 -080053import android.text.SpannableStringBuilder;
54import android.text.Spanned;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055import android.text.TextUtils;
Selim Cinek60a54252016-02-26 17:03:25 -080056import android.text.style.AbsoluteSizeSpan;
Selim Cinek981962e2016-07-20 20:41:58 -070057import android.text.style.BackgroundColorSpan;
Selim Cinek89991a22016-03-07 19:51:54 -080058import android.text.style.CharacterStyle;
Selim Cinek981962e2016-07-20 20:41:58 -070059import android.text.style.ForegroundColorSpan;
Selim Cinek60a54252016-02-26 17:03:25 -080060import android.text.style.RelativeSizeSpan;
61import android.text.style.TextAppearanceSpan;
Svet Ganovddb94882016-06-23 19:55:24 -070062import android.util.ArraySet;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -040063import android.util.Log;
Dan Sandler50128532015-12-08 15:42:41 -050064import android.util.SparseArray;
Griff Hazen61a9e862014-05-22 16:05:19 -070065import android.view.Gravity;
Selim Cinekea4bef72015-12-02 15:51:10 -080066import android.view.NotificationHeaderView;
Joe Onorato8595a3d2010-11-19 18:12:07 -080067import android.view.View;
Selim Cinek954cc232016-05-20 13:29:23 -070068import android.view.ViewGroup;
Jeff Sharkey1c400132011-08-05 14:50:13 -070069import android.widget.ProgressBar;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070import android.widget.RemoteViews;
71
Griff Hazen959591e2014-05-15 22:26:18 -070072import com.android.internal.R;
Svet Ganovddb94882016-06-23 19:55:24 -070073import com.android.internal.util.ArrayUtils;
Griff Hazenc091ba82014-05-16 10:13:26 -070074import com.android.internal.util.NotificationColorUtil;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -080075import com.android.internal.util.Preconditions;
Griff Hazen959591e2014-05-15 22:26:18 -070076
Tor Norbyed9273d62013-05-30 15:59:53 -070077import java.lang.annotation.Retention;
78import java.lang.annotation.RetentionPolicy;
Christoph Studer4600f9b2014-07-22 22:44:43 +020079import java.lang.reflect.Constructor;
Daniel Sandler2561b0b2012-02-13 21:04:12 -050080import java.util.ArrayList;
Griff Hazen61a9e862014-05-22 16:05:19 -070081import java.util.Arrays;
Griff Hazen5cadc3b2014-05-20 09:55:39 -070082import java.util.Collections;
Griff Hazen61a9e862014-05-22 16:05:19 -070083import java.util.List;
Dan Sandler50128532015-12-08 15:42:41 -050084import java.util.Set;
Joe Onorato561d3852010-11-20 18:09:34 -080085
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086/**
87 * A class that represents how a persistent notification is to be presented to
88 * the user using the {@link android.app.NotificationManager}.
89 *
Joe Onoratocb109a02011-01-18 17:57:41 -080090 * <p>The {@link Notification.Builder Notification.Builder} has been added to make it
91 * easier to construct Notifications.</p>
92 *
Joe Fernandez558459f2011-10-13 16:47:36 -070093 * <div class="special reference">
94 * <h3>Developer Guides</h3>
95 * <p>For a guide to creating notifications, read the
96 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html">Status Bar Notifications</a>
97 * developer guide.</p>
98 * </div>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 */
100public class Notification implements Parcelable
101{
Daniel Sandlerdcbaf662013-04-26 16:23:09 -0400102 private static final String TAG = "Notification";
103
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800104 /**
Daniel Sandler01df1c62014-06-09 10:54:01 -0400105 * An activity that provides a user interface for adjusting notification preferences for its
Julia Reynolds3aedded2017-03-31 14:42:09 -0400106 * containing application.
Daniel Sandler01df1c62014-06-09 10:54:01 -0400107 */
108 @SdkConstant(SdkConstantType.INTENT_CATEGORY)
109 public static final String INTENT_CATEGORY_NOTIFICATION_PREFERENCES
110 = "android.intent.category.NOTIFICATION_PREFERENCES";
111
112 /**
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500113 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
114 * contain a {@link NotificationChannel#getId() channel id} that can be used to narrow down
Julia Reynolds3aedded2017-03-31 14:42:09 -0400115 * what settings should be shown in the target app.
Julia Reynolds2619b5e2017-02-09 09:58:15 -0500116 */
117 public static final String EXTRA_CHANNEL_ID = "android.intent.extra.CHANNEL_ID";
118
119 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -0400120 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
121 * contain the tag provided to {@link NotificationManager#notify(String, int, Notification)}
122 * that can be used to narrow down what settings should be shown in the target app.
123 */
124 public static final String EXTRA_NOTIFICATION_TAG = "android.intent.extra.NOTIFICATION_TAG";
125
126 /**
127 * Optional extra for {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}. If provided, will
128 * contain the id provided to {@link NotificationManager#notify(String, int, Notification)}
129 * that can be used to narrow down what settings should be shown in the target app.
130 */
131 public static final String EXTRA_NOTIFICATION_ID = "android.intent.extra.NOTIFICATION_ID";
132
133 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 * Use all default values (where applicable).
135 */
136 public static final int DEFAULT_ALL = ~0;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138 /**
139 * Use the default notification sound. This will ignore any given
140 * {@link #sound}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500141 *
Chris Wren47c20a12014-06-18 17:27:29 -0400142 * <p>
143 * A notification that is noisy is more likely to be presented as a heads-up notification.
144 * </p>
145 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500147 */
148
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 public static final int DEFAULT_SOUND = 1;
150
151 /**
152 * Use the default notification vibrate. This will ignore any given
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500153 * {@link #vibrate}. Using phone vibration requires the
Scott Mainb8b36452009-04-26 15:50:49 -0700154 * {@link android.Manifest.permission#VIBRATE VIBRATE} permission.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500155 *
Chris Wren47c20a12014-06-18 17:27:29 -0400156 * <p>
157 * A notification that vibrates is more likely to be presented as a heads-up notification.
158 * </p>
159 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800160 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500161 */
162
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 public static final int DEFAULT_VIBRATE = 2;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500164
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 /**
166 * Use the default notification lights. This will ignore the
167 * {@link #FLAG_SHOW_LIGHTS} bit, and {@link #ledARGB}, {@link #ledOffMS}, or
168 * {@link #ledOnMS}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500169 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800170 * @see #defaults
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500171 */
172
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 public static final int DEFAULT_LIGHTS = 4;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500174
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 /**
Christoph Studer535ec612014-09-03 15:47:47 +0200176 * Maximum length of CharSequences accepted by Builder and friends.
177 *
178 * <p>
179 * Avoids spamming the system with overly large strings such as full e-mails.
180 */
181 private static final int MAX_CHARSEQUENCE_LENGTH = 5 * 1024;
182
183 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800184 * Maximum entries of reply text that are accepted by Builder and friends.
185 */
186 private static final int MAX_REPLY_HISTORY = 5;
187
188 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500189 * A timestamp related to this notification, in milliseconds since the epoch.
Joe Malin8d40d042012-11-05 11:36:40 -0800190 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500191 * Default value: {@link System#currentTimeMillis() Now}.
192 *
193 * Choose a timestamp that will be most relevant to the user. For most finite events, this
194 * corresponds to the time the event happened (or will happen, in the case of events that have
195 * yet to occur but about which the user is being informed). Indefinite events should be
Joe Malin8d40d042012-11-05 11:36:40 -0800196 * timestamped according to when the activity began.
197 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500198 * Some examples:
Joe Malin8d40d042012-11-05 11:36:40 -0800199 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500200 * <ul>
201 * <li>Notification of a new chat message should be stamped when the message was received.</li>
202 * <li>Notification of an ongoing file download (with a progress bar, for example) should be stamped when the download started.</li>
203 * <li>Notification of a completed file download should be stamped when the download finished.</li>
204 * <li>Notification of an upcoming meeting should be stamped with the time the meeting will begin (that is, in the future).</li>
205 * <li>Notification of an ongoing stopwatch (increasing timer) should be stamped with the watch's start time.
206 * <li>Notification of an ongoing countdown timer should be stamped with the timer's end time.
Joe Malin8d40d042012-11-05 11:36:40 -0800207 * </ul>
208 *
Selim Cinek0ff1ce602016-04-05 18:27:16 -0700209 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not shown
210 * anymore by default and must be opted into by using
211 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 */
213 public long when;
214
215 /**
Selim Cinekb85f36fd2016-04-20 18:46:36 -0700216 * The creation time of the notification
217 */
218 private long creationTime;
219
220 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 * The resource id of a drawable to use as the icon in the status bar.
Dan Sandler86647982015-05-13 23:41:13 -0400222 *
223 * @deprecated Use {@link Builder#setSmallIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 */
Dan Sandler86647982015-05-13 23:41:13 -0400225 @Deprecated
Tor Norbye7b9c9122013-05-30 16:48:33 -0700226 @DrawableRes
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 public int icon;
228
229 /**
Joe Onorato46439ce2010-11-19 13:56:21 -0800230 * If the icon in the status bar is to have more than one level, you can set this. Otherwise,
231 * leave it at its default value of 0.
232 *
233 * @see android.widget.ImageView#setImageLevel
Griff Hazen959591e2014-05-15 22:26:18 -0700234 * @see android.graphics.drawable.Drawable#setLevel
Joe Onorato46439ce2010-11-19 13:56:21 -0800235 */
236 public int iconLevel;
237
238 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500239 * The number of events that this notification represents. For example, in a new mail
240 * notification, this could be the number of unread messages.
Joe Malin8d40d042012-11-05 11:36:40 -0800241 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500242 * The system may or may not use this field to modify the appearance of the notification. For
243 * example, before {@link android.os.Build.VERSION_CODES#HONEYCOMB}, this number was
244 * superimposed over the icon in the status bar. Starting with
245 * {@link android.os.Build.VERSION_CODES#HONEYCOMB}, the template used by
246 * {@link Notification.Builder} has displayed the number in the expanded notification view.
Julia Reynolds13d898c2017-02-02 12:22:05 -0500247 * Starting with {@link android.os.Build.VERSION_CODES#O}, the number may be displayed as a
248 * badge icon in Launchers that support badging.
Joe Malin8d40d042012-11-05 11:36:40 -0800249 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800250 */
Julia Reynolds13d898c2017-02-02 12:22:05 -0500251 public int number = 1;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800252
253 /**
254 * The intent to execute when the expanded status entry is clicked. If
255 * this is an activity, it must include the
256 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -0800257 * that you take care of task management as described in the
258 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
Dianne Hackborn6ceca582012-01-10 15:24:26 -0800259 * Stack</a> document. In particular, make sure to read the notification section
260 * <a href="{@docRoot}guide/topics/ui/notifiers/notifications.html#HandlingNotifications">Handling
261 * Notifications</a> for the correct ways to launch an application from a
262 * notification.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800263 */
264 public PendingIntent contentIntent;
265
266 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500267 * The intent to execute when the notification is explicitly dismissed by the user, either with
268 * the "Clear All" button or by swiping it away individually.
269 *
270 * This probably shouldn't be launching an activity since several of those will be sent
271 * at the same time.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 */
273 public PendingIntent deleteIntent;
274
275 /**
Dianne Hackborn170bae72010-09-03 15:14:28 -0700276 * An intent to launch instead of posting the notification to the status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -0800277 *
Chris Wren47c20a12014-06-18 17:27:29 -0400278 * <p>
279 * The system UI may choose to display a heads-up notification, instead of
280 * launching this intent, while the user is using the device.
281 * </p>
282 *
Joe Onoratocb109a02011-01-18 17:57:41 -0800283 * @see Notification.Builder#setFullScreenIntent
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400284 */
285 public PendingIntent fullScreenIntent;
286
287 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400288 * Text that summarizes this notification for accessibility services.
289 *
290 * As of the L release, this text is no longer shown on screen, but it is still useful to
291 * accessibility services (where it serves as an audible announcement of the notification's
292 * appearance).
Joe Onoratoef1e7762010-09-17 18:38:38 -0400293 *
Joe Onorato46439ce2010-11-19 13:56:21 -0800294 * @see #tickerView
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 */
296 public CharSequence tickerText;
297
298 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400299 * Formerly, a view showing the {@link #tickerText}.
300 *
301 * No longer displayed in the status bar as of API 21.
Joe Onoratoef1e7762010-09-17 18:38:38 -0400302 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -0400303 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800304 public RemoteViews tickerView;
Joe Onoratoef1e7762010-09-17 18:38:38 -0400305
306 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400307 * The view that will represent this notification in the notification list (which is pulled
308 * down from the status bar).
309 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500310 * As of N, this field may be null. The notification view is determined by the inputs
311 * to {@link Notification.Builder}; a custom RemoteViews can optionally be
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400312 * supplied with {@link Notification.Builder#setCustomContentView(RemoteViews)}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400314 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800315 public RemoteViews contentView;
316
Daniel Sandlera0a938c2012-03-15 08:42:37 -0400317 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -0400318 * A large-format version of {@link #contentView}, giving the Notification an
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400319 * opportunity to show more detail. The system UI may choose to show this
320 * instead of the normal content view at its discretion.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400321 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500322 * As of N, this field may be null. The expanded notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400323 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
324 * supplied with {@link Notification.Builder#setCustomBigContentView(RemoteViews)}.
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400325 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400326 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400327 public RemoteViews bigContentView;
328
Chris Wren8fd39ec2014-02-27 17:43:26 -0500329
330 /**
Chris Wren47c20a12014-06-18 17:27:29 -0400331 * A medium-format version of {@link #contentView}, providing the Notification an
332 * opportunity to add action buttons to contentView. At its discretion, the system UI may
333 * choose to show this as a heads-up notification, which will pop up so the user can see
334 * it without leaving their current activity.
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400335 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -0500336 * As of N, this field may be null. The heads-up notification view is determined by the
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400337 * inputs to {@link Notification.Builder}; a custom RemoteViews can optionally be
338 * supplied with {@link Notification.Builder#setCustomHeadsUpContentView(RemoteViews)}.
Chris Wren8fd39ec2014-02-27 17:43:26 -0500339 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -0400340 @Deprecated
Chris Wren8fd39ec2014-02-27 17:43:26 -0500341 public RemoteViews headsUpContentView;
342
Daniel Sandlerf3b73432012-03-27 15:01:25 -0400343 /**
Dan Sandler86647982015-05-13 23:41:13 -0400344 * A large bitmap to be shown in the notification content area.
345 *
346 * @deprecated Use {@link Builder#setLargeIcon(Icon)} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800347 */
Dan Sandler86647982015-05-13 23:41:13 -0400348 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -0800349 public Bitmap largeIcon;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800350
351 /**
352 * The sound to play.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500353 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800354 * <p>
Chris Wren47c20a12014-06-18 17:27:29 -0400355 * A notification that is noisy is more likely to be presented as a heads-up notification.
356 * </p>
357 *
358 * <p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500359 * To play the default notification sound, see {@link #defaults}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800360 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500361 * @deprecated use {@link NotificationChannel#getSound()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800362 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500363 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800364 public Uri sound;
365
366 /**
367 * Use this constant as the value for audioStreamType to request that
368 * the default stream type for notifications be used. Currently the
Jeff Sharkey098d5802012-04-26 17:30:34 -0700369 * default stream type is {@link AudioManager#STREAM_NOTIFICATION}.
John Spurlockc0650f022014-07-19 13:22:39 -0400370 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500371 * @deprecated Use {@link NotificationChannel#getAudioAttributes()} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800372 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700373 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800374 public static final int STREAM_DEFAULT = -1;
375
376 /**
377 * The audio stream type to use when playing the sound.
378 * Should be one of the STREAM_ constants from
379 * {@link android.media.AudioManager}.
John Spurlockc0650f022014-07-19 13:22:39 -0400380 *
381 * @deprecated Use {@link #audioAttributes} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800382 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -0700383 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800384 public int audioStreamType = STREAM_DEFAULT;
385
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800386 /**
John Spurlockc0650f022014-07-19 13:22:39 -0400387 * The default value of {@link #audioAttributes}.
388 */
389 public static final AudioAttributes AUDIO_ATTRIBUTES_DEFAULT = new AudioAttributes.Builder()
390 .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
391 .setUsage(AudioAttributes.USAGE_NOTIFICATION)
392 .build();
393
394 /**
395 * The {@link AudioAttributes audio attributes} to use when playing the sound.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500396 *
397 * @deprecated use {@link NotificationChannel#getAudioAttributes()} instead.
John Spurlockc0650f022014-07-19 13:22:39 -0400398 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500399 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -0400400 public AudioAttributes audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
401
402 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500403 * The pattern with which to vibrate.
404 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800405 * <p>
406 * To vibrate the default pattern, see {@link #defaults}.
407 * </p>
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500408 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 * @see android.os.Vibrator#vibrate(long[],int)
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500410 * @deprecated use {@link NotificationChannel#getVibrationPattern()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800411 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500412 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800413 public long[] vibrate;
414
415 /**
416 * The color of the led. The hardware will do its best approximation.
417 *
418 * @see #FLAG_SHOW_LIGHTS
419 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500420 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800421 */
Tor Norbye80756e32015-03-02 09:39:27 -0800422 @ColorInt
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500423 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800424 public int ledARGB;
425
426 /**
427 * The number of milliseconds for the LED to be on while it's flashing.
428 * The hardware will do its best approximation.
429 *
430 * @see #FLAG_SHOW_LIGHTS
431 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500432 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800433 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500434 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800435 public int ledOnMS;
436
437 /**
438 * The number of milliseconds for the LED to be off while it's flashing.
439 * The hardware will do its best approximation.
440 *
441 * @see #FLAG_SHOW_LIGHTS
442 * @see #flags
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500443 *
444 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800445 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500446 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447 public int ledOffMS;
448
449 /**
450 * Specifies which values should be taken from the defaults.
451 * <p>
452 * To set, OR the desired from {@link #DEFAULT_SOUND},
453 * {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}. For all default
454 * values, use {@link #DEFAULT_ALL}.
455 * </p>
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500456 *
457 * @deprecated use {@link NotificationChannel#getSound()} and
458 * {@link NotificationChannel#shouldShowLights()} and
459 * {@link NotificationChannel#shouldVibrate()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800460 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500461 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800462 public int defaults;
463
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 /**
465 * Bit to be bitwise-ored into the {@link #flags} field that should be
466 * set if you want the LED on for this notification.
467 * <ul>
468 * <li>To turn the LED off, pass 0 in the alpha channel for colorARGB
469 * or 0 for both ledOnMS and ledOffMS.</li>
470 * <li>To turn the LED on, pass 1 for ledOnMS and 0 for ledOffMS.</li>
471 * <li>To flash the LED, pass the number of milliseconds that it should
472 * be on and off to ledOnMS and ledOffMS.</li>
473 * </ul>
474 * <p>
475 * Since hardware varies, you are not guaranteed that any of the values
476 * you pass are honored exactly. Use the system defaults (TODO) if possible
477 * because they will be set to values that work on any given hardware.
478 * <p>
479 * The alpha channel must be set for forward compatibility.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500480 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500481 * @deprecated use {@link NotificationChannel#shouldShowLights()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800482 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500483 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800484 public static final int FLAG_SHOW_LIGHTS = 0x00000001;
485
486 /**
487 * Bit to be bitwise-ored into the {@link #flags} field that should be
488 * set if this notification is in reference to something that is ongoing,
489 * like a phone call. It should not be set if this notification is in
490 * reference to something that happened at a particular point in time,
491 * like a missed phone call.
492 */
493 public static final int FLAG_ONGOING_EVENT = 0x00000002;
494
495 /**
496 * Bit to be bitwise-ored into the {@link #flags} field that if set,
Scott Mainb8b36452009-04-26 15:50:49 -0700497 * the audio will be repeated until the notification is
498 * cancelled or the notification window is opened.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800499 */
500 public static final int FLAG_INSISTENT = 0x00000004;
501
502 /**
503 * Bit to be bitwise-ored into the {@link #flags} field that should be
Griff Hazen293977b2014-04-28 08:37:20 -0700504 * set if you would only like the sound, vibrate and ticker to be played
505 * if the notification was not already showing.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800506 */
507 public static final int FLAG_ONLY_ALERT_ONCE = 0x00000008;
508
509 /**
510 * Bit to be bitwise-ored into the {@link #flags} field that should be
511 * set if the notification should be canceled when it is clicked by the
Daniel Sandler8aa9ae62012-12-04 23:31:47 -0500512 * user.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800513 */
514 public static final int FLAG_AUTO_CANCEL = 0x00000010;
515
516 /**
517 * Bit to be bitwise-ored into the {@link #flags} field that should be
518 * set if the notification should not be canceled when the user clicks
519 * the Clear all button.
520 */
521 public static final int FLAG_NO_CLEAR = 0x00000020;
522
Dianne Hackbornd8a43f62009-08-17 23:33:56 -0700523 /**
524 * Bit to be bitwise-ored into the {@link #flags} field that should be
525 * set if this notification represents a currently running service. This
526 * will normally be set for you by {@link Service#startForeground}.
527 */
528 public static final int FLAG_FOREGROUND_SERVICE = 0x00000040;
529
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400530 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500531 * Obsolete flag indicating high-priority notifications; use the priority field instead.
Joe Malin8d40d042012-11-05 11:36:40 -0800532 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500533 * @deprecated Use {@link #priority} with a positive value.
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400534 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700535 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500536 public static final int FLAG_HIGH_PRIORITY = 0x00000080;
Daniel Sandlere46cbd32010-06-17 10:35:26 -0400537
Griff Hazendfcb0802014-02-11 12:00:00 -0800538 /**
539 * Bit to be bitswise-ored into the {@link #flags} field that should be
540 * set if this notification is relevant to the current device only
541 * and it is not recommended that it bridge to other devices.
542 */
543 public static final int FLAG_LOCAL_ONLY = 0x00000100;
544
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700545 /**
546 * Bit to be bitswise-ored into the {@link #flags} field that should be
547 * set if this notification is the group summary for a group of notifications.
548 * Grouped notifications may display in a cluster or stack on devices which
549 * support such rendering. Requires a group key also be set using {@link Builder#setGroup}.
550 */
551 public static final int FLAG_GROUP_SUMMARY = 0x00000200;
552
Julia Reynoldse46bb372016-03-17 11:05:58 -0400553 /**
554 * Bit to be bitswise-ored into the {@link #flags} field that should be
555 * set if this notification is the group summary for an auto-group of notifications.
556 *
557 * @hide
558 */
559 @SystemApi
560 public static final int FLAG_AUTOGROUP_SUMMARY = 0x00000400;
561
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 public int flags;
563
Tor Norbyed9273d62013-05-30 15:59:53 -0700564 /** @hide */
565 @IntDef({PRIORITY_DEFAULT,PRIORITY_LOW,PRIORITY_MIN,PRIORITY_HIGH,PRIORITY_MAX})
566 @Retention(RetentionPolicy.SOURCE)
567 public @interface Priority {}
568
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500570 * Default notification {@link #priority}. If your application does not prioritize its own
571 * notifications, use this value for all notifications.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500572 *
573 * @deprecated use {@link NotificationManager#IMPORTANCE_DEFAULT} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500574 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500575 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500576 public static final int PRIORITY_DEFAULT = 0;
577
578 /**
579 * Lower {@link #priority}, for items that are less important. The UI may choose to show these
580 * items smaller, or at a different position in the list, compared with your app's
581 * {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500582 *
583 * @deprecated use {@link NotificationManager#IMPORTANCE_LOW} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500584 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500585 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500586 public static final int PRIORITY_LOW = -1;
587
588 /**
589 * Lowest {@link #priority}; these items might not be shown to the user except under special
590 * circumstances, such as detailed notification logs.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500591 *
592 * @deprecated use {@link NotificationManager#IMPORTANCE_MIN} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500593 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500594 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500595 public static final int PRIORITY_MIN = -2;
596
597 /**
598 * Higher {@link #priority}, for more important notifications or alerts. The UI may choose to
599 * show these items larger, or at a different position in notification lists, compared with
600 * your app's {@link #PRIORITY_DEFAULT} items.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500601 *
602 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500603 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500604 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500605 public static final int PRIORITY_HIGH = 1;
606
607 /**
608 * Highest {@link #priority}, for your application's most important items that require the
609 * user's prompt attention or input.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500610 *
611 * @deprecated use {@link NotificationManager#IMPORTANCE_HIGH} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500612 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500613 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500614 public static final int PRIORITY_MAX = 2;
615
616 /**
617 * Relative priority for this notification.
Joe Malin8d40d042012-11-05 11:36:40 -0800618 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500619 * Priority is an indication of how much of the user's valuable attention should be consumed by
620 * this notification. Low-priority notifications may be hidden from the user in certain
621 * situations, while the user might be interrupted for a higher-priority notification. The
Daniel Sandler6738eee2012-11-16 12:03:32 -0500622 * system will make a determination about how to interpret this priority when presenting
623 * the notification.
Chris Wren47c20a12014-06-18 17:27:29 -0400624 *
625 * <p>
626 * A notification that is at least {@link #PRIORITY_HIGH} is more likely to be presented
627 * as a heads-up notification.
628 * </p>
629 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500630 * @deprecated use {@link NotificationChannel#getImportance()} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500631 */
Tor Norbyed9273d62013-05-30 15:59:53 -0700632 @Priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -0500633 @Deprecated
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500634 public int priority;
Joe Malin8d40d042012-11-05 11:36:40 -0800635
Dan Sandler26e81cf2014-05-06 10:01:27 -0400636 /**
637 * Accent color (an ARGB integer like the constants in {@link android.graphics.Color})
638 * to be applied by the standard Style templates when presenting this notification.
639 *
640 * The current template design constructs a colorful header image by overlaying the
641 * {@link #icon} image (stenciled in white) atop a field of this color. Alpha components are
642 * ignored.
643 */
Tor Norbye80756e32015-03-02 09:39:27 -0800644 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400645 public int color = COLOR_DEFAULT;
646
647 /**
648 * Special value of {@link #color} telling the system not to decorate this notification with
649 * any special color but instead use default colors when presenting this notification.
650 */
Tor Norbye80756e32015-03-02 09:39:27 -0800651 @ColorInt
Dan Sandler26e81cf2014-05-06 10:01:27 -0400652 public static final int COLOR_DEFAULT = 0; // AKA Color.TRANSPARENT
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600653
654 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -0800655 * Special value of {@link #color} used as a place holder for an invalid color.
Selim Cinek99104832017-01-25 14:47:33 -0800656 * @hide
Adrian Roos4ff3b122016-02-01 12:26:13 -0800657 */
658 @ColorInt
Selim Cinek99104832017-01-25 14:47:33 -0800659 public static final int COLOR_INVALID = 1;
Adrian Roos4ff3b122016-02-01 12:26:13 -0800660
661 /**
Adrian Roosc1a80b02016-04-05 14:54:55 -0700662 * Sphere of visibility of this notification, which affects how and when the SystemUI reveals
663 * the notification's presence and contents in untrusted situations (namely, on the secure
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600664 * lockscreen).
665 *
666 * The default level, {@link #VISIBILITY_PRIVATE}, behaves exactly as notifications have always
667 * done on Android: The notification's {@link #icon} and {@link #tickerText} (if available) are
668 * shown in all situations, but the contents are only available if the device is unlocked for
669 * the appropriate user.
670 *
671 * A more permissive policy can be expressed by {@link #VISIBILITY_PUBLIC}; such a notification
672 * can be read even in an "insecure" context (that is, above a secure lockscreen).
673 * To modify the public version of this notification—for example, to redact some portions—see
674 * {@link Builder#setPublicVersion(Notification)}.
675 *
676 * Finally, a notification can be made {@link #VISIBILITY_SECRET}, which will suppress its icon
677 * and ticker until the user has bypassed the lockscreen.
678 */
679 public int visibility;
680
Griff Hazenfc3922d2014-08-20 11:56:44 -0700681 /**
682 * Notification visibility: Show this notification in its entirety on all lockscreens.
683 *
684 * {@see #visibility}
685 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600686 public static final int VISIBILITY_PUBLIC = 1;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700687
688 /**
689 * Notification visibility: Show this notification on all lockscreens, but conceal sensitive or
690 * private information on secure lockscreens.
691 *
692 * {@see #visibility}
693 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600694 public static final int VISIBILITY_PRIVATE = 0;
Griff Hazenfc3922d2014-08-20 11:56:44 -0700695
696 /**
697 * Notification visibility: Do not reveal any part of this notification on a secure lockscreen.
698 *
699 * {@see #visibility}
700 */
Dan Sandler0bf2ed82013-12-21 23:33:41 -0600701 public static final int VISIBILITY_SECRET = -1;
702
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500703 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400704 * Notification category: incoming call (voice or video) or similar synchronous communication request.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500705 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400706 public static final String CATEGORY_CALL = "call";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500707
708 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400709 * Notification category: incoming direct message (SMS, instant message, etc.).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500710 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400711 public static final String CATEGORY_MESSAGE = "msg";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500712
713 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400714 * Notification category: asynchronous bulk message (email).
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500715 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400716 public static final String CATEGORY_EMAIL = "email";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500717
718 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400719 * Notification category: calendar event.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500720 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400721 public static final String CATEGORY_EVENT = "event";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500722
723 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400724 * Notification category: promotion or advertisement.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500725 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400726 public static final String CATEGORY_PROMO = "promo";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500727
728 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400729 * Notification category: alarm or timer.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500730 */
John Spurlockfd7f1e02014-03-18 16:41:57 -0400731 public static final String CATEGORY_ALARM = "alarm";
732
733 /**
734 * Notification category: progress of a long-running background operation.
735 */
736 public static final String CATEGORY_PROGRESS = "progress";
737
738 /**
739 * Notification category: social network or sharing update.
740 */
741 public static final String CATEGORY_SOCIAL = "social";
742
743 /**
744 * Notification category: error in background operation or authentication status.
745 */
746 public static final String CATEGORY_ERROR = "err";
747
748 /**
749 * Notification category: media transport control for playback.
750 */
751 public static final String CATEGORY_TRANSPORT = "transport";
752
753 /**
754 * Notification category: system or device status update. Reserved for system use.
755 */
756 public static final String CATEGORY_SYSTEM = "sys";
757
758 /**
759 * Notification category: indication of running background service.
760 */
761 public static final String CATEGORY_SERVICE = "service";
762
763 /**
John Spurlock0a69c8c2014-03-21 13:30:57 -0400764 * Notification category: a specific, timely recommendation for a single thing.
765 * For example, a news app might want to recommend a news story it believes the user will
766 * want to read next.
767 */
768 public static final String CATEGORY_RECOMMENDATION = "recommendation";
769
770 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400771 * Notification category: ongoing information about device or contextual status.
772 */
773 public static final String CATEGORY_STATUS = "status";
774
775 /**
John Spurlock24d3dad2015-04-02 12:24:02 -0400776 * Notification category: user-scheduled reminder.
777 */
778 public static final String CATEGORY_REMINDER = "reminder";
779
780 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400781 * One of the predefined notification categories (see the <code>CATEGORY_*</code> constants)
782 * that best describes this Notification. May be used by the system for ranking and filtering.
783 */
784 public String category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500785
Griff Hazen5cadc3b2014-05-20 09:55:39 -0700786 private String mGroupKey;
787
788 /**
789 * Get the key used to group this notification into a cluster or stack
790 * with other notifications on devices which support such rendering.
791 */
792 public String getGroup() {
793 return mGroupKey;
794 }
795
796 private String mSortKey;
797
798 /**
799 * Get a sort key that orders this notification among other notifications from the
800 * same package. This can be useful if an external sort was already applied and an app
801 * would like to preserve this. Notifications will be sorted lexicographically using this
802 * value, although providing different priorities in addition to providing sort key may
803 * cause this value to be ignored.
804 *
805 * <p>This sort key can also be used to order members of a notification group. See
806 * {@link Builder#setGroup}.
807 *
808 * @see String#compareTo(String)
809 */
810 public String getSortKey() {
811 return mSortKey;
812 }
813
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500814 /**
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400815 * Additional semantic data to be carried around with this Notification.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400816 * <p>
817 * The extras keys defined here are intended to capture the original inputs to {@link Builder}
818 * APIs, and are intended to be used by
819 * {@link android.service.notification.NotificationListenerService} implementations to extract
820 * detailed information from notification objects.
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500821 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400822 public Bundle extras = new Bundle();
823
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400824 /**
Felipe Lemedd85da62016-06-28 11:29:54 -0700825 * All pending intents in the notification as the system needs to be able to access them but
826 * touching the extras bundle in the system process is not safe because the bundle may contain
Svet Ganovddb94882016-06-23 19:55:24 -0700827 * custom parcelable objects.
828 *
829 * @hide
830 */
Felipe Lemedd85da62016-06-28 11:29:54 -0700831 public ArraySet<PendingIntent> allPendingIntents;
Svet Ganovddb94882016-06-23 19:55:24 -0700832
833 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400834 * {@link #extras} key: this is the title of the notification,
835 * as supplied to {@link Builder#setContentTitle(CharSequence)}.
836 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500837 public static final String EXTRA_TITLE = "android.title";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400838
839 /**
840 * {@link #extras} key: this is the title of the notification when shown in expanded form,
841 * e.g. as supplied to {@link BigTextStyle#setBigContentTitle(CharSequence)}.
842 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400843 public static final String EXTRA_TITLE_BIG = EXTRA_TITLE + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400844
845 /**
846 * {@link #extras} key: this is the main text payload, as supplied to
847 * {@link Builder#setContentText(CharSequence)}.
848 */
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500849 public static final String EXTRA_TEXT = "android.text";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400850
851 /**
852 * {@link #extras} key: this is a third line of text, as supplied to
853 * {@link Builder#setSubText(CharSequence)}.
854 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400855 public static final String EXTRA_SUB_TEXT = "android.subText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400856
857 /**
Adrian Roose458aa82015-12-08 16:17:19 -0800858 * {@link #extras} key: this is the remote input history, as supplied to
859 * {@link Builder#setRemoteInputHistory(CharSequence[])}.
Adrian Roos005e7742016-04-13 15:02:20 -0700860 *
861 * Apps can fill this through {@link Builder#setRemoteInputHistory(CharSequence[])}
862 * with the most recent inputs that have been sent through a {@link RemoteInput} of this
863 * Notification and are expected to clear it once the it is no longer relevant (e.g. for chat
864 * notifications once the other party has responded).
865 *
866 * The extra with this key is of type CharSequence[] and contains the most recent entry at
867 * the 0 index, the second most recent at the 1 index, etc.
868 *
869 * @see Builder#setRemoteInputHistory(CharSequence[])
Adrian Roose458aa82015-12-08 16:17:19 -0800870 */
871 public static final String EXTRA_REMOTE_INPUT_HISTORY = "android.remoteInputHistory";
872
873 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400874 * {@link #extras} key: this is a small piece of additional text as supplied to
875 * {@link Builder#setContentInfo(CharSequence)}.
876 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400877 public static final String EXTRA_INFO_TEXT = "android.infoText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400878
879 /**
880 * {@link #extras} key: this is a line of summary information intended to be shown
881 * alongside expanded notifications, as supplied to (e.g.)
882 * {@link BigTextStyle#setSummaryText(CharSequence)}.
883 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400884 public static final String EXTRA_SUMMARY_TEXT = "android.summaryText";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400885
886 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +0200887 * {@link #extras} key: this is the longer text shown in the big form of a
888 * {@link BigTextStyle} notification, as supplied to
889 * {@link BigTextStyle#bigText(CharSequence)}.
890 */
891 public static final String EXTRA_BIG_TEXT = "android.bigText";
892
893 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400894 * {@link #extras} key: this is the resource ID of the notification's main small icon, as
895 * supplied to {@link Builder#setSmallIcon(int)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400896 *
897 * @deprecated Use {@link #getSmallIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400898 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400899 @Deprecated
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -0500900 public static final String EXTRA_SMALL_ICON = "android.icon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400901
902 /**
903 * {@link #extras} key: this is a bitmap to be used instead of the small icon when showing the
904 * notification payload, as
905 * supplied to {@link Builder#setLargeIcon(android.graphics.Bitmap)}.
Julia Reynoldse071abd2017-03-22 10:52:11 -0400906 *
907 * @deprecated Use {@link #getLargeIcon()}, which supports a wider variety of icon sources.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400908 */
Julia Reynoldse071abd2017-03-22 10:52:11 -0400909 @Deprecated
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400910 public static final String EXTRA_LARGE_ICON = "android.largeIcon";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400911
912 /**
913 * {@link #extras} key: this is a bitmap to be used instead of the one from
914 * {@link Builder#setLargeIcon(android.graphics.Bitmap)} when the notification is
915 * shown in its expanded form, as supplied to
916 * {@link BigPictureStyle#bigLargeIcon(android.graphics.Bitmap)}.
917 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400918 public static final String EXTRA_LARGE_ICON_BIG = EXTRA_LARGE_ICON + ".big";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400919
920 /**
921 * {@link #extras} key: this is the progress value supplied to
922 * {@link Builder#setProgress(int, int, boolean)}.
923 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400924 public static final String EXTRA_PROGRESS = "android.progress";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400925
926 /**
927 * {@link #extras} key: this is the maximum value supplied to
928 * {@link Builder#setProgress(int, int, boolean)}.
929 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400930 public static final String EXTRA_PROGRESS_MAX = "android.progressMax";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400931
932 /**
933 * {@link #extras} key: whether the progress bar is indeterminate, supplied to
934 * {@link Builder#setProgress(int, int, boolean)}.
935 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400936 public static final String EXTRA_PROGRESS_INDETERMINATE = "android.progressIndeterminate";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400937
938 /**
939 * {@link #extras} key: whether {@link #when} should be shown as a count-up timer (specifically
940 * a {@link android.widget.Chronometer}) instead of a timestamp, as supplied to
941 * {@link Builder#setUsesChronometer(boolean)}.
942 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400943 public static final String EXTRA_SHOW_CHRONOMETER = "android.showChronometer";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400944
945 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -0800946 * {@link #extras} key: whether the chronometer set on the notification should count down
947 * instead of counting up. Is only relevant if key {@link #EXTRA_SHOW_CHRONOMETER} is present.
Adrian Roos96b7e202016-05-17 13:50:38 -0700948 * This extra is a boolean. The default is false.
Selim Cinek81c23aa2016-02-25 16:23:13 -0800949 */
Adrian Roos96b7e202016-05-17 13:50:38 -0700950 public static final String EXTRA_CHRONOMETER_COUNT_DOWN = "android.chronometerCountDown";
Selim Cinek81c23aa2016-02-25 16:23:13 -0800951
952 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400953 * {@link #extras} key: whether {@link #when} should be shown,
954 * as supplied to {@link Builder#setShowWhen(boolean)}.
955 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400956 public static final String EXTRA_SHOW_WHEN = "android.showWhen";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400957
958 /**
959 * {@link #extras} key: this is a bitmap to be shown in {@link BigPictureStyle} expanded
960 * notifications, supplied to {@link BigPictureStyle#bigPicture(android.graphics.Bitmap)}.
961 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400962 public static final String EXTRA_PICTURE = "android.picture";
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400963
964 /**
965 * {@link #extras} key: An array of CharSequences to show in {@link InboxStyle} expanded
966 * notifications, each of which was supplied to {@link InboxStyle#addLine(CharSequence)}.
967 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400968 public static final String EXTRA_TEXT_LINES = "android.textLines";
Dan Sandler842dd772014-05-15 09:36:47 -0400969
970 /**
971 * {@link #extras} key: A string representing the name of the specific
972 * {@link android.app.Notification.Style} used to create this notification.
973 */
Chris Wren91ad5632013-06-05 15:05:57 -0400974 public static final String EXTRA_TEMPLATE = "android.template";
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400975
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400976 /**
Chris Wrene6c48932014-09-29 17:19:27 -0400977 * {@link #extras} key: A String array containing the people that this notification relates to,
978 * each of which was supplied to {@link Builder#addPerson(String)}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -0400979 */
Daniel Sandlerf45564e2013-04-15 15:05:08 -0400980 public static final String EXTRA_PEOPLE = "android.people";
Daniel Sandler2561b0b2012-02-13 21:04:12 -0500981
982 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -0400983 * Allow certain system-generated notifications to appear before the device is provisioned.
984 * Only available to notifications coming from the android package.
985 * @hide
986 */
Maurice Lam96c10032017-03-29 15:42:38 -0700987 @SystemApi
John Spurlockfd7f1e02014-03-18 16:41:57 -0400988 public static final String EXTRA_ALLOW_DURING_SETUP = "android.allowDuringSetup";
989
990 /**
Jose Limae9e3b3b2014-05-18 23:44:50 -0700991 * {@link #extras} key: A
992 * {@link android.content.ContentUris content URI} pointing to an image that can be displayed
993 * in the background when the notification is selected. The URI must point to an image stream
994 * suitable for passing into
995 * {@link android.graphics.BitmapFactory#decodeStream(java.io.InputStream)
996 * BitmapFactory.decodeStream}; all other content types will be ignored. The content provider
997 * URI used for this purpose must require no permissions to read the image data.
998 */
999 public static final String EXTRA_BACKGROUND_IMAGE_URI = "android.backgroundImageUri";
1000
1001 /**
Dan Sandler842dd772014-05-15 09:36:47 -04001002 * {@link #extras} key: A
Jeff Browndba34ba2014-06-24 20:46:03 -07001003 * {@link android.media.session.MediaSession.Token} associated with a
Dan Sandler842dd772014-05-15 09:36:47 -04001004 * {@link android.app.Notification.MediaStyle} notification.
1005 */
1006 public static final String EXTRA_MEDIA_SESSION = "android.mediaSession";
1007
1008 /**
Bryan Mawhinneye191f902014-07-22 12:50:09 +01001009 * {@link #extras} key: the indices of actions to be shown in the compact view,
1010 * as supplied to (e.g.) {@link MediaStyle#setShowActionsInCompactView(int...)}.
1011 */
1012 public static final String EXTRA_COMPACT_ACTIONS = "android.compactActions";
1013
Christoph Studer943aa672014-08-03 20:31:16 +02001014 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04001015 * {@link #extras} key: the username to be displayed for all messages sent by the user including
1016 * direct replies
Adrian Roos96b7e202016-05-17 13:50:38 -07001017 * {@link android.app.Notification.MessagingStyle} notification. This extra is a
1018 * {@link CharSequence}
Alex Hillsfc737de2016-03-23 17:33:02 -04001019 */
1020 public static final String EXTRA_SELF_DISPLAY_NAME = "android.selfDisplayName";
1021
1022 /**
Adrian Roos96b7e202016-05-17 13:50:38 -07001023 * {@link #extras} key: a {@link CharSequence} to be displayed as the title to a conversation
Alex Hillsd9b04d92016-04-11 16:38:16 -04001024 * represented by a {@link android.app.Notification.MessagingStyle}
Alex Hillsfc737de2016-03-23 17:33:02 -04001025 */
Alex Hillsd9b04d92016-04-11 16:38:16 -04001026 public static final String EXTRA_CONVERSATION_TITLE = "android.conversationTitle";
Alex Hillsfc737de2016-03-23 17:33:02 -04001027
1028 /**
1029 * {@link #extras} key: an array of {@link android.app.Notification.MessagingStyle.Message}
1030 * bundles provided by a
Adrian Roos96b7e202016-05-17 13:50:38 -07001031 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1032 * array of bundles.
Alex Hillsfc737de2016-03-23 17:33:02 -04001033 */
1034 public static final String EXTRA_MESSAGES = "android.messages";
1035
1036 /**
Adrian Roos437cd562017-01-18 15:47:03 -08001037 * {@link #extras} key: an array of
1038 * {@link android.app.Notification.MessagingStyle#addHistoricMessage historic}
1039 * {@link android.app.Notification.MessagingStyle.Message} bundles provided by a
1040 * {@link android.app.Notification.MessagingStyle} notification. This extra is a parcelable
1041 * array of bundles.
1042 */
1043 public static final String EXTRA_HISTORIC_MESSAGES = "android.messages.historic";
1044
1045 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08001046 * {@link #extras} key: whether the notification should be colorized as
1047 * supplied to {@link Builder#setColorized(boolean)}}.
1048 */
1049 public static final String EXTRA_COLORIZED = "android.colorized";
1050
1051 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04001052 * @hide
1053 */
1054 public static final String EXTRA_BUILDER_APPLICATION_INFO = "android.appInfo";
1055
Selim Cinek247fa012016-02-18 09:50:48 -08001056 /**
1057 * @hide
1058 */
1059 public static final String EXTRA_CONTAINS_CUSTOM_VIEW = "android.contains.customView";
1060
Shane Brennan472a3b32016-12-12 15:28:10 -08001061 /**
1062 * {@link #extras} key: the audio contents of this notification.
1063 *
1064 * This is for use when rendering the notification on an audio-focused interface;
1065 * the audio contents are a complete sound sample that contains the contents/body of the
1066 * notification. This may be used in substitute of a Text-to-Speech reading of the
1067 * notification. For example if the notification represents a voice message this should point
1068 * to the audio of that message.
1069 *
1070 * The data stored under this key should be a String representation of a Uri that contains the
1071 * audio contents in one of the following formats: WAV, PCM 16-bit, AMR-WB.
1072 *
1073 * This extra is unnecessary if you are using {@code MessagingStyle} since each {@code Message}
1074 * has a field for holding data URI. That field can be used for audio.
1075 * See {@code Message#setData}.
1076 *
1077 * Example usage:
1078 * <pre>
1079 * {@code
1080 * Notification.Builder myBuilder = (build your Notification as normal);
1081 * myBuilder.getExtras().putString(EXTRA_AUDIO_CONTENTS_URI, myAudioUri.toString());
1082 * }
1083 * </pre>
1084 */
1085 public static final String EXTRA_AUDIO_CONTENTS_URI = "android.audioContents";
1086
Dan Sandler80eaa592016-04-14 23:34:54 -04001087 /** @hide */
1088 @SystemApi
Dan Sandler732bd6c2016-04-12 14:20:32 -04001089 public static final String EXTRA_SUBSTITUTE_APP_NAME = "android.substName";
1090
Dan Sandlerd63f9322015-05-06 15:18:49 -04001091 private Icon mSmallIcon;
1092 private Icon mLargeIcon;
1093
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001094 private String mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001095 private long mTimeout;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001096
Julia Reynolds13d898c2017-02-02 12:22:05 -05001097 private String mShortcutId;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001098 private CharSequence mSettingsText;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001099
1100 /**
1101 * If this notification is being shown as a badge, always show as a number.
1102 */
1103 public static final int BADGE_ICON_NONE = 0;
1104
1105 /**
1106 * If this notification is being shown as a badge, use the {@link #getSmallIcon()} to
1107 * represent this notification.
1108 */
1109 public static final int BADGE_ICON_SMALL = 1;
1110
1111 /**
1112 * If this notification is being shown as a badge, use the {@link #getLargeIcon()} to
1113 * represent this notification.
1114 */
1115 public static final int BADGE_ICON_LARGE = 2;
Julia Reynolds7d5b4da2017-03-20 10:18:49 -04001116 private int mBadgeIcon = BADGE_ICON_NONE;
Julia Reynolds13d898c2017-02-02 12:22:05 -05001117
Chris Wren51c75102013-07-16 20:49:17 -04001118 /**
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001119 * Structure to encapsulate a named action that can be shown as part of this notification.
1120 * It must include an icon, a label, and a {@link PendingIntent} to be fired when the action is
1121 * selected by the user.
1122 * <p>
Griff Hazen959591e2014-05-15 22:26:18 -07001123 * Apps should use {@link Notification.Builder#addAction(int, CharSequence, PendingIntent)}
1124 * or {@link Notification.Builder#addAction(Notification.Action)}
1125 * to attach actions.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001126 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001127 public static class Action implements Parcelable {
Shane Brennan472a3b32016-12-12 15:28:10 -08001128 /**
1129 * {@link #extras} key: Keys to a {@link Parcelable} {@link ArrayList} of
1130 * {@link RemoteInput}s.
1131 *
1132 * This is intended for {@link RemoteInput}s that only accept data, meaning
1133 * {@link RemoteInput#getAllowFreeFormInput} is false, {@link RemoteInput#getChoices}
1134 * is null or empty, and {@link RemoteInput#getAllowedDataTypes is non-null and not
1135 * empty. These {@link RemoteInput}s will be ignored by devices that do not
1136 * support non-text-based {@link RemoteInput}s. See {@link Builder#build}.
1137 *
1138 * You can test if a RemoteInput matches these constraints using
1139 * {@link RemoteInput#isDataOnly}.
1140 */
1141 private static final String EXTRA_DATA_ONLY_INPUTS = "android.extra.DATA_ONLY_INPUTS";
1142
Griff Hazen959591e2014-05-15 22:26:18 -07001143 private final Bundle mExtras;
Dan Sandler86647982015-05-13 23:41:13 -04001144 private Icon mIcon;
Griff Hazen61a9e862014-05-22 16:05:19 -07001145 private final RemoteInput[] mRemoteInputs;
Alex Hills1f27f882017-01-12 11:24:46 -05001146 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001147
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001148 /**
1149 * Small icon representing the action.
Dan Sandler86647982015-05-13 23:41:13 -04001150 *
1151 * @deprecated Use {@link Action#getIcon()} instead.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001152 */
Dan Sandler86647982015-05-13 23:41:13 -04001153 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001154 public int icon;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001155
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001156 /**
1157 * Title of the action.
1158 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001159 public CharSequence title;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001160
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001161 /**
1162 * Intent to send when the user invokes this action. May be null, in which case the action
1163 * may be rendered in a disabled presentation by the system UI.
1164 */
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001165 public PendingIntent actionIntent;
Griff Hazen959591e2014-05-15 22:26:18 -07001166
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001167 private Action(Parcel in) {
Dan Sandler86647982015-05-13 23:41:13 -04001168 if (in.readInt() != 0) {
1169 mIcon = Icon.CREATOR.createFromParcel(in);
Dan Sandler68079d52015-07-22 10:45:30 -04001170 if (mIcon.getType() == Icon.TYPE_RESOURCE) {
1171 icon = mIcon.getResId();
1172 }
Dan Sandler86647982015-05-13 23:41:13 -04001173 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001174 title = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);
1175 if (in.readInt() == 1) {
1176 actionIntent = PendingIntent.CREATOR.createFromParcel(in);
1177 }
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001178 mExtras = Bundle.setDefusable(in.readBundle(), true);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001179 mRemoteInputs = in.createTypedArray(RemoteInput.CREATOR);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001180 mAllowGeneratedReplies = in.readInt() == 1;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001181 }
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001182
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001183 /**
Dan Sandler86647982015-05-13 23:41:13 -04001184 * @deprecated Use {@link android.app.Notification.Action.Builder}.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001185 */
Dan Sandler86647982015-05-13 23:41:13 -04001186 @Deprecated
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001187 public Action(int icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001188 this(Icon.createWithResource("", icon), title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001189 }
1190
Adrian Roos7af53622016-10-12 13:44:05 -07001191 /** Keep in sync with {@link Notification.Action.Builder#Builder(Action)}! */
Dan Sandler86647982015-05-13 23:41:13 -04001192 private Action(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001193 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Dan Sandler86647982015-05-13 23:41:13 -04001194 this.mIcon = icon;
Gus Prevasf5bff46f2015-08-24 10:34:28 -04001195 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
1196 this.icon = icon.getResId();
1197 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001198 this.title = title;
1199 this.actionIntent = intent;
Griff Hazen959591e2014-05-15 22:26:18 -07001200 this.mExtras = extras != null ? extras : new Bundle();
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001201 this.mRemoteInputs = remoteInputs;
Alex Hills42b0c4d2016-04-26 13:35:36 -04001202 this.mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001203 }
1204
1205 /**
Dan Sandler86647982015-05-13 23:41:13 -04001206 * Return an icon representing the action.
1207 */
1208 public Icon getIcon() {
1209 if (mIcon == null && icon != 0) {
1210 // you snuck an icon in here without using the builder; let's try to keep it
1211 mIcon = Icon.createWithResource("", icon);
1212 }
1213 return mIcon;
1214 }
1215
1216 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001217 * Get additional metadata carried around with this Action.
1218 */
1219 public Bundle getExtras() {
1220 return mExtras;
1221 }
1222
1223 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001224 * Return whether the platform should automatically generate possible replies for this
1225 * {@link Action}
1226 */
1227 public boolean getAllowGeneratedReplies() {
1228 return mAllowGeneratedReplies;
1229 }
1230
1231 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001232 * Get the list of inputs to be collected from the user when this action is sent.
Shane Brennan472a3b32016-12-12 15:28:10 -08001233 * May return null if no remote inputs were added. Only returns inputs which accept
1234 * a text input. For inputs which only accept data use {@link #getDataOnlyRemoteInputs}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001235 */
1236 public RemoteInput[] getRemoteInputs() {
1237 return mRemoteInputs;
1238 }
1239
1240 /**
Shane Brennan472a3b32016-12-12 15:28:10 -08001241 * Get the list of inputs to be collected from the user that ONLY accept data when this
1242 * action is sent. These remote inputs are guaranteed to return true on a call to
1243 * {@link RemoteInput#isDataOnly}.
1244 *
1245 * May return null if no data-only remote inputs were added.
1246 *
1247 * This method exists so that legacy RemoteInput collectors that pre-date the addition
1248 * of non-textual RemoteInputs do not access these remote inputs.
1249 */
1250 public RemoteInput[] getDataOnlyRemoteInputs() {
1251 return (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
1252 }
1253
1254 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001255 * Builder class for {@link Action} objects.
1256 */
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001257 public static final class Builder {
Dan Sandler86647982015-05-13 23:41:13 -04001258 private final Icon mIcon;
Griff Hazen959591e2014-05-15 22:26:18 -07001259 private final CharSequence mTitle;
1260 private final PendingIntent mIntent;
Alex Hills1f27f882017-01-12 11:24:46 -05001261 private boolean mAllowGeneratedReplies = true;
Griff Hazen959591e2014-05-15 22:26:18 -07001262 private final Bundle mExtras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001263 private ArrayList<RemoteInput> mRemoteInputs;
Griff Hazen959591e2014-05-15 22:26:18 -07001264
1265 /**
1266 * Construct a new builder for {@link Action} object.
1267 * @param icon icon to show for this action
1268 * @param title the title of the action
1269 * @param intent the {@link PendingIntent} to fire when users trigger this action
1270 */
Dan Sandler86647982015-05-13 23:41:13 -04001271 @Deprecated
Griff Hazen959591e2014-05-15 22:26:18 -07001272 public Builder(int icon, CharSequence title, PendingIntent intent) {
Adrian Roos7af53622016-10-12 13:44:05 -07001273 this(Icon.createWithResource("", icon), title, intent);
Dan Sandler86647982015-05-13 23:41:13 -04001274 }
1275
1276 /**
1277 * Construct a new builder for {@link Action} object.
1278 * @param icon icon to show for this action
1279 * @param title the title of the action
1280 * @param intent the {@link PendingIntent} to fire when users trigger this action
1281 */
1282 public Builder(Icon icon, CharSequence title, PendingIntent intent) {
Alex Hills1f27f882017-01-12 11:24:46 -05001283 this(icon, title, intent, new Bundle(), null, true);
Griff Hazen959591e2014-05-15 22:26:18 -07001284 }
1285
1286 /**
1287 * Construct a new builder for {@link Action} object using the fields from an
1288 * {@link Action}.
1289 * @param action the action to read fields from.
1290 */
1291 public Builder(Action action) {
Adrian Roos7af53622016-10-12 13:44:05 -07001292 this(action.getIcon(), action.title, action.actionIntent,
1293 new Bundle(action.mExtras), action.getRemoteInputs(),
1294 action.getAllowGeneratedReplies());
Griff Hazen959591e2014-05-15 22:26:18 -07001295 }
1296
Dan Sandler86647982015-05-13 23:41:13 -04001297 private Builder(Icon icon, CharSequence title, PendingIntent intent, Bundle extras,
Adrian Roos7af53622016-10-12 13:44:05 -07001298 RemoteInput[] remoteInputs, boolean allowGeneratedReplies) {
Griff Hazen959591e2014-05-15 22:26:18 -07001299 mIcon = icon;
1300 mTitle = title;
1301 mIntent = intent;
1302 mExtras = extras;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001303 if (remoteInputs != null) {
1304 mRemoteInputs = new ArrayList<RemoteInput>(remoteInputs.length);
1305 Collections.addAll(mRemoteInputs, remoteInputs);
1306 }
Adrian Roos7af53622016-10-12 13:44:05 -07001307 mAllowGeneratedReplies = allowGeneratedReplies;
Griff Hazen959591e2014-05-15 22:26:18 -07001308 }
1309
1310 /**
1311 * Merge additional metadata into this builder.
1312 *
1313 * <p>Values within the Bundle will replace existing extras values in this Builder.
1314 *
1315 * @see Notification.Action#extras
1316 */
1317 public Builder addExtras(Bundle extras) {
1318 if (extras != null) {
1319 mExtras.putAll(extras);
1320 }
1321 return this;
1322 }
1323
1324 /**
1325 * Get the metadata Bundle used by this Builder.
1326 *
1327 * <p>The returned Bundle is shared with this Builder.
1328 */
1329 public Bundle getExtras() {
1330 return mExtras;
1331 }
1332
1333 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001334 * Add an input to be collected from the user when this action is sent.
1335 * Response values can be retrieved from the fired intent by using the
1336 * {@link RemoteInput#getResultsFromIntent} function.
1337 * @param remoteInput a {@link RemoteInput} to add to the action
1338 * @return this object for method chaining
1339 */
1340 public Builder addRemoteInput(RemoteInput remoteInput) {
1341 if (mRemoteInputs == null) {
1342 mRemoteInputs = new ArrayList<RemoteInput>();
1343 }
1344 mRemoteInputs.add(remoteInput);
1345 return this;
1346 }
1347
1348 /**
Alex Hills42b0c4d2016-04-26 13:35:36 -04001349 * Set whether the platform should automatically generate possible replies to add to
1350 * {@link RemoteInput#getChoices()}. If the {@link Action} doesn't have a
1351 * {@link RemoteInput}, this has no effect.
1352 * @param allowGeneratedReplies {@code true} to allow generated replies, {@code false}
1353 * otherwise
1354 * @return this object for method chaining
Alex Hills1f27f882017-01-12 11:24:46 -05001355 * The default value is {@code true}
Alex Hills42b0c4d2016-04-26 13:35:36 -04001356 */
1357 public Builder setAllowGeneratedReplies(boolean allowGeneratedReplies) {
1358 mAllowGeneratedReplies = allowGeneratedReplies;
1359 return this;
1360 }
1361
1362 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001363 * Apply an extender to this action builder. Extenders may be used to add
1364 * metadata or change options on this builder.
1365 */
Griff Hazen61a9e862014-05-22 16:05:19 -07001366 public Builder extend(Extender extender) {
1367 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001368 return this;
1369 }
1370
1371 /**
Griff Hazen959591e2014-05-15 22:26:18 -07001372 * Combine all of the options that have been set and return a new {@link Action}
1373 * object.
1374 * @return the built action
1375 */
1376 public Action build() {
Shane Brennan472a3b32016-12-12 15:28:10 -08001377 ArrayList<RemoteInput> dataOnlyInputs = new ArrayList<>();
1378 RemoteInput[] previousDataInputs =
1379 (RemoteInput[]) mExtras.getParcelableArray(EXTRA_DATA_ONLY_INPUTS);
Felipe Lemedfd11962017-01-18 18:38:46 -08001380 if (previousDataInputs != null) {
Shane Brennan472a3b32016-12-12 15:28:10 -08001381 for (RemoteInput input : previousDataInputs) {
1382 dataOnlyInputs.add(input);
1383 }
1384 }
1385 List<RemoteInput> textInputs = new ArrayList<>();
1386 if (mRemoteInputs != null) {
1387 for (RemoteInput input : mRemoteInputs) {
1388 if (input.isDataOnly()) {
1389 dataOnlyInputs.add(input);
1390 } else {
1391 textInputs.add(input);
1392 }
1393 }
1394 }
1395 if (!dataOnlyInputs.isEmpty()) {
1396 RemoteInput[] dataInputsArr =
1397 dataOnlyInputs.toArray(new RemoteInput[dataOnlyInputs.size()]);
1398 mExtras.putParcelableArray(EXTRA_DATA_ONLY_INPUTS, dataInputsArr);
1399 }
1400 RemoteInput[] textInputsArr = textInputs.isEmpty()
1401 ? null : textInputs.toArray(new RemoteInput[textInputs.size()]);
1402 return new Action(mIcon, mTitle, mIntent, mExtras, textInputsArr,
Alex Hills42b0c4d2016-04-26 13:35:36 -04001403 mAllowGeneratedReplies);
Griff Hazen959591e2014-05-15 22:26:18 -07001404 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001405 }
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001406
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001407 @Override
1408 public Action clone() {
1409 return new Action(
Dan Sandler86647982015-05-13 23:41:13 -04001410 getIcon(),
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001411 title,
1412 actionIntent, // safe to alias
Julia Reynolds53c934c2016-09-16 14:03:43 -04001413 mExtras == null ? new Bundle() : new Bundle(mExtras),
Alex Hills42b0c4d2016-04-26 13:35:36 -04001414 getRemoteInputs(),
1415 getAllowGeneratedReplies());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001416 }
1417 @Override
1418 public int describeContents() {
1419 return 0;
1420 }
1421 @Override
1422 public void writeToParcel(Parcel out, int flags) {
Dan Sandler86647982015-05-13 23:41:13 -04001423 final Icon ic = getIcon();
1424 if (ic != null) {
1425 out.writeInt(1);
1426 ic.writeToParcel(out, 0);
1427 } else {
1428 out.writeInt(0);
1429 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001430 TextUtils.writeToParcel(title, out, flags);
1431 if (actionIntent != null) {
1432 out.writeInt(1);
1433 actionIntent.writeToParcel(out, flags);
1434 } else {
1435 out.writeInt(0);
1436 }
Griff Hazen959591e2014-05-15 22:26:18 -07001437 out.writeBundle(mExtras);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001438 out.writeTypedArray(mRemoteInputs, flags);
Alex Hills42b0c4d2016-04-26 13:35:36 -04001439 out.writeInt(mAllowGeneratedReplies ? 1 : 0);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001440 }
Griff Hazen959591e2014-05-15 22:26:18 -07001441 public static final Parcelable.Creator<Action> CREATOR =
1442 new Parcelable.Creator<Action>() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001443 public Action createFromParcel(Parcel in) {
1444 return new Action(in);
1445 }
1446 public Action[] newArray(int size) {
1447 return new Action[size];
1448 }
1449 };
Griff Hazen61a9e862014-05-22 16:05:19 -07001450
1451 /**
1452 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
1453 * metadata or change options on an action builder.
1454 */
1455 public interface Extender {
1456 /**
1457 * Apply this extender to a notification action builder.
1458 * @param builder the builder to be modified.
1459 * @return the build object for chaining.
1460 */
1461 public Builder extend(Builder builder);
1462 }
1463
1464 /**
1465 * Wearable extender for notification actions. To add extensions to an action,
1466 * create a new {@link android.app.Notification.Action.WearableExtender} object using
1467 * the {@code WearableExtender()} constructor and apply it to a
1468 * {@link android.app.Notification.Action.Builder} using
1469 * {@link android.app.Notification.Action.Builder#extend}.
1470 *
1471 * <pre class="prettyprint">
1472 * Notification.Action action = new Notification.Action.Builder(
1473 * R.drawable.archive_all, "Archive all", actionIntent)
Griff Hazen14f57992014-05-26 09:07:14 -07001474 * .extend(new Notification.Action.WearableExtender()
Griff Hazen61a9e862014-05-22 16:05:19 -07001475 * .setAvailableOffline(false))
Griff Hazen14f57992014-05-26 09:07:14 -07001476 * .build();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07001477 */
1478 public static final class WearableExtender implements Extender {
1479 /** Notification action extra which contains wearable extensions */
1480 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
1481
Pete Gastaf6781d2014-10-07 15:17:05 -04001482 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07001483 private static final String KEY_FLAGS = "flags";
Pete Gastaf6781d2014-10-07 15:17:05 -04001484 private static final String KEY_IN_PROGRESS_LABEL = "inProgressLabel";
1485 private static final String KEY_CONFIRM_LABEL = "confirmLabel";
1486 private static final String KEY_CANCEL_LABEL = "cancelLabel";
Griff Hazen61a9e862014-05-22 16:05:19 -07001487
1488 // Flags bitwise-ored to mFlags
1489 private static final int FLAG_AVAILABLE_OFFLINE = 0x1;
Alex Hills9ab3a232016-04-05 14:54:56 -04001490 private static final int FLAG_HINT_LAUNCHES_ACTIVITY = 1 << 1;
Alex Hills9f087612016-06-07 09:08:59 -04001491 private static final int FLAG_HINT_DISPLAY_INLINE = 1 << 2;
Griff Hazen61a9e862014-05-22 16:05:19 -07001492
1493 // Default value for flags integer
1494 private static final int DEFAULT_FLAGS = FLAG_AVAILABLE_OFFLINE;
1495
1496 private int mFlags = DEFAULT_FLAGS;
1497
Pete Gastaf6781d2014-10-07 15:17:05 -04001498 private CharSequence mInProgressLabel;
1499 private CharSequence mConfirmLabel;
1500 private CharSequence mCancelLabel;
1501
Griff Hazen61a9e862014-05-22 16:05:19 -07001502 /**
1503 * Create a {@link android.app.Notification.Action.WearableExtender} with default
1504 * options.
1505 */
1506 public WearableExtender() {
1507 }
1508
1509 /**
1510 * Create a {@link android.app.Notification.Action.WearableExtender} by reading
1511 * wearable options present in an existing notification action.
1512 * @param action the notification action to inspect.
1513 */
1514 public WearableExtender(Action action) {
1515 Bundle wearableBundle = action.getExtras().getBundle(EXTRA_WEARABLE_EXTENSIONS);
1516 if (wearableBundle != null) {
1517 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
Pete Gastaf6781d2014-10-07 15:17:05 -04001518 mInProgressLabel = wearableBundle.getCharSequence(KEY_IN_PROGRESS_LABEL);
1519 mConfirmLabel = wearableBundle.getCharSequence(KEY_CONFIRM_LABEL);
1520 mCancelLabel = wearableBundle.getCharSequence(KEY_CANCEL_LABEL);
Griff Hazen61a9e862014-05-22 16:05:19 -07001521 }
1522 }
1523
1524 /**
1525 * Apply wearable extensions to a notification action that is being built. This is
1526 * typically called by the {@link android.app.Notification.Action.Builder#extend}
1527 * method of {@link android.app.Notification.Action.Builder}.
1528 */
1529 @Override
1530 public Action.Builder extend(Action.Builder builder) {
1531 Bundle wearableBundle = new Bundle();
1532
1533 if (mFlags != DEFAULT_FLAGS) {
1534 wearableBundle.putInt(KEY_FLAGS, mFlags);
1535 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001536 if (mInProgressLabel != null) {
1537 wearableBundle.putCharSequence(KEY_IN_PROGRESS_LABEL, mInProgressLabel);
1538 }
1539 if (mConfirmLabel != null) {
1540 wearableBundle.putCharSequence(KEY_CONFIRM_LABEL, mConfirmLabel);
1541 }
1542 if (mCancelLabel != null) {
1543 wearableBundle.putCharSequence(KEY_CANCEL_LABEL, mCancelLabel);
1544 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001545
1546 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
1547 return builder;
1548 }
1549
1550 @Override
1551 public WearableExtender clone() {
1552 WearableExtender that = new WearableExtender();
1553 that.mFlags = this.mFlags;
Pete Gastaf6781d2014-10-07 15:17:05 -04001554 that.mInProgressLabel = this.mInProgressLabel;
1555 that.mConfirmLabel = this.mConfirmLabel;
1556 that.mCancelLabel = this.mCancelLabel;
Griff Hazen61a9e862014-05-22 16:05:19 -07001557 return that;
1558 }
1559
1560 /**
1561 * Set whether this action is available when the wearable device is not connected to
1562 * a companion device. The user can still trigger this action when the wearable device is
1563 * offline, but a visual hint will indicate that the action may not be available.
1564 * Defaults to true.
1565 */
1566 public WearableExtender setAvailableOffline(boolean availableOffline) {
1567 setFlag(FLAG_AVAILABLE_OFFLINE, availableOffline);
1568 return this;
1569 }
1570
1571 /**
1572 * Get whether this action is available when the wearable device is not connected to
1573 * a companion device. The user can still trigger this action when the wearable device is
1574 * offline, but a visual hint will indicate that the action may not be available.
1575 * Defaults to true.
1576 */
1577 public boolean isAvailableOffline() {
1578 return (mFlags & FLAG_AVAILABLE_OFFLINE) != 0;
1579 }
1580
1581 private void setFlag(int mask, boolean value) {
1582 if (value) {
1583 mFlags |= mask;
1584 } else {
1585 mFlags &= ~mask;
1586 }
1587 }
Pete Gastaf6781d2014-10-07 15:17:05 -04001588
1589 /**
1590 * Set a label to display while the wearable is preparing to automatically execute the
1591 * action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1592 *
1593 * @param label the label to display while the action is being prepared to execute
1594 * @return this object for method chaining
1595 */
1596 public WearableExtender setInProgressLabel(CharSequence label) {
1597 mInProgressLabel = label;
1598 return this;
1599 }
1600
1601 /**
1602 * Get the label to display while the wearable is preparing to automatically execute
1603 * the action. This is usually a 'ing' verb ending in ellipsis like "Sending..."
1604 *
1605 * @return the label to display while the action is being prepared to execute
1606 */
1607 public CharSequence getInProgressLabel() {
1608 return mInProgressLabel;
1609 }
1610
1611 /**
1612 * Set a label to display to confirm that the action should be executed.
1613 * This is usually an imperative verb like "Send".
1614 *
1615 * @param label the label to confirm the action should be executed
1616 * @return this object for method chaining
1617 */
1618 public WearableExtender setConfirmLabel(CharSequence label) {
1619 mConfirmLabel = label;
1620 return this;
1621 }
1622
1623 /**
1624 * Get the label to display to confirm that the action should be executed.
1625 * This is usually an imperative verb like "Send".
1626 *
1627 * @return the label to confirm the action should be executed
1628 */
1629 public CharSequence getConfirmLabel() {
1630 return mConfirmLabel;
1631 }
1632
1633 /**
1634 * Set a label to display to cancel the action.
1635 * This is usually an imperative verb, like "Cancel".
1636 *
1637 * @param label the label to display to cancel the action
1638 * @return this object for method chaining
1639 */
1640 public WearableExtender setCancelLabel(CharSequence label) {
1641 mCancelLabel = label;
1642 return this;
1643 }
1644
1645 /**
1646 * Get the label to display to cancel the action.
1647 * This is usually an imperative verb like "Cancel".
1648 *
1649 * @return the label to display to cancel the action
1650 */
1651 public CharSequence getCancelLabel() {
1652 return mCancelLabel;
1653 }
Alex Hills9ab3a232016-04-05 14:54:56 -04001654
1655 /**
1656 * Set a hint that this Action will launch an {@link Activity} directly, telling the
1657 * platform that it can generate the appropriate transitions.
1658 * @param hintLaunchesActivity {@code true} if the content intent will launch
1659 * an activity and transitions should be generated, false otherwise.
1660 * @return this object for method chaining
1661 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001662 public WearableExtender setHintLaunchesActivity(
Alex Hills9ab3a232016-04-05 14:54:56 -04001663 boolean hintLaunchesActivity) {
1664 setFlag(FLAG_HINT_LAUNCHES_ACTIVITY, hintLaunchesActivity);
1665 return this;
1666 }
1667
1668 /**
1669 * Get a hint that this Action will launch an {@link Activity} directly, telling the
1670 * platform that it can generate the appropriate transitions
1671 * @return {@code true} if the content intent will launch an activity and transitions
1672 * should be generated, false otherwise. The default value is {@code false} if this was
1673 * never set.
1674 */
Alex Hills4ec3ff42016-04-12 11:36:18 -04001675 public boolean getHintLaunchesActivity() {
Alex Hills9ab3a232016-04-05 14:54:56 -04001676 return (mFlags & FLAG_HINT_LAUNCHES_ACTIVITY) != 0;
1677 }
Alex Hills9f087612016-06-07 09:08:59 -04001678
1679 /**
1680 * Set a hint that this Action should be displayed inline.
1681 *
1682 * @param hintDisplayInline {@code true} if action should be displayed inline, false
1683 * otherwise
1684 * @return this object for method chaining
1685 */
1686 public WearableExtender setHintDisplayActionInline(
1687 boolean hintDisplayInline) {
1688 setFlag(FLAG_HINT_DISPLAY_INLINE, hintDisplayInline);
1689 return this;
1690 }
1691
1692 /**
1693 * Get a hint that this Action should be displayed inline.
1694 *
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08001695 * @return {@code true} if the Action should be displayed inline, {@code false}
Alex Hills9f087612016-06-07 09:08:59 -04001696 * otherwise. The default value is {@code false} if this was never set.
1697 */
1698 public boolean getHintDisplayActionInline() {
1699 return (mFlags & FLAG_HINT_DISPLAY_INLINE) != 0;
1700 }
Griff Hazen61a9e862014-05-22 16:05:19 -07001701 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001702 }
1703
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04001704 /**
1705 * Array of all {@link Action} structures attached to this notification by
1706 * {@link Builder#addAction(int, CharSequence, PendingIntent)}. Mostly useful for instances of
1707 * {@link android.service.notification.NotificationListenerService} that provide an alternative
1708 * interface for invoking actions.
1709 */
Daniel Sandlerea2a3172013-02-20 22:24:20 -05001710 public Action[] actions;
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001711
1712 /**
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001713 * Replacement version of this notification whose content will be shown
1714 * in an insecure context such as atop a secure keyguard. See {@link #visibility}
1715 * and {@link #VISIBILITY_PUBLIC}.
1716 */
1717 public Notification publicVersion;
1718
1719 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001720 * Constructs a Notification object with default values.
Joe Onorato46439ce2010-11-19 13:56:21 -08001721 * You might want to consider using {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001722 */
1723 public Notification()
1724 {
1725 this.when = System.currentTimeMillis();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001726 this.creationTime = System.currentTimeMillis();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001727 this.priority = PRIORITY_DEFAULT;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001728 }
1729
1730 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001731 * @hide
1732 */
1733 public Notification(Context context, int icon, CharSequence tickerText, long when,
1734 CharSequence contentTitle, CharSequence contentText, Intent contentIntent)
1735 {
Chris Wren1ce4b6d2015-06-11 10:19:43 -04001736 new Builder(context)
1737 .setWhen(when)
1738 .setSmallIcon(icon)
1739 .setTicker(tickerText)
1740 .setContentTitle(contentTitle)
1741 .setContentText(contentText)
1742 .setContentIntent(PendingIntent.getActivity(context, 0, contentIntent, 0))
1743 .buildInto(this);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001744 }
1745
1746 /**
1747 * Constructs a Notification object with the information needed to
1748 * have a status bar icon without the standard expanded view.
1749 *
1750 * @param icon The resource id of the icon to put in the status bar.
1751 * @param tickerText The text that flows by in the status bar when the notification first
1752 * activates.
1753 * @param when The time to show in the time field. In the System.currentTimeMillis
1754 * timebase.
Joe Onorato46439ce2010-11-19 13:56:21 -08001755 *
1756 * @deprecated Use {@link Builder} instead.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001757 */
Joe Onorato46439ce2010-11-19 13:56:21 -08001758 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001759 public Notification(int icon, CharSequence tickerText, long when)
1760 {
1761 this.icon = icon;
1762 this.tickerText = tickerText;
1763 this.when = when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001764 this.creationTime = System.currentTimeMillis();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001765 }
1766
1767 /**
1768 * Unflatten the notification from a parcel.
1769 */
Svet Ganovddb94882016-06-23 19:55:24 -07001770 @SuppressWarnings("unchecked")
1771 public Notification(Parcel parcel) {
1772 // IMPORTANT: Add unmarshaling code in readFromParcel as the pending
1773 // intents in extras are always written as the last entry.
1774 readFromParcelImpl(parcel);
1775 // Must be read last!
Felipe Lemedd85da62016-06-28 11:29:54 -07001776 allPendingIntents = (ArraySet<PendingIntent>) parcel.readArraySet(null);
Svet Ganovddb94882016-06-23 19:55:24 -07001777 }
1778
1779 private void readFromParcelImpl(Parcel parcel)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001780 {
1781 int version = parcel.readInt();
1782
1783 when = parcel.readLong();
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001784 creationTime = parcel.readLong();
Dan Sandler3936e7a2015-05-19 20:59:12 -04001785 if (parcel.readInt() != 0) {
1786 mSmallIcon = Icon.CREATOR.createFromParcel(parcel);
Dan Sandler4e787062015-06-17 15:09:48 -04001787 if (mSmallIcon.getType() == Icon.TYPE_RESOURCE) {
1788 icon = mSmallIcon.getResId();
1789 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04001790 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001791 number = parcel.readInt();
1792 if (parcel.readInt() != 0) {
1793 contentIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1794 }
1795 if (parcel.readInt() != 0) {
1796 deleteIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1797 }
1798 if (parcel.readInt() != 0) {
1799 tickerText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1800 }
1801 if (parcel.readInt() != 0) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001802 tickerView = RemoteViews.CREATOR.createFromParcel(parcel);
Joe Onoratoef1e7762010-09-17 18:38:38 -04001803 }
1804 if (parcel.readInt() != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 contentView = RemoteViews.CREATOR.createFromParcel(parcel);
1806 }
Joe Onorato561d3852010-11-20 18:09:34 -08001807 if (parcel.readInt() != 0) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04001808 mLargeIcon = Icon.CREATOR.createFromParcel(parcel);
Joe Onorato561d3852010-11-20 18:09:34 -08001809 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 defaults = parcel.readInt();
1811 flags = parcel.readInt();
1812 if (parcel.readInt() != 0) {
1813 sound = Uri.CREATOR.createFromParcel(parcel);
1814 }
1815
1816 audioStreamType = parcel.readInt();
John Spurlockc0650f022014-07-19 13:22:39 -04001817 if (parcel.readInt() != 0) {
1818 audioAttributes = AudioAttributes.CREATOR.createFromParcel(parcel);
1819 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001820 vibrate = parcel.createLongArray();
1821 ledARGB = parcel.readInt();
1822 ledOnMS = parcel.readInt();
1823 ledOffMS = parcel.readInt();
1824 iconLevel = parcel.readInt();
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001825
1826 if (parcel.readInt() != 0) {
1827 fullScreenIntent = PendingIntent.CREATOR.createFromParcel(parcel);
1828 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001829
1830 priority = parcel.readInt();
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001831
John Spurlockfd7f1e02014-03-18 16:41:57 -04001832 category = parcel.readString();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001833
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001834 mGroupKey = parcel.readString();
1835
1836 mSortKey = parcel.readString();
1837
Jeff Sharkeya04c7a72016-03-18 12:20:36 -06001838 extras = Bundle.setDefusable(parcel.readBundle(), true); // may be null
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001839
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001840 actions = parcel.createTypedArray(Action.CREATOR); // may be null
1841
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001842 if (parcel.readInt() != 0) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001843 bigContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1844 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001845
Chris Wren8fd39ec2014-02-27 17:43:26 -05001846 if (parcel.readInt() != 0) {
1847 headsUpContentView = RemoteViews.CREATOR.createFromParcel(parcel);
1848 }
1849
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001850 visibility = parcel.readInt();
1851
1852 if (parcel.readInt() != 0) {
1853 publicVersion = Notification.CREATOR.createFromParcel(parcel);
1854 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04001855
1856 color = parcel.readInt();
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001857
1858 if (parcel.readInt() != 0) {
1859 mChannelId = parcel.readString();
1860 }
Julia Reynolds2a128742016-11-28 14:29:25 -05001861 mTimeout = parcel.readLong();
Julia Reynolds13d898c2017-02-02 12:22:05 -05001862
1863 if (parcel.readInt() != 0) {
1864 mShortcutId = parcel.readString();
1865 }
1866
1867 mBadgeIcon = parcel.readInt();
Julia Reynolds3aedded2017-03-31 14:42:09 -04001868
1869 if (parcel.readInt() != 0) {
1870 mSettingsText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
1871 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 }
1873
Andy Stadler110988c2010-12-03 14:29:16 -08001874 @Override
Joe Onorato18e69df2010-05-17 22:26:12 -07001875 public Notification clone() {
1876 Notification that = new Notification();
Daniel Sandler1a497d32013-04-18 14:52:45 -04001877 cloneInto(that, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001878 return that;
1879 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001880
Daniel Sandler1a497d32013-04-18 14:52:45 -04001881 /**
1882 * Copy all (or if heavy is false, all except Bitmaps and RemoteViews) members
1883 * of this into that.
1884 * @hide
1885 */
1886 public void cloneInto(Notification that, boolean heavy) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001887 that.when = this.when;
Selim Cinekb85f36fd2016-04-20 18:46:36 -07001888 that.creationTime = this.creationTime;
Dan Sandlerd63f9322015-05-06 15:18:49 -04001889 that.mSmallIcon = this.mSmallIcon;
Joe Onorato18e69df2010-05-17 22:26:12 -07001890 that.number = this.number;
1891
1892 // PendingIntents are global, so there's no reason (or way) to clone them.
1893 that.contentIntent = this.contentIntent;
1894 that.deleteIntent = this.deleteIntent;
Daniel Sandlere46cbd32010-06-17 10:35:26 -04001895 that.fullScreenIntent = this.fullScreenIntent;
Joe Onorato18e69df2010-05-17 22:26:12 -07001896
1897 if (this.tickerText != null) {
1898 that.tickerText = this.tickerText.toString();
1899 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001900 if (heavy && this.tickerView != null) {
Joe Onorato46439ce2010-11-19 13:56:21 -08001901 that.tickerView = this.tickerView.clone();
Joe Onoratoef1e7762010-09-17 18:38:38 -04001902 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001903 if (heavy && this.contentView != null) {
Joe Onorato18e69df2010-05-17 22:26:12 -07001904 that.contentView = this.contentView.clone();
1905 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04001906 if (heavy && this.mLargeIcon != null) {
1907 that.mLargeIcon = this.mLargeIcon;
Joe Onorato561d3852010-11-20 18:09:34 -08001908 }
Jozef BABJAKa8b91832011-02-22 08:05:08 +01001909 that.iconLevel = this.iconLevel;
Joe Onorato18e69df2010-05-17 22:26:12 -07001910 that.sound = this.sound; // android.net.Uri is immutable
1911 that.audioStreamType = this.audioStreamType;
John Spurlockc0650f022014-07-19 13:22:39 -04001912 if (this.audioAttributes != null) {
1913 that.audioAttributes = new AudioAttributes.Builder(this.audioAttributes).build();
1914 }
Joe Onorato18e69df2010-05-17 22:26:12 -07001915
1916 final long[] vibrate = this.vibrate;
1917 if (vibrate != null) {
1918 final int N = vibrate.length;
1919 final long[] vib = that.vibrate = new long[N];
1920 System.arraycopy(vibrate, 0, vib, 0, N);
1921 }
1922
1923 that.ledARGB = this.ledARGB;
1924 that.ledOnMS = this.ledOnMS;
1925 that.ledOffMS = this.ledOffMS;
1926 that.defaults = this.defaults;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001927
Joe Onorato18e69df2010-05-17 22:26:12 -07001928 that.flags = this.flags;
1929
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001930 that.priority = this.priority;
Joe Malin8d40d042012-11-05 11:36:40 -08001931
John Spurlockfd7f1e02014-03-18 16:41:57 -04001932 that.category = this.category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001933
Griff Hazen5cadc3b2014-05-20 09:55:39 -07001934 that.mGroupKey = this.mGroupKey;
1935
1936 that.mSortKey = this.mSortKey;
1937
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001938 if (this.extras != null) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04001939 try {
1940 that.extras = new Bundle(this.extras);
1941 // will unparcel
1942 that.extras.size();
1943 } catch (BadParcelableException e) {
1944 Log.e(TAG, "could not unparcel extras from notification: " + this, e);
1945 that.extras = null;
1946 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05001947 }
1948
Felipe Lemedd85da62016-06-28 11:29:54 -07001949 if (!ArrayUtils.isEmpty(allPendingIntents)) {
1950 that.allPendingIntents = new ArraySet<>(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07001951 }
1952
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001953 if (this.actions != null) {
1954 that.actions = new Action[this.actions.length];
1955 for(int i=0; i<this.actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08001956 if ( this.actions[i] != null) {
1957 that.actions[i] = this.actions[i].clone();
1958 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001959 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04001960 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05001961
Daniel Sandler1a497d32013-04-18 14:52:45 -04001962 if (heavy && this.bigContentView != null) {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04001963 that.bigContentView = this.bigContentView.clone();
1964 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04001965
Chris Wren8fd39ec2014-02-27 17:43:26 -05001966 if (heavy && this.headsUpContentView != null) {
1967 that.headsUpContentView = this.headsUpContentView.clone();
1968 }
1969
Dan Sandler0bf2ed82013-12-21 23:33:41 -06001970 that.visibility = this.visibility;
1971
1972 if (this.publicVersion != null) {
1973 that.publicVersion = new Notification();
1974 this.publicVersion.cloneInto(that.publicVersion, heavy);
1975 }
1976
Dan Sandler26e81cf2014-05-06 10:01:27 -04001977 that.color = this.color;
1978
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001979 that.mChannelId = this.mChannelId;
Julia Reynolds2a128742016-11-28 14:29:25 -05001980 that.mTimeout = this.mTimeout;
Julia Reynolds3aedded2017-03-31 14:42:09 -04001981 that.mShortcutId = this.mShortcutId;
1982 that.mBadgeIcon = this.mBadgeIcon;
1983 that.mSettingsText = this.mSettingsText;
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04001984
Daniel Sandler1a497d32013-04-18 14:52:45 -04001985 if (!heavy) {
1986 that.lightenPayload(); // will clean out extras
1987 }
1988 }
1989
1990 /**
1991 * Removes heavyweight parts of the Notification object for archival or for sending to
1992 * listeners when the full contents are not necessary.
1993 * @hide
1994 */
1995 public final void lightenPayload() {
1996 tickerView = null;
1997 contentView = null;
1998 bigContentView = null;
Chris Wren8fd39ec2014-02-27 17:43:26 -05001999 headsUpContentView = null;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002000 mLargeIcon = null;
Dan Sandler50128532015-12-08 15:42:41 -05002001 if (extras != null && !extras.isEmpty()) {
2002 final Set<String> keyset = extras.keySet();
2003 final int N = keyset.size();
2004 final String[] keys = keyset.toArray(new String[N]);
2005 for (int i=0; i<N; i++) {
2006 final String key = keys[i];
Dmitri Plotnikov22281362017-01-30 11:16:21 -08002007 if (TvExtender.EXTRA_TV_EXTENDER.equals(key)) {
2008 continue;
2009 }
Dan Sandler50128532015-12-08 15:42:41 -05002010 final Object obj = extras.get(key);
2011 if (obj != null &&
2012 ( obj instanceof Parcelable
2013 || obj instanceof Parcelable[]
2014 || obj instanceof SparseArray
2015 || obj instanceof ArrayList)) {
2016 extras.remove(key);
2017 }
2018 }
Daniel Sandler1a497d32013-04-18 14:52:45 -04002019 }
Joe Onorato18e69df2010-05-17 22:26:12 -07002020 }
2021
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002022 /**
2023 * Make sure this CharSequence is safe to put into a bundle, which basically
2024 * means it had better not be some custom Parcelable implementation.
2025 * @hide
2026 */
2027 public static CharSequence safeCharSequence(CharSequence cs) {
Christoph Studer535ec612014-09-03 15:47:47 +02002028 if (cs == null) return cs;
2029 if (cs.length() > MAX_CHARSEQUENCE_LENGTH) {
2030 cs = cs.subSequence(0, MAX_CHARSEQUENCE_LENGTH);
2031 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002032 if (cs instanceof Parcelable) {
2033 Log.e(TAG, "warning: " + cs.getClass().getCanonicalName()
2034 + " instance is a custom Parcelable and not allowed in Notification");
2035 return cs.toString();
2036 }
Selim Cinek60a54252016-02-26 17:03:25 -08002037 return removeTextSizeSpans(cs);
2038 }
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002039
Selim Cinek60a54252016-02-26 17:03:25 -08002040 private static CharSequence removeTextSizeSpans(CharSequence charSequence) {
2041 if (charSequence instanceof Spanned) {
2042 Spanned ss = (Spanned) charSequence;
2043 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
2044 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
2045 for (Object span : spans) {
2046 Object resultSpan = span;
Selim Cinek89991a22016-03-07 19:51:54 -08002047 if (resultSpan instanceof CharacterStyle) {
2048 resultSpan = ((CharacterStyle) span).getUnderlying();
2049 }
2050 if (resultSpan instanceof TextAppearanceSpan) {
2051 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
Selim Cinek60a54252016-02-26 17:03:25 -08002052 resultSpan = new TextAppearanceSpan(
2053 originalSpan.getFamily(),
2054 originalSpan.getTextStyle(),
2055 -1,
2056 originalSpan.getTextColor(),
2057 originalSpan.getLinkTextColor());
Selim Cinek89991a22016-03-07 19:51:54 -08002058 } else if (resultSpan instanceof RelativeSizeSpan
2059 || resultSpan instanceof AbsoluteSizeSpan) {
Selim Cinek60a54252016-02-26 17:03:25 -08002060 continue;
Selim Cinek89991a22016-03-07 19:51:54 -08002061 } else {
2062 resultSpan = span;
Selim Cinek60a54252016-02-26 17:03:25 -08002063 }
2064 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
2065 ss.getSpanFlags(span));
2066 }
2067 return builder;
2068 }
2069 return charSequence;
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04002070 }
2071
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002072 public int describeContents() {
2073 return 0;
2074 }
2075
2076 /**
Dan Sandler4e787062015-06-17 15:09:48 -04002077 * Flatten this notification into a parcel.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 */
Svet Ganovddb94882016-06-23 19:55:24 -07002079 public void writeToParcel(Parcel parcel, int flags) {
2080 // We need to mark all pending intents getting into the notification
2081 // system as being put there to later allow the notification ranker
2082 // to launch them and by doing so add the app to the battery saver white
2083 // list for a short period of time. The problem is that the system
2084 // cannot look into the extras as there may be parcelables there that
2085 // the platform does not know how to handle. To go around that we have
2086 // an explicit list of the pending intents in the extras bundle.
Felipe Lemedd85da62016-06-28 11:29:54 -07002087 final boolean collectPendingIntents = (allPendingIntents == null);
Svet Ganovddb94882016-06-23 19:55:24 -07002088 if (collectPendingIntents) {
2089 PendingIntent.setOnMarshaledListener(
2090 (PendingIntent intent, Parcel out, int outFlags) -> {
2091 if (parcel == out) {
Felipe Lemedd85da62016-06-28 11:29:54 -07002092 if (allPendingIntents == null) {
2093 allPendingIntents = new ArraySet<>();
Svet Ganovddb94882016-06-23 19:55:24 -07002094 }
Felipe Lemedd85da62016-06-28 11:29:54 -07002095 allPendingIntents.add(intent);
Svet Ganovddb94882016-06-23 19:55:24 -07002096 }
2097 });
2098 }
2099 try {
2100 // IMPORTANT: Add marshaling code in writeToParcelImpl as we
Julia Reynolds13d898c2017-02-02 12:22:05 -05002101 // want to intercept all pending events written to the parcel.
Svet Ganovddb94882016-06-23 19:55:24 -07002102 writeToParcelImpl(parcel, flags);
2103 // Must be written last!
Felipe Lemedd85da62016-06-28 11:29:54 -07002104 parcel.writeArraySet(allPendingIntents);
Svet Ganovddb94882016-06-23 19:55:24 -07002105 } finally {
2106 if (collectPendingIntents) {
2107 PendingIntent.setOnMarshaledListener(null);
2108 }
2109 }
2110 }
2111
2112 private void writeToParcelImpl(Parcel parcel, int flags) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002113 parcel.writeInt(1);
2114
2115 parcel.writeLong(when);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07002116 parcel.writeLong(creationTime);
Dan Sandler4e787062015-06-17 15:09:48 -04002117 if (mSmallIcon == null && icon != 0) {
2118 // you snuck an icon in here without using the builder; let's try to keep it
2119 mSmallIcon = Icon.createWithResource("", icon);
2120 }
Dan Sandler3936e7a2015-05-19 20:59:12 -04002121 if (mSmallIcon != null) {
2122 parcel.writeInt(1);
2123 mSmallIcon.writeToParcel(parcel, 0);
2124 } else {
2125 parcel.writeInt(0);
2126 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002127 parcel.writeInt(number);
2128 if (contentIntent != null) {
2129 parcel.writeInt(1);
2130 contentIntent.writeToParcel(parcel, 0);
2131 } else {
2132 parcel.writeInt(0);
2133 }
2134 if (deleteIntent != null) {
2135 parcel.writeInt(1);
2136 deleteIntent.writeToParcel(parcel, 0);
2137 } else {
2138 parcel.writeInt(0);
2139 }
2140 if (tickerText != null) {
2141 parcel.writeInt(1);
2142 TextUtils.writeToParcel(tickerText, parcel, flags);
2143 } else {
2144 parcel.writeInt(0);
2145 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002146 if (tickerView != null) {
Joe Onoratoef1e7762010-09-17 18:38:38 -04002147 parcel.writeInt(1);
Joe Onorato46439ce2010-11-19 13:56:21 -08002148 tickerView.writeToParcel(parcel, 0);
Joe Onoratoef1e7762010-09-17 18:38:38 -04002149 } else {
2150 parcel.writeInt(0);
2151 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002152 if (contentView != null) {
2153 parcel.writeInt(1);
2154 contentView.writeToParcel(parcel, 0);
2155 } else {
2156 parcel.writeInt(0);
2157 }
Selim Cinek279fa862016-06-14 10:57:25 -07002158 if (mLargeIcon == null && largeIcon != null) {
2159 // you snuck an icon in here without using the builder; let's try to keep it
2160 mLargeIcon = Icon.createWithBitmap(largeIcon);
2161 }
Dan Sandlerd63f9322015-05-06 15:18:49 -04002162 if (mLargeIcon != null) {
Joe Onorato561d3852010-11-20 18:09:34 -08002163 parcel.writeInt(1);
Dan Sandlerd63f9322015-05-06 15:18:49 -04002164 mLargeIcon.writeToParcel(parcel, 0);
Joe Onorato561d3852010-11-20 18:09:34 -08002165 } else {
2166 parcel.writeInt(0);
2167 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002168
2169 parcel.writeInt(defaults);
2170 parcel.writeInt(this.flags);
2171
2172 if (sound != null) {
2173 parcel.writeInt(1);
2174 sound.writeToParcel(parcel, 0);
2175 } else {
2176 parcel.writeInt(0);
2177 }
2178 parcel.writeInt(audioStreamType);
John Spurlockc0650f022014-07-19 13:22:39 -04002179
2180 if (audioAttributes != null) {
2181 parcel.writeInt(1);
2182 audioAttributes.writeToParcel(parcel, 0);
2183 } else {
2184 parcel.writeInt(0);
2185 }
2186
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002187 parcel.writeLongArray(vibrate);
2188 parcel.writeInt(ledARGB);
2189 parcel.writeInt(ledOnMS);
2190 parcel.writeInt(ledOffMS);
2191 parcel.writeInt(iconLevel);
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002192
2193 if (fullScreenIntent != null) {
2194 parcel.writeInt(1);
2195 fullScreenIntent.writeToParcel(parcel, 0);
2196 } else {
2197 parcel.writeInt(0);
2198 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002199
2200 parcel.writeInt(priority);
Joe Malin8d40d042012-11-05 11:36:40 -08002201
John Spurlockfd7f1e02014-03-18 16:41:57 -04002202 parcel.writeString(category);
Joe Malin8d40d042012-11-05 11:36:40 -08002203
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002204 parcel.writeString(mGroupKey);
2205
2206 parcel.writeString(mSortKey);
2207
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002208 parcel.writeBundle(extras); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002209
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002210 parcel.writeTypedArray(actions, 0); // null ok
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002211
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002212 if (bigContentView != null) {
2213 parcel.writeInt(1);
2214 bigContentView.writeToParcel(parcel, 0);
2215 } else {
2216 parcel.writeInt(0);
2217 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002218
Chris Wren8fd39ec2014-02-27 17:43:26 -05002219 if (headsUpContentView != null) {
2220 parcel.writeInt(1);
2221 headsUpContentView.writeToParcel(parcel, 0);
2222 } else {
2223 parcel.writeInt(0);
2224 }
2225
Dan Sandler0bf2ed82013-12-21 23:33:41 -06002226 parcel.writeInt(visibility);
2227
2228 if (publicVersion != null) {
2229 parcel.writeInt(1);
2230 publicVersion.writeToParcel(parcel, 0);
2231 } else {
2232 parcel.writeInt(0);
2233 }
Dan Sandler26e81cf2014-05-06 10:01:27 -04002234
2235 parcel.writeInt(color);
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002236
2237 if (mChannelId != null) {
2238 parcel.writeInt(1);
2239 parcel.writeString(mChannelId);
2240 } else {
2241 parcel.writeInt(0);
2242 }
Julia Reynolds2a128742016-11-28 14:29:25 -05002243 parcel.writeLong(mTimeout);
Julia Reynolds13d898c2017-02-02 12:22:05 -05002244
2245 if (mShortcutId != null) {
2246 parcel.writeInt(1);
2247 parcel.writeString(mShortcutId);
2248 } else {
2249 parcel.writeInt(0);
2250 }
2251
2252 parcel.writeInt(mBadgeIcon);
Julia Reynolds3aedded2017-03-31 14:42:09 -04002253
2254 if (mSettingsText != null) {
2255 parcel.writeInt(1);
2256 TextUtils.writeToParcel(mSettingsText, parcel, flags);
2257 } else {
2258 parcel.writeInt(0);
2259 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002260 }
2261
2262 /**
2263 * Parcelable.Creator that instantiates Notification objects
2264 */
2265 public static final Parcelable.Creator<Notification> CREATOR
2266 = new Parcelable.Creator<Notification>()
2267 {
2268 public Notification createFromParcel(Parcel parcel)
2269 {
2270 return new Notification(parcel);
2271 }
2272
2273 public Notification[] newArray(int size)
2274 {
2275 return new Notification[size];
2276 }
2277 };
2278
2279 /**
2280 * Sets the {@link #contentView} field to be a view with the standard "Latest Event"
2281 * layout.
2282 *
2283 * <p>Uses the {@link #icon} and {@link #when} fields to set the icon and time fields
2284 * in the view.</p>
2285 * @param context The context for your application / activity.
2286 * @param contentTitle The title that goes in the expanded entry.
2287 * @param contentText The text that goes in the expanded entry.
2288 * @param contentIntent The intent to launch when the user clicks the expanded notification.
2289 * If this is an activity, it must include the
2290 * {@link android.content.Intent#FLAG_ACTIVITY_NEW_TASK} flag, which requires
Scott Main7aee61f2011-02-08 11:25:01 -08002291 * that you take care of task management as described in the
2292 * <a href="{@docRoot}guide/topics/fundamentals/tasks-and-back-stack.html">Tasks and Back
2293 * Stack</a> document.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002294 *
Joe Onorato46439ce2010-11-19 13:56:21 -08002295 * @deprecated Use {@link Builder} instead.
Chris Wrena05db382015-06-24 15:18:34 -04002296 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002297 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002298 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002299 public void setLatestEventInfo(Context context,
2300 CharSequence contentTitle, CharSequence contentText, PendingIntent contentIntent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002301 if (context.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1){
2302 Log.e(TAG, "setLatestEventInfo() is deprecated and you should feel deprecated.",
2303 new Throwable());
2304 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002305
Selim Cinek4ac6f602016-06-13 15:47:03 -07002306 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2307 extras.putBoolean(EXTRA_SHOW_WHEN, true);
2308 }
2309
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002310 // ensure that any information already set directly is preserved
2311 final Notification.Builder builder = new Notification.Builder(context, this);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002312
2313 // now apply the latestEventInfo fields
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002314 if (contentTitle != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002315 builder.setContentTitle(contentTitle);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002316 }
2317 if (contentText != null) {
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002318 builder.setContentText(contentText);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002319 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05002320 builder.setContentIntent(contentIntent);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002321
2322 builder.build(); // callers expect this notification to be ready to use
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002323 }
2324
Julia Reynoldsda303542015-11-23 14:00:20 -05002325 /**
2326 * @hide
2327 */
2328 public static void addFieldsFromContext(Context context, Notification notification) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002329 addFieldsFromContext(context.getApplicationInfo(), notification);
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002330 }
2331
2332 /**
2333 * @hide
2334 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002335 public static void addFieldsFromContext(ApplicationInfo ai, Notification notification) {
Jeff Sharkey012bc7b2016-04-11 16:30:27 -06002336 notification.extras.putParcelable(EXTRA_BUILDER_APPLICATION_INFO, ai);
Julia Reynoldsda303542015-11-23 14:00:20 -05002337 }
2338
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002339 @Override
2340 public String toString() {
2341 StringBuilder sb = new StringBuilder();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002342 sb.append("Notification(pri=");
2343 sb.append(priority);
2344 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002345 if (contentView != null) {
2346 sb.append(contentView.getPackage());
2347 sb.append("/0x");
2348 sb.append(Integer.toHexString(contentView.getLayoutId()));
2349 } else {
2350 sb.append("null");
2351 }
2352 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002353 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2354 sb.append("default");
2355 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002356 int N = this.vibrate.length-1;
2357 sb.append("[");
2358 for (int i=0; i<N; i++) {
2359 sb.append(this.vibrate[i]);
2360 sb.append(',');
2361 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002362 if (N != -1) {
2363 sb.append(this.vibrate[N]);
2364 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002365 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002366 } else {
2367 sb.append("null");
2368 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002369 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002370 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002371 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002372 } else if (this.sound != null) {
2373 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002374 } else {
2375 sb.append("null");
2376 }
Chris Wren365b6d32015-07-16 10:39:26 -04002377 if (this.tickerText != null) {
2378 sb.append(" tick");
2379 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002380 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002381 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002382 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002383 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002384 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002385 if (this.category != null) {
2386 sb.append(" category=");
2387 sb.append(this.category);
2388 }
2389 if (this.mGroupKey != null) {
2390 sb.append(" groupKey=");
2391 sb.append(this.mGroupKey);
2392 }
2393 if (this.mSortKey != null) {
2394 sb.append(" sortKey=");
2395 sb.append(this.mSortKey);
2396 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002397 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002398 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002399 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002400 }
2401 sb.append(" vis=");
2402 sb.append(visibilityToString(this.visibility));
2403 if (this.publicVersion != null) {
2404 sb.append(" publicVersion=");
2405 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002406 }
2407 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002408 return sb.toString();
2409 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002410
Dan Sandler1b718782014-07-18 12:43:45 -04002411 /**
2412 * {@hide}
2413 */
2414 public static String visibilityToString(int vis) {
2415 switch (vis) {
2416 case VISIBILITY_PRIVATE:
2417 return "PRIVATE";
2418 case VISIBILITY_PUBLIC:
2419 return "PUBLIC";
2420 case VISIBILITY_SECRET:
2421 return "SECRET";
2422 default:
2423 return "UNKNOWN(" + String.valueOf(vis) + ")";
2424 }
2425 }
2426
Joe Onoratocb109a02011-01-18 17:57:41 -08002427 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002428 * {@hide}
2429 */
2430 public static String priorityToString(@Priority int pri) {
2431 switch (pri) {
2432 case PRIORITY_MIN:
2433 return "MIN";
2434 case PRIORITY_LOW:
2435 return "LOW";
2436 case PRIORITY_DEFAULT:
2437 return "DEFAULT";
2438 case PRIORITY_HIGH:
2439 return "HIGH";
2440 case PRIORITY_MAX:
2441 return "MAX";
2442 default:
2443 return "UNKNOWN(" + String.valueOf(pri) + ")";
2444 }
2445 }
2446
2447 /**
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002448 * Returns the id of the channel this notification posts to.
2449 */
Julia Reynolds37856052016-11-11 09:20:07 -05002450 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002451 return mChannelId;
2452 }
2453
2454 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002455 * Returns the time at which this notification should be canceled by the system, if it's not
2456 * canceled already.
Julia Reynolds2a128742016-11-28 14:29:25 -05002457 */
2458 public long getTimeout() {
2459 return mTimeout;
2460 }
2461
2462 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002463 * @removed
Julia Reynolds13d898c2017-02-02 12:22:05 -05002464 * Returns what icon should be shown for this notification if it is being displayed in a
2465 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2466 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2467 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002468 @Deprecated
Julia Reynolds13d898c2017-02-02 12:22:05 -05002469 public int getBadgeIcon() {
2470 return mBadgeIcon;
2471 }
2472
2473 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002474 * Returns what icon should be shown for this notification if it is being displayed in a
2475 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2476 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2477 */
2478 public int getBadgeIconType() {
2479 return mBadgeIcon;
2480 }
2481
2482 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002483 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
2484 */
2485 public String getShortcutId() {
2486 return mShortcutId;
2487 }
2488
Julia Reynolds3aedded2017-03-31 14:42:09 -04002489
2490 /**
2491 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
2492 */
2493 public CharSequence getSettingsText() {
2494 return mSettingsText;
2495 }
2496
Julia Reynolds13d898c2017-02-02 12:22:05 -05002497 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002498 * The small icon representing this notification in the status bar and content view.
2499 *
2500 * @return the small icon representing this notification.
2501 *
2502 * @see Builder#getSmallIcon()
2503 * @see Builder#setSmallIcon(Icon)
2504 */
2505 public Icon getSmallIcon() {
2506 return mSmallIcon;
2507 }
2508
2509 /**
2510 * Used when notifying to clean up legacy small icons.
2511 * @hide
2512 */
2513 public void setSmallIcon(Icon icon) {
2514 mSmallIcon = icon;
2515 }
2516
2517 /**
2518 * The large icon shown in this notification's content view.
2519 * @see Builder#getLargeIcon()
2520 * @see Builder#setLargeIcon(Icon)
2521 */
2522 public Icon getLargeIcon() {
2523 return mLargeIcon;
2524 }
2525
2526 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002527 * @hide
2528 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002529 public boolean isGroupSummary() {
2530 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2531 }
2532
2533 /**
2534 * @hide
2535 */
2536 public boolean isGroupChild() {
2537 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2538 }
2539
2540 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002541 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002542 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002543 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002544 * content views using the platform's notification layout template. If your app supports
2545 * versions of Android as old as API level 4, you can instead use
2546 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2547 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2548 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002549 *
Scott Main183bf112012-08-13 19:12:13 -07002550 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002551 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002552 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002553 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002554 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2555 * .setContentText(subject)
2556 * .setSmallIcon(R.drawable.new_mail)
2557 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002558 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002559 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002560 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002561 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002562 /**
2563 * @hide
2564 */
2565 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2566 "android.rebuild.contentViewActionCount";
2567 /**
2568 * @hide
2569 */
2570 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2571 = "android.rebuild.bigViewActionCount";
2572 /**
2573 * @hide
2574 */
2575 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2576 = "android.rebuild.hudViewActionCount";
2577
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002578 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002579
Selim Cinek6743c0b2017-01-18 18:24:01 -08002580 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
2581 SystemProperties.getBoolean("notifications.only_title", true);
2582
Joe Onorato46439ce2010-11-19 13:56:21 -08002583 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002584 private Notification mN;
2585 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002586 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002587 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2588 private ArrayList<String> mPersonList = new ArrayList<String>();
2589 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08002590 private boolean mIsLegacy;
2591 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002592
2593 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002594 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2595 */
2596 private int mCachedContrastColor = COLOR_INVALID;
2597 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08002598 /**
2599 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
2600 */
2601 private int mCachedAmbientColor = COLOR_INVALID;
2602 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08002603
2604 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08002605 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
2606 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
2607 */
2608 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08002609 private int mTextColorsAreForBackground = COLOR_INVALID;
2610 private int mPrimaryTextColor = COLOR_INVALID;
2611 private int mSecondaryTextColor = COLOR_INVALID;
2612 private int mActionBarColor = COLOR_INVALID;
Adrian Roos70d7aa32017-01-11 15:39:06 -08002613
2614 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002615 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002616 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002617
2618 * <table>
2619 * <tr><th align=right>priority</th>
2620 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2621 * <tr><th align=right>when</th>
2622 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2623 * <tr><th align=right>audio stream</th>
2624 * <td>{@link #STREAM_DEFAULT}</td></tr>
2625 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002626 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002627
2628 * @param context
2629 * A {@link Context} that will be used by the Builder to construct the
2630 * RemoteViews. The Context will not be held past the lifetime of this Builder
2631 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002632 * @param channelId
2633 * The constructed Notification will be posted on this
2634 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
2635 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08002636 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002637 public Builder(Context context, String channelId) {
2638 this(context, (Notification) null);
2639 mN.mChannelId = channelId;
2640 }
2641
2642 /**
2643 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
2644 * instead. All posted Notifications must specify a NotificationChannel Id.
2645 */
2646 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002647 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002648 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002649 }
2650
Joe Onoratocb109a02011-01-18 17:57:41 -08002651 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002652 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002653 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002654 public Builder(Context context, Notification toAdopt) {
2655 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002656
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002657 if (toAdopt == null) {
2658 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002659 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2660 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2661 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002662 mN.priority = PRIORITY_DEFAULT;
2663 mN.visibility = VISIBILITY_PRIVATE;
2664 } else {
2665 mN = toAdopt;
2666 if (mN.actions != null) {
2667 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002668 }
2669
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002670 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2671 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2672 }
2673
Selim Cinek4ac6f602016-06-13 15:47:03 -07002674 if (mN.getSmallIcon() == null && mN.icon != 0) {
2675 setSmallIcon(mN.icon);
2676 }
2677
2678 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
2679 setLargeIcon(mN.largeIcon);
2680 }
2681
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002682 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2683 if (!TextUtils.isEmpty(templateClass)) {
2684 final Class<? extends Style> styleClass
2685 = getNotificationStyleClass(templateClass);
2686 if (styleClass == null) {
2687 Log.d(TAG, "Unknown style class: " + templateClass);
2688 } else {
2689 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002690 final Constructor<? extends Style> ctor =
2691 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002692 ctor.setAccessible(true);
2693 final Style style = ctor.newInstance();
2694 style.restoreFromExtras(mN.extras);
2695
2696 if (style != null) {
2697 setStyle(style);
2698 }
2699 } catch (Throwable t) {
2700 Log.e(TAG, "Could not create Style", t);
2701 }
2702 }
2703 }
2704
2705 }
2706 }
2707
2708 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08002709 if (mColorUtil == null) {
2710 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002711 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002712 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002713 }
2714
2715 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002716 * If this notification is duplicative of a Launcher shortcut, sets the
2717 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
2718 * the shortcut.
2719 *
2720 * This field will be ignored by Launchers that don't support badging or
2721 * {@link android.content.pm.ShortcutManager shortcuts}.
2722 *
2723 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
2724 * supersedes
2725 */
2726 public Builder setShortcutId(String shortcutId) {
2727 mN.mShortcutId = shortcutId;
2728 return this;
2729 }
2730
2731 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002732 * @removed
Julia Reynolds13d898c2017-02-02 12:22:05 -05002733 * Sets which icon to display as a badge for this notification.
2734 *
2735 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
2736 * {@link #BADGE_ICON_LARGE}.
2737 *
2738 * Note: This value might be ignored, for launchers that don't support badge icons.
2739 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002740 @Deprecated
Julia Reynolds13d898c2017-02-02 12:22:05 -05002741 public Builder chooseBadgeIcon(int icon) {
2742 mN.mBadgeIcon = icon;
2743 return this;
2744 }
2745
2746 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002747 * Sets which icon to display as a badge for this notification.
2748 *
2749 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
2750 * {@link #BADGE_ICON_LARGE}.
2751 *
2752 * Note: This value might be ignored, for launchers that don't support badge icons.
2753 */
Julia Reynolds612beb22017-03-30 10:48:30 -04002754 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002755 mN.mBadgeIcon = icon;
2756 return this;
2757 }
2758
2759 /**
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002760 * Specifies the channel the notification should be delivered on.
2761 */
2762 public Builder setChannel(String channelId) {
2763 mN.mChannelId = channelId;
2764 return this;
2765 }
2766
2767 /**
Julia Reynolds50989772017-02-23 14:32:16 -05002768 * Specifies a duration in milliseconds after which this notification should be canceled,
2769 * if it is not already canceled.
Julia Reynolds2a128742016-11-28 14:29:25 -05002770 */
Julia Reynolds50989772017-02-23 14:32:16 -05002771 public Builder setTimeout(long durationMs) {
2772 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05002773 return this;
2774 }
2775
2776 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002777 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002778 *
2779 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2780 * shown anymore by default and must be opted into by using
2781 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002782 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002783 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002784 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002785 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002786 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002787 return this;
2788 }
2789
Joe Onoratocb109a02011-01-18 17:57:41 -08002790 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002791 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002792 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002793 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2794 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002795 */
2796 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002797 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002798 return this;
2799 }
2800
2801 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002802 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002803 *
2804 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002805 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002806 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002807 * Useful when showing an elapsed time (like an ongoing phone call).
2808 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002809 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07002810 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08002811 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002812 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002813 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07002814 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002815 */
2816 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002817 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002818 return this;
2819 }
2820
2821 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002822 * Sets the Chronometer to count down instead of counting up.
2823 *
2824 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2825 * If it isn't set the chronometer will count up.
2826 *
2827 * @see #setUsesChronometer(boolean)
2828 */
Adrian Roos96b7e202016-05-17 13:50:38 -07002829 public Builder setChronometerCountDown(boolean countDown) {
2830 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08002831 return this;
2832 }
2833
2834 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002835 * Set the small icon resource, which will be used to represent the notification in the
2836 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002837 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002838
2839 * The platform template for the expanded view will draw this icon in the left, unless a
2840 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2841 * icon will be moved to the right-hand side.
2842 *
2843
2844 * @param icon
2845 * A resource ID in the application's package of the drawable to use.
2846 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002847 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002848 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002849 return setSmallIcon(icon != 0
2850 ? Icon.createWithResource(mContext, icon)
2851 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002852 }
2853
Joe Onoratocb109a02011-01-18 17:57:41 -08002854 /**
2855 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2856 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2857 * LevelListDrawable}.
2858 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002859 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002860 * @param level The level to use for the icon.
2861 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002862 * @see Notification#icon
2863 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002864 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002865 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002866 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002867 return setSmallIcon(icon);
2868 }
2869
2870 /**
2871 * Set the small icon, which will be used to represent the notification in the
2872 * status bar and content view (unless overriden there by a
2873 * {@link #setLargeIcon(Bitmap) large icon}).
2874 *
2875 * @param icon An Icon object to use.
2876 * @see Notification#icon
2877 */
2878 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002879 mN.setSmallIcon(icon);
2880 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2881 mN.icon = icon.getResId();
2882 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002883 return this;
2884 }
2885
Joe Onoratocb109a02011-01-18 17:57:41 -08002886 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002887 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002888 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002889 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002890 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002891 return this;
2892 }
2893
Joe Onoratocb109a02011-01-18 17:57:41 -08002894 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002895 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002896 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002897 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002898 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002899 return this;
2900 }
2901
Joe Onoratocb109a02011-01-18 17:57:41 -08002902 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002903 * This provides some additional information that is displayed in the notification. No
2904 * guarantees are given where exactly it is displayed.
2905 *
2906 * <p>This information should only be provided if it provides an essential
2907 * benefit to the understanding of the notification. The more text you provide the
2908 * less readable it becomes. For example, an email client should only provide the account
2909 * name here if more than one email account has been added.</p>
2910 *
2911 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
2912 * notification header area.
2913 *
2914 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
2915 * this will be shown in the third line of text in the platform notification template.
2916 * You should not be using {@link #setProgress(int, int, boolean)} at the
2917 * same time on those versions; they occupy the same place.
2918 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002919 */
2920 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002921 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002922 return this;
2923 }
2924
2925 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04002926 * Provides text that will appear as a link to your application's settings.
2927 *
2928 * <p>This text does not appear within notification {@link Style templates} but may
2929 * appear when the user uses an affordance to learn more about the notification.
2930 * Additionally, this text will not appear unless you provide a valid link target by
2931 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
2932 *
2933 * <p>This text is meant to be concise description about what the user can customize
2934 * when they click on this link. The recommended maximum length is 40 characters.
2935 * @param text
2936 * @return
2937 */
2938 public Builder setSettingsText(CharSequence text) {
2939 mN.mSettingsText = safeCharSequence(text);
2940 return this;
2941 }
2942
2943 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002944 * Set the remote input history.
2945 *
2946 * This should be set to the most recent inputs that have been sent
2947 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2948 * longer relevant (e.g. for chat notifications once the other party has responded).
2949 *
2950 * The most recent input must be stored at the 0 index, the second most recent at the
2951 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2952 * and how much of each individual input is shown.
2953 *
2954 * <p>Note: The reply text will only be shown on notifications that have least one action
2955 * with a {@code RemoteInput}.</p>
2956 */
2957 public Builder setRemoteInputHistory(CharSequence[] text) {
2958 if (text == null) {
2959 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2960 } else {
2961 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2962 CharSequence[] safe = new CharSequence[N];
2963 for (int i = 0; i < N; i++) {
2964 safe[i] = safeCharSequence(text[i]);
2965 }
2966 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2967 }
2968 return this;
2969 }
2970
2971 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002972 * Sets the number of items this notification represents. May be displayed as a badge count
2973 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08002974 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002975 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002976 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002977 return this;
2978 }
2979
Joe Onoratocb109a02011-01-18 17:57:41 -08002980 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002981 * A small piece of additional information pertaining to this notification.
2982 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002983 * The platform template will draw this on the last line of the notification, at the far
2984 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002985 *
2986 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
2987 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
2988 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08002989 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002990 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002991 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002992 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002993 return this;
2994 }
2995
Joe Onoratocb109a02011-01-18 17:57:41 -08002996 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002997 * Set the progress this notification represents.
2998 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002999 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003000 */
3001 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003002 mN.extras.putInt(EXTRA_PROGRESS, progress);
3003 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3004 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003005 return this;
3006 }
3007
3008 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003009 * Supply a custom RemoteViews to use instead of the platform template.
3010 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003011 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003012 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003013 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003014 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003015 return setCustomContentView(views);
3016 }
3017
3018 /**
3019 * Supply custom RemoteViews to use instead of the platform template.
3020 *
3021 * This will override the layout that would otherwise be constructed by this Builder
3022 * object.
3023 */
3024 public Builder setCustomContentView(RemoteViews contentView) {
3025 mN.contentView = contentView;
3026 return this;
3027 }
3028
3029 /**
3030 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3031 *
3032 * This will override the expanded layout that would otherwise be constructed by this
3033 * Builder object.
3034 */
3035 public Builder setCustomBigContentView(RemoteViews contentView) {
3036 mN.bigContentView = contentView;
3037 return this;
3038 }
3039
3040 /**
3041 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3042 *
3043 * This will override the heads-up layout that would otherwise be constructed by this
3044 * Builder object.
3045 */
3046 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3047 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003048 return this;
3049 }
3050
Joe Onoratocb109a02011-01-18 17:57:41 -08003051 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003052 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3053 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003054 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3055 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3056 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003057 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003058 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003059 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003060 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003061 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003062 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003063 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003064 return this;
3065 }
3066
Joe Onoratocb109a02011-01-18 17:57:41 -08003067 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003068 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3069 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003070 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003071 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003072 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003073 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003074 return this;
3075 }
3076
Joe Onoratocb109a02011-01-18 17:57:41 -08003077 /**
3078 * An intent to launch instead of posting the notification to the status bar.
3079 * Only for use with extremely high-priority notifications demanding the user's
3080 * <strong>immediate</strong> attention, such as an incoming phone call or
3081 * alarm clock that the user has explicitly set to a particular time.
3082 * If this facility is used for something else, please give the user an option
3083 * to turn it off and use a normal notification, as this can be extremely
3084 * disruptive.
3085 *
Chris Wren47c20a12014-06-18 17:27:29 -04003086 * <p>
3087 * The system UI may choose to display a heads-up notification, instead of
3088 * launching this intent, while the user is using the device.
3089 * </p>
3090 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003091 * @param intent The pending intent to launch.
3092 * @param highPriority Passing true will cause this notification to be sent
3093 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003094 *
3095 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003096 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003097 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003098 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003099 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3100 return this;
3101 }
3102
Joe Onoratocb109a02011-01-18 17:57:41 -08003103 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003104 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003105 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003106 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003107 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003108 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003109 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003110 return this;
3111 }
3112
Joe Onoratocb109a02011-01-18 17:57:41 -08003113 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003114 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003115 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003116 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003117 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003118 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003119 setTicker(tickerText);
3120 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003121 return this;
3122 }
3123
Joe Onoratocb109a02011-01-18 17:57:41 -08003124 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003125 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003126 *
3127 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003128 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3129 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003130 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003131 public Builder setLargeIcon(Bitmap b) {
3132 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3133 }
3134
3135 /**
3136 * Add a large icon to the notification content view.
3137 *
3138 * In the platform template, this image will be shown on the left of the notification view
3139 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3140 * badge atop the large icon).
3141 */
3142 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003143 mN.mLargeIcon = icon;
3144 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003145 return this;
3146 }
3147
Joe Onoratocb109a02011-01-18 17:57:41 -08003148 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003149 * Set the sound to play.
3150 *
John Spurlockc0650f022014-07-19 13:22:39 -04003151 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3152 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003153 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003154 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003155 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003156 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003157 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003158 mN.sound = sound;
3159 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003160 return this;
3161 }
3162
Joe Onoratocb109a02011-01-18 17:57:41 -08003163 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003164 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003165 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003166 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3167 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003168 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003169 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003170 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003171 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003172 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003173 mN.sound = sound;
3174 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003175 return this;
3176 }
3177
Joe Onoratocb109a02011-01-18 17:57:41 -08003178 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003179 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3180 * use during playback.
3181 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003182 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003183 * @see Notification#sound
3184 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003185 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003186 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003187 mN.sound = sound;
3188 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003189 return this;
3190 }
3191
3192 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003193 * Set the vibration pattern to use.
3194 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003195 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3196 * <code>pattern</code> parameter.
3197 *
Chris Wren47c20a12014-06-18 17:27:29 -04003198 * <p>
3199 * A notification that vibrates is more likely to be presented as a heads-up notification.
3200 * </p>
3201 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003202 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003203 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003204 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003205 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003206 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003207 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003208 return this;
3209 }
3210
Joe Onoratocb109a02011-01-18 17:57:41 -08003211 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003212 * Set the desired color for the indicator LED on the device, as well as the
3213 * blink duty cycle (specified in milliseconds).
3214 *
3215
3216 * Not all devices will honor all (or even any) of these values.
3217 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003218 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003219 * @see Notification#ledARGB
3220 * @see Notification#ledOnMS
3221 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003222 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003223 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003224 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003225 mN.ledARGB = argb;
3226 mN.ledOnMS = onMs;
3227 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003228 if (onMs != 0 || offMs != 0) {
3229 mN.flags |= FLAG_SHOW_LIGHTS;
3230 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003231 return this;
3232 }
3233
Joe Onoratocb109a02011-01-18 17:57:41 -08003234 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003235 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003236 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003237
3238 * Ongoing notifications cannot be dismissed by the user, so your application or service
3239 * must take care of canceling them.
3240 *
3241
3242 * They are typically used to indicate a background task that the user is actively engaged
3243 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3244 * (e.g., a file download, sync operation, active network connection).
3245 *
3246
3247 * @see Notification#FLAG_ONGOING_EVENT
3248 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003249 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003250 public Builder setOngoing(boolean ongoing) {
3251 setFlag(FLAG_ONGOING_EVENT, ongoing);
3252 return this;
3253 }
3254
Joe Onoratocb109a02011-01-18 17:57:41 -08003255 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003256 * Set whether this notification should be colorized. When set, the color set with
3257 * {@link #setColor(int)} will be used as the background color of this notification.
3258 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003259 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3260 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003261 * <p>
3262 * For most styles, the coloring will only be applied if the notification is ongoing.
3263 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
3264 * that have a media session attached there is no requirement for it to be ongoing.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003265 *
3266 * @see Builder#setOngoing(boolean)
3267 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003268 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003269 */
3270 public Builder setColorized(boolean colorize) {
3271 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3272 return this;
3273 }
3274
3275 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003276 * Set this flag if you would only like the sound, vibrate
3277 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003278 *
3279 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003280 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003281 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3282 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3283 return this;
3284 }
3285
Joe Onoratocb109a02011-01-18 17:57:41 -08003286 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003287 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003288 *
3289 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003290 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003291 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003292 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003293 return this;
3294 }
3295
Joe Onoratocb109a02011-01-18 17:57:41 -08003296 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003297 * Set whether or not this notification should not bridge to other devices.
3298 *
3299 * <p>Some notifications can be bridged to other devices for remote display.
3300 * This hint can be set to recommend this notification not be bridged.
3301 */
3302 public Builder setLocalOnly(boolean localOnly) {
3303 setFlag(FLAG_LOCAL_ONLY, localOnly);
3304 return this;
3305 }
3306
3307 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003308 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003309 * <p>
3310 * The value should be one or more of the following fields combined with
3311 * bitwise-or:
3312 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3313 * <p>
3314 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003315 *
3316 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003317 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003318 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003319 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003320 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003321 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003322 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003323 return this;
3324 }
3325
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003326 /**
3327 * Set the priority of this notification.
3328 *
3329 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003330 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003331 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003332 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003333 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003334 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003335 return this;
3336 }
Joe Malin8d40d042012-11-05 11:36:40 -08003337
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003338 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003339 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003340 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003341 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003342 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003343 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003344 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003345 return this;
3346 }
3347
3348 /**
Chris Wrendde75302014-03-26 17:24:15 -04003349 * Add a person that is relevant to this notification.
3350 *
Chris Wrene6c48932014-09-29 17:19:27 -04003351 * <P>
3352 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003353 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3354 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3355 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003356 * </P>
3357 *
3358 * <P>
3359 * The person should be specified by the {@code String} representation of a
3360 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3361 * </P>
3362 *
3363 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3364 * URIs. The path part of these URIs must exist in the contacts database, in the
3365 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3366 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
3367 * </P>
3368 *
3369 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003370 * @see Notification#EXTRA_PEOPLE
3371 */
Chris Wrene6c48932014-09-29 17:19:27 -04003372 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003373 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04003374 return this;
3375 }
3376
3377 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003378 * Set this notification to be part of a group of notifications sharing the same key.
3379 * Grouped notifications may display in a cluster or stack on devices which
3380 * support such rendering.
3381 *
3382 * <p>To make this notification the summary for its group, also call
3383 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3384 * {@link #setSortKey}.
3385 * @param groupKey The group key of the group.
3386 * @return this object for method chaining
3387 */
3388 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003389 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003390 return this;
3391 }
3392
3393 /**
3394 * Set this notification to be the group summary for a group of notifications.
3395 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003396 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3397 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003398 * @param isGroupSummary Whether this notification should be a group summary.
3399 * @return this object for method chaining
3400 */
3401 public Builder setGroupSummary(boolean isGroupSummary) {
3402 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3403 return this;
3404 }
3405
3406 /**
3407 * Set a sort key that orders this notification among other notifications from the
3408 * same package. This can be useful if an external sort was already applied and an app
3409 * would like to preserve this. Notifications will be sorted lexicographically using this
3410 * value, although providing different priorities in addition to providing sort key may
3411 * cause this value to be ignored.
3412 *
3413 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003414 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003415 *
3416 * @see String#compareTo(String)
3417 */
3418 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003419 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003420 return this;
3421 }
3422
3423 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003424 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003425 *
Griff Hazen720042b2014-02-24 15:46:56 -08003426 * <p>Values within the Bundle will replace existing extras values in this Builder.
3427 *
3428 * @see Notification#extras
3429 */
Griff Hazen959591e2014-05-15 22:26:18 -07003430 public Builder addExtras(Bundle extras) {
3431 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003432 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003433 }
3434 return this;
3435 }
3436
3437 /**
3438 * Set metadata for this notification.
3439 *
3440 * <p>A reference to the Bundle is held for the lifetime of this Builder, and the Bundle's
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003441 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003442 * called.
3443 *
Griff Hazen720042b2014-02-24 15:46:56 -08003444 * <p>Replaces any existing extras values with those from the provided Bundle.
3445 * Use {@link #addExtras} to merge in metadata instead.
3446 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003447 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003448 */
Griff Hazen959591e2014-05-15 22:26:18 -07003449 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003450 if (extras != null) {
3451 mUserExtras = extras;
3452 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003453 return this;
3454 }
3455
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003456 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003457 * Get the current metadata Bundle used by this notification Builder.
3458 *
3459 * <p>The returned Bundle is shared with this Builder.
3460 *
3461 * <p>The current contents of this Bundle are copied into the Notification each time
3462 * {@link #build()} is called.
3463 *
3464 * @see Notification#extras
3465 */
3466 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003467 return mUserExtras;
3468 }
3469
3470 private Bundle getAllExtras() {
3471 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3472 saveExtras.putAll(mN.extras);
3473 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003474 }
3475
3476 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003477 * Add an action to this notification. Actions are typically displayed by
3478 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003479 * <p>
3480 * Every action must have an icon (32dp square and matching the
3481 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3482 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3483 * <p>
3484 * A notification in its expanded form can display up to 3 actions, from left to right in
3485 * the order they were added. Actions will not be displayed when the notification is
3486 * collapsed, however, so be sure that any essential functions may be accessed by the user
3487 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003488 *
3489 * @param icon Resource ID of a drawable that represents the action.
3490 * @param title Text describing the action.
3491 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003492 *
3493 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003494 */
Dan Sandler86647982015-05-13 23:41:13 -04003495 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003496 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003497 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003498 return this;
3499 }
3500
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003501 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003502 * Add an action to this notification. Actions are typically displayed by
3503 * the system as a button adjacent to the notification content.
3504 * <p>
3505 * Every action must have an icon (32dp square and matching the
3506 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3507 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3508 * <p>
3509 * A notification in its expanded form can display up to 3 actions, from left to right in
3510 * the order they were added. Actions will not be displayed when the notification is
3511 * collapsed, however, so be sure that any essential functions may be accessed by the user
3512 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3513 *
3514 * @param action The action to add.
3515 */
3516 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08003517 if (action != null) {
3518 mActions.add(action);
3519 }
Griff Hazen959591e2014-05-15 22:26:18 -07003520 return this;
3521 }
3522
3523 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003524 * Alter the complete list of actions attached to this notification.
3525 * @see #addAction(Action).
3526 *
3527 * @param actions
3528 * @return
3529 */
3530 public Builder setActions(Action... actions) {
3531 mActions.clear();
3532 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08003533 if (actions[i] != null) {
3534 mActions.add(actions[i]);
3535 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003536 }
3537 return this;
3538 }
3539
3540 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003541 * Add a rich notification style to be applied at build time.
3542 *
3543 * @param style Object responsible for modifying the notification style.
3544 */
3545 public Builder setStyle(Style style) {
3546 if (mStyle != style) {
3547 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003548 if (mStyle != null) {
3549 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003550 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3551 } else {
3552 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003553 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003554 }
3555 return this;
3556 }
3557
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003558 /**
3559 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003560 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003561 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
3562 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
3563 *
3564 * @return The same Builder.
3565 */
3566 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003567 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003568 return this;
3569 }
3570
3571 /**
3572 * Supply a replacement Notification whose contents should be shown in insecure contexts
3573 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3574 * @param n A replacement notification, presumably with some or all info redacted.
3575 * @return The same Builder.
3576 */
3577 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003578 if (n != null) {
3579 mN.publicVersion = new Notification();
3580 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3581 } else {
3582 mN.publicVersion = null;
3583 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003584 return this;
3585 }
3586
Griff Hazenb720abe2014-05-20 13:15:30 -07003587 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003588 * Apply an extender to this notification builder. Extenders may be used to add
3589 * metadata or change options on this builder.
3590 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003591 public Builder extend(Extender extender) {
3592 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003593 return this;
3594 }
3595
Dan Sandler4e787062015-06-17 15:09:48 -04003596 /**
3597 * @hide
3598 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003599 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003600 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003601 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003602 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003603 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003604 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003605 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003606 }
3607
Dan Sandler26e81cf2014-05-06 10:01:27 -04003608 /**
3609 * Sets {@link Notification#color}.
3610 *
3611 * @param argb The accent color to use
3612 *
3613 * @return The same Builder.
3614 */
Tor Norbye80756e32015-03-02 09:39:27 -08003615 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003616 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003617 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003618 return this;
3619 }
3620
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003621 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04003622 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
3623 // This user can never be a badged profile,
3624 // and also includes USER_ALL system notifications.
3625 return null;
3626 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02003627 // Note: This assumes that the current user can read the profile badge of the
3628 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003629 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003630 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003631 }
3632
3633 private Bitmap getProfileBadge() {
3634 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003635 if (badge == null) {
3636 return null;
3637 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003638 final int size = mContext.getResources().getDimensionPixelSize(
3639 R.dimen.notification_badge_size);
3640 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003641 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003642 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003643 badge.draw(canvas);
3644 return bitmap;
3645 }
3646
Selim Cinekc848c3a2016-01-13 15:27:30 -08003647 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003648 Bitmap profileBadge = getProfileBadge();
3649
Kenny Guy98193ea2014-07-24 19:54:37 +01003650 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003651 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3652 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003653 if (isColorized()) {
3654 contentView.setDrawableParameters(R.id.profile_badge, false, -1,
3655 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP, -1);
3656 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003657 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003658 }
3659
Christoph Studerfe718432014-09-01 18:21:18 +02003660 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003661 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003662 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003663 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003664 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003665 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003666 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003667 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003668 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003669 contentView.setTextViewText(R.id.text_line_1, null);
Christoph Studerfe718432014-09-01 18:21:18 +02003670 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003671 }
3672
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003673 /**
3674 * Resets the notification header to its original state
3675 */
3676 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07003677 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
3678 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08003679 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003680 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003681 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003682 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07003683 contentView.setTextViewText(R.id.header_text, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003684 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003685 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003686 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003687 contentView.setImageViewIcon(R.id.profile_badge, null);
3688 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003689 }
3690
3691 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003692 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
3693 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003694 }
3695
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003696 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003697 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003698 }
3699
3700 /**
3701 * @param hasProgress whether the progress bar should be shown and set
3702 */
3703 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003704 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
3705 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07003706 }
3707
Adrian Roos70d7aa32017-01-11 15:39:06 -08003708 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01003709 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003710
Christoph Studerfe718432014-09-01 18:21:18 +02003711 resetStandardTemplate(contentView);
3712
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003713 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003714 updateBackgroundColor(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08003715 bindNotificationHeader(contentView, p.ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003716 bindLargeIcon(contentView);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003717 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
3718 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003719 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003720 contentView.setTextViewText(R.id.title, p.title);
Adrian Roos72171622017-01-27 10:32:06 -08003721 if (!p.ambient) {
3722 setTextViewColorPrimary(contentView, R.id.title);
3723 }
Selim Cinek954cc232016-05-20 13:29:23 -07003724 contentView.setViewLayoutWidth(R.id.title, showProgress
3725 ? ViewGroup.LayoutParams.WRAP_CONTENT
3726 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08003727 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08003728 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003729 int textId = showProgress ? com.android.internal.R.id.text_line_1
3730 : com.android.internal.R.id.text;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003731 contentView.setTextViewText(textId, p.text);
Adrian Roos72171622017-01-27 10:32:06 -08003732 if (!p.ambient) {
3733 setTextViewColorSecondary(contentView, textId);
3734 }
Selim Cinek41598732016-01-11 16:58:37 -08003735 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003736 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003737
Selim Cinek279fa862016-06-14 10:57:25 -07003738 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003739
Selim Cinek29603462015-11-17 19:04:39 -08003740 return contentView;
3741 }
3742
Selim Cinek7b9605b2017-01-19 17:36:00 -08003743 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
3744 ensureColors();
3745 contentView.setTextColor(id, mPrimaryTextColor);
3746 }
3747
3748 private int getPrimaryTextColor() {
3749 ensureColors();
3750 return mPrimaryTextColor;
3751 }
3752
3753 private int getActionBarColor() {
3754 ensureColors();
3755 return mActionBarColor;
3756 }
3757
3758 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
3759 ensureColors();
3760 contentView.setTextColor(id, mSecondaryTextColor);
3761 }
3762
3763 private void ensureColors() {
3764 int backgroundColor = getBackgroundColor();
3765 if (mPrimaryTextColor == COLOR_INVALID
3766 || mSecondaryTextColor == COLOR_INVALID
3767 || mActionBarColor == COLOR_INVALID
3768 || mTextColorsAreForBackground != backgroundColor) {
3769 mTextColorsAreForBackground = backgroundColor;
3770 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(
3771 mContext, backgroundColor);
3772 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(
3773 mContext, backgroundColor);
Selim Cinek875ba9b2017-02-13 16:20:17 -08003774 mActionBarColor = NotificationColorUtil.resolveActionBarColor(mContext,
3775 backgroundColor);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003776 }
3777 }
3778
3779 private void updateBackgroundColor(RemoteViews contentView) {
3780 if (isColorized()) {
3781 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
3782 getBackgroundColor());
3783 } else {
3784 // Clear it!
3785 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
3786 0);
3787 }
3788 }
3789
Selim Cinek860b6da2015-12-16 19:02:19 -08003790 /**
3791 * @param remoteView the remote view to update the minheight in
3792 * @param hasMinHeight does it have a mimHeight
3793 * @hide
3794 */
3795 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3796 int minHeight = 0;
3797 if (hasMinHeight) {
3798 // we need to set the minHeight of the notification
3799 minHeight = mContext.getResources().getDimensionPixelSize(
3800 com.android.internal.R.dimen.notification_min_content_height);
3801 }
3802 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3803 }
3804
Selim Cinek29603462015-11-17 19:04:39 -08003805 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003806 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3807 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3808 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3809 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003810 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003811 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003812 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003813 contentView.setProgressBackgroundTintList(
3814 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3815 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003816 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003817 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003818 contentView.setProgressTintList(R.id.progress, colorStateList);
3819 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003820 }
Selim Cinek29603462015-11-17 19:04:39 -08003821 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003822 } else {
3823 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003824 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003825 }
Joe Onorato561d3852010-11-20 18:09:34 -08003826 }
3827
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003828 private void bindLargeIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07003829 if (mN.mLargeIcon == null && mN.largeIcon != null) {
3830 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
3831 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003832 if (mN.mLargeIcon != null) {
3833 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3834 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3835 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003836 int endMargin = R.dimen.notification_content_picture_margin;
3837 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
3838 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
3839 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003840 }
3841 }
3842
Adrian Roos487374f2017-01-11 15:48:14 -08003843 private void bindNotificationHeader(RemoteViews contentView, boolean ambient) {
3844 bindSmallIcon(contentView, ambient);
3845 bindHeaderAppName(contentView, ambient);
3846 if (!ambient) {
3847 // Ambient view does not have these
3848 bindHeaderText(contentView);
3849 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08003850 bindProfileBadge(contentView);
3851 }
Adrian Roosd83e9992017-03-16 15:17:57 -07003852 bindExpandButton(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003853 }
3854
3855 private void bindExpandButton(RemoteViews contentView) {
Selim Cinek99104832017-01-25 14:47:33 -08003856 int color = getPrimaryHighlightColor();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003857 contentView.setDrawableParameters(R.id.expand_button, false, -1, color,
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003858 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003859 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08003860 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003861 }
3862
Selim Cinek99104832017-01-25 14:47:33 -08003863 /**
3864 * @return the color that is used as the first primary highlight color. This is applied
3865 * in several places like the action buttons or the app name in the header.
3866 */
3867 private int getPrimaryHighlightColor() {
3868 return isColorized() ? getPrimaryTextColor() : resolveContrastColor();
3869 }
3870
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003871 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3872 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003873 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003874 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003875 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3876 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3877 contentView.setLong(R.id.chronometer, "setBase",
3878 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3879 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07003880 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07003881 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003882 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003883 } else {
3884 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3885 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003886 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003887 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003888 } else {
3889 // We still want a time to be set but gone, such that we can show and hide it
3890 // on demand in case it's a child notification without anything in the header
3891 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003892 }
3893 }
3894
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003895 private void bindHeaderText(RemoteViews contentView) {
3896 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3897 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05003898 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003899 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05003900 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003901 if (headerText == null
3902 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3903 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3904 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
3905 }
3906 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003907 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003908 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08003909 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003910 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
3911 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003912 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003913 }
3914 }
3915
Adrian Rooseba05822016-04-22 17:09:27 -07003916 /**
3917 * @hide
3918 */
3919 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04003920 CharSequence name = null;
3921 final PackageManager pm = mContext.getPackageManager();
3922 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
3923 // only system packages which lump together a bunch of unrelated stuff
3924 // may substitute a different name to make the purpose of the
3925 // notification more clear. the correct package label should always
3926 // be accessible via SystemUI.
3927 final String pkg = mContext.getPackageName();
3928 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
3929 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
3930 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
3931 name = subName;
3932 } else {
3933 Log.w(TAG, "warning: pkg "
3934 + pkg + " attempting to substitute app name '" + subName
3935 + "' without holding perm "
3936 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
3937 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003938 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04003939 if (TextUtils.isEmpty(name)) {
3940 name = pm.getApplicationLabel(mContext.getApplicationInfo());
3941 }
3942 if (TextUtils.isEmpty(name)) {
3943 // still nothing?
3944 return null;
3945 }
3946
3947 return String.valueOf(name);
3948 }
Adrian Roos487374f2017-01-11 15:48:14 -08003949 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04003950 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08003951 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08003952 setTextViewColorPrimary(contentView, R.id.app_name_text);
3953 } else {
3954 contentView.setTextColor(R.id.app_name_text,
3955 ambient ? resolveAmbientColor() : resolveContrastColor());
3956 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003957 }
3958
Adrian Roos487374f2017-01-11 15:48:14 -08003959 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07003960 if (mN.mSmallIcon == null && mN.icon != 0) {
3961 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
3962 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003963 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Adrian Roos9b45a152016-06-28 13:32:29 -07003964 contentView.setDrawableParameters(R.id.icon, false /* targetBackground */,
3965 -1 /* alpha */, -1 /* colorFilter */, null /* mode */, mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08003966 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003967 }
3968
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003969 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003970 * @return true if the built notification will show the time or the chronometer; false
3971 * otherwise
3972 */
3973 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07003974 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003975 }
3976
Christoph Studerfe718432014-09-01 18:21:18 +02003977 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003978 // actions_container is only reset when there are no actions to avoid focus issues with
3979 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02003980 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003981 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003982
3983 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3984 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3985
3986 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3987 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3988 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3989 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07003990
3991 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003992 }
3993
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003994 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003995 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07003996 }
3997
Adrian Roos70d7aa32017-01-11 15:39:06 -08003998 private RemoteViews applyStandardTemplateWithActions(int layoutId,
3999 StandardTemplateParams p) {
4000 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004001
Christoph Studerfe718432014-09-01 18:21:18 +02004002 resetStandardTemplateWithActions(big);
4003
Adrian Roose458aa82015-12-08 16:17:19 -08004004 boolean validRemoteInput = false;
4005
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004006 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004007 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004008 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004009 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004010 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004011 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roos487374f2017-01-11 15:48:14 -08004012 if (p.ambient) {
4013 big.setInt(R.id.actions, "setBackgroundColor", Color.TRANSPARENT);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004014 } else if (isColorized()) {
4015 big.setInt(R.id.actions, "setBackgroundColor", getActionBarColor());
4016 } else {
4017 big.setInt(R.id.actions, "setBackgroundColor", mContext.getColor(
4018 R.color.notification_action_list));
Adrian Roos487374f2017-01-11 15:48:14 -08004019 }
Adrian Roosf852a422016-06-03 13:33:43 -07004020 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4021 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004022 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004023 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004024 Action action = mActions.get(i);
4025 validRemoteInput |= hasValidRemoteInput(action);
4026
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004027 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004028 i % 2 != 0, p.ambient);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004029 big.addView(R.id.actions, button);
4030 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004031 } else {
4032 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004033 }
Adrian Roose458aa82015-12-08 16:17:19 -08004034
4035 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004036 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08004037 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
4038 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
4039 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004040 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08004041
4042 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
4043 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
4044 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004045 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004046
4047 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
4048 big.setViewVisibility(
4049 R.id.notification_material_reply_text_3, View.VISIBLE);
4050 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004051 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004052 }
4053 }
4054 }
4055
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004056 return big;
4057 }
4058
Adrian Roose458aa82015-12-08 16:17:19 -08004059 private boolean hasValidRemoteInput(Action action) {
4060 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4061 // Weird actions
4062 return false;
4063 }
4064
4065 RemoteInput[] remoteInputs = action.getRemoteInputs();
4066 if (remoteInputs == null) {
4067 return false;
4068 }
4069
4070 for (RemoteInput r : remoteInputs) {
4071 CharSequence[] choices = r.getChoices();
4072 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4073 return true;
4074 }
4075 }
4076 return false;
4077 }
4078
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004079 /**
4080 * Construct a RemoteViews for the final 1U notification layout. In order:
4081 * 1. Custom contentView from the caller
4082 * 2. Style's proposed content view
4083 * 3. Standard template view
4084 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004085 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004086 return createContentView(false /* increasedheight */ );
4087 }
4088
4089 /**
4090 * Construct a RemoteViews for the smaller content view.
4091 *
4092 * @param increasedHeight true if this layout be created with an increased height. Some
4093 * styles may support showing more then just that basic 1U size
4094 * and the system may decide to render important notifications
4095 * slightly bigger even when collapsed.
4096 *
4097 * @hide
4098 */
4099 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08004100 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004101 return mN.contentView;
4102 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004103 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004104 if (styleView != null) {
4105 return styleView;
4106 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004107 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004108 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004109 }
4110
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004111 /**
4112 * Construct a RemoteViews for the final big notification layout.
4113 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004114 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004115 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08004116 if (mN.bigContentView != null
4117 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004118 return mN.bigContentView;
4119 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004120 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004121 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004122 } else if (mActions.size() != 0) {
4123 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004124 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004125 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004126 return result;
4127 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004128
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004129 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004130 * Construct a RemoteViews for the final notification header only. This will not be
4131 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004132 *
4133 * @hide
4134 */
4135 public RemoteViews makeNotificationHeader() {
Selim Cinek414ad332017-02-24 19:06:12 -08004136 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4137 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004138 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
4139 R.layout.notification_template_header);
4140 resetNotificationHeader(header);
Adrian Roos487374f2017-01-11 15:48:14 -08004141 bindNotificationHeader(header, false /* ambient */);
Selim Cinek414ad332017-02-24 19:06:12 -08004142 if (colorized != null) {
4143 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4144 } else {
4145 mN.extras.remove(EXTRA_COLORIZED);
4146 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004147 return header;
4148 }
4149
Adrian Roos487374f2017-01-11 15:48:14 -08004150 /**
4151 * Construct a RemoteViews for the ambient version of the notification.
4152 *
4153 * @hide
4154 */
4155 public RemoteViews makeAmbientNotification() {
4156 RemoteViews ambient = applyStandardTemplateWithActions(
4157 R.layout.notification_template_material_ambient,
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004158 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false));
Adrian Roos487374f2017-01-11 15:48:14 -08004159 return ambient;
4160 }
4161
Selim Cinek29603462015-11-17 19:04:39 -08004162 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004163 if (result != null) {
4164 result.setViewVisibility(R.id.text_line_1, View.GONE);
4165 }
Selim Cinek29603462015-11-17 19:04:39 -08004166 }
4167
Selim Cinek6743c0b2017-01-18 18:24:01 -08004168 /**
4169 * Adapt the Notification header if this view is used as an expanded view.
4170 *
4171 * @hide
4172 */
4173 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004174 if (result != null) {
4175 result.setBoolean(R.id.notification_header, "setExpanded", true);
4176 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004177 }
4178
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004179 /**
4180 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004181 *
4182 * @param increasedHeight true if this layout be created with an increased height. Some
4183 * styles may support showing more then just that basic 1U size
4184 * and the system may decide to render important notifications
4185 * slightly bigger even when collapsed.
4186 *
4187 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004188 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004189 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08004190 if (mN.headsUpContentView != null
4191 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004192 return mN.headsUpContentView;
4193 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004194 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4195 if (styleView != null) {
4196 return styleView;
4197 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004198 } else if (mActions.size() == 0) {
4199 return null;
4200 }
4201
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004202 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004203 }
4204
Selim Cinek624c02db2015-12-14 21:00:02 -08004205 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08004206 * Construct a RemoteViews for the final heads-up notification layout.
4207 */
4208 public RemoteViews createHeadsUpContentView() {
4209 return createHeadsUpContentView(false /* useIncreasedHeight */);
4210 }
4211
4212 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08004213 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4214 *
4215 * @hide
4216 */
4217 public RemoteViews makePublicContentView() {
4218 if (mN.publicVersion != null) {
4219 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Julia Reynolds3b848122016-02-26 10:45:32 -05004220 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004221 }
4222 Bundle savedBundle = mN.extras;
4223 Style style = mStyle;
4224 mStyle = null;
4225 Icon largeIcon = mN.mLargeIcon;
4226 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004227 Bitmap largeIconLegacy = mN.largeIcon;
4228 mN.largeIcon = null;
Selim Cinek624c02db2015-12-14 21:00:02 -08004229 Bundle publicExtras = new Bundle();
4230 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
4231 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
4232 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
4233 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07004234 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
4235 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08004236 publicExtras.putCharSequence(EXTRA_TITLE,
4237 mContext.getString(R.string.notification_hidden_text));
4238 mN.extras = publicExtras;
4239 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
4240 mN.extras = savedBundle;
4241 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07004242 mN.largeIcon = largeIconLegacy;
Selim Cinek624c02db2015-12-14 21:00:02 -08004243 mStyle = style;
4244 return publicView;
4245 }
4246
Selim Cinek6743c0b2017-01-18 18:24:01 -08004247 /**
4248 * Construct a content view for the display when low - priority
4249 *
4250 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
4251 * a new subtext is created consisting of the content of the
4252 * notification.
4253 * @hide
4254 */
4255 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
4256 int color = mN.color;
4257 mN.color = COLOR_DEFAULT;
4258 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4259 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
4260 CharSequence newSummary = createSummaryText();
4261 if (!TextUtils.isEmpty(newSummary)) {
4262 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
4263 }
4264 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004265
Selim Cinek6743c0b2017-01-18 18:24:01 -08004266 RemoteViews header = makeNotificationHeader();
Selim Cinek1b554392017-02-28 17:22:49 -08004267 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08004268 if (summary != null) {
4269 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
4270 } else {
4271 mN.extras.remove(EXTRA_SUB_TEXT);
4272 }
4273 mN.color = color;
4274 return header;
4275 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004276
Selim Cinek6743c0b2017-01-18 18:24:01 -08004277 private CharSequence createSummaryText() {
4278 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
4279 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
4280 return titleText;
4281 }
4282 SpannableStringBuilder summary = new SpannableStringBuilder();
4283 if (titleText == null) {
4284 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
4285 }
4286 BidiFormatter bidi = BidiFormatter.getInstance();
4287 if (titleText != null) {
4288 summary.append(bidi.unicodeWrap(titleText));
4289 }
4290 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
4291 if (titleText != null && contentText != null) {
4292 summary.append(bidi.unicodeWrap(mContext.getText(
4293 R.string.notification_header_divider_symbol_with_spaces)));
4294 }
4295 if (contentText != null) {
4296 summary.append(bidi.unicodeWrap(contentText));
4297 }
4298 return summary;
4299 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05004300
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004301 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004302 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04004303 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004304 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004305 emphazisedMode ? getEmphasizedActionLayoutResource()
4306 : tombstone ? getActionTombstoneLayoutResource()
4307 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04004308 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04004309 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04004310 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004311 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004312 if (action.mRemoteInputs != null) {
4313 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
4314 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004315 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004316 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07004317 // change the background bgColor
4318 int bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
4319 : R.color.notification_action_list_dark);
4320 button.setDrawableParameters(R.id.button_holder, true, -1, bgColor,
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004321 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinek981962e2016-07-20 20:41:58 -07004322 CharSequence title = action.title;
4323 ColorStateList[] outResultColor = null;
4324 if (isLegacy()) {
4325 title = clearColorSpans(title);
4326 } else {
4327 outResultColor = new ColorStateList[1];
4328 title = ensureColorSpanContrast(title, bgColor, outResultColor);
4329 }
4330 button.setTextViewText(R.id.action0, title);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004331 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07004332 if (outResultColor != null && outResultColor[0] != null) {
4333 // We need to set the text color as well since changing a text to uppercase
4334 // clears its spans.
4335 button.setTextColor(R.id.action0, outResultColor[0]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004336 } else if (mN.color != COLOR_DEFAULT && !isColorized()) {
Selim Cinek981962e2016-07-20 20:41:58 -07004337 button.setTextColor(R.id.action0,resolveContrastColor());
4338 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004339 } else {
Selim Cinek981962e2016-07-20 20:41:58 -07004340 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Adrian Roos72171622017-01-27 10:32:06 -08004341 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004342 setTextViewColorPrimary(button, R.id.action0);
4343 } else if (mN.color != COLOR_DEFAULT) {
Adrian Roos487374f2017-01-11 15:48:14 -08004344 button.setTextColor(R.id.action0,
4345 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004346 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004347 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004348 return button;
4349 }
4350
Joe Onoratocb109a02011-01-18 17:57:41 -08004351 /**
Selim Cinek981962e2016-07-20 20:41:58 -07004352 * Clears all color spans of a text
4353 * @param charSequence the input text
4354 * @return the same text but without color spans
4355 */
4356 private CharSequence clearColorSpans(CharSequence charSequence) {
4357 if (charSequence instanceof Spanned) {
4358 Spanned ss = (Spanned) charSequence;
4359 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4360 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4361 for (Object span : spans) {
4362 Object resultSpan = span;
4363 if (resultSpan instanceof CharacterStyle) {
4364 resultSpan = ((CharacterStyle) span).getUnderlying();
4365 }
4366 if (resultSpan instanceof TextAppearanceSpan) {
4367 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4368 if (originalSpan.getTextColor() != null) {
4369 resultSpan = new TextAppearanceSpan(
4370 originalSpan.getFamily(),
4371 originalSpan.getTextStyle(),
4372 originalSpan.getTextSize(),
4373 null,
4374 originalSpan.getLinkTextColor());
4375 }
4376 } else if (resultSpan instanceof ForegroundColorSpan
4377 || (resultSpan instanceof BackgroundColorSpan)) {
4378 continue;
4379 } else {
4380 resultSpan = span;
4381 }
4382 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
4383 ss.getSpanFlags(span));
4384 }
4385 return builder;
4386 }
4387 return charSequence;
4388 }
4389
4390 /**
4391 * Ensures contrast on color spans against a background color. also returns the color of the
4392 * text if a span was found that spans over the whole text.
4393 *
4394 * @param charSequence the charSequence on which the spans are
4395 * @param background the background color to ensure the contrast against
4396 * @param outResultColor an array in which a color will be returned as the first element if
4397 * there exists a full length color span.
4398 * @return the contrasted charSequence
4399 */
4400 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
4401 ColorStateList[] outResultColor) {
4402 if (charSequence instanceof Spanned) {
4403 Spanned ss = (Spanned) charSequence;
4404 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4405 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4406 for (Object span : spans) {
4407 Object resultSpan = span;
4408 int spanStart = ss.getSpanStart(span);
4409 int spanEnd = ss.getSpanEnd(span);
4410 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
4411 if (resultSpan instanceof CharacterStyle) {
4412 resultSpan = ((CharacterStyle) span).getUnderlying();
4413 }
4414 if (resultSpan instanceof TextAppearanceSpan) {
4415 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4416 ColorStateList textColor = originalSpan.getTextColor();
4417 if (textColor != null) {
4418 int[] colors = textColor.getColors();
4419 int[] newColors = new int[colors.length];
4420 for (int i = 0; i < newColors.length; i++) {
4421 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
4422 colors[i], background);
4423 }
4424 textColor = new ColorStateList(textColor.getStates().clone(),
4425 newColors);
4426 resultSpan = new TextAppearanceSpan(
4427 originalSpan.getFamily(),
4428 originalSpan.getTextStyle(),
4429 originalSpan.getTextSize(),
4430 textColor,
4431 originalSpan.getLinkTextColor());
4432 if (fullLength) {
4433 outResultColor[0] = new ColorStateList(
4434 textColor.getStates().clone(), newColors);
4435 }
4436 }
4437 } else if (resultSpan instanceof ForegroundColorSpan) {
4438 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
4439 int foregroundColor = originalSpan.getForegroundColor();
4440 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
4441 foregroundColor, background);
4442 resultSpan = new ForegroundColorSpan(foregroundColor);
4443 if (fullLength) {
4444 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
4445 }
4446 } else {
4447 resultSpan = span;
4448 }
4449
4450 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
4451 }
4452 return builder;
4453 }
4454 return charSequence;
4455 }
4456
4457 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004458 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07004459 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004460 */
4461 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004462 if (!mIsLegacyInitialized) {
4463 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
4464 < Build.VERSION_CODES.LOLLIPOP;
4465 mIsLegacyInitialized = true;
4466 }
4467 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004468 }
4469
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004470 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004471 return processLegacyText(charSequence, false /* ambient */);
4472 }
4473
4474 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
4475 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
4476 boolean wantLightText = ambient;
4477 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004478 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004479 } else {
4480 return charSequence;
4481 }
4482 }
4483
Dan Sandler26e81cf2014-05-06 10:01:27 -04004484 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004485 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04004486 */
Adrian Roos487374f2017-01-11 15:48:14 -08004487 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
4488 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08004489 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinek99104832017-01-25 14:47:33 -08004490 int color = ambient ? resolveAmbientColor() : getPrimaryHighlightColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08004491 if (colorable) {
Adrian Roos487374f2017-01-11 15:48:14 -08004492 contentView.setDrawableParameters(R.id.icon, false, -1, color,
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004493 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08004494
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004495 }
Selim Cinekea4bef72015-12-02 15:51:10 -08004496 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08004497 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004498 }
4499
Dan Sandler26e81cf2014-05-06 10:01:27 -04004500 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004501 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04004502 * if it's grayscale).
4503 */
4504 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04004505 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
4506 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004507 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004508 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08004509 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04004510 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004511 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004512 }
4513
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004514 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004515 if (mN.color != COLOR_DEFAULT) {
4516 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02004517 }
Jorim Jaggi74419312014-06-10 20:57:21 +02004518 }
4519
Adrian Roos4ff3b122016-02-01 12:26:13 -08004520 int resolveContrastColor() {
4521 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
4522 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004523 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004524 final int contrasted = NotificationColorUtil.resolveContrastColor(mContext, mN.color);
4525
4526 mCachedContrastColorIsFor = mN.color;
4527 return mCachedContrastColor = contrasted;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004528 }
4529
Adrian Roos487374f2017-01-11 15:48:14 -08004530 int resolveAmbientColor() {
4531 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
4532 return mCachedAmbientColor;
4533 }
4534 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
4535
4536 mCachedAmbientColorIsFor = mN.color;
4537 return mCachedAmbientColor = contrasted;
4538 }
4539
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004540 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004541 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004542 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08004543 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004544 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004545 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004546 mN.actions = new Action[mActions.size()];
4547 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004548 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004549 if (!mPersonList.isEmpty()) {
4550 mN.extras.putStringArray(EXTRA_PEOPLE,
4551 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004552 }
Selim Cinek247fa012016-02-18 09:50:48 -08004553 if (mN.bigContentView != null || mN.contentView != null
4554 || mN.headsUpContentView != null) {
4555 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
4556 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004557 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08004558 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004559
Julia Reynolds3b848122016-02-26 10:45:32 -05004560 /**
4561 * Creates a Builder from an existing notification so further changes can be made.
4562 * @param context The context for your application / activity.
4563 * @param n The notification to create a Builder from.
4564 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004565 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004566 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004567 ApplicationInfo applicationInfo = n.extras.getParcelable(
4568 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004569 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05004570 if (applicationInfo != null) {
4571 try {
4572 builderContext = context.createApplicationContext(applicationInfo,
4573 Context.CONTEXT_RESTRICTED);
4574 } catch (NameNotFoundException e) {
4575 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
4576 builderContext = context; // try with our context
4577 }
4578 } else {
4579 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02004580 }
4581
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004582 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004583 }
4584
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004585 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004586 * @deprecated Use {@link #build()} instead.
4587 */
4588 @Deprecated
4589 public Notification getNotification() {
4590 return build();
4591 }
4592
4593 /**
4594 * Combine all of the options that have been set and return a new {@link Notification}
4595 * object.
4596 */
4597 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004598 // first, add any extras from the calling code
4599 if (mUserExtras != null) {
4600 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004601 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004602
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004603 mN.creationTime = System.currentTimeMillis();
4604
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004605 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05004606 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02004607
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004608 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004609
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004610 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004611 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004612 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004613
Adrian Roos5081c0d2016-02-26 16:04:19 -08004614 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4615 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004616 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004617 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004618 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
4619 mN.contentView.getSequenceNumber());
4620 }
4621 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004622 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004623 if (mN.bigContentView != null) {
4624 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
4625 mN.bigContentView.getSequenceNumber());
4626 }
4627 }
4628 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004629 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004630 if (mN.headsUpContentView != null) {
4631 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
4632 mN.headsUpContentView.getSequenceNumber());
4633 }
4634 }
4635 }
4636
Julia Reynolds4c0c2022016-02-02 15:11:59 -05004637 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
4638 mN.flags |= FLAG_SHOW_LIGHTS;
4639 }
4640
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004641 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004642 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004643
4644 /**
4645 * Apply this Builder to an existing {@link Notification} object.
4646 *
4647 * @hide
4648 */
4649 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04004650 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004651 return n;
4652 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004653
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004654 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08004655 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
4656 * change.
4657 *
4658 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
4659 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004660 * @hide
4661 */
Adrian Roos184bfe022016-03-03 13:41:44 -08004662 public static Notification maybeCloneStrippedForDelivery(Notification n) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004663 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08004664
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004665 // Only strip views for known Styles because we won't know how to
4666 // re-create them otherwise.
Adrian Roos184bfe022016-03-03 13:41:44 -08004667 if (!TextUtils.isEmpty(templateClass)
4668 && getNotificationStyleClass(templateClass) == null) {
4669 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004670 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004671
4672 // Only strip unmodified BuilderRemoteViews.
4673 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004674 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004675 n.contentView.getSequenceNumber();
4676 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004677 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004678 n.bigContentView.getSequenceNumber();
4679 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004680 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004681 n.headsUpContentView.getSequenceNumber();
4682
4683 // Nothing to do here, no need to clone.
4684 if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
4685 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004686 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004687
4688 Notification clone = n.clone();
4689 if (stripContentView) {
4690 clone.contentView = null;
4691 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
4692 }
4693 if (stripBigContentView) {
4694 clone.bigContentView = null;
4695 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
4696 }
4697 if (stripHeadsUpContentView) {
4698 clone.headsUpContentView = null;
4699 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
4700 }
4701 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004702 }
4703
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004704 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004705 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004706 }
4707
4708 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004709 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004710 }
4711
4712 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004713 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004714 }
4715
4716 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004717 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004718 }
4719
4720 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004721 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004722 }
4723
Adrian Roosc1a80b02016-04-05 14:54:55 -07004724 private int getMessagingLayoutResource() {
4725 return R.layout.notification_template_material_messaging;
4726 }
4727
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004728 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004729 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004730 }
4731
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004732 private int getEmphasizedActionLayoutResource() {
4733 return R.layout.notification_material_action_emphasized;
4734 }
4735
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004736 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004737 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004738 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004739
4740 private int getBackgroundColor() {
4741 if (isColorized()) {
4742 return mN.color;
4743 } else {
4744 return COLOR_DEFAULT;
4745 }
4746 }
4747
4748 private boolean isColorized() {
4749 return mN.isColorized();
4750 }
Selim Cinek99104832017-01-25 14:47:33 -08004751
4752 private boolean textColorsNeedInversion() {
4753 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
4754 return false;
4755 }
4756 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
4757 return targetSdkVersion > Build.VERSION_CODES.M
4758 && targetSdkVersion < Build.VERSION_CODES.O;
4759 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004760 }
4761
4762 /**
4763 * @return whether this notification is ongoing and can't be dismissed by the user.
4764 */
4765 private boolean isOngoing() {
4766 final int ongoingFlags = Notification.FLAG_FOREGROUND_SERVICE
4767 | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
4768 return (flags & ongoingFlags) != 0;
4769 }
4770
4771 /**
Selim Cinek99104832017-01-25 14:47:33 -08004772 * @return whether this notification has a media session attached
4773 * @hide
4774 */
4775 public boolean hasMediaSession() {
4776 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
4777 }
4778
4779 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08004780 * @return the style class of this notification
4781 * @hide
4782 */
4783 public Class<? extends Notification.Style> getNotificationStyle() {
4784 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
4785
4786 if (!TextUtils.isEmpty(templateClass)) {
4787 return Notification.getNotificationStyleClass(templateClass);
4788 }
4789 return null;
4790 }
4791
4792 /**
Selim Cinek99104832017-01-25 14:47:33 -08004793 * @return true if this notification is colorized. This also factors in whether the
Selim Cinek7b9605b2017-01-19 17:36:00 -08004794 * notification is ongoing.
4795 *
4796 * @hide
4797 */
4798 public boolean isColorized() {
Selim Cinek99104832017-01-25 14:47:33 -08004799 Class<? extends Style> style = getNotificationStyle();
4800 if (MediaStyle.class.equals(style)) {
4801 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
4802 if ((colorized == null || colorized) && hasMediaSession()) {
4803 return true;
4804 }
4805 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
4806 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
4807 return true;
4808 }
4809 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004810 return extras.getBoolean(EXTRA_COLORIZED) && isOngoing();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004811 }
4812
Selim Cinek279fa862016-06-14 10:57:25 -07004813 private boolean hasLargeIcon() {
4814 return mLargeIcon != null || largeIcon != null;
4815 }
4816
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004817 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004818 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004819 * @hide
4820 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07004821 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004822 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
4823 }
4824
4825 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004826 * @return true if the notification will show a chronometer; false otherwise
4827 * @hide
4828 */
4829 public boolean showsChronometer() {
4830 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
4831 }
4832
4833 /**
Julia Reynolds4a02afb2016-12-13 13:39:52 -05004834 * @hide
4835 */
4836 @SystemApi
4837 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
4838 Class<? extends Style>[] classes = new Class[] {
4839 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
4840 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
4841 MessagingStyle.class };
4842 for (Class<? extends Style> innerClass : classes) {
4843 if (templateClass.equals(innerClass.getName())) {
4844 return innerClass;
4845 }
4846 }
4847 return null;
4848 }
4849
4850 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004851 * An object that can apply a rich notification style to a {@link Notification.Builder}
4852 * object.
4853 */
Griff Hazendfcb0802014-02-11 12:00:00 -08004854 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04004855 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004856
4857 /**
4858 * @hide
4859 */
4860 protected CharSequence mSummaryText = null;
4861
4862 /**
4863 * @hide
4864 */
4865 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04004866
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004867 protected Builder mBuilder;
4868
Chris Wrend6297db2012-05-03 16:20:13 -04004869 /**
4870 * Overrides ContentTitle in the big form of the template.
4871 * This defaults to the value passed to setContentTitle().
4872 */
4873 protected void internalSetBigContentTitle(CharSequence title) {
4874 mBigContentTitle = title;
4875 }
4876
4877 /**
4878 * Set the first line of text after the detail section in the big form of the template.
4879 */
4880 protected void internalSetSummaryText(CharSequence cs) {
4881 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04004882 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04004883 }
4884
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004885 public void setBuilder(Builder builder) {
4886 if (mBuilder != builder) {
4887 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004888 if (mBuilder != null) {
4889 mBuilder.setStyle(this);
4890 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004891 }
4892 }
4893
Chris Wrend6297db2012-05-03 16:20:13 -04004894 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004895 if (mBuilder == null) {
4896 throw new IllegalArgumentException("Style requires a valid Builder object");
4897 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004898 }
Chris Wrend6297db2012-05-03 16:20:13 -04004899
4900 protected RemoteViews getStandardView(int layoutId) {
4901 checkBuilder();
4902
Christoph Studer4600f9b2014-07-22 22:44:43 +02004903 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004904 CharSequence oldBuilderContentTitle =
4905 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004906 if (mBigContentTitle != null) {
4907 mBuilder.setContentTitle(mBigContentTitle);
4908 }
4909
Chris Wrend6297db2012-05-03 16:20:13 -04004910 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
4911
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004912 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004913
Chris Wrend6297db2012-05-03 16:20:13 -04004914 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
4915 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04004916 } else {
4917 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004918 }
4919
Chris Wrend6297db2012-05-03 16:20:13 -04004920 return contentView;
4921 }
4922
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004923 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08004924 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004925 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08004926 *
4927 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004928 * @hide
4929 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08004930 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004931 return null;
4932 }
4933
4934 /**
4935 * Construct a Style-specific RemoteViews for the final big notification layout.
4936 * @hide
4937 */
4938 public RemoteViews makeBigContentView() {
4939 return null;
4940 }
4941
4942 /**
4943 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004944 *
4945 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004946 * @hide
4947 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004948 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004949 return null;
4950 }
4951
4952 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004953 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004954 * @hide
4955 */
4956 public void addExtras(Bundle extras) {
4957 if (mSummaryTextSet) {
4958 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
4959 }
4960 if (mBigContentTitle != null) {
4961 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
4962 }
Chris Wren91ad5632013-06-05 15:05:57 -04004963 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004964 }
4965
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004966 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004967 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004968 * @hide
4969 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02004970 protected void restoreFromExtras(Bundle extras) {
4971 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
4972 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
4973 mSummaryTextSet = true;
4974 }
4975 if (extras.containsKey(EXTRA_TITLE_BIG)) {
4976 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
4977 }
4978 }
4979
4980
4981 /**
4982 * @hide
4983 */
4984 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004985 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004986 return wip;
4987 }
4988
Daniel Sandler0ec46202015-06-24 01:27:05 -04004989 /**
4990 * @hide
4991 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004992 public void purgeResources() {}
4993
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004994 /**
4995 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
4996 * attached to.
4997 *
4998 * @return the fully constructed Notification.
4999 */
5000 public Notification build() {
5001 checkBuilder();
5002 return mBuilder.build();
5003 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005004
5005 /**
5006 * @hide
5007 * @return true if the style positions the progress bar on the second line; false if the
5008 * style hides the progress bar
5009 */
5010 protected boolean hasProgress() {
5011 return true;
5012 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005013
5014 /**
5015 * @hide
5016 * @return Whether we should put the summary be put into the notification header
5017 */
5018 public boolean hasSummaryInHeader() {
5019 return true;
5020 }
Selim Cinek593610c2016-02-16 18:42:57 -08005021
5022 /**
5023 * @hide
5024 * @return Whether custom content views are displayed inline in the style
5025 */
5026 public boolean displayCustomViewInline() {
5027 return false;
5028 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005029 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005030
5031 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005032 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08005033 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005034 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005035 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005036 * Notification notif = new Notification.Builder(mContext)
5037 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
5038 * .setContentText(subject)
5039 * .setSmallIcon(R.drawable.new_post)
5040 * .setLargeIcon(aBitmap)
5041 * .setStyle(new Notification.BigPictureStyle()
5042 * .bigPicture(aBigBitmap))
5043 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005044 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005045 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005046 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005047 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005048 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005049 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005050 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005051 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005052
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005053 public BigPictureStyle() {
5054 }
5055
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005056 /**
5057 * @deprecated use {@code BigPictureStyle()}.
5058 */
5059 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005060 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005061 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005062 }
5063
Chris Wrend6297db2012-05-03 16:20:13 -04005064 /**
5065 * Overrides ContentTitle in the big form of the template.
5066 * This defaults to the value passed to setContentTitle().
5067 */
5068 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005069 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005070 return this;
5071 }
5072
5073 /**
5074 * Set the first line of text after the detail section in the big form of the template.
5075 */
5076 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005077 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005078 return this;
5079 }
5080
Chris Wren0bd664d2012-08-01 13:56:56 -04005081 /**
5082 * Provide the bitmap to be used as the payload for the BigPicture notification.
5083 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005084 public BigPictureStyle bigPicture(Bitmap b) {
5085 mPicture = b;
5086 return this;
5087 }
5088
Chris Wren3745a3d2012-05-22 15:11:52 -04005089 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04005090 * Override the large icon when the big notification is shown.
5091 */
5092 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005093 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
5094 }
5095
5096 /**
5097 * Override the large icon when the big notification is shown.
5098 */
5099 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04005100 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005101 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005102 return this;
5103 }
5104
Riley Andrews0394a0c2015-11-03 23:36:52 -08005105 /** @hide */
5106 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
5107
Daniel Sandler0ec46202015-06-24 01:27:05 -04005108 /**
5109 * @hide
5110 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005111 @Override
5112 public void purgeResources() {
5113 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08005114 if (mPicture != null &&
5115 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08005116 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005117 mPicture = mPicture.createAshmemBitmap();
5118 }
5119 if (mBigLargeIcon != null) {
5120 mBigLargeIcon.convertToAshmem();
5121 }
5122 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01005123
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005124 /**
5125 * @hide
5126 */
5127 public RemoteViews makeBigContentView() {
5128 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01005129 // This covers the following cases:
5130 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005131 // mN.mLargeIcon
5132 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04005133 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07005134 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005135 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005136 oldLargeIcon = mBuilder.mN.mLargeIcon;
5137 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005138 // The legacy largeIcon might not allow us to clear the image, as it's taken in
5139 // replacement if the other one is null. Because we're restoring these legacy icons
5140 // for old listeners, this is in general non-null.
5141 largeIconLegacy = mBuilder.mN.largeIcon;
5142 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005143 }
5144
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005145 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05005146 if (mSummaryTextSet) {
5147 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005148 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005149 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05005150 }
Selim Cinek279fa862016-06-14 10:57:25 -07005151 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08005152
Christoph Studer5c510ee2014-12-15 16:32:27 +01005153 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005154 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005155 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005156 }
5157
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005158 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005159 return contentView;
5160 }
5161
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005162 /**
5163 * @hide
5164 */
5165 public void addExtras(Bundle extras) {
5166 super.addExtras(extras);
5167
5168 if (mBigLargeIconSet) {
5169 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
5170 }
5171 extras.putParcelable(EXTRA_PICTURE, mPicture);
5172 }
5173
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005174 /**
5175 * @hide
5176 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005177 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02005178 protected void restoreFromExtras(Bundle extras) {
5179 super.restoreFromExtras(extras);
5180
5181 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01005182 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02005183 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04005184 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005185 mPicture = extras.getParcelable(EXTRA_PICTURE);
5186 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005187
5188 /**
5189 * @hide
5190 */
5191 @Override
5192 public boolean hasSummaryInHeader() {
5193 return false;
5194 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005195 }
5196
5197 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005198 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08005199 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005200 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005201 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005202 * Notification notif = new Notification.Builder(mContext)
5203 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5204 * .setContentText(subject)
5205 * .setSmallIcon(R.drawable.new_mail)
5206 * .setLargeIcon(aBitmap)
5207 * .setStyle(new Notification.BigTextStyle()
5208 * .bigText(aVeryLongString))
5209 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005210 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005211 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005212 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005213 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005214 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005215
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005216 private CharSequence mBigText;
5217
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005218 public BigTextStyle() {
5219 }
5220
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005221 /**
5222 * @deprecated use {@code BigTextStyle()}.
5223 */
5224 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005225 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005226 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005227 }
5228
Chris Wrend6297db2012-05-03 16:20:13 -04005229 /**
5230 * Overrides ContentTitle in the big form of the template.
5231 * This defaults to the value passed to setContentTitle().
5232 */
5233 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005234 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005235 return this;
5236 }
5237
5238 /**
5239 * Set the first line of text after the detail section in the big form of the template.
5240 */
5241 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005242 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005243 return this;
5244 }
5245
Chris Wren0bd664d2012-08-01 13:56:56 -04005246 /**
5247 * Provide the longer text to be displayed in the big form of the
5248 * template in place of the content text.
5249 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005250 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005251 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005252 return this;
5253 }
5254
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005255 /**
5256 * @hide
5257 */
5258 public void addExtras(Bundle extras) {
5259 super.addExtras(extras);
5260
Christoph Studer4600f9b2014-07-22 22:44:43 +02005261 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
5262 }
5263
5264 /**
5265 * @hide
5266 */
5267 @Override
5268 protected void restoreFromExtras(Bundle extras) {
5269 super.restoreFromExtras(extras);
5270
5271 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005272 }
5273
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005274 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005275 * @param increasedHeight true if this layout be created with an increased height.
5276 *
5277 * @hide
5278 */
5279 @Override
5280 public RemoteViews makeContentView(boolean increasedHeight) {
5281 if (increasedHeight) {
5282 ArrayList<Action> actions = mBuilder.mActions;
5283 mBuilder.mActions = new ArrayList<>();
5284 RemoteViews remoteViews = makeBigContentView();
5285 mBuilder.mActions = actions;
5286 return remoteViews;
5287 }
5288 return super.makeContentView(increasedHeight);
5289 }
5290
5291 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005292 * @hide
5293 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005294 @Override
5295 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
5296 if (increasedHeight && mBuilder.mActions.size() > 0) {
5297 return makeBigContentView();
5298 }
5299 return super.makeHeadsUpContentView(increasedHeight);
5300 }
5301
5302 /**
5303 * @hide
5304 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005305 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005306
5307 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07005308 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005309 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04005310
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005311 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08005312
Selim Cinek75998782016-04-26 10:39:17 -07005313 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005314
Selim Cinek3a2c4b92015-12-17 17:01:17 -08005315 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07005316 if (TextUtils.isEmpty(bigTextText)) {
5317 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
5318 // experience
5319 bigTextText = mBuilder.processLegacyText(text);
5320 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07005321 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08005322
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005323 return contentView;
5324 }
5325
Adrian Roosb1f427c2016-05-26 12:27:15 -07005326 static void applyBigTextContentView(Builder builder,
5327 RemoteViews contentView, CharSequence bigTextText) {
5328 contentView.setTextViewText(R.id.big_text, bigTextText);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005329 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07005330 contentView.setViewVisibility(R.id.big_text,
5331 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07005332 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07005333 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005334 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005335
5336 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005337 * Helper class for generating large-format notifications that include multiple back-and-forth
5338 * messages of varying types between any number of people.
5339 *
5340 * <br>
5341 * If the platform does not provide large-format notifications, this method has no effect. The
5342 * user will always see the normal notification view.
5343 * <br>
5344 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
5345 * so:
5346 * <pre class="prettyprint">
5347 *
5348 * Notification noti = new Notification.Builder()
5349 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
5350 * .setContentText(subject)
5351 * .setSmallIcon(R.drawable.new_message)
5352 * .setLargeIcon(aBitmap)
5353 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
5354 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
5355 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
5356 * .build();
5357 * </pre>
5358 */
5359 public static class MessagingStyle extends Style {
5360
5361 /**
5362 * The maximum number of messages that will be retained in the Notification itself (the
5363 * number displayed is up to the platform).
5364 */
5365 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
5366
5367 CharSequence mUserDisplayName;
5368 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04005369 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08005370 List<Message> mHistoricMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04005371
5372 MessagingStyle() {
5373 }
5374
5375 /**
Alex Hillsfd590442016-10-07 09:52:44 -04005376 * @param userDisplayName Required - the name to be displayed for any replies sent by the
5377 * user before the posting app reposts the notification with those messages after they've
5378 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04005379 * {@link #addMessage(Notification.MessagingStyle.Message)}
5380 */
Alex Hillsfd590442016-10-07 09:52:44 -04005381 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Alex Hillsfc737de2016-03-23 17:33:02 -04005382 mUserDisplayName = userDisplayName;
5383 }
5384
5385 /**
5386 * Returns the name to be displayed for any replies sent by the user
5387 */
5388 public CharSequence getUserDisplayName() {
5389 return mUserDisplayName;
5390 }
5391
5392 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005393 * Sets the title to be displayed on this conversation. This should only be used for
5394 * group messaging and left unset for one-on-one conversations.
5395 * @param conversationTitle
5396 * @return this object for method chaining.
5397 */
5398 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
5399 mConversationTitle = conversationTitle;
5400 return this;
5401 }
5402
5403 /**
5404 * Return the title to be displayed on this conversation. Can be <code>null</code> and
5405 * should be for one-on-one conversations
5406 */
5407 public CharSequence getConversationTitle() {
5408 return mConversationTitle;
5409 }
5410
5411 /**
5412 * Adds a message for display by this notification. Convenience call for a simple
5413 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
5414 * @param text A {@link CharSequence} to be displayed as the message content
5415 * @param timestamp Time at which the message arrived
5416 * @param sender A {@link CharSequence} to be used for displaying the name of the
5417 * sender. Should be <code>null</code> for messages by the current user, in which case
5418 * the platform will insert {@link #getUserDisplayName()}.
5419 * Should be unique amongst all individuals in the conversation, and should be
5420 * consistent during re-posts of the notification.
5421 *
5422 * @see Message#Message(CharSequence, long, CharSequence)
5423 *
5424 * @return this object for method chaining
5425 */
5426 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08005427 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04005428 }
5429
5430 /**
5431 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08005432 *
5433 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5434 * the newest last.
5435 *
Alex Hillsfc737de2016-03-23 17:33:02 -04005436 * @param message The {@link Message} to be displayed
5437 * @return this object for method chaining
5438 */
5439 public MessagingStyle addMessage(Message message) {
5440 mMessages.add(message);
5441 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5442 mMessages.remove(0);
5443 }
5444 return this;
5445 }
5446
5447 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005448 * Adds a {@link Message} for historic context in this notification.
5449 *
5450 * <p>Messages should be added as historic if they are not the main subject of the
5451 * notification but may give context to a conversation. The system may choose to present
5452 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
5453 *
5454 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5455 * the newest last.
5456 *
5457 * @param message The historic {@link Message} to be added
5458 * @return this object for method chaining
5459 */
5460 public MessagingStyle addHistoricMessage(Message message) {
5461 mHistoricMessages.add(message);
5462 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5463 mHistoricMessages.remove(0);
5464 }
5465 return this;
5466 }
5467
5468 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005469 * Gets the list of {@code Message} objects that represent the notification
5470 */
5471 public List<Message> getMessages() {
5472 return mMessages;
5473 }
5474
5475 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005476 * Gets the list of historic {@code Message}s in the notification.
5477 */
5478 public List<Message> getHistoricMessages() {
5479 return mHistoricMessages;
5480 }
5481
5482 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005483 * @hide
5484 */
5485 @Override
5486 public void addExtras(Bundle extras) {
5487 super.addExtras(extras);
5488 if (mUserDisplayName != null) {
5489 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
5490 }
5491 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005492 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04005493 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005494 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
5495 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04005496 }
Adrian Roos437cd562017-01-18 15:47:03 -08005497 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
5498 Message.getBundleArrayForMessages(mHistoricMessages));
5499 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07005500
5501 fixTitleAndTextExtras(extras);
5502 }
5503
5504 private void fixTitleAndTextExtras(Bundle extras) {
5505 Message m = findLatestIncomingMessage();
5506 CharSequence text = (m == null) ? null : m.mText;
5507 CharSequence sender = m == null ? null
5508 : TextUtils.isEmpty(m.mSender) ? mUserDisplayName : m.mSender;
5509 CharSequence title;
5510 if (!TextUtils.isEmpty(mConversationTitle)) {
5511 if (!TextUtils.isEmpty(sender)) {
5512 BidiFormatter bidi = BidiFormatter.getInstance();
5513 title = mBuilder.mContext.getString(
5514 com.android.internal.R.string.notification_messaging_title_template,
5515 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(m.mSender));
5516 } else {
5517 title = mConversationTitle;
5518 }
5519 } else {
5520 title = sender;
5521 }
5522
5523 if (title != null) {
5524 extras.putCharSequence(EXTRA_TITLE, title);
5525 }
5526 if (text != null) {
5527 extras.putCharSequence(EXTRA_TEXT, text);
5528 }
Alex Hillsfc737de2016-03-23 17:33:02 -04005529 }
5530
5531 /**
5532 * @hide
5533 */
5534 @Override
5535 protected void restoreFromExtras(Bundle extras) {
5536 super.restoreFromExtras(extras);
5537
5538 mMessages.clear();
Adrian Roos437cd562017-01-18 15:47:03 -08005539 mHistoricMessages.clear();
Adrian Roos96b7e202016-05-17 13:50:38 -07005540 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
5541 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08005542 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
5543 if (messages != null && messages instanceof Parcelable[]) {
5544 mMessages = Message.getMessagesFromBundleArray(messages);
5545 }
5546 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
5547 if (histMessages != null && histMessages instanceof Parcelable[]) {
5548 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Alex Hillsfc737de2016-03-23 17:33:02 -04005549 }
5550 }
5551
5552 /**
5553 * @hide
5554 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07005555 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08005556 public RemoteViews makeContentView(boolean increasedHeight) {
5557 if (!increasedHeight) {
5558 Message m = findLatestIncomingMessage();
5559 CharSequence title = mConversationTitle != null
5560 ? mConversationTitle
5561 : (m == null) ? null : m.mSender;
5562 CharSequence text = (m == null)
5563 ? null
5564 : mConversationTitle != null ? makeMessageLine(m, mBuilder) : m.mText;
Alex Hillsfc737de2016-03-23 17:33:02 -04005565
Selim Cinek7d1009b2017-01-25 15:28:28 -08005566 return mBuilder.applyStandardTemplate(mBuilder.getBaseLayoutResource(),
5567 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
5568 } else {
5569 ArrayList<Action> actions = mBuilder.mActions;
5570 mBuilder.mActions = new ArrayList<>();
5571 RemoteViews remoteViews = makeBigContentView();
5572 mBuilder.mActions = actions;
5573 return remoteViews;
5574 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07005575 }
5576
5577 private Message findLatestIncomingMessage() {
5578 for (int i = mMessages.size() - 1; i >= 0; i--) {
5579 Message m = mMessages.get(i);
5580 // Incoming messages have a non-empty sender.
5581 if (!TextUtils.isEmpty(m.mSender)) {
5582 return m;
5583 }
5584 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07005585 if (!mMessages.isEmpty()) {
5586 // No incoming messages, fall back to outgoing message
5587 return mMessages.get(mMessages.size() - 1);
5588 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07005589 return null;
5590 }
5591
5592 /**
5593 * @hide
5594 */
5595 @Override
5596 public RemoteViews makeBigContentView() {
5597 CharSequence title = !TextUtils.isEmpty(super.mBigContentTitle)
5598 ? super.mBigContentTitle
5599 : mConversationTitle;
5600 boolean hasTitle = !TextUtils.isEmpty(title);
5601
Adrian Roosfeafa052016-06-01 17:09:45 -07005602 if (mMessages.size() == 1) {
5603 // Special case for a single message: Use the big text style
5604 // so the collapsed and expanded versions match nicely.
5605 CharSequence bigTitle;
5606 CharSequence text;
5607 if (hasTitle) {
5608 bigTitle = title;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005609 text = makeMessageLine(mMessages.get(0), mBuilder);
Adrian Roosfeafa052016-06-01 17:09:45 -07005610 } else {
5611 bigTitle = mMessages.get(0).mSender;
5612 text = mMessages.get(0).mText;
5613 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07005614 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
5615 mBuilder.getBigTextLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08005616 mBuilder.mParams.reset().hasProgress(false).title(bigTitle).text(null));
Adrian Roosb1f427c2016-05-26 12:27:15 -07005617 BigTextStyle.applyBigTextContentView(mBuilder, contentView, text);
5618 return contentView;
5619 }
5620
Adrian Roos48d746a2016-04-12 14:57:28 -07005621 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07005622 mBuilder.getMessagingLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08005623 mBuilder.mParams.reset().hasProgress(false).title(title).text(null));
Adrian Roosc1a80b02016-04-05 14:54:55 -07005624
5625 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
5626 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
5627
5628 // Make sure all rows are gone in case we reuse a view.
5629 for (int rowId : rowIds) {
5630 contentView.setViewVisibility(rowId, View.GONE);
5631 }
5632
5633 int i=0;
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005634 contentView.setViewLayoutMarginBottomDimen(R.id.line1,
5635 hasTitle ? R.dimen.notification_messaging_spacing : 0);
Adrian Roosc1a80b02016-04-05 14:54:55 -07005636 contentView.setInt(R.id.notification_messaging, "setNumIndentLines",
Selim Cinek279fa862016-06-14 10:57:25 -07005637 !mBuilder.mN.hasLargeIcon() ? 0 : (hasTitle ? 1 : 2));
Adrian Roosc1a80b02016-04-05 14:54:55 -07005638
Adrian Roosfeafa052016-06-01 17:09:45 -07005639 int contractedChildId = View.NO_ID;
5640 Message contractedMessage = findLatestIncomingMessage();
Adrian Roos437cd562017-01-18 15:47:03 -08005641 int firstHistoricMessage = Math.max(0, mHistoricMessages.size()
5642 - (rowIds.length - mMessages.size()));
5643 while (firstHistoricMessage + i < mHistoricMessages.size() && i < rowIds.length) {
5644 Message m = mHistoricMessages.get(firstHistoricMessage + i);
5645 int rowId = rowIds[i];
5646
Selim Cinek7b9605b2017-01-19 17:36:00 -08005647 contentView.setTextViewText(rowId, makeMessageLine(m, mBuilder));
Adrian Roos437cd562017-01-18 15:47:03 -08005648
5649 if (contractedMessage == m) {
5650 contractedChildId = rowId;
5651 }
5652
5653 i++;
5654 }
5655
Adrian Roosc1a80b02016-04-05 14:54:55 -07005656 int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
5657 while (firstMessage + i < mMessages.size() && i < rowIds.length) {
5658 Message m = mMessages.get(firstMessage + i);
5659 int rowId = rowIds[i];
5660
5661 contentView.setViewVisibility(rowId, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005662 contentView.setTextViewText(rowId, makeMessageLine(m, mBuilder));
5663 mBuilder.setTextViewColorSecondary(contentView, rowId);
Adrian Roosc1a80b02016-04-05 14:54:55 -07005664
Adrian Roosfeafa052016-06-01 17:09:45 -07005665 if (contractedMessage == m) {
5666 contractedChildId = rowId;
5667 }
5668
Adrian Roosc1a80b02016-04-05 14:54:55 -07005669 i++;
5670 }
Adrian Roos437cd562017-01-18 15:47:03 -08005671 // Clear the remaining views for reapply. Ensures that historic message views can
5672 // reliably be identified as being GONE and having non-null text.
5673 while (i < rowIds.length) {
5674 int rowId = rowIds[i];
5675 contentView.setTextViewText(rowId, null);
5676 i++;
5677 }
5678
Adrian Roosfeafa052016-06-01 17:09:45 -07005679 // Record this here to allow transformation between the contracted and expanded views.
5680 contentView.setInt(R.id.notification_messaging, "setContractedChildId",
5681 contractedChildId);
Alex Hillsfc737de2016-03-23 17:33:02 -04005682 return contentView;
5683 }
5684
Selim Cinek7b9605b2017-01-19 17:36:00 -08005685 private CharSequence makeMessageLine(Message m, Builder builder) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07005686 BidiFormatter bidi = BidiFormatter.getInstance();
5687 SpannableStringBuilder sb = new SpannableStringBuilder();
Selim Cinek7b9605b2017-01-19 17:36:00 -08005688 boolean colorize = builder.isColorized();
Adrian Roosc1a80b02016-04-05 14:54:55 -07005689 if (TextUtils.isEmpty(m.mSender)) {
5690 CharSequence replyName = mUserDisplayName == null ? "" : mUserDisplayName;
5691 sb.append(bidi.unicodeWrap(replyName),
Selim Cinek7b9605b2017-01-19 17:36:00 -08005692 makeFontColorSpan(colorize
5693 ? builder.getPrimaryTextColor()
5694 : mBuilder.resolveContrastColor()),
Adrian Roosc1a80b02016-04-05 14:54:55 -07005695 0 /* flags */);
5696 } else {
5697 sb.append(bidi.unicodeWrap(m.mSender),
Selim Cinek7b9605b2017-01-19 17:36:00 -08005698 makeFontColorSpan(colorize
5699 ? builder.getPrimaryTextColor()
5700 : Color.BLACK),
Adrian Roosc1a80b02016-04-05 14:54:55 -07005701 0 /* flags */);
5702 }
5703 CharSequence text = m.mText == null ? "" : m.mText;
5704 sb.append(" ").append(bidi.unicodeWrap(text));
5705 return sb;
5706 }
5707
Adrian Roosdedd1df2016-04-26 16:38:47 -07005708 /**
5709 * @hide
5710 */
5711 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08005712 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
5713 if (increasedHeight) {
5714 return makeBigContentView();
5715 }
Adrian Roosdedd1df2016-04-26 16:38:47 -07005716 Message m = findLatestIncomingMessage();
5717 CharSequence title = mConversationTitle != null
5718 ? mConversationTitle
5719 : (m == null) ? null : m.mSender;
5720 CharSequence text = (m == null)
5721 ? null
Selim Cinek7b9605b2017-01-19 17:36:00 -08005722 : mConversationTitle != null ? makeMessageLine(m, mBuilder) : m.mText;
Adrian Roosdedd1df2016-04-26 16:38:47 -07005723
5724 return mBuilder.applyStandardTemplateWithActions(mBuilder.getBigBaseLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08005725 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
Adrian Roosdedd1df2016-04-26 16:38:47 -07005726 }
5727
Adrian Roosc1a80b02016-04-05 14:54:55 -07005728 private static TextAppearanceSpan makeFontColorSpan(int color) {
5729 return new TextAppearanceSpan(null, 0, 0,
5730 ColorStateList.valueOf(color), null);
5731 }
5732
Alex Hillsd9b04d92016-04-11 16:38:16 -04005733 public static final class Message {
5734
5735 static final String KEY_TEXT = "text";
5736 static final String KEY_TIMESTAMP = "time";
5737 static final String KEY_SENDER = "sender";
5738 static final String KEY_DATA_MIME_TYPE = "type";
5739 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08005740 static final String KEY_EXTRAS_BUNDLE = "extras";
Alex Hillsfc737de2016-03-23 17:33:02 -04005741
5742 private final CharSequence mText;
5743 private final long mTimestamp;
5744 private final CharSequence mSender;
5745
Shane Brennan5a871862017-03-11 13:14:17 -08005746 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04005747 private String mDataMimeType;
5748 private Uri mDataUri;
5749
5750 /**
5751 * Constructor
5752 * @param text A {@link CharSequence} to be displayed as the message content
5753 * @param timestamp Time at which the message arrived
5754 * @param sender A {@link CharSequence} to be used for displaying the name of the
5755 * sender. Should be <code>null</code> for messages by the current user, in which case
5756 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
5757 * Should be unique amongst all individuals in the conversation, and should be
5758 * consistent during re-posts of the notification.
5759 */
5760 public Message(CharSequence text, long timestamp, CharSequence sender){
5761 mText = text;
5762 mTimestamp = timestamp;
5763 mSender = sender;
5764 }
5765
5766 /**
5767 * Sets a binary blob of data and an associated MIME type for a message. In the case
5768 * where the platform doesn't support the MIME type, the original text provided in the
5769 * constructor will be used.
5770 * @param dataMimeType The MIME type of the content. See
5771 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
5772 * types on Android and Android Wear.
5773 * @param dataUri The uri containing the content whose type is given by the MIME type.
5774 * <p class="note">
5775 * <ol>
5776 * <li>Notification Listeners including the System UI need permission to access the
5777 * data the Uri points to. The recommended ways to do this are:</li>
5778 * <li>Store the data in your own ContentProvider, making sure that other apps have
5779 * the correct permission to access your provider. The preferred mechanism for
5780 * providing access is to use per-URI permissions which are temporary and only
5781 * grant access to the receiving application. An easy way to create a
5782 * ContentProvider like this is to use the FileProvider helper class.</li>
5783 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
5784 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
5785 * also store non-media types (see MediaStore.Files for more info). Files can be
5786 * inserted into the MediaStore using scanFile() after which a content:// style
5787 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
5788 * Note that once added to the system MediaStore the content is accessible to any
5789 * app on the device.</li>
5790 * </ol>
5791 * @return this object for method chaining
5792 */
5793 public Message setData(String dataMimeType, Uri dataUri) {
5794 mDataMimeType = dataMimeType;
5795 mDataUri = dataUri;
5796 return this;
5797 }
5798
Alex Hillsfc737de2016-03-23 17:33:02 -04005799 /**
5800 * Get the text to be used for this message, or the fallback text if a type and content
5801 * Uri have been set
5802 */
5803 public CharSequence getText() {
5804 return mText;
5805 }
5806
5807 /**
5808 * Get the time at which this message arrived
5809 */
5810 public long getTimestamp() {
5811 return mTimestamp;
5812 }
5813
5814 /**
Shane Brennan5a871862017-03-11 13:14:17 -08005815 * Get the extras Bundle for this message.
5816 */
5817 public Bundle getExtras() {
5818 return mExtras;
5819 }
5820
5821 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005822 * Get the text used to display the contact's name in the messaging experience
5823 */
5824 public CharSequence getSender() {
5825 return mSender;
5826 }
5827
5828 /**
5829 * Get the MIME type of the data pointed to by the Uri
5830 */
5831 public String getDataMimeType() {
5832 return mDataMimeType;
5833 }
5834
5835 /**
5836 * Get the the Uri pointing to the content of the message. Can be null, in which case
5837 * {@see #getText()} is used.
5838 */
5839 public Uri getDataUri() {
5840 return mDataUri;
5841 }
5842
Alex Hillsd9b04d92016-04-11 16:38:16 -04005843 private Bundle toBundle() {
5844 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04005845 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005846 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04005847 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005848 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04005849 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005850 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04005851 }
5852 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005853 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04005854 }
5855 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005856 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04005857 }
Shane Brennan5a871862017-03-11 13:14:17 -08005858 if (mExtras != null) {
5859 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
5860 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005861 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04005862 }
5863
Alex Hillsd9b04d92016-04-11 16:38:16 -04005864 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
5865 Bundle[] bundles = new Bundle[messages.size()];
5866 final int N = messages.size();
5867 for (int i = 0; i < N; i++) {
5868 bundles[i] = messages.get(i).toBundle();
5869 }
5870 return bundles;
5871 }
5872
Adrian Roosdedd1df2016-04-26 16:38:47 -07005873 static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005874 List<Message> messages = new ArrayList<>(bundles.length);
5875 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07005876 if (bundles[i] instanceof Bundle) {
5877 Message message = getMessageFromBundle((Bundle)bundles[i]);
5878 if (message != null) {
5879 messages.add(message);
5880 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005881 }
5882 }
5883 return messages;
5884 }
5885
5886 static Message getMessageFromBundle(Bundle bundle) {
5887 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07005888 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005889 return null;
5890 } else {
5891 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
5892 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
5893 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
5894 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005895 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
5896 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04005897 }
Shane Brennan5a871862017-03-11 13:14:17 -08005898 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
5899 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
5900 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005901 return message;
5902 }
5903 } catch (ClassCastException e) {
5904 return null;
5905 }
5906 }
Alex Hillsfc737de2016-03-23 17:33:02 -04005907 }
5908 }
5909
5910 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04005911 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08005912 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005913 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04005914 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005915 * Notification notif = new Notification.Builder(mContext)
5916 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
5917 * .setContentText(subject)
5918 * .setSmallIcon(R.drawable.new_mail)
5919 * .setLargeIcon(aBitmap)
5920 * .setStyle(new Notification.InboxStyle()
5921 * .addLine(str1)
5922 * .addLine(str2)
5923 * .setContentTitle(&quot;&quot;)
5924 * .setSummaryText(&quot;+3 more&quot;))
5925 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04005926 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005927 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04005928 * @see Notification#bigContentView
5929 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005930 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005931 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
5932
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005933 public InboxStyle() {
5934 }
5935
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005936 /**
5937 * @deprecated use {@code InboxStyle()}.
5938 */
5939 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04005940 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005941 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04005942 }
5943
Chris Wrend6297db2012-05-03 16:20:13 -04005944 /**
5945 * Overrides ContentTitle in the big form of the template.
5946 * This defaults to the value passed to setContentTitle().
5947 */
5948 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005949 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005950 return this;
5951 }
5952
5953 /**
5954 * Set the first line of text after the detail section in the big form of the template.
5955 */
5956 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005957 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005958 return this;
5959 }
5960
Chris Wren0bd664d2012-08-01 13:56:56 -04005961 /**
5962 * Append a line to the digest section of the Inbox notification.
5963 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04005964 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005965 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04005966 return this;
5967 }
5968
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005969 /**
5970 * @hide
5971 */
5972 public void addExtras(Bundle extras) {
5973 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005974
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005975 CharSequence[] a = new CharSequence[mTexts.size()];
5976 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
5977 }
5978
Christoph Studer4600f9b2014-07-22 22:44:43 +02005979 /**
5980 * @hide
5981 */
5982 @Override
5983 protected void restoreFromExtras(Bundle extras) {
5984 super.restoreFromExtras(extras);
5985
5986 mTexts.clear();
5987 if (extras.containsKey(EXTRA_TEXT_LINES)) {
5988 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
5989 }
5990 }
5991
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005992 /**
5993 * @hide
5994 */
5995 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08005996 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02005997 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005998 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
5999 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006000
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006001 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04006002
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006003 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006004
Chris Wrend6297db2012-05-03 16:20:13 -04006005 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
Chris Wren29bb6d92012-05-17 18:09:42 -04006006 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04006007
Chris Wren4ed80d52012-05-17 09:30:03 -04006008 // Make sure all rows are gone in case we reuse a view.
6009 for (int rowId : rowIds) {
6010 contentView.setViewVisibility(rowId, View.GONE);
6011 }
6012
Daniel Sandler879c5e02012-04-17 16:46:51 -04006013 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07006014 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6015 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08006016 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07006017 int onlyViewId = 0;
6018 int maxRows = rowIds.length;
6019 if (mBuilder.mActions.size() > 0) {
6020 maxRows--;
6021 }
6022 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006023 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07006024 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006025 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01006026 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006027 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07006028 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08006029 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07006030 if (first) {
6031 onlyViewId = rowIds[i];
6032 } else {
6033 onlyViewId = 0;
6034 }
Selim Cinek247fa012016-02-18 09:50:48 -08006035 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04006036 }
6037 i++;
6038 }
Selim Cinek07c80172016-04-21 16:40:47 -07006039 if (onlyViewId != 0) {
6040 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
6041 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6042 R.dimen.notification_text_margin_top);
6043 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
6044 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006045
Daniel Sandler879c5e02012-04-17 16:46:51 -04006046 return contentView;
6047 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006048
Selim Cinek247fa012016-02-18 09:50:48 -08006049 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08006050 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08006051 if (first) {
6052 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
6053 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
6054 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07006055 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006056 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08006057 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006058 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006059 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08006060 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006061 }
Dan Sandler842dd772014-05-15 09:36:47 -04006062
6063 /**
6064 * Notification style for media playback notifications.
6065 *
6066 * In the expanded form, {@link Notification#bigContentView}, up to 5
6067 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04006068 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04006069 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
6070 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
6071 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08006072 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04006073 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
6074 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01006075 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04006076 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08006077 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006078 * Notifications created with MediaStyle will have their category set to
6079 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
6080 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08006081 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07006082 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
6083 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04006084 * the System UI can identify this as a notification representing an active media session
6085 * and respond accordingly (by showing album artwork in the lockscreen, for example).
6086 *
Selim Cinek99104832017-01-25 14:47:33 -08006087 * <p>
6088 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
6089 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
6090 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
6091 * <p>
6092 *
Dan Sandler842dd772014-05-15 09:36:47 -04006093 * To use this style with your Notification, feed it to
6094 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6095 * <pre class="prettyprint">
6096 * Notification noti = new Notification.Builder()
6097 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01006098 * .setContentTitle(&quot;Track title&quot;)
6099 * .setContentText(&quot;Artist - Album&quot;)
6100 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006101 * .setStyle(<b>new Notification.MediaStyle()</b>
6102 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04006103 * .build();
6104 * </pre>
6105 *
6106 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08006107 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04006108 */
6109 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006110 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04006111 static final int MAX_MEDIA_BUTTONS = 5;
6112
6113 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07006114 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04006115
6116 public MediaStyle() {
6117 }
6118
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006119 /**
6120 * @deprecated use {@code MediaStyle()}.
6121 */
6122 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04006123 public MediaStyle(Builder builder) {
6124 setBuilder(builder);
6125 }
6126
6127 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006128 * Request up to 3 actions (by index in the order of addition) to be shown in the compact
Dan Sandler842dd772014-05-15 09:36:47 -04006129 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006130 *
6131 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04006132 */
6133 public MediaStyle setShowActionsInCompactView(int...actions) {
6134 mActionsToShowInCompact = actions;
6135 return this;
6136 }
6137
6138 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07006139 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
6140 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04006141 */
Jeff Browndba34ba2014-06-24 20:46:03 -07006142 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04006143 mToken = token;
6144 return this;
6145 }
6146
Christoph Studer4600f9b2014-07-22 22:44:43 +02006147 /**
6148 * @hide
6149 */
Dan Sandler842dd772014-05-15 09:36:47 -04006150 @Override
6151 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006152 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006153 if (wip.category == null) {
6154 wip.category = Notification.CATEGORY_TRANSPORT;
6155 }
Dan Sandler842dd772014-05-15 09:36:47 -04006156 return wip;
6157 }
6158
Christoph Studer4600f9b2014-07-22 22:44:43 +02006159 /**
6160 * @hide
6161 */
6162 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006163 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006164 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006165 }
6166
6167 /**
6168 * @hide
6169 */
6170 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006171 public RemoteViews makeBigContentView() {
6172 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006173 }
6174
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006175 /**
6176 * @hide
6177 */
6178 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006179 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006180 RemoteViews expanded = makeMediaBigContentView();
6181 return expanded != null ? expanded : makeMediaContentView();
6182 }
6183
Dan Sandler842dd772014-05-15 09:36:47 -04006184 /** @hide */
6185 @Override
6186 public void addExtras(Bundle extras) {
6187 super.addExtras(extras);
6188
6189 if (mToken != null) {
6190 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
6191 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01006192 if (mActionsToShowInCompact != null) {
6193 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
6194 }
Dan Sandler842dd772014-05-15 09:36:47 -04006195 }
6196
Christoph Studer4600f9b2014-07-22 22:44:43 +02006197 /**
6198 * @hide
6199 */
6200 @Override
6201 protected void restoreFromExtras(Bundle extras) {
6202 super.restoreFromExtras(extras);
6203
6204 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
6205 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
6206 }
6207 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
6208 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
6209 }
6210 }
6211
Selim Cinek5bf069a2015-11-10 19:14:27 -05006212 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04006213 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07006214 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07006215 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04006216 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05006217 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
6218 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04006219 if (!tombstone) {
6220 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
6221 }
6222 button.setContentDescription(R.id.action0, action.title);
6223 return button;
6224 }
6225
6226 private RemoteViews makeMediaContentView() {
6227 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006228 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04006229
6230 final int numActions = mBuilder.mActions.size();
6231 final int N = mActionsToShowInCompact == null
6232 ? 0
6233 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
6234 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006235 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04006236 for (int i = 0; i < N; i++) {
6237 if (i >= numActions) {
6238 throw new IllegalArgumentException(String.format(
6239 "setShowActionsInCompactView: action %d out of bounds (max %d)",
6240 i, numActions - 1));
6241 }
6242
6243 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05006244 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08006245 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006246 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006247 }
6248 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08006249 handleImage(view);
6250 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006251 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006252 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006253 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08006254 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006255 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04006256 return view;
6257 }
6258
Selim Cinek99104832017-01-25 14:47:33 -08006259 private int getPrimaryHighlightColor() {
6260 return mBuilder.getPrimaryHighlightColor();
6261 }
6262
Dan Sandler842dd772014-05-15 09:36:47 -04006263 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006264 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006265 // Dont add an expanded view if there is no more content to be revealed
6266 int actionsInCompact = mActionsToShowInCompact == null
6267 ? 0
6268 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07006269 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006270 return null;
6271 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006272 RemoteViews big = mBuilder.applyStandardTemplate(
6273 R.layout.notification_template_material_big_media,
6274 false);
Dan Sandler842dd772014-05-15 09:36:47 -04006275
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006276 if (actionCount > 0) {
6277 big.removeAllViews(com.android.internal.R.id.media_actions);
6278 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05006279 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08006280 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006281 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006282 }
6283 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006284 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04006285 return big;
6286 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006287
Selim Cinek5bf069a2015-11-10 19:14:27 -05006288 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07006289 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006290 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
6291 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006292 }
6293 }
6294
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006295 /**
6296 * @hide
6297 */
6298 @Override
6299 protected boolean hasProgress() {
6300 return false;
6301 }
Dan Sandler842dd772014-05-15 09:36:47 -04006302 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006303
Selim Cinek593610c2016-02-16 18:42:57 -08006304 /**
6305 * Notification style for custom views that are decorated by the system
6306 *
6307 * <p>Instead of providing a notification that is completely custom, a developer can set this
6308 * style and still obtain system decorations like the notification header with the expand
6309 * affordance and actions.
6310 *
6311 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6312 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6313 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6314 * corresponding custom views to display.
6315 *
6316 * To use this style with your Notification, feed it to
6317 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6318 * <pre class="prettyprint">
6319 * Notification noti = new Notification.Builder()
6320 * .setSmallIcon(R.drawable.ic_stat_player)
6321 * .setLargeIcon(albumArtBitmap))
6322 * .setCustomContentView(contentView);
6323 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
6324 * .build();
6325 * </pre>
6326 */
6327 public static class DecoratedCustomViewStyle extends Style {
6328
6329 public DecoratedCustomViewStyle() {
6330 }
6331
Selim Cinek593610c2016-02-16 18:42:57 -08006332 /**
6333 * @hide
6334 */
6335 public boolean displayCustomViewInline() {
6336 return true;
6337 }
6338
6339 /**
6340 * @hide
6341 */
6342 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006343 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006344 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
6345 }
6346
6347 /**
6348 * @hide
6349 */
6350 @Override
6351 public RemoteViews makeBigContentView() {
6352 return makeDecoratedBigContentView();
6353 }
6354
6355 /**
6356 * @hide
6357 */
6358 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006359 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006360 return makeDecoratedHeadsUpContentView();
6361 }
6362
Selim Cinek593610c2016-02-16 18:42:57 -08006363 private RemoteViews makeDecoratedHeadsUpContentView() {
6364 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
6365 ? mBuilder.mN.contentView
6366 : mBuilder.mN.headsUpContentView;
6367 if (mBuilder.mActions.size() == 0) {
6368 return makeStandardTemplateWithCustomContent(headsUpContentView);
6369 }
6370 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6371 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006372 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006373 return remoteViews;
6374 }
6375
Selim Cinek593610c2016-02-16 18:42:57 -08006376 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
6377 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
6378 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006379 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08006380 return remoteViews;
6381 }
6382
Selim Cinek593610c2016-02-16 18:42:57 -08006383 private RemoteViews makeDecoratedBigContentView() {
6384 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
6385 ? mBuilder.mN.contentView
6386 : mBuilder.mN.bigContentView;
6387 if (mBuilder.mActions.size() == 0) {
6388 return makeStandardTemplateWithCustomContent(bigContentView);
6389 }
6390 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6391 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006392 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006393 return remoteViews;
6394 }
Selim Cinek247fa012016-02-18 09:50:48 -08006395
6396 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
6397 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08006398 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07006399 // Need to clone customContent before adding, because otherwise it can no longer be
6400 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08006401 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07006402 remoteViews.removeAllViews(R.id.notification_main_column);
6403 remoteViews.addView(R.id.notification_main_column, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08006404 }
Selim Cinek247fa012016-02-18 09:50:48 -08006405 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006406 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006407 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006408 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08006409 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006410 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08006411 }
Selim Cinek593610c2016-02-16 18:42:57 -08006412 }
6413
Selim Cinek03eb3b72016-02-18 10:39:45 -08006414 /**
6415 * Notification style for media custom views that are decorated by the system
6416 *
6417 * <p>Instead of providing a media notification that is completely custom, a developer can set
6418 * this style and still obtain system decorations like the notification header with the expand
6419 * affordance and actions.
6420 *
6421 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6422 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6423 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6424 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08006425 * <p>
6426 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
6427 * notification by using {@link Notification.Builder#setColorized(boolean)}.
6428 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08006429 * To use this style with your Notification, feed it to
6430 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6431 * <pre class="prettyprint">
6432 * Notification noti = new Notification.Builder()
6433 * .setSmallIcon(R.drawable.ic_stat_player)
6434 * .setLargeIcon(albumArtBitmap))
6435 * .setCustomContentView(contentView);
6436 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
6437 * .setMediaSession(mySession))
6438 * .build();
6439 * </pre>
6440 *
6441 * @see android.app.Notification.DecoratedCustomViewStyle
6442 * @see android.app.Notification.MediaStyle
6443 */
6444 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
6445
6446 public DecoratedMediaCustomViewStyle() {
6447 }
6448
Selim Cinek03eb3b72016-02-18 10:39:45 -08006449 /**
6450 * @hide
6451 */
6452 public boolean displayCustomViewInline() {
6453 return true;
6454 }
6455
6456 /**
6457 * @hide
6458 */
6459 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006460 public RemoteViews makeContentView(boolean increasedHeight) {
6461 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006462 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6463 mBuilder.mN.contentView);
6464 }
6465
6466 /**
6467 * @hide
6468 */
6469 @Override
6470 public RemoteViews makeBigContentView() {
6471 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
6472 ? mBuilder.mN.bigContentView
6473 : mBuilder.mN.contentView;
6474 return makeBigContentViewWithCustomContent(customRemoteView);
6475 }
6476
6477 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
6478 RemoteViews remoteViews = super.makeBigContentView();
6479 if (remoteViews != null) {
6480 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
6481 customRemoteView);
6482 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08006483 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006484 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6485 customRemoteView);
6486 } else {
6487 return null;
6488 }
6489 }
6490
6491 /**
6492 * @hide
6493 */
6494 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006495 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08006496 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
6497 ? mBuilder.mN.headsUpContentView
6498 : mBuilder.mN.contentView;
6499 return makeBigContentViewWithCustomContent(customRemoteView);
6500 }
6501
6502 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
6503 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08006504 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07006505 // Need to clone customContent before adding, because otherwise it can no longer be
6506 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08006507 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07006508 remoteViews.removeAllViews(id);
6509 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08006510 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08006511 return remoteViews;
6512 }
6513 }
6514
Christoph Studer4600f9b2014-07-22 22:44:43 +02006515 // When adding a new Style subclass here, don't forget to update
6516 // Builder.getNotificationStyleClass.
6517
Griff Hazen61a9e862014-05-22 16:05:19 -07006518 /**
6519 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
6520 * metadata or change options on a notification builder.
6521 */
6522 public interface Extender {
6523 /**
6524 * Apply this extender to a notification builder.
6525 * @param builder the builder to be modified.
6526 * @return the build object for chaining.
6527 */
6528 public Builder extend(Builder builder);
6529 }
6530
6531 /**
6532 * Helper class to add wearable extensions to notifications.
6533 * <p class="note"> See
6534 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
6535 * for Android Wear</a> for more information on how to use this class.
6536 * <p>
6537 * To create a notification with wearable extensions:
6538 * <ol>
6539 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
6540 * properties.
6541 * <li>Create a {@link android.app.Notification.WearableExtender}.
6542 * <li>Set wearable-specific properties using the
6543 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
6544 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
6545 * notification.
6546 * <li>Post the notification to the notification system with the
6547 * {@code NotificationManager.notify(...)} methods.
6548 * </ol>
6549 *
6550 * <pre class="prettyprint">
6551 * Notification notif = new Notification.Builder(mContext)
6552 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6553 * .setContentText(subject)
6554 * .setSmallIcon(R.drawable.new_mail)
6555 * .extend(new Notification.WearableExtender()
6556 * .setContentIcon(R.drawable.new_mail))
6557 * .build();
6558 * NotificationManager notificationManger =
6559 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
6560 * notificationManger.notify(0, notif);</pre>
6561 *
6562 * <p>Wearable extensions can be accessed on an existing notification by using the
6563 * {@code WearableExtender(Notification)} constructor,
6564 * and then using the {@code get} methods to access values.
6565 *
6566 * <pre class="prettyprint">
6567 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
6568 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07006569 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07006570 */
6571 public static final class WearableExtender implements Extender {
6572 /**
6573 * Sentinel value for an action index that is unset.
6574 */
6575 public static final int UNSET_ACTION_INDEX = -1;
6576
6577 /**
6578 * Size value for use with {@link #setCustomSizePreset} to show this notification with
6579 * default sizing.
6580 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07006581 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07006582 * on their content.
6583 */
6584 public static final int SIZE_DEFAULT = 0;
6585
6586 /**
6587 * Size value for use with {@link #setCustomSizePreset} to show this notification
6588 * with an extra small size.
6589 * <p>This value is only applicable for custom display notifications created using
6590 * {@link #setDisplayIntent}.
6591 */
6592 public static final int SIZE_XSMALL = 1;
6593
6594 /**
6595 * Size value for use with {@link #setCustomSizePreset} to show this notification
6596 * with a small size.
6597 * <p>This value is only applicable for custom display notifications created using
6598 * {@link #setDisplayIntent}.
6599 */
6600 public static final int SIZE_SMALL = 2;
6601
6602 /**
6603 * Size value for use with {@link #setCustomSizePreset} to show this notification
6604 * with a medium size.
6605 * <p>This value is only applicable for custom display notifications created using
6606 * {@link #setDisplayIntent}.
6607 */
6608 public static final int SIZE_MEDIUM = 3;
6609
6610 /**
6611 * Size value for use with {@link #setCustomSizePreset} to show this notification
6612 * with a large size.
6613 * <p>This value is only applicable for custom display notifications created using
6614 * {@link #setDisplayIntent}.
6615 */
6616 public static final int SIZE_LARGE = 4;
6617
Griff Hazend5f11f92014-05-27 15:40:09 -07006618 /**
6619 * Size value for use with {@link #setCustomSizePreset} to show this notification
6620 * full screen.
6621 * <p>This value is only applicable for custom display notifications created using
6622 * {@link #setDisplayIntent}.
6623 */
6624 public static final int SIZE_FULL_SCREEN = 5;
6625
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006626 /**
6627 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
6628 * short amount of time when this notification is displayed on the screen. This
6629 * is the default value.
6630 */
6631 public static final int SCREEN_TIMEOUT_SHORT = 0;
6632
6633 /**
6634 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
6635 * for a longer amount of time when this notification is displayed on the screen.
6636 */
6637 public static final int SCREEN_TIMEOUT_LONG = -1;
6638
Griff Hazen61a9e862014-05-22 16:05:19 -07006639 /** Notification extra which contains wearable extensions */
6640 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
6641
Pete Gastaf6781d2014-10-07 15:17:05 -04006642 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07006643 private static final String KEY_ACTIONS = "actions";
6644 private static final String KEY_FLAGS = "flags";
6645 private static final String KEY_DISPLAY_INTENT = "displayIntent";
6646 private static final String KEY_PAGES = "pages";
6647 private static final String KEY_BACKGROUND = "background";
6648 private static final String KEY_CONTENT_ICON = "contentIcon";
6649 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
6650 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
6651 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
6652 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
6653 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006654 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04006655 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006656 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07006657
6658 // Flags bitwise-ored to mFlags
6659 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
6660 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
6661 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
6662 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006663 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04006664 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04006665 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07006666
6667 // Default value for flags integer
6668 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
6669
6670 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
6671 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
6672
6673 private ArrayList<Action> mActions = new ArrayList<Action>();
6674 private int mFlags = DEFAULT_FLAGS;
6675 private PendingIntent mDisplayIntent;
6676 private ArrayList<Notification> mPages = new ArrayList<Notification>();
6677 private Bitmap mBackground;
6678 private int mContentIcon;
6679 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
6680 private int mContentActionIndex = UNSET_ACTION_INDEX;
6681 private int mCustomSizePreset = SIZE_DEFAULT;
6682 private int mCustomContentHeight;
6683 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006684 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04006685 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006686 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07006687
6688 /**
6689 * Create a {@link android.app.Notification.WearableExtender} with default
6690 * options.
6691 */
6692 public WearableExtender() {
6693 }
6694
6695 public WearableExtender(Notification notif) {
6696 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
6697 if (wearableBundle != null) {
6698 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
6699 if (actions != null) {
6700 mActions.addAll(actions);
6701 }
6702
6703 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
6704 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
6705
6706 Notification[] pages = getNotificationArrayFromBundle(
6707 wearableBundle, KEY_PAGES);
6708 if (pages != null) {
6709 Collections.addAll(mPages, pages);
6710 }
6711
6712 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
6713 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
6714 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
6715 DEFAULT_CONTENT_ICON_GRAVITY);
6716 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
6717 UNSET_ACTION_INDEX);
6718 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
6719 SIZE_DEFAULT);
6720 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
6721 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006722 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04006723 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006724 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07006725 }
6726 }
6727
6728 /**
6729 * Apply wearable extensions to a notification that is being built. This is typically
6730 * called by the {@link android.app.Notification.Builder#extend} method of
6731 * {@link android.app.Notification.Builder}.
6732 */
6733 @Override
6734 public Notification.Builder extend(Notification.Builder builder) {
6735 Bundle wearableBundle = new Bundle();
6736
6737 if (!mActions.isEmpty()) {
6738 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
6739 }
6740 if (mFlags != DEFAULT_FLAGS) {
6741 wearableBundle.putInt(KEY_FLAGS, mFlags);
6742 }
6743 if (mDisplayIntent != null) {
6744 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
6745 }
6746 if (!mPages.isEmpty()) {
6747 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
6748 new Notification[mPages.size()]));
6749 }
6750 if (mBackground != null) {
6751 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
6752 }
6753 if (mContentIcon != 0) {
6754 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
6755 }
6756 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
6757 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
6758 }
6759 if (mContentActionIndex != UNSET_ACTION_INDEX) {
6760 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
6761 mContentActionIndex);
6762 }
6763 if (mCustomSizePreset != SIZE_DEFAULT) {
6764 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
6765 }
6766 if (mCustomContentHeight != 0) {
6767 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
6768 }
6769 if (mGravity != DEFAULT_GRAVITY) {
6770 wearableBundle.putInt(KEY_GRAVITY, mGravity);
6771 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006772 if (mHintScreenTimeout != 0) {
6773 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
6774 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04006775 if (mDismissalId != null) {
6776 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
6777 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006778 if (mBridgeTag != null) {
6779 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
6780 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006781
6782 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
6783 return builder;
6784 }
6785
6786 @Override
6787 public WearableExtender clone() {
6788 WearableExtender that = new WearableExtender();
6789 that.mActions = new ArrayList<Action>(this.mActions);
6790 that.mFlags = this.mFlags;
6791 that.mDisplayIntent = this.mDisplayIntent;
6792 that.mPages = new ArrayList<Notification>(this.mPages);
6793 that.mBackground = this.mBackground;
6794 that.mContentIcon = this.mContentIcon;
6795 that.mContentIconGravity = this.mContentIconGravity;
6796 that.mContentActionIndex = this.mContentActionIndex;
6797 that.mCustomSizePreset = this.mCustomSizePreset;
6798 that.mCustomContentHeight = this.mCustomContentHeight;
6799 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006800 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04006801 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006802 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07006803 return that;
6804 }
6805
6806 /**
6807 * Add a wearable action to this notification.
6808 *
6809 * <p>When wearable actions are added using this method, the set of actions that
6810 * show on a wearable device splits from devices that only show actions added
6811 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
6812 * of which actions display on different devices.
6813 *
6814 * @param action the action to add to this notification
6815 * @return this object for method chaining
6816 * @see android.app.Notification.Action
6817 */
6818 public WearableExtender addAction(Action action) {
6819 mActions.add(action);
6820 return this;
6821 }
6822
6823 /**
6824 * Adds wearable actions to this notification.
6825 *
6826 * <p>When wearable actions are added using this method, the set of actions that
6827 * show on a wearable device splits from devices that only show actions added
6828 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
6829 * of which actions display on different devices.
6830 *
6831 * @param actions the actions to add to this notification
6832 * @return this object for method chaining
6833 * @see android.app.Notification.Action
6834 */
6835 public WearableExtender addActions(List<Action> actions) {
6836 mActions.addAll(actions);
6837 return this;
6838 }
6839
6840 /**
6841 * Clear all wearable actions present on this builder.
6842 * @return this object for method chaining.
6843 * @see #addAction
6844 */
6845 public WearableExtender clearActions() {
6846 mActions.clear();
6847 return this;
6848 }
6849
6850 /**
6851 * Get the wearable actions present on this notification.
6852 */
6853 public List<Action> getActions() {
6854 return mActions;
6855 }
6856
6857 /**
6858 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07006859 * this notification. The {@link PendingIntent} provided should be for an activity.
6860 *
6861 * <pre class="prettyprint">
6862 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
6863 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
6864 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
6865 * Notification notif = new Notification.Builder(context)
6866 * .extend(new Notification.WearableExtender()
6867 * .setDisplayIntent(displayPendingIntent)
6868 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
6869 * .build();</pre>
6870 *
6871 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07006872 * should have an empty task affinity. It is also recommended to use the device
6873 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07006874 *
6875 * <p>Example AndroidManifest.xml entry:
6876 * <pre class="prettyprint">
6877 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
6878 * android:exported=&quot;true&quot;
6879 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07006880 * android:taskAffinity=&quot;&quot;
6881 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07006882 *
6883 * @param intent the {@link PendingIntent} for an activity
6884 * @return this object for method chaining
6885 * @see android.app.Notification.WearableExtender#getDisplayIntent
6886 */
6887 public WearableExtender setDisplayIntent(PendingIntent intent) {
6888 mDisplayIntent = intent;
6889 return this;
6890 }
6891
6892 /**
6893 * Get the intent to launch inside of an activity view when displaying this
6894 * notification. This {@code PendingIntent} should be for an activity.
6895 */
6896 public PendingIntent getDisplayIntent() {
6897 return mDisplayIntent;
6898 }
6899
6900 /**
6901 * Add an additional page of content to display with this notification. The current
6902 * notification forms the first page, and pages added using this function form
6903 * subsequent pages. This field can be used to separate a notification into multiple
6904 * sections.
6905 *
6906 * @param page the notification to add as another page
6907 * @return this object for method chaining
6908 * @see android.app.Notification.WearableExtender#getPages
6909 */
6910 public WearableExtender addPage(Notification page) {
6911 mPages.add(page);
6912 return this;
6913 }
6914
6915 /**
6916 * Add additional pages of content to display with this notification. The current
6917 * notification forms the first page, and pages added using this function form
6918 * subsequent pages. This field can be used to separate a notification into multiple
6919 * sections.
6920 *
6921 * @param pages a list of notifications
6922 * @return this object for method chaining
6923 * @see android.app.Notification.WearableExtender#getPages
6924 */
6925 public WearableExtender addPages(List<Notification> pages) {
6926 mPages.addAll(pages);
6927 return this;
6928 }
6929
6930 /**
6931 * Clear all additional pages present on this builder.
6932 * @return this object for method chaining.
6933 * @see #addPage
6934 */
6935 public WearableExtender clearPages() {
6936 mPages.clear();
6937 return this;
6938 }
6939
6940 /**
6941 * Get the array of additional pages of content for displaying this notification. The
6942 * current notification forms the first page, and elements within this array form
6943 * subsequent pages. This field can be used to separate a notification into multiple
6944 * sections.
6945 * @return the pages for this notification
6946 */
6947 public List<Notification> getPages() {
6948 return mPages;
6949 }
6950
6951 /**
6952 * Set a background image to be displayed behind the notification content.
6953 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
6954 * will work with any notification style.
6955 *
6956 * @param background the background bitmap
6957 * @return this object for method chaining
6958 * @see android.app.Notification.WearableExtender#getBackground
6959 */
6960 public WearableExtender setBackground(Bitmap background) {
6961 mBackground = background;
6962 return this;
6963 }
6964
6965 /**
6966 * Get a background image to be displayed behind the notification content.
6967 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
6968 * will work with any notification style.
6969 *
6970 * @return the background image
6971 * @see android.app.Notification.WearableExtender#setBackground
6972 */
6973 public Bitmap getBackground() {
6974 return mBackground;
6975 }
6976
6977 /**
6978 * Set an icon that goes with the content of this notification.
6979 */
6980 public WearableExtender setContentIcon(int icon) {
6981 mContentIcon = icon;
6982 return this;
6983 }
6984
6985 /**
6986 * Get an icon that goes with the content of this notification.
6987 */
6988 public int getContentIcon() {
6989 return mContentIcon;
6990 }
6991
6992 /**
6993 * Set the gravity that the content icon should have within the notification display.
6994 * Supported values include {@link android.view.Gravity#START} and
6995 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
6996 * @see #setContentIcon
6997 */
6998 public WearableExtender setContentIconGravity(int contentIconGravity) {
6999 mContentIconGravity = contentIconGravity;
7000 return this;
7001 }
7002
7003 /**
7004 * Get the gravity that the content icon should have within the notification display.
7005 * Supported values include {@link android.view.Gravity#START} and
7006 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7007 * @see #getContentIcon
7008 */
7009 public int getContentIconGravity() {
7010 return mContentIconGravity;
7011 }
7012
7013 /**
7014 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07007015 * this notification. This action will no longer display separately from the
7016 * notification's content.
7017 *
Griff Hazenca48d352014-05-28 22:37:13 -07007018 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007019 * set, although the list of available actions comes from the main notification and not
7020 * from the child page's notification.
7021 *
7022 * @param actionIndex The index of the action to hoist onto the current notification page.
7023 * If wearable actions were added to the main notification, this index
7024 * will apply to that list, otherwise it will apply to the regular
7025 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07007026 */
7027 public WearableExtender setContentAction(int actionIndex) {
7028 mContentActionIndex = actionIndex;
7029 return this;
7030 }
7031
7032 /**
Griff Hazenca48d352014-05-28 22:37:13 -07007033 * Get the index of the notification action, if any, that was specified as being clickable
7034 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07007035 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07007036 *
Griff Hazenca48d352014-05-28 22:37:13 -07007037 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007038 * set, although the list of available actions comes from the main notification and not
7039 * from the child page's notification.
7040 *
7041 * <p>If wearable specific actions were added to the main notification, this index will
7042 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07007043 *
7044 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07007045 */
7046 public int getContentAction() {
7047 return mContentActionIndex;
7048 }
7049
7050 /**
7051 * Set the gravity that this notification should have within the available viewport space.
7052 * Supported values include {@link android.view.Gravity#TOP},
7053 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7054 * The default value is {@link android.view.Gravity#BOTTOM}.
7055 */
7056 public WearableExtender setGravity(int gravity) {
7057 mGravity = gravity;
7058 return this;
7059 }
7060
7061 /**
7062 * Get the gravity that this notification should have within the available viewport space.
7063 * Supported values include {@link android.view.Gravity#TOP},
7064 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7065 * The default value is {@link android.view.Gravity#BOTTOM}.
7066 */
7067 public int getGravity() {
7068 return mGravity;
7069 }
7070
7071 /**
7072 * Set the custom size preset for the display of this notification out of the available
7073 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7074 * {@link #SIZE_LARGE}.
7075 * <p>Some custom size presets are only applicable for custom display notifications created
7076 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
7077 * documentation for the preset in question. See also
7078 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
7079 */
7080 public WearableExtender setCustomSizePreset(int sizePreset) {
7081 mCustomSizePreset = sizePreset;
7082 return this;
7083 }
7084
7085 /**
7086 * Get the custom size preset for the display of this notification out of the available
7087 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7088 * {@link #SIZE_LARGE}.
7089 * <p>Some custom size presets are only applicable for custom display notifications created
7090 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
7091 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
7092 */
7093 public int getCustomSizePreset() {
7094 return mCustomSizePreset;
7095 }
7096
7097 /**
7098 * Set the custom height in pixels for the display of this notification's content.
7099 * <p>This option is only available for custom display notifications created
7100 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
7101 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
7102 * {@link #getCustomContentHeight}.
7103 */
7104 public WearableExtender setCustomContentHeight(int height) {
7105 mCustomContentHeight = height;
7106 return this;
7107 }
7108
7109 /**
7110 * Get the custom height in pixels for the display of this notification's content.
7111 * <p>This option is only available for custom display notifications created
7112 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
7113 * {@link #setCustomContentHeight}.
7114 */
7115 public int getCustomContentHeight() {
7116 return mCustomContentHeight;
7117 }
7118
7119 /**
7120 * Set whether the scrolling position for the contents of this notification should start
7121 * at the bottom of the contents instead of the top when the contents are too long to
7122 * display within the screen. Default is false (start scroll at the top).
7123 */
7124 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
7125 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
7126 return this;
7127 }
7128
7129 /**
7130 * Get whether the scrolling position for the contents of this notification should start
7131 * at the bottom of the contents instead of the top when the contents are too long to
7132 * display within the screen. Default is false (start scroll at the top).
7133 */
7134 public boolean getStartScrollBottom() {
7135 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
7136 }
7137
7138 /**
7139 * Set whether the content intent is available when the wearable device is not connected
7140 * to a companion device. The user can still trigger this intent when the wearable device
7141 * is offline, but a visual hint will indicate that the content intent may not be available.
7142 * Defaults to true.
7143 */
7144 public WearableExtender setContentIntentAvailableOffline(
7145 boolean contentIntentAvailableOffline) {
7146 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
7147 return this;
7148 }
7149
7150 /**
7151 * Get whether the content intent is available when the wearable device is not connected
7152 * to a companion device. The user can still trigger this intent when the wearable device
7153 * is offline, but a visual hint will indicate that the content intent may not be available.
7154 * Defaults to true.
7155 */
7156 public boolean getContentIntentAvailableOffline() {
7157 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
7158 }
7159
7160 /**
7161 * Set a hint that this notification's icon should not be displayed.
7162 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
7163 * @return this object for method chaining
7164 */
7165 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
7166 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
7167 return this;
7168 }
7169
7170 /**
7171 * Get a hint that this notification's icon should not be displayed.
7172 * @return {@code true} if this icon should not be displayed, false otherwise.
7173 * The default value is {@code false} if this was never set.
7174 */
7175 public boolean getHintHideIcon() {
7176 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
7177 }
7178
7179 /**
7180 * Set a visual hint that only the background image of this notification should be
7181 * displayed, and other semantic content should be hidden. This hint is only applicable
7182 * to sub-pages added using {@link #addPage}.
7183 */
7184 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
7185 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
7186 return this;
7187 }
7188
7189 /**
7190 * Get a visual hint that only the background image of this notification should be
7191 * displayed, and other semantic content should be hidden. This hint is only applicable
7192 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
7193 */
7194 public boolean getHintShowBackgroundOnly() {
7195 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
7196 }
7197
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007198 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007199 * Set a hint that this notification's background should not be clipped if possible,
7200 * and should instead be resized to fully display on the screen, retaining the aspect
7201 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007202 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
7203 * @return this object for method chaining
7204 */
7205 public WearableExtender setHintAvoidBackgroundClipping(
7206 boolean hintAvoidBackgroundClipping) {
7207 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
7208 return this;
7209 }
7210
7211 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007212 * Get a hint that this notification's background should not be clipped if possible,
7213 * and should instead be resized to fully display on the screen, retaining the aspect
7214 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007215 * @return {@code true} if it's ok if the background is clipped on the screen, false
7216 * otherwise. The default value is {@code false} if this was never set.
7217 */
7218 public boolean getHintAvoidBackgroundClipping() {
7219 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
7220 }
7221
7222 /**
7223 * Set a hint that the screen should remain on for at least this duration when
7224 * this notification is displayed on the screen.
7225 * @param timeout The requested screen timeout in milliseconds. Can also be either
7226 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7227 * @return this object for method chaining
7228 */
7229 public WearableExtender setHintScreenTimeout(int timeout) {
7230 mHintScreenTimeout = timeout;
7231 return this;
7232 }
7233
7234 /**
7235 * Get the duration, in milliseconds, that the screen should remain on for
7236 * when this notification is displayed.
7237 * @return the duration in milliseconds if > 0, or either one of the sentinel values
7238 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7239 */
7240 public int getHintScreenTimeout() {
7241 return mHintScreenTimeout;
7242 }
7243
Alex Hills9ab3a232016-04-05 14:54:56 -04007244 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04007245 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
7246 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7247 * qr codes, as well as other simple black-and-white tickets.
7248 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
7249 * @return this object for method chaining
7250 */
7251 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
7252 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
7253 return this;
7254 }
7255
7256 /**
7257 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
7258 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7259 * qr codes, as well as other simple black-and-white tickets.
7260 * @return {@code true} if it should be displayed in ambient, false otherwise
7261 * otherwise. The default value is {@code false} if this was never set.
7262 */
7263 public boolean getHintAmbientBigPicture() {
7264 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
7265 }
7266
7267 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04007268 * Set a hint that this notification's content intent will launch an {@link Activity}
7269 * directly, telling the platform that it can generate the appropriate transitions.
7270 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
7271 * an activity and transitions should be generated, false otherwise.
7272 * @return this object for method chaining
7273 */
7274 public WearableExtender setHintContentIntentLaunchesActivity(
7275 boolean hintContentIntentLaunchesActivity) {
7276 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
7277 return this;
7278 }
7279
7280 /**
7281 * Get a hint that this notification's content intent will launch an {@link Activity}
7282 * directly, telling the platform that it can generate the appropriate transitions
7283 * @return {@code true} if the content intent will launch an activity and transitions should
7284 * be generated, false otherwise. The default value is {@code false} if this was never set.
7285 */
7286 public boolean getHintContentIntentLaunchesActivity() {
7287 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
7288 }
7289
Nadia Benbernou948627e2016-04-14 14:41:08 -04007290 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007291 * Sets the dismissal id for this notification. If a notification is posted with a
7292 * dismissal id, then when that notification is canceled, notifications on other wearables
7293 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04007294 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007295 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04007296 * @param dismissalId the dismissal id of the notification.
7297 * @return this object for method chaining
7298 */
7299 public WearableExtender setDismissalId(String dismissalId) {
7300 mDismissalId = dismissalId;
7301 return this;
7302 }
7303
7304 /**
7305 * Returns the dismissal id of the notification.
7306 * @return the dismissal id of the notification or null if it has not been set.
7307 */
7308 public String getDismissalId() {
7309 return mDismissalId;
7310 }
7311
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007312 /**
7313 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
7314 * posted from a phone to provide finer-grained control on what notifications are bridged
7315 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
7316 * Features to Notifications</a> for more information.
7317 * @param bridgeTag the bridge tag of the notification.
7318 * @return this object for method chaining
7319 */
7320 public WearableExtender setBridgeTag(String bridgeTag) {
7321 mBridgeTag = bridgeTag;
7322 return this;
7323 }
7324
7325 /**
7326 * Returns the bridge tag of the notification.
7327 * @return the bridge tag or null if not present.
7328 */
7329 public String getBridgeTag() {
7330 return mBridgeTag;
7331 }
7332
Griff Hazen61a9e862014-05-22 16:05:19 -07007333 private void setFlag(int mask, boolean value) {
7334 if (value) {
7335 mFlags |= mask;
7336 } else {
7337 mFlags &= ~mask;
7338 }
7339 }
7340 }
7341
7342 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007343 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
7344 * with car extensions:
7345 *
7346 * <ol>
7347 * <li>Create an {@link Notification.Builder}, setting any desired
7348 * properties.
7349 * <li>Create a {@link CarExtender}.
7350 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
7351 * {@link CarExtender}.
7352 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
7353 * to apply the extensions to a notification.
7354 * </ol>
7355 *
7356 * <pre class="prettyprint">
7357 * Notification notification = new Notification.Builder(context)
7358 * ...
7359 * .extend(new CarExtender()
7360 * .set*(...))
7361 * .build();
7362 * </pre>
7363 *
7364 * <p>Car extensions can be accessed on an existing notification by using the
7365 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
7366 * to access values.
7367 */
7368 public static final class CarExtender implements Extender {
7369 private static final String TAG = "CarExtender";
7370
7371 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
7372 private static final String EXTRA_LARGE_ICON = "large_icon";
7373 private static final String EXTRA_CONVERSATION = "car_conversation";
7374 private static final String EXTRA_COLOR = "app_color";
7375
7376 private Bitmap mLargeIcon;
7377 private UnreadConversation mUnreadConversation;
7378 private int mColor = Notification.COLOR_DEFAULT;
7379
7380 /**
7381 * Create a {@link CarExtender} with default options.
7382 */
7383 public CarExtender() {
7384 }
7385
7386 /**
7387 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
7388 *
7389 * @param notif The notification from which to copy options.
7390 */
7391 public CarExtender(Notification notif) {
7392 Bundle carBundle = notif.extras == null ?
7393 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
7394 if (carBundle != null) {
7395 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
7396 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
7397
7398 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
7399 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
7400 }
7401 }
7402
7403 /**
7404 * Apply car extensions to a notification that is being built. This is typically called by
7405 * the {@link Notification.Builder#extend(Notification.Extender)}
7406 * method of {@link Notification.Builder}.
7407 */
7408 @Override
7409 public Notification.Builder extend(Notification.Builder builder) {
7410 Bundle carExtensions = new Bundle();
7411
7412 if (mLargeIcon != null) {
7413 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
7414 }
7415 if (mColor != Notification.COLOR_DEFAULT) {
7416 carExtensions.putInt(EXTRA_COLOR, mColor);
7417 }
7418
7419 if (mUnreadConversation != null) {
7420 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
7421 carExtensions.putBundle(EXTRA_CONVERSATION, b);
7422 }
7423
7424 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
7425 return builder;
7426 }
7427
7428 /**
7429 * Sets the accent color to use when Android Auto presents the notification.
7430 *
7431 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
7432 * to accent the displayed notification. However, not all colors are acceptable in an
7433 * automotive setting. This method can be used to override the color provided in the
7434 * notification in such a situation.
7435 */
Tor Norbye80756e32015-03-02 09:39:27 -08007436 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007437 mColor = color;
7438 return this;
7439 }
7440
7441 /**
7442 * Gets the accent color.
7443 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007444 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007445 */
Tor Norbye80756e32015-03-02 09:39:27 -08007446 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007447 public int getColor() {
7448 return mColor;
7449 }
7450
7451 /**
7452 * Sets the large icon of the car notification.
7453 *
7454 * If no large icon is set in the extender, Android Auto will display the icon
7455 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
7456 *
7457 * @param largeIcon The large icon to use in the car notification.
7458 * @return This object for method chaining.
7459 */
7460 public CarExtender setLargeIcon(Bitmap largeIcon) {
7461 mLargeIcon = largeIcon;
7462 return this;
7463 }
7464
7465 /**
7466 * Gets the large icon used in this car notification, or null if no icon has been set.
7467 *
7468 * @return The large icon for the car notification.
7469 * @see CarExtender#setLargeIcon
7470 */
7471 public Bitmap getLargeIcon() {
7472 return mLargeIcon;
7473 }
7474
7475 /**
7476 * Sets the unread conversation in a message notification.
7477 *
7478 * @param unreadConversation The unread part of the conversation this notification conveys.
7479 * @return This object for method chaining.
7480 */
7481 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
7482 mUnreadConversation = unreadConversation;
7483 return this;
7484 }
7485
7486 /**
7487 * Returns the unread conversation conveyed by this notification.
7488 * @see #setUnreadConversation(UnreadConversation)
7489 */
7490 public UnreadConversation getUnreadConversation() {
7491 return mUnreadConversation;
7492 }
7493
7494 /**
7495 * A class which holds the unread messages from a conversation.
7496 */
7497 public static class UnreadConversation {
7498 private static final String KEY_AUTHOR = "author";
7499 private static final String KEY_TEXT = "text";
7500 private static final String KEY_MESSAGES = "messages";
7501 private static final String KEY_REMOTE_INPUT = "remote_input";
7502 private static final String KEY_ON_REPLY = "on_reply";
7503 private static final String KEY_ON_READ = "on_read";
7504 private static final String KEY_PARTICIPANTS = "participants";
7505 private static final String KEY_TIMESTAMP = "timestamp";
7506
7507 private final String[] mMessages;
7508 private final RemoteInput mRemoteInput;
7509 private final PendingIntent mReplyPendingIntent;
7510 private final PendingIntent mReadPendingIntent;
7511 private final String[] mParticipants;
7512 private final long mLatestTimestamp;
7513
7514 UnreadConversation(String[] messages, RemoteInput remoteInput,
7515 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
7516 String[] participants, long latestTimestamp) {
7517 mMessages = messages;
7518 mRemoteInput = remoteInput;
7519 mReadPendingIntent = readPendingIntent;
7520 mReplyPendingIntent = replyPendingIntent;
7521 mParticipants = participants;
7522 mLatestTimestamp = latestTimestamp;
7523 }
7524
7525 /**
7526 * Gets the list of messages conveyed by this notification.
7527 */
7528 public String[] getMessages() {
7529 return mMessages;
7530 }
7531
7532 /**
7533 * Gets the remote input that will be used to convey the response to a message list, or
7534 * null if no such remote input exists.
7535 */
7536 public RemoteInput getRemoteInput() {
7537 return mRemoteInput;
7538 }
7539
7540 /**
7541 * Gets the pending intent that will be triggered when the user replies to this
7542 * notification.
7543 */
7544 public PendingIntent getReplyPendingIntent() {
7545 return mReplyPendingIntent;
7546 }
7547
7548 /**
7549 * Gets the pending intent that Android Auto will send after it reads aloud all messages
7550 * in this object's message list.
7551 */
7552 public PendingIntent getReadPendingIntent() {
7553 return mReadPendingIntent;
7554 }
7555
7556 /**
7557 * Gets the participants in the conversation.
7558 */
7559 public String[] getParticipants() {
7560 return mParticipants;
7561 }
7562
7563 /**
7564 * Gets the firs participant in the conversation.
7565 */
7566 public String getParticipant() {
7567 return mParticipants.length > 0 ? mParticipants[0] : null;
7568 }
7569
7570 /**
7571 * Gets the timestamp of the conversation.
7572 */
7573 public long getLatestTimestamp() {
7574 return mLatestTimestamp;
7575 }
7576
7577 Bundle getBundleForUnreadConversation() {
7578 Bundle b = new Bundle();
7579 String author = null;
7580 if (mParticipants != null && mParticipants.length > 1) {
7581 author = mParticipants[0];
7582 }
7583 Parcelable[] messages = new Parcelable[mMessages.length];
7584 for (int i = 0; i < messages.length; i++) {
7585 Bundle m = new Bundle();
7586 m.putString(KEY_TEXT, mMessages[i]);
7587 m.putString(KEY_AUTHOR, author);
7588 messages[i] = m;
7589 }
7590 b.putParcelableArray(KEY_MESSAGES, messages);
7591 if (mRemoteInput != null) {
7592 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
7593 }
7594 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
7595 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
7596 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
7597 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
7598 return b;
7599 }
7600
7601 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
7602 if (b == null) {
7603 return null;
7604 }
7605 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
7606 String[] messages = null;
7607 if (parcelableMessages != null) {
7608 String[] tmp = new String[parcelableMessages.length];
7609 boolean success = true;
7610 for (int i = 0; i < tmp.length; i++) {
7611 if (!(parcelableMessages[i] instanceof Bundle)) {
7612 success = false;
7613 break;
7614 }
7615 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
7616 if (tmp[i] == null) {
7617 success = false;
7618 break;
7619 }
7620 }
7621 if (success) {
7622 messages = tmp;
7623 } else {
7624 return null;
7625 }
7626 }
7627
7628 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
7629 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
7630
7631 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
7632
7633 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
7634 if (participants == null || participants.length != 1) {
7635 return null;
7636 }
7637
7638 return new UnreadConversation(messages,
7639 remoteInput,
7640 onReply,
7641 onRead,
7642 participants, b.getLong(KEY_TIMESTAMP));
7643 }
7644 };
7645
7646 /**
7647 * Builder class for {@link CarExtender.UnreadConversation} objects.
7648 */
7649 public static class Builder {
7650 private final List<String> mMessages = new ArrayList<String>();
7651 private final String mParticipant;
7652 private RemoteInput mRemoteInput;
7653 private PendingIntent mReadPendingIntent;
7654 private PendingIntent mReplyPendingIntent;
7655 private long mLatestTimestamp;
7656
7657 /**
7658 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
7659 *
7660 * @param name The name of the other participant in the conversation.
7661 */
7662 public Builder(String name) {
7663 mParticipant = name;
7664 }
7665
7666 /**
7667 * Appends a new unread message to the list of messages for this conversation.
7668 *
7669 * The messages should be added from oldest to newest.
7670 *
7671 * @param message The text of the new unread message.
7672 * @return This object for method chaining.
7673 */
7674 public Builder addMessage(String message) {
7675 mMessages.add(message);
7676 return this;
7677 }
7678
7679 /**
7680 * Sets the pending intent and remote input which will convey the reply to this
7681 * notification.
7682 *
7683 * @param pendingIntent The pending intent which will be triggered on a reply.
7684 * @param remoteInput The remote input parcelable which will carry the reply.
7685 * @return This object for method chaining.
7686 *
7687 * @see CarExtender.UnreadConversation#getRemoteInput
7688 * @see CarExtender.UnreadConversation#getReplyPendingIntent
7689 */
7690 public Builder setReplyAction(
7691 PendingIntent pendingIntent, RemoteInput remoteInput) {
7692 mRemoteInput = remoteInput;
7693 mReplyPendingIntent = pendingIntent;
7694
7695 return this;
7696 }
7697
7698 /**
7699 * Sets the pending intent that will be sent once the messages in this notification
7700 * are read.
7701 *
7702 * @param pendingIntent The pending intent to use.
7703 * @return This object for method chaining.
7704 */
7705 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
7706 mReadPendingIntent = pendingIntent;
7707 return this;
7708 }
7709
7710 /**
7711 * Sets the timestamp of the most recent message in an unread conversation.
7712 *
7713 * If a messaging notification has been posted by your application and has not
7714 * yet been cancelled, posting a later notification with the same id and tag
7715 * but without a newer timestamp may result in Android Auto not displaying a
7716 * heads up notification for the later notification.
7717 *
7718 * @param timestamp The timestamp of the most recent message in the conversation.
7719 * @return This object for method chaining.
7720 */
7721 public Builder setLatestTimestamp(long timestamp) {
7722 mLatestTimestamp = timestamp;
7723 return this;
7724 }
7725
7726 /**
7727 * Builds a new unread conversation object.
7728 *
7729 * @return The new unread conversation object.
7730 */
7731 public UnreadConversation build() {
7732 String[] messages = mMessages.toArray(new String[mMessages.size()]);
7733 String[] participants = { mParticipant };
7734 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
7735 mReadPendingIntent, participants, mLatestTimestamp);
7736 }
7737 }
7738 }
7739
7740 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007741 * <p>Helper class to add Android TV extensions to notifications. To create a notification
7742 * with a TV extension:
7743 *
7744 * <ol>
7745 * <li>Create an {@link Notification.Builder}, setting any desired properties.
7746 * <li>Create a {@link TvExtender}.
7747 * <li>Set TV-specific properties using the {@code set} methods of
7748 * {@link TvExtender}.
7749 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
7750 * to apply the extension to a notification.
7751 * </ol>
7752 *
7753 * <pre class="prettyprint">
7754 * Notification notification = new Notification.Builder(context)
7755 * ...
7756 * .extend(new TvExtender()
7757 * .set*(...))
7758 * .build();
7759 * </pre>
7760 *
7761 * <p>TV extensions can be accessed on an existing notification by using the
7762 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
7763 * to access values.
7764 *
7765 * @hide
7766 */
7767 @SystemApi
7768 public static final class TvExtender implements Extender {
7769 private static final String TAG = "TvExtender";
7770
7771 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
7772 private static final String EXTRA_FLAGS = "flags";
7773 private static final String EXTRA_CONTENT_INTENT = "content_intent";
7774 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007775 private static final String EXTRA_CHANNEL_ID = "channel_id";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007776
7777 // Flags bitwise-ored to mFlags
7778 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
7779
7780 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007781 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007782 private PendingIntent mContentIntent;
7783 private PendingIntent mDeleteIntent;
7784
7785 /**
7786 * Create a {@link TvExtender} with default options.
7787 */
7788 public TvExtender() {
7789 mFlags = FLAG_AVAILABLE_ON_TV;
7790 }
7791
7792 /**
7793 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
7794 *
7795 * @param notif The notification from which to copy options.
7796 */
7797 public TvExtender(Notification notif) {
7798 Bundle bundle = notif.extras == null ?
7799 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
7800 if (bundle != null) {
7801 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007802 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007803 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
7804 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
7805 }
7806 }
7807
7808 /**
7809 * Apply a TV extension to a notification that is being built. This is typically called by
7810 * the {@link Notification.Builder#extend(Notification.Extender)}
7811 * method of {@link Notification.Builder}.
7812 */
7813 @Override
7814 public Notification.Builder extend(Notification.Builder builder) {
7815 Bundle bundle = new Bundle();
7816
7817 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007818 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007819 if (mContentIntent != null) {
7820 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
7821 }
7822
7823 if (mDeleteIntent != null) {
7824 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
7825 }
7826
7827 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
7828 return builder;
7829 }
7830
7831 /**
7832 * Returns true if this notification should be shown on TV. This method return true
7833 * if the notification was extended with a TvExtender.
7834 */
7835 public boolean isAvailableOnTv() {
7836 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
7837 }
7838
7839 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007840 * Specifies the channel the notification should be delivered on when shown on TV.
7841 * It can be different from the channel that the notification is delivered to when
7842 * posting on a non-TV device.
7843 */
7844 public TvExtender setChannel(String channelId) {
7845 mChannelId = channelId;
7846 return this;
7847 }
7848
7849 /**
7850 * Returns the id of the channel this notification posts to on TV.
7851 */
7852 public String getChannel() {
7853 return mChannelId;
7854 }
7855
7856 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007857 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
7858 * If provided, it is used instead of the content intent specified
7859 * at the level of Notification.
7860 */
7861 public TvExtender setContentIntent(PendingIntent intent) {
7862 mContentIntent = intent;
7863 return this;
7864 }
7865
7866 /**
7867 * Returns the TV-specific content intent. If this method returns null, the
7868 * main content intent on the notification should be used.
7869 *
7870 * @see {@link Notification#contentIntent}
7871 */
7872 public PendingIntent getContentIntent() {
7873 return mContentIntent;
7874 }
7875
7876 /**
7877 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
7878 * by the user on TV. If provided, it is used instead of the delete intent specified
7879 * at the level of Notification.
7880 */
7881 public TvExtender setDeleteIntent(PendingIntent intent) {
7882 mDeleteIntent = intent;
7883 return this;
7884 }
7885
7886 /**
7887 * Returns the TV-specific delete intent. If this method returns null, the
7888 * main delete intent on the notification should be used.
7889 *
7890 * @see {@link Notification#deleteIntent}
7891 */
7892 public PendingIntent getDeleteIntent() {
7893 return mDeleteIntent;
7894 }
7895 }
7896
7897 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07007898 * Get an array of Notification objects from a parcelable array bundle field.
7899 * Update the bundle to have a typed array so fetches in the future don't need
7900 * to do an array copy.
7901 */
7902 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
7903 Parcelable[] array = bundle.getParcelableArray(key);
7904 if (array instanceof Notification[] || array == null) {
7905 return (Notification[]) array;
7906 }
7907 Notification[] typedArray = Arrays.copyOf(array, array.length,
7908 Notification[].class);
7909 bundle.putParcelableArray(key, typedArray);
7910 return typedArray;
7911 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02007912
7913 private static class BuilderRemoteViews extends RemoteViews {
7914 public BuilderRemoteViews(Parcel parcel) {
7915 super(parcel);
7916 }
7917
Kenny Guy77320062014-08-27 21:37:15 +01007918 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
7919 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007920 }
7921
7922 @Override
7923 public BuilderRemoteViews clone() {
7924 Parcel p = Parcel.obtain();
7925 writeToParcel(p, 0);
7926 p.setDataPosition(0);
7927 BuilderRemoteViews brv = new BuilderRemoteViews(p);
7928 p.recycle();
7929 return brv;
7930 }
7931 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08007932
7933 private static class StandardTemplateParams {
7934 boolean hasProgress = true;
7935 boolean ambient = false;
7936 CharSequence title;
7937 CharSequence text;
7938
7939 final StandardTemplateParams reset() {
7940 hasProgress = true;
7941 ambient = false;
7942 title = null;
7943 text = null;
7944 return this;
7945 }
7946
7947 final StandardTemplateParams hasProgress(boolean hasProgress) {
7948 this.hasProgress = hasProgress;
7949 return this;
7950 }
7951
7952 final StandardTemplateParams title(CharSequence title) {
7953 this.title = title;
7954 return this;
7955 }
7956
7957 final StandardTemplateParams text(CharSequence text) {
7958 this.text = text;
7959 return this;
7960 }
7961
7962 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08007963 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08007964 this.ambient = ambient;
7965 return this;
7966 }
7967
7968 final StandardTemplateParams fillTextsFrom(Builder b) {
7969 Bundle extras = b.mN.extras;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08007970 title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
7971 text = b.processLegacyText(extras.getCharSequence(EXTRA_TEXT), ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08007972 return this;
7973 }
7974 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007975}