blob: b31e03f061e8db77dec968580c860a619be2934f [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();
Julia Reynoldsb9e712e2017-04-17 10:31:03 -04002342 sb.append("Notification(channel=");
2343 sb.append(getChannel());
2344 sb.append(" pri=");
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002345 sb.append(priority);
2346 sb.append(" contentView=");
Joe Onoratoc9596d62011-01-12 17:03:11 -08002347 if (contentView != null) {
2348 sb.append(contentView.getPackage());
2349 sb.append("/0x");
2350 sb.append(Integer.toHexString(contentView.getLayoutId()));
2351 } else {
2352 sb.append("null");
2353 }
2354 sb.append(" vibrate=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002355 if ((this.defaults & DEFAULT_VIBRATE) != 0) {
2356 sb.append("default");
2357 } else if (this.vibrate != null) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002358 int N = this.vibrate.length-1;
2359 sb.append("[");
2360 for (int i=0; i<N; i++) {
2361 sb.append(this.vibrate[i]);
2362 sb.append(',');
2363 }
Simon Schoar8cf97d92009-06-10 22:08:37 +02002364 if (N != -1) {
2365 sb.append(this.vibrate[N]);
2366 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002367 sb.append("]");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002368 } else {
2369 sb.append("null");
2370 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002371 sb.append(" sound=");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002372 if ((this.defaults & DEFAULT_SOUND) != 0) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002373 sb.append("default");
Daniel Sandler6738eee2012-11-16 12:03:32 -05002374 } else if (this.sound != null) {
2375 sb.append(this.sound.toString());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002376 } else {
2377 sb.append("null");
2378 }
Chris Wren365b6d32015-07-16 10:39:26 -04002379 if (this.tickerText != null) {
2380 sb.append(" tick");
2381 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002382 sb.append(" defaults=0x");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002383 sb.append(Integer.toHexString(this.defaults));
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002384 sb.append(" flags=0x");
Daniel Sandlere46cbd32010-06-17 10:35:26 -04002385 sb.append(Integer.toHexString(this.flags));
Dan Sandler26e81cf2014-05-06 10:01:27 -04002386 sb.append(String.format(" color=0x%08x", this.color));
Griff Hazen5cadc3b2014-05-20 09:55:39 -07002387 if (this.category != null) {
2388 sb.append(" category=");
2389 sb.append(this.category);
2390 }
2391 if (this.mGroupKey != null) {
2392 sb.append(" groupKey=");
2393 sb.append(this.mGroupKey);
2394 }
2395 if (this.mSortKey != null) {
2396 sb.append(" sortKey=");
2397 sb.append(this.mSortKey);
2398 }
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002399 if (actions != null) {
Dan Sandler1b718782014-07-18 12:43:45 -04002400 sb.append(" actions=");
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002401 sb.append(actions.length);
Dan Sandler1b718782014-07-18 12:43:45 -04002402 }
2403 sb.append(" vis=");
2404 sb.append(visibilityToString(this.visibility));
2405 if (this.publicVersion != null) {
2406 sb.append(" publicVersion=");
2407 sb.append(publicVersion.toString());
Daniel Sandlera0a938c2012-03-15 08:42:37 -04002408 }
2409 sb.append(")");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002410 return sb.toString();
2411 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002412
Dan Sandler1b718782014-07-18 12:43:45 -04002413 /**
2414 * {@hide}
2415 */
2416 public static String visibilityToString(int vis) {
2417 switch (vis) {
2418 case VISIBILITY_PRIVATE:
2419 return "PRIVATE";
2420 case VISIBILITY_PUBLIC:
2421 return "PUBLIC";
2422 case VISIBILITY_SECRET:
2423 return "SECRET";
2424 default:
2425 return "UNKNOWN(" + String.valueOf(vis) + ")";
2426 }
2427 }
2428
Joe Onoratocb109a02011-01-18 17:57:41 -08002429 /**
John Spurlock1d881a12015-03-18 19:21:54 -04002430 * {@hide}
2431 */
2432 public static String priorityToString(@Priority int pri) {
2433 switch (pri) {
2434 case PRIORITY_MIN:
2435 return "MIN";
2436 case PRIORITY_LOW:
2437 return "LOW";
2438 case PRIORITY_DEFAULT:
2439 return "DEFAULT";
2440 case PRIORITY_HIGH:
2441 return "HIGH";
2442 case PRIORITY_MAX:
2443 return "MAX";
2444 default:
2445 return "UNKNOWN(" + String.valueOf(pri) + ")";
2446 }
2447 }
2448
2449 /**
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002450 * Returns the id of the channel this notification posts to.
2451 */
Julia Reynolds37856052016-11-11 09:20:07 -05002452 public String getChannel() {
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002453 return mChannelId;
2454 }
2455
2456 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002457 * Returns the time at which this notification should be canceled by the system, if it's not
2458 * canceled already.
Julia Reynolds2a128742016-11-28 14:29:25 -05002459 */
2460 public long getTimeout() {
2461 return mTimeout;
2462 }
2463
2464 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002465 * @removed
Julia Reynolds13d898c2017-02-02 12:22:05 -05002466 * Returns what icon should be shown for this notification if it is being displayed in a
2467 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2468 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2469 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002470 @Deprecated
Julia Reynolds13d898c2017-02-02 12:22:05 -05002471 public int getBadgeIcon() {
2472 return mBadgeIcon;
2473 }
2474
2475 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002476 * Returns what icon should be shown for this notification if it is being displayed in a
2477 * Launcher that supports badging. Will be one of {@link #BADGE_ICON_NONE},
2478 * {@link #BADGE_ICON_SMALL}, or {@link #BADGE_ICON_LARGE}.
2479 */
2480 public int getBadgeIconType() {
2481 return mBadgeIcon;
2482 }
2483
2484 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002485 * Returns the {@link ShortcutInfo#getId() id} that this notification supersedes, if any.
2486 */
2487 public String getShortcutId() {
2488 return mShortcutId;
2489 }
2490
Julia Reynolds3aedded2017-03-31 14:42:09 -04002491
2492 /**
2493 * Returns the settings text provided to {@link Builder#setSettingsText(CharSequence)}.
2494 */
2495 public CharSequence getSettingsText() {
2496 return mSettingsText;
2497 }
2498
Julia Reynolds13d898c2017-02-02 12:22:05 -05002499 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04002500 * The small icon representing this notification in the status bar and content view.
2501 *
2502 * @return the small icon representing this notification.
2503 *
2504 * @see Builder#getSmallIcon()
2505 * @see Builder#setSmallIcon(Icon)
2506 */
2507 public Icon getSmallIcon() {
2508 return mSmallIcon;
2509 }
2510
2511 /**
2512 * Used when notifying to clean up legacy small icons.
2513 * @hide
2514 */
2515 public void setSmallIcon(Icon icon) {
2516 mSmallIcon = icon;
2517 }
2518
2519 /**
2520 * The large icon shown in this notification's content view.
2521 * @see Builder#getLargeIcon()
2522 * @see Builder#setLargeIcon(Icon)
2523 */
2524 public Icon getLargeIcon() {
2525 return mLargeIcon;
2526 }
2527
2528 /**
Christoph Studer4600f9b2014-07-22 22:44:43 +02002529 * @hide
2530 */
Christoph Studerc8db24b2014-07-25 17:50:30 +02002531 public boolean isGroupSummary() {
2532 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) != 0;
2533 }
2534
2535 /**
2536 * @hide
2537 */
2538 public boolean isGroupChild() {
2539 return mGroupKey != null && (flags & FLAG_GROUP_SUMMARY) == 0;
2540 }
2541
2542 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002543 * Builder class for {@link Notification} objects.
Joe Malin8d40d042012-11-05 11:36:40 -08002544 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002545 * Provides a convenient way to set the various fields of a {@link Notification} and generate
Scott Main183bf112012-08-13 19:12:13 -07002546 * content views using the platform's notification layout template. If your app supports
2547 * versions of Android as old as API level 4, you can instead use
2548 * {@link android.support.v4.app.NotificationCompat.Builder NotificationCompat.Builder},
2549 * available in the <a href="{@docRoot}tools/extras/support-library.html">Android Support
2550 * library</a>.
Joe Malin8d40d042012-11-05 11:36:40 -08002551 *
Scott Main183bf112012-08-13 19:12:13 -07002552 * <p>Example:
Joe Malin8d40d042012-11-05 11:36:40 -08002553 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002554 * <pre class="prettyprint">
Scott Main183bf112012-08-13 19:12:13 -07002555 * Notification noti = new Notification.Builder(mContext)
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002556 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
2557 * .setContentText(subject)
2558 * .setSmallIcon(R.drawable.new_mail)
2559 * .setLargeIcon(aBitmap)
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002560 * .build();
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002561 * </pre>
Joe Onoratocb109a02011-01-18 17:57:41 -08002562 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002563 public static class Builder {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05002564 /**
2565 * @hide
2566 */
2567 public static final String EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT =
2568 "android.rebuild.contentViewActionCount";
2569 /**
2570 * @hide
2571 */
2572 public static final String EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT
2573 = "android.rebuild.bigViewActionCount";
2574 /**
2575 * @hide
2576 */
2577 public static final String EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT
2578 = "android.rebuild.hudViewActionCount";
2579
Daniel Sandler602ad1c2012-06-12 16:06:27 -04002580 private static final int MAX_ACTION_BUTTONS = 3;
Daniel Sandler8680bf82012-05-15 16:52:52 -04002581
Selim Cinek6743c0b2017-01-18 18:24:01 -08002582 private static final boolean USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY =
2583 SystemProperties.getBoolean("notifications.only_title", true);
2584
Joe Onorato46439ce2010-11-19 13:56:21 -08002585 private Context mContext;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002586 private Notification mN;
2587 private Bundle mUserExtras = new Bundle();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04002588 private Style mStyle;
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002589 private ArrayList<Action> mActions = new ArrayList<Action>(MAX_ACTION_BUTTONS);
2590 private ArrayList<String> mPersonList = new ArrayList<String>();
2591 private NotificationColorUtil mColorUtil;
Selim Cinek7b9605b2017-01-19 17:36:00 -08002592 private boolean mIsLegacy;
2593 private boolean mIsLegacyInitialized;
Christoph Studer7ac80e62014-08-04 16:01:57 +02002594
2595 /**
Adrian Roos4ff3b122016-02-01 12:26:13 -08002596 * Caches a contrast-enhanced version of {@link #mCachedContrastColorIsFor}.
2597 */
2598 private int mCachedContrastColor = COLOR_INVALID;
2599 private int mCachedContrastColorIsFor = COLOR_INVALID;
Adrian Roos487374f2017-01-11 15:48:14 -08002600 /**
2601 * Caches a ambient version of {@link #mCachedContrastColorIsFor}.
2602 */
2603 private int mCachedAmbientColor = COLOR_INVALID;
2604 private int mCachedAmbientColorIsFor = COLOR_INVALID;
Adrian Roos4ff3b122016-02-01 12:26:13 -08002605
2606 /**
Adrian Roos70d7aa32017-01-11 15:39:06 -08002607 * Caches an instance of StandardTemplateParams. Note that this may have been used before,
2608 * so make sure to call {@link StandardTemplateParams#reset()} before using it.
2609 */
2610 StandardTemplateParams mParams = new StandardTemplateParams();
Selim Cinek7b9605b2017-01-19 17:36:00 -08002611 private int mTextColorsAreForBackground = COLOR_INVALID;
2612 private int mPrimaryTextColor = COLOR_INVALID;
2613 private int mSecondaryTextColor = COLOR_INVALID;
2614 private int mActionBarColor = COLOR_INVALID;
Adrian Roos70d7aa32017-01-11 15:39:06 -08002615
2616 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002617 * Constructs a new Builder with the defaults:
Joe Onoratocb109a02011-01-18 17:57:41 -08002618 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002619
2620 * <table>
2621 * <tr><th align=right>priority</th>
2622 * <td>{@link #PRIORITY_DEFAULT}</td></tr>
2623 * <tr><th align=right>when</th>
2624 * <td>now ({@link System#currentTimeMillis()})</td></tr>
2625 * <tr><th align=right>audio stream</th>
2626 * <td>{@link #STREAM_DEFAULT}</td></tr>
2627 * </table>
Joe Onoratocb109a02011-01-18 17:57:41 -08002628 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002629
2630 * @param context
2631 * A {@link Context} that will be used by the Builder to construct the
2632 * RemoteViews. The Context will not be held past the lifetime of this Builder
2633 * object.
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002634 * @param channelId
2635 * The constructed Notification will be posted on this
2636 * {@link NotificationChannel}. To use a NotificationChannel, it must first be
2637 * created using {@link NotificationManager#createNotificationChannel}.
Joe Onoratocb109a02011-01-18 17:57:41 -08002638 */
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002639 public Builder(Context context, String channelId) {
2640 this(context, (Notification) null);
2641 mN.mChannelId = channelId;
2642 }
2643
2644 /**
2645 * @deprecated use {@link Notification.Builder#Notification.Builder(Context, String)}
2646 * instead. All posted Notifications must specify a NotificationChannel Id.
2647 */
2648 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002649 public Builder(Context context) {
Geoffrey Pitsch5caa2762017-01-12 09:35:54 -05002650 this(context, (Notification) null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002651 }
2652
Joe Onoratocb109a02011-01-18 17:57:41 -08002653 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002654 * @hide
Christoph Studer4600f9b2014-07-22 22:44:43 +02002655 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002656 public Builder(Context context, Notification toAdopt) {
2657 mContext = context;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002658
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002659 if (toAdopt == null) {
2660 mN = new Notification();
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002661 if (context.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N) {
2662 mN.extras.putBoolean(EXTRA_SHOW_WHEN, true);
2663 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002664 mN.priority = PRIORITY_DEFAULT;
2665 mN.visibility = VISIBILITY_PRIVATE;
2666 } else {
2667 mN = toAdopt;
2668 if (mN.actions != null) {
2669 Collections.addAll(mActions, mN.actions);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002670 }
2671
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002672 if (mN.extras.containsKey(EXTRA_PEOPLE)) {
2673 Collections.addAll(mPersonList, mN.extras.getStringArray(EXTRA_PEOPLE));
2674 }
2675
Selim Cinek4ac6f602016-06-13 15:47:03 -07002676 if (mN.getSmallIcon() == null && mN.icon != 0) {
2677 setSmallIcon(mN.icon);
2678 }
2679
2680 if (mN.getLargeIcon() == null && mN.largeIcon != null) {
2681 setLargeIcon(mN.largeIcon);
2682 }
2683
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002684 String templateClass = mN.extras.getString(EXTRA_TEMPLATE);
2685 if (!TextUtils.isEmpty(templateClass)) {
2686 final Class<? extends Style> styleClass
2687 = getNotificationStyleClass(templateClass);
2688 if (styleClass == null) {
2689 Log.d(TAG, "Unknown style class: " + templateClass);
2690 } else {
2691 try {
Adrian Roosc1a80b02016-04-05 14:54:55 -07002692 final Constructor<? extends Style> ctor =
2693 styleClass.getDeclaredConstructor();
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002694 ctor.setAccessible(true);
2695 final Style style = ctor.newInstance();
2696 style.restoreFromExtras(mN.extras);
2697
2698 if (style != null) {
2699 setStyle(style);
2700 }
2701 } catch (Throwable t) {
2702 Log.e(TAG, "Could not create Style", t);
2703 }
2704 }
2705 }
2706
2707 }
2708 }
2709
2710 private NotificationColorUtil getColorUtil() {
Selim Cinek99104832017-01-25 14:47:33 -08002711 if (mColorUtil == null) {
2712 mColorUtil = NotificationColorUtil.getInstance(mContext);
Christoph Studer4600f9b2014-07-22 22:44:43 +02002713 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002714 return mColorUtil;
Christoph Studer4600f9b2014-07-22 22:44:43 +02002715 }
2716
2717 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002718 * If this notification is duplicative of a Launcher shortcut, sets the
2719 * {@link ShortcutInfo#getId() id} of the shortcut, in case the Launcher wants to hide
2720 * the shortcut.
2721 *
2722 * This field will be ignored by Launchers that don't support badging or
2723 * {@link android.content.pm.ShortcutManager shortcuts}.
2724 *
2725 * @param shortcutId the {@link ShortcutInfo#getId() id} of the shortcut this notification
2726 * supersedes
2727 */
2728 public Builder setShortcutId(String shortcutId) {
2729 mN.mShortcutId = shortcutId;
2730 return this;
2731 }
2732
2733 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002734 * @removed
Julia Reynolds13d898c2017-02-02 12:22:05 -05002735 * Sets which icon to display as a badge for this notification.
2736 *
2737 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
2738 * {@link #BADGE_ICON_LARGE}.
2739 *
2740 * Note: This value might be ignored, for launchers that don't support badge icons.
2741 */
Julia Reynoldse071abd2017-03-22 10:52:11 -04002742 @Deprecated
Julia Reynolds13d898c2017-02-02 12:22:05 -05002743 public Builder chooseBadgeIcon(int icon) {
2744 mN.mBadgeIcon = icon;
2745 return this;
2746 }
2747
2748 /**
Julia Reynoldse071abd2017-03-22 10:52:11 -04002749 * Sets which icon to display as a badge for this notification.
2750 *
2751 * Must be one of {@link #BADGE_ICON_NONE}, {@link #BADGE_ICON_SMALL},
2752 * {@link #BADGE_ICON_LARGE}.
2753 *
2754 * Note: This value might be ignored, for launchers that don't support badge icons.
2755 */
Julia Reynolds612beb22017-03-30 10:48:30 -04002756 public Builder setBadgeIconType(int icon) {
Julia Reynoldse071abd2017-03-22 10:52:11 -04002757 mN.mBadgeIcon = icon;
2758 return this;
2759 }
2760
2761 /**
Julia Reynoldsb5e44b72016-08-16 15:00:25 -04002762 * Specifies the channel the notification should be delivered on.
2763 */
2764 public Builder setChannel(String channelId) {
2765 mN.mChannelId = channelId;
2766 return this;
2767 }
2768
2769 /**
Julia Reynolds50989772017-02-23 14:32:16 -05002770 * Specifies a duration in milliseconds after which this notification should be canceled,
2771 * if it is not already canceled.
Julia Reynolds2a128742016-11-28 14:29:25 -05002772 */
Julia Reynolds50989772017-02-23 14:32:16 -05002773 public Builder setTimeout(long durationMs) {
2774 mN.mTimeout = durationMs;
Julia Reynolds2a128742016-11-28 14:29:25 -05002775 return this;
2776 }
2777
2778 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002779 * Add a timestamp pertaining to the notification (usually the time the event occurred).
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002780 *
2781 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this time is not
2782 * shown anymore by default and must be opted into by using
2783 * {@link android.app.Notification.Builder#setShowWhen(boolean)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002784 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002785 * @see Notification#when
Joe Onoratocb109a02011-01-18 17:57:41 -08002786 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002787 public Builder setWhen(long when) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002788 mN.when = when;
Joe Onorato46439ce2010-11-19 13:56:21 -08002789 return this;
2790 }
2791
Joe Onoratocb109a02011-01-18 17:57:41 -08002792 /**
Griff Hazen50c11652014-05-16 09:46:31 -07002793 * Control whether the timestamp set with {@link #setWhen(long) setWhen} is shown
Daniel Sandler0c890492012-09-12 17:23:10 -07002794 * in the content view.
Selim Cinek0ff1ce602016-04-05 18:27:16 -07002795 * For apps targeting {@link android.os.Build.VERSION_CODES#N} and above, this defaults to
2796 * {@code false}. For earlier apps, the default is {@code true}.
Daniel Sandler0c890492012-09-12 17:23:10 -07002797 */
2798 public Builder setShowWhen(boolean show) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002799 mN.extras.putBoolean(EXTRA_SHOW_WHEN, show);
Daniel Sandler0c890492012-09-12 17:23:10 -07002800 return this;
2801 }
2802
2803 /**
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002804 * Show the {@link Notification#when} field as a stopwatch.
Joe Malin8d40d042012-11-05 11:36:40 -08002805 *
2806 * Instead of presenting <code>when</code> as a timestamp, the notification will show an
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002807 * automatically updating display of the minutes and seconds since <code>when</code>.
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002808 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002809 * Useful when showing an elapsed time (like an ongoing phone call).
2810 *
Selim Cinek81c23aa2016-02-25 16:23:13 -08002811 * The counter can also be set to count down to <code>when</code> when using
Adrian Roos96b7e202016-05-17 13:50:38 -07002812 * {@link #setChronometerCountDown(boolean)}.
Selim Cinek81c23aa2016-02-25 16:23:13 -08002813 *
Daniel Sandlerd33b8032012-05-10 11:41:48 -04002814 * @see android.widget.Chronometer
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002815 * @see Notification#when
Adrian Roos96b7e202016-05-17 13:50:38 -07002816 * @see #setChronometerCountDown(boolean)
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002817 */
2818 public Builder setUsesChronometer(boolean b) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002819 mN.extras.putBoolean(EXTRA_SHOW_CHRONOMETER, b);
Daniel Sandlera2985ed2012-04-03 16:42:00 -04002820 return this;
2821 }
2822
2823 /**
Selim Cinek81c23aa2016-02-25 16:23:13 -08002824 * Sets the Chronometer to count down instead of counting up.
2825 *
2826 * <p>This is only relevant if {@link #setUsesChronometer(boolean)} has been set to true.
2827 * If it isn't set the chronometer will count up.
2828 *
2829 * @see #setUsesChronometer(boolean)
2830 */
Adrian Roos96b7e202016-05-17 13:50:38 -07002831 public Builder setChronometerCountDown(boolean countDown) {
2832 mN.extras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN, countDown);
Selim Cinek81c23aa2016-02-25 16:23:13 -08002833 return this;
2834 }
2835
2836 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002837 * Set the small icon resource, which will be used to represent the notification in the
2838 * status bar.
Joe Onoratocb109a02011-01-18 17:57:41 -08002839 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002840
2841 * The platform template for the expanded view will draw this icon in the left, unless a
2842 * {@link #setLargeIcon(Bitmap) large icon} has also been specified, in which case the small
2843 * icon will be moved to the right-hand side.
2844 *
2845
2846 * @param icon
2847 * A resource ID in the application's package of the drawable to use.
2848 * @see Notification#icon
Joe Onoratocb109a02011-01-18 17:57:41 -08002849 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002850 public Builder setSmallIcon(@DrawableRes int icon) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04002851 return setSmallIcon(icon != 0
2852 ? Icon.createWithResource(mContext, icon)
2853 : null);
Joe Onorato46439ce2010-11-19 13:56:21 -08002854 }
2855
Joe Onoratocb109a02011-01-18 17:57:41 -08002856 /**
2857 * A variant of {@link #setSmallIcon(int) setSmallIcon(int)} that takes an additional
2858 * level parameter for when the icon is a {@link android.graphics.drawable.LevelListDrawable
2859 * LevelListDrawable}.
2860 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002861 * @param icon A resource ID in the application's package of the drawable to use.
Joe Onoratocb109a02011-01-18 17:57:41 -08002862 * @param level The level to use for the icon.
2863 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002864 * @see Notification#icon
2865 * @see Notification#iconLevel
Joe Onoratocb109a02011-01-18 17:57:41 -08002866 */
Tor Norbye7b9c9122013-05-30 16:48:33 -07002867 public Builder setSmallIcon(@DrawableRes int icon, int level) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002868 mN.iconLevel = level;
Dan Sandlerd63f9322015-05-06 15:18:49 -04002869 return setSmallIcon(icon);
2870 }
2871
2872 /**
2873 * Set the small icon, which will be used to represent the notification in the
2874 * status bar and content view (unless overriden there by a
2875 * {@link #setLargeIcon(Bitmap) large icon}).
2876 *
2877 * @param icon An Icon object to use.
2878 * @see Notification#icon
2879 */
2880 public Builder setSmallIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002881 mN.setSmallIcon(icon);
2882 if (icon != null && icon.getType() == Icon.TYPE_RESOURCE) {
2883 mN.icon = icon.getResId();
2884 }
Joe Onorato46439ce2010-11-19 13:56:21 -08002885 return this;
2886 }
2887
Joe Onoratocb109a02011-01-18 17:57:41 -08002888 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002889 * Set the first line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002890 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002891 public Builder setContentTitle(CharSequence title) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002892 mN.extras.putCharSequence(EXTRA_TITLE, safeCharSequence(title));
Joe Onorato46439ce2010-11-19 13:56:21 -08002893 return this;
2894 }
2895
Joe Onoratocb109a02011-01-18 17:57:41 -08002896 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002897 * Set the second line of text in the platform notification template.
Joe Onoratocb109a02011-01-18 17:57:41 -08002898 */
Joe Onorato46439ce2010-11-19 13:56:21 -08002899 public Builder setContentText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002900 mN.extras.putCharSequence(EXTRA_TEXT, safeCharSequence(text));
Joe Onorato46439ce2010-11-19 13:56:21 -08002901 return this;
2902 }
2903
Joe Onoratocb109a02011-01-18 17:57:41 -08002904 /**
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002905 * This provides some additional information that is displayed in the notification. No
2906 * guarantees are given where exactly it is displayed.
2907 *
2908 * <p>This information should only be provided if it provides an essential
2909 * benefit to the understanding of the notification. The more text you provide the
2910 * less readable it becomes. For example, an email client should only provide the account
2911 * name here if more than one email account has been added.</p>
2912 *
2913 * <p>As of {@link android.os.Build.VERSION_CODES#N} this information is displayed in the
2914 * notification header area.
2915 *
2916 * On Android versions before {@link android.os.Build.VERSION_CODES#N}
2917 * this will be shown in the third line of text in the platform notification template.
2918 * You should not be using {@link #setProgress(int, int, boolean)} at the
2919 * same time on those versions; they occupy the same place.
2920 * </p>
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002921 */
2922 public Builder setSubText(CharSequence text) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002923 mN.extras.putCharSequence(EXTRA_SUB_TEXT, safeCharSequence(text));
Daniel Sandlerf3b73432012-03-27 15:01:25 -04002924 return this;
2925 }
2926
2927 /**
Julia Reynolds3aedded2017-03-31 14:42:09 -04002928 * Provides text that will appear as a link to your application's settings.
2929 *
2930 * <p>This text does not appear within notification {@link Style templates} but may
2931 * appear when the user uses an affordance to learn more about the notification.
2932 * Additionally, this text will not appear unless you provide a valid link target by
2933 * handling {@link #INTENT_CATEGORY_NOTIFICATION_PREFERENCES}.
2934 *
2935 * <p>This text is meant to be concise description about what the user can customize
2936 * when they click on this link. The recommended maximum length is 40 characters.
2937 * @param text
2938 * @return
2939 */
2940 public Builder setSettingsText(CharSequence text) {
2941 mN.mSettingsText = safeCharSequence(text);
2942 return this;
2943 }
2944
2945 /**
Adrian Roose458aa82015-12-08 16:17:19 -08002946 * Set the remote input history.
2947 *
2948 * This should be set to the most recent inputs that have been sent
2949 * through a {@link RemoteInput} of this Notification and cleared once the it is no
2950 * longer relevant (e.g. for chat notifications once the other party has responded).
2951 *
2952 * The most recent input must be stored at the 0 index, the second most recent at the
2953 * 1 index, etc. Note that the system will limit both how far back the inputs will be shown
2954 * and how much of each individual input is shown.
2955 *
2956 * <p>Note: The reply text will only be shown on notifications that have least one action
2957 * with a {@code RemoteInput}.</p>
2958 */
2959 public Builder setRemoteInputHistory(CharSequence[] text) {
2960 if (text == null) {
2961 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, null);
2962 } else {
2963 final int N = Math.min(MAX_REPLY_HISTORY, text.length);
2964 CharSequence[] safe = new CharSequence[N];
2965 for (int i = 0; i < N; i++) {
2966 safe[i] = safeCharSequence(text[i]);
2967 }
2968 mN.extras.putCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY, safe);
2969 }
2970 return this;
2971 }
2972
2973 /**
Julia Reynolds13d898c2017-02-02 12:22:05 -05002974 * Sets the number of items this notification represents. May be displayed as a badge count
2975 * for Launchers that support badging.
Joe Onoratocb109a02011-01-18 17:57:41 -08002976 */
Joe Onorato8595a3d2010-11-19 18:12:07 -08002977 public Builder setNumber(int number) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002978 mN.number = number;
Joe Onorato8595a3d2010-11-19 18:12:07 -08002979 return this;
2980 }
2981
Joe Onoratocb109a02011-01-18 17:57:41 -08002982 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002983 * A small piece of additional information pertaining to this notification.
2984 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002985 * The platform template will draw this on the last line of the notification, at the far
2986 * right (to the right of a smallIcon if it has been placed there).
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07002987 *
2988 * @deprecated use {@link #setSubText(CharSequence)} instead to set a text in the header.
2989 * For legacy apps targeting a version below {@link android.os.Build.VERSION_CODES#N} this
2990 * field will still show up, but the subtext will take precedence.
Joe Onoratocb109a02011-01-18 17:57:41 -08002991 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002992 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08002993 public Builder setContentInfo(CharSequence info) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04002994 mN.extras.putCharSequence(EXTRA_INFO_TEXT, safeCharSequence(info));
Joe Onorato46439ce2010-11-19 13:56:21 -08002995 return this;
2996 }
2997
Joe Onoratocb109a02011-01-18 17:57:41 -08002998 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05002999 * Set the progress this notification represents.
3000 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003001 * The platform template will represent this using a {@link ProgressBar}.
Jeff Sharkey1c400132011-08-05 14:50:13 -07003002 */
3003 public Builder setProgress(int max, int progress, boolean indeterminate) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003004 mN.extras.putInt(EXTRA_PROGRESS, progress);
3005 mN.extras.putInt(EXTRA_PROGRESS_MAX, max);
3006 mN.extras.putBoolean(EXTRA_PROGRESS_INDETERMINATE, indeterminate);
Jeff Sharkey1c400132011-08-05 14:50:13 -07003007 return this;
3008 }
3009
3010 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003011 * Supply a custom RemoteViews to use instead of the platform template.
3012 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003013 * Use {@link #setCustomContentView(RemoteViews)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003014 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003015 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003016 public Builder setContent(RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003017 return setCustomContentView(views);
3018 }
3019
3020 /**
3021 * Supply custom RemoteViews to use instead of the platform template.
3022 *
3023 * This will override the layout that would otherwise be constructed by this Builder
3024 * object.
3025 */
3026 public Builder setCustomContentView(RemoteViews contentView) {
3027 mN.contentView = contentView;
3028 return this;
3029 }
3030
3031 /**
3032 * Supply custom RemoteViews to use instead of the platform template in the expanded form.
3033 *
3034 * This will override the expanded layout that would otherwise be constructed by this
3035 * Builder object.
3036 */
3037 public Builder setCustomBigContentView(RemoteViews contentView) {
3038 mN.bigContentView = contentView;
3039 return this;
3040 }
3041
3042 /**
3043 * Supply custom RemoteViews to use instead of the platform template in the heads up dialog.
3044 *
3045 * This will override the heads-up layout that would otherwise be constructed by this
3046 * Builder object.
3047 */
3048 public Builder setCustomHeadsUpContentView(RemoteViews contentView) {
3049 mN.headsUpContentView = contentView;
Joe Onorato46439ce2010-11-19 13:56:21 -08003050 return this;
3051 }
3052
Joe Onoratocb109a02011-01-18 17:57:41 -08003053 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003054 * Supply a {@link PendingIntent} to be sent when the notification is clicked.
3055 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003056 * As of {@link android.os.Build.VERSION_CODES#HONEYCOMB}, if this field is unset and you
3057 * have specified a custom RemoteViews with {@link #setContent(RemoteViews)}, you can use
3058 * {@link RemoteViews#setOnClickPendingIntent RemoteViews.setOnClickPendingIntent(int,PendingIntent)}
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003059 * to assign PendingIntents to individual views in that custom layout (i.e., to create
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003060 * clickable buttons inside the notification view).
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003061 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003062 * @see Notification#contentIntent Notification.contentIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003063 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003064 public Builder setContentIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003065 mN.contentIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003066 return this;
3067 }
3068
Joe Onoratocb109a02011-01-18 17:57:41 -08003069 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003070 * Supply a {@link PendingIntent} to send when the notification is cleared explicitly by the user.
3071 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003072 * @see Notification#deleteIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003073 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003074 public Builder setDeleteIntent(PendingIntent intent) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003075 mN.deleteIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003076 return this;
3077 }
3078
Joe Onoratocb109a02011-01-18 17:57:41 -08003079 /**
3080 * An intent to launch instead of posting the notification to the status bar.
3081 * Only for use with extremely high-priority notifications demanding the user's
3082 * <strong>immediate</strong> attention, such as an incoming phone call or
3083 * alarm clock that the user has explicitly set to a particular time.
3084 * If this facility is used for something else, please give the user an option
3085 * to turn it off and use a normal notification, as this can be extremely
3086 * disruptive.
3087 *
Chris Wren47c20a12014-06-18 17:27:29 -04003088 * <p>
3089 * The system UI may choose to display a heads-up notification, instead of
3090 * launching this intent, while the user is using the device.
3091 * </p>
3092 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003093 * @param intent The pending intent to launch.
3094 * @param highPriority Passing true will cause this notification to be sent
3095 * even if other notifications are suppressed.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003096 *
3097 * @see Notification#fullScreenIntent
Joe Onoratocb109a02011-01-18 17:57:41 -08003098 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003099 public Builder setFullScreenIntent(PendingIntent intent, boolean highPriority) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003100 mN.fullScreenIntent = intent;
Joe Onorato46439ce2010-11-19 13:56:21 -08003101 setFlag(FLAG_HIGH_PRIORITY, highPriority);
3102 return this;
3103 }
3104
Joe Onoratocb109a02011-01-18 17:57:41 -08003105 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003106 * Set the "ticker" text which is sent to accessibility services.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003107 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003108 * @see Notification#tickerText
Joe Onoratocb109a02011-01-18 17:57:41 -08003109 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003110 public Builder setTicker(CharSequence tickerText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003111 mN.tickerText = safeCharSequence(tickerText);
Joe Onorato46439ce2010-11-19 13:56:21 -08003112 return this;
3113 }
3114
Joe Onoratocb109a02011-01-18 17:57:41 -08003115 /**
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003116 * Obsolete version of {@link #setTicker(CharSequence)}.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003117 *
Joe Onoratocb109a02011-01-18 17:57:41 -08003118 */
Dan Sandler5fcdf6e2014-07-18 11:31:15 -04003119 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003120 public Builder setTicker(CharSequence tickerText, RemoteViews views) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003121 setTicker(tickerText);
3122 // views is ignored
Joe Onorato46439ce2010-11-19 13:56:21 -08003123 return this;
3124 }
3125
Joe Onoratocb109a02011-01-18 17:57:41 -08003126 /**
Dan Sandlerd63f9322015-05-06 15:18:49 -04003127 * Add a large icon to the notification content view.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003128 *
3129 * In the platform template, this image will be shown on the left of the notification view
Dan Sandlerd63f9322015-05-06 15:18:49 -04003130 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3131 * badge atop the large icon).
Dan Sandler08a04c12015-05-06 15:18:49 -04003132 */
Dan Sandlerd63f9322015-05-06 15:18:49 -04003133 public Builder setLargeIcon(Bitmap b) {
3134 return setLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
3135 }
3136
3137 /**
3138 * Add a large icon to the notification content view.
3139 *
3140 * In the platform template, this image will be shown on the left of the notification view
3141 * in place of the {@link #setSmallIcon(Icon) small icon} (which will be placed in a small
3142 * badge atop the large icon).
3143 */
3144 public Builder setLargeIcon(Icon icon) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003145 mN.mLargeIcon = icon;
3146 mN.extras.putParcelable(EXTRA_LARGE_ICON, icon);
Joe Onorato46439ce2010-11-19 13:56:21 -08003147 return this;
3148 }
3149
Joe Onoratocb109a02011-01-18 17:57:41 -08003150 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003151 * Set the sound to play.
3152 *
John Spurlockc0650f022014-07-19 13:22:39 -04003153 * It will be played using the {@link #AUDIO_ATTRIBUTES_DEFAULT default audio attributes}
3154 * for notifications.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003155 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003156 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003157 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003158 @Deprecated
Joe Onorato52f80cd2010-11-21 15:34:48 -08003159 public Builder setSound(Uri sound) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003160 mN.sound = sound;
3161 mN.audioAttributes = AUDIO_ATTRIBUTES_DEFAULT;
Joe Onorato52f80cd2010-11-21 15:34:48 -08003162 return this;
3163 }
3164
Joe Onoratocb109a02011-01-18 17:57:41 -08003165 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003166 * Set the sound to play, along with a specific stream on which to play it.
Joe Onoratocb109a02011-01-18 17:57:41 -08003167 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003168 * See {@link android.media.AudioManager} for the <code>STREAM_</code> constants.
3169 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003170 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)}.
Joe Onoratocb109a02011-01-18 17:57:41 -08003171 */
Jean-Michel Trivi81f871e2014-08-06 16:32:38 -07003172 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003173 public Builder setSound(Uri sound, int streamType) {
Jean-Michel Trivi2f7511f2016-11-28 15:40:27 -08003174 PlayerBase.deprecateStreamTypeForPlayback(streamType, "Notification", "setSound()");
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003175 mN.sound = sound;
3176 mN.audioStreamType = streamType;
Joe Onorato46439ce2010-11-19 13:56:21 -08003177 return this;
3178 }
3179
Joe Onoratocb109a02011-01-18 17:57:41 -08003180 /**
John Spurlockc0650f022014-07-19 13:22:39 -04003181 * Set the sound to play, along with specific {@link AudioAttributes audio attributes} to
3182 * use during playback.
3183 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003184 * @deprecated use {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
John Spurlockc0650f022014-07-19 13:22:39 -04003185 * @see Notification#sound
3186 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003187 @Deprecated
John Spurlockc0650f022014-07-19 13:22:39 -04003188 public Builder setSound(Uri sound, AudioAttributes audioAttributes) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003189 mN.sound = sound;
3190 mN.audioAttributes = audioAttributes;
John Spurlockc0650f022014-07-19 13:22:39 -04003191 return this;
3192 }
3193
3194 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003195 * Set the vibration pattern to use.
3196 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003197 * See {@link android.os.Vibrator#vibrate(long[], int)} for a discussion of the
3198 * <code>pattern</code> parameter.
3199 *
Chris Wren47c20a12014-06-18 17:27:29 -04003200 * <p>
3201 * A notification that vibrates is more likely to be presented as a heads-up notification.
3202 * </p>
3203 *
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003204 * @deprecated use {@link NotificationChannel#setVibrationPattern(long[])} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003205 * @see Notification#vibrate
Joe Onoratocb109a02011-01-18 17:57:41 -08003206 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003207 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003208 public Builder setVibrate(long[] pattern) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003209 mN.vibrate = pattern;
Joe Onorato46439ce2010-11-19 13:56:21 -08003210 return this;
3211 }
3212
Joe Onoratocb109a02011-01-18 17:57:41 -08003213 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003214 * Set the desired color for the indicator LED on the device, as well as the
3215 * blink duty cycle (specified in milliseconds).
3216 *
3217
3218 * Not all devices will honor all (or even any) of these values.
3219 *
Julia Reynolds529e3322017-02-06 08:33:01 -05003220 * @deprecated use {@link NotificationChannel#enableLights(boolean)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003221 * @see Notification#ledARGB
3222 * @see Notification#ledOnMS
3223 * @see Notification#ledOffMS
Joe Onoratocb109a02011-01-18 17:57:41 -08003224 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003225 @Deprecated
Tor Norbye80756e32015-03-02 09:39:27 -08003226 public Builder setLights(@ColorInt int argb, int onMs, int offMs) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003227 mN.ledARGB = argb;
3228 mN.ledOnMS = onMs;
3229 mN.ledOffMS = offMs;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003230 if (onMs != 0 || offMs != 0) {
3231 mN.flags |= FLAG_SHOW_LIGHTS;
3232 }
Joe Onorato46439ce2010-11-19 13:56:21 -08003233 return this;
3234 }
3235
Joe Onoratocb109a02011-01-18 17:57:41 -08003236 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003237 * Set whether this is an "ongoing" notification.
Joe Onoratocb109a02011-01-18 17:57:41 -08003238 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003239
3240 * Ongoing notifications cannot be dismissed by the user, so your application or service
3241 * must take care of canceling them.
3242 *
3243
3244 * They are typically used to indicate a background task that the user is actively engaged
3245 * with (e.g., playing music) or is pending in some way and therefore occupying the device
3246 * (e.g., a file download, sync operation, active network connection).
3247 *
3248
3249 * @see Notification#FLAG_ONGOING_EVENT
3250 * @see Service#setForeground(boolean)
Joe Onoratocb109a02011-01-18 17:57:41 -08003251 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003252 public Builder setOngoing(boolean ongoing) {
3253 setFlag(FLAG_ONGOING_EVENT, ongoing);
3254 return this;
3255 }
3256
Joe Onoratocb109a02011-01-18 17:57:41 -08003257 /**
Selim Cinek7b9605b2017-01-19 17:36:00 -08003258 * Set whether this notification should be colorized. When set, the color set with
3259 * {@link #setColor(int)} will be used as the background color of this notification.
3260 * <p>
Selim Cinek7b9605b2017-01-19 17:36:00 -08003261 * This should only be used for high priority ongoing tasks like navigation, an ongoing
3262 * call, or other similarly high-priority events for the user.
Selim Cinek99104832017-01-25 14:47:33 -08003263 * <p>
3264 * For most styles, the coloring will only be applied if the notification is ongoing.
3265 * However, for {@link MediaStyle} and {@link DecoratedMediaCustomViewStyle} notifications
3266 * that have a media session attached there is no requirement for it to be ongoing.
Selim Cinek7b9605b2017-01-19 17:36:00 -08003267 *
3268 * @see Builder#setOngoing(boolean)
3269 * @see Builder#setColor(int)
Selim Cinek99104832017-01-25 14:47:33 -08003270 * @see MediaStyle#setMediaSession(MediaSession.Token)
Selim Cinek7b9605b2017-01-19 17:36:00 -08003271 */
3272 public Builder setColorized(boolean colorize) {
3273 mN.extras.putBoolean(EXTRA_COLORIZED, colorize);
3274 return this;
3275 }
3276
3277 /**
Joe Onoratocb109a02011-01-18 17:57:41 -08003278 * Set this flag if you would only like the sound, vibrate
3279 * and ticker to be played if the notification is not already showing.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003280 *
3281 * @see Notification#FLAG_ONLY_ALERT_ONCE
Joe Onoratocb109a02011-01-18 17:57:41 -08003282 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003283 public Builder setOnlyAlertOnce(boolean onlyAlertOnce) {
3284 setFlag(FLAG_ONLY_ALERT_ONCE, onlyAlertOnce);
3285 return this;
3286 }
3287
Joe Onoratocb109a02011-01-18 17:57:41 -08003288 /**
Julia Reynolds04499532016-09-13 14:04:53 -04003289 * Make this notification automatically dismissed when the user touches it.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003290 *
3291 * @see Notification#FLAG_AUTO_CANCEL
Joe Onoratocb109a02011-01-18 17:57:41 -08003292 */
Joe Onorato46439ce2010-11-19 13:56:21 -08003293 public Builder setAutoCancel(boolean autoCancel) {
Joe Onorato281d83f2011-01-04 17:13:10 -08003294 setFlag(FLAG_AUTO_CANCEL, autoCancel);
Joe Onorato46439ce2010-11-19 13:56:21 -08003295 return this;
3296 }
3297
Joe Onoratocb109a02011-01-18 17:57:41 -08003298 /**
Griff Hazendfcb0802014-02-11 12:00:00 -08003299 * Set whether or not this notification should not bridge to other devices.
3300 *
3301 * <p>Some notifications can be bridged to other devices for remote display.
3302 * This hint can be set to recommend this notification not be bridged.
3303 */
3304 public Builder setLocalOnly(boolean localOnly) {
3305 setFlag(FLAG_LOCAL_ONLY, localOnly);
3306 return this;
3307 }
3308
3309 /**
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003310 * Set which notification properties will be inherited from system defaults.
Joe Onoratocb109a02011-01-18 17:57:41 -08003311 * <p>
3312 * The value should be one or more of the following fields combined with
3313 * bitwise-or:
3314 * {@link #DEFAULT_SOUND}, {@link #DEFAULT_VIBRATE}, {@link #DEFAULT_LIGHTS}.
3315 * <p>
3316 * For all default values, use {@link #DEFAULT_ALL}.
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003317 *
3318 * @deprecated use {@link NotificationChannel#enableVibration(boolean)} and
Julia Reynolds529e3322017-02-06 08:33:01 -05003319 * {@link NotificationChannel#enableLights(boolean)} and
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003320 * {@link NotificationChannel#setSound(Uri, AudioAttributes)} instead.
Joe Onoratocb109a02011-01-18 17:57:41 -08003321 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003322 @Deprecated
Joe Onorato46439ce2010-11-19 13:56:21 -08003323 public Builder setDefaults(int defaults) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003324 mN.defaults = defaults;
Joe Onorato46439ce2010-11-19 13:56:21 -08003325 return this;
3326 }
3327
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003328 /**
3329 * Set the priority of this notification.
3330 *
3331 * @see Notification#priority
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003332 * @deprecated use {@link NotificationChannel#setImportance(int)} instead.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003333 */
Julia Reynoldsa33f5c42017-01-31 16:53:35 -05003334 @Deprecated
Tor Norbyed9273d62013-05-30 15:59:53 -07003335 public Builder setPriority(@Priority int pri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003336 mN.priority = pri;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003337 return this;
3338 }
Joe Malin8d40d042012-11-05 11:36:40 -08003339
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003340 /**
John Spurlockfd7f1e02014-03-18 16:41:57 -04003341 * Set the notification category.
Joe Malin8d40d042012-11-05 11:36:40 -08003342 *
John Spurlockfd7f1e02014-03-18 16:41:57 -04003343 * @see Notification#category
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003344 */
John Spurlockfd7f1e02014-03-18 16:41:57 -04003345 public Builder setCategory(String category) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003346 mN.category = category;
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003347 return this;
3348 }
3349
3350 /**
Chris Wrendde75302014-03-26 17:24:15 -04003351 * Add a person that is relevant to this notification.
3352 *
Chris Wrene6c48932014-09-29 17:19:27 -04003353 * <P>
3354 * Depending on user preferences, this annotation may allow the notification to pass
Julia Reynoldse071abd2017-03-22 10:52:11 -04003355 * through interruption filters, if this notification is of category {@link #CATEGORY_CALL}
3356 * or {@link #CATEGORY_MESSAGE}. The addition of people may also cause this notification to
3357 * appear more prominently in the user interface.
Chris Wrene6c48932014-09-29 17:19:27 -04003358 * </P>
3359 *
3360 * <P>
3361 * The person should be specified by the {@code String} representation of a
3362 * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}.
3363 * </P>
3364 *
3365 * <P>The system will also attempt to resolve {@code mailto:} and {@code tel:} schema
3366 * URIs. The path part of these URIs must exist in the contacts database, in the
3367 * appropriate column, or the reference will be discarded as invalid. Telephone schema
3368 * URIs will be resolved by {@link android.provider.ContactsContract.PhoneLookup}.
3369 * </P>
3370 *
3371 * @param uri A URI for the person.
Chris Wrendde75302014-03-26 17:24:15 -04003372 * @see Notification#EXTRA_PEOPLE
3373 */
Chris Wrene6c48932014-09-29 17:19:27 -04003374 public Builder addPerson(String uri) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003375 mPersonList.add(uri);
Chris Wrendde75302014-03-26 17:24:15 -04003376 return this;
3377 }
3378
3379 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003380 * Set this notification to be part of a group of notifications sharing the same key.
3381 * Grouped notifications may display in a cluster or stack on devices which
3382 * support such rendering.
3383 *
3384 * <p>To make this notification the summary for its group, also call
3385 * {@link #setGroupSummary}. A sort order can be specified for group members by using
3386 * {@link #setSortKey}.
3387 * @param groupKey The group key of the group.
3388 * @return this object for method chaining
3389 */
3390 public Builder setGroup(String groupKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003391 mN.mGroupKey = groupKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003392 return this;
3393 }
3394
3395 /**
3396 * Set this notification to be the group summary for a group of notifications.
3397 * Grouped notifications may display in a cluster or stack on devices which
Julia Reynolds04499532016-09-13 14:04:53 -04003398 * support such rendering. If thereRequires a group key also be set using {@link #setGroup}.
3399 * The group summary may be suppressed if too few notifications are included in the group.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003400 * @param isGroupSummary Whether this notification should be a group summary.
3401 * @return this object for method chaining
3402 */
3403 public Builder setGroupSummary(boolean isGroupSummary) {
3404 setFlag(FLAG_GROUP_SUMMARY, isGroupSummary);
3405 return this;
3406 }
3407
3408 /**
3409 * Set a sort key that orders this notification among other notifications from the
3410 * same package. This can be useful if an external sort was already applied and an app
3411 * would like to preserve this. Notifications will be sorted lexicographically using this
3412 * value, although providing different priorities in addition to providing sort key may
3413 * cause this value to be ignored.
3414 *
3415 * <p>This sort key can also be used to order members of a notification group. See
Griff Hazen9e1379f2014-05-20 12:50:51 -07003416 * {@link #setGroup}.
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003417 *
3418 * @see String#compareTo(String)
3419 */
3420 public Builder setSortKey(String sortKey) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003421 mN.mSortKey = sortKey;
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003422 return this;
3423 }
3424
3425 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003426 * Merge additional metadata into this notification.
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003427 *
Griff Hazen720042b2014-02-24 15:46:56 -08003428 * <p>Values within the Bundle will replace existing extras values in this Builder.
3429 *
3430 * @see Notification#extras
3431 */
Griff Hazen959591e2014-05-15 22:26:18 -07003432 public Builder addExtras(Bundle extras) {
3433 if (extras != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003434 mUserExtras.putAll(extras);
Griff Hazen720042b2014-02-24 15:46:56 -08003435 }
3436 return this;
3437 }
3438
3439 /**
3440 * Set metadata for this notification.
3441 *
3442 * <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 -04003443 * current contents are copied into the Notification each time {@link #build()} is
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003444 * called.
3445 *
Griff Hazen720042b2014-02-24 15:46:56 -08003446 * <p>Replaces any existing extras values with those from the provided Bundle.
3447 * Use {@link #addExtras} to merge in metadata instead.
3448 *
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003449 * @see Notification#extras
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003450 */
Griff Hazen959591e2014-05-15 22:26:18 -07003451 public Builder setExtras(Bundle extras) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003452 if (extras != null) {
3453 mUserExtras = extras;
3454 }
Daniel Sandler2561b0b2012-02-13 21:04:12 -05003455 return this;
3456 }
3457
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003458 /**
Griff Hazen720042b2014-02-24 15:46:56 -08003459 * Get the current metadata Bundle used by this notification Builder.
3460 *
3461 * <p>The returned Bundle is shared with this Builder.
3462 *
3463 * <p>The current contents of this Bundle are copied into the Notification each time
3464 * {@link #build()} is called.
3465 *
3466 * @see Notification#extras
3467 */
3468 public Bundle getExtras() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003469 return mUserExtras;
3470 }
3471
3472 private Bundle getAllExtras() {
3473 final Bundle saveExtras = (Bundle) mUserExtras.clone();
3474 saveExtras.putAll(mN.extras);
3475 return saveExtras;
Griff Hazen720042b2014-02-24 15:46:56 -08003476 }
3477
3478 /**
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003479 * Add an action to this notification. Actions are typically displayed by
3480 * the system as a button adjacent to the notification content.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04003481 * <p>
3482 * Every action must have an icon (32dp square and matching the
3483 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3484 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3485 * <p>
3486 * A notification in its expanded form can display up to 3 actions, from left to right in
3487 * the order they were added. Actions will not be displayed when the notification is
3488 * collapsed, however, so be sure that any essential functions may be accessed by the user
3489 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003490 *
3491 * @param icon Resource ID of a drawable that represents the action.
3492 * @param title Text describing the action.
3493 * @param intent PendingIntent to be fired when the action is invoked.
Dan Sandler86647982015-05-13 23:41:13 -04003494 *
3495 * @deprecated Use {@link #addAction(Action)} instead.
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003496 */
Dan Sandler86647982015-05-13 23:41:13 -04003497 @Deprecated
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003498 public Builder addAction(int icon, CharSequence title, PendingIntent intent) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04003499 mActions.add(new Action(icon, safeCharSequence(title), intent));
Daniel Sandlera0a938c2012-03-15 08:42:37 -04003500 return this;
3501 }
3502
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003503 /**
Griff Hazen959591e2014-05-15 22:26:18 -07003504 * Add an action to this notification. Actions are typically displayed by
3505 * the system as a button adjacent to the notification content.
3506 * <p>
3507 * Every action must have an icon (32dp square and matching the
3508 * <a href="{@docRoot}design/style/iconography.html#action-bar">Holo
3509 * Dark action bar</a> visual style), a textual label, and a {@link PendingIntent}.
3510 * <p>
3511 * A notification in its expanded form can display up to 3 actions, from left to right in
3512 * the order they were added. Actions will not be displayed when the notification is
3513 * collapsed, however, so be sure that any essential functions may be accessed by the user
3514 * in some other way (for example, in the Activity pointed to by {@link #contentIntent}).
3515 *
3516 * @param action The action to add.
3517 */
3518 public Builder addAction(Action action) {
liangweikang63b03b52017-03-16 19:22:15 +08003519 if (action != null) {
3520 mActions.add(action);
3521 }
Griff Hazen959591e2014-05-15 22:26:18 -07003522 return this;
3523 }
3524
3525 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003526 * Alter the complete list of actions attached to this notification.
3527 * @see #addAction(Action).
3528 *
3529 * @param actions
3530 * @return
3531 */
3532 public Builder setActions(Action... actions) {
3533 mActions.clear();
3534 for (int i = 0; i < actions.length; i++) {
liangweikang63b03b52017-03-16 19:22:15 +08003535 if (actions[i] != null) {
3536 mActions.add(actions[i]);
3537 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003538 }
3539 return this;
3540 }
3541
3542 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003543 * Add a rich notification style to be applied at build time.
3544 *
3545 * @param style Object responsible for modifying the notification style.
3546 */
3547 public Builder setStyle(Style style) {
3548 if (mStyle != style) {
3549 mStyle = style;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003550 if (mStyle != null) {
3551 mStyle.setBuilder(this);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003552 mN.extras.putString(EXTRA_TEMPLATE, style.getClass().getName());
3553 } else {
3554 mN.extras.remove(EXTRA_TEMPLATE);
Daniel Sandlerc08dea22012-06-28 08:35:24 -07003555 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04003556 }
3557 return this;
3558 }
3559
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003560 /**
3561 * Specify the value of {@link #visibility}.
Griff Hazenb720abe2014-05-20 13:15:30 -07003562 *
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003563 * @param visibility One of {@link #VISIBILITY_PRIVATE} (the default),
3564 * {@link #VISIBILITY_SECRET}, or {@link #VISIBILITY_PUBLIC}.
3565 *
3566 * @return The same Builder.
3567 */
3568 public Builder setVisibility(int visibility) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003569 mN.visibility = visibility;
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003570 return this;
3571 }
3572
3573 /**
3574 * Supply a replacement Notification whose contents should be shown in insecure contexts
3575 * (i.e. atop the secure lockscreen). See {@link #visibility} and {@link #VISIBILITY_PUBLIC}.
3576 * @param n A replacement notification, presumably with some or all info redacted.
3577 * @return The same Builder.
3578 */
3579 public Builder setPublicVersion(Notification n) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003580 if (n != null) {
3581 mN.publicVersion = new Notification();
3582 n.cloneInto(mN.publicVersion, /*heavy=*/ true);
3583 } else {
3584 mN.publicVersion = null;
3585 }
Dan Sandler0bf2ed82013-12-21 23:33:41 -06003586 return this;
3587 }
3588
Griff Hazenb720abe2014-05-20 13:15:30 -07003589 /**
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003590 * Apply an extender to this notification builder. Extenders may be used to add
3591 * metadata or change options on this builder.
3592 */
Griff Hazen61a9e862014-05-22 16:05:19 -07003593 public Builder extend(Extender extender) {
3594 extender.extend(this);
Griff Hazen5cadc3b2014-05-20 09:55:39 -07003595 return this;
3596 }
3597
Dan Sandler4e787062015-06-17 15:09:48 -04003598 /**
3599 * @hide
3600 */
Julia Reynoldse46bb372016-03-17 11:05:58 -04003601 public Builder setFlag(int mask, boolean value) {
Joe Onorato46439ce2010-11-19 13:56:21 -08003602 if (value) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003603 mN.flags |= mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003604 } else {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003605 mN.flags &= ~mask;
Joe Onorato46439ce2010-11-19 13:56:21 -08003606 }
Julia Reynoldse46bb372016-03-17 11:05:58 -04003607 return this;
Joe Onorato46439ce2010-11-19 13:56:21 -08003608 }
3609
Dan Sandler26e81cf2014-05-06 10:01:27 -04003610 /**
3611 * Sets {@link Notification#color}.
3612 *
3613 * @param argb The accent color to use
3614 *
3615 * @return The same Builder.
3616 */
Tor Norbye80756e32015-03-02 09:39:27 -08003617 public Builder setColor(@ColorInt int argb) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003618 mN.color = argb;
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05003619 sanitizeColor();
Dan Sandler26e81cf2014-05-06 10:01:27 -04003620 return this;
3621 }
3622
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003623 private Drawable getProfileBadgeDrawable() {
Chris Wren66619a22016-05-12 16:42:37 -04003624 if (mContext.getUserId() == UserHandle.USER_SYSTEM) {
3625 // This user can never be a badged profile,
3626 // and also includes USER_ALL system notifications.
3627 return null;
3628 }
Christoph Studer7ac80e62014-08-04 16:01:57 +02003629 // Note: This assumes that the current user can read the profile badge of the
3630 // originating user.
Selim Cineke6ff9462016-01-15 15:07:06 -08003631 return mContext.getPackageManager().getUserBadgeForDensityNoBackground(
Julia Reynoldsda303542015-11-23 14:00:20 -05003632 new UserHandle(mContext.getUserId()), 0);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003633 }
3634
3635 private Bitmap getProfileBadge() {
3636 Drawable badge = getProfileBadgeDrawable();
Kenny Guy8a0101b2014-05-08 23:34:12 +01003637 if (badge == null) {
3638 return null;
3639 }
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003640 final int size = mContext.getResources().getDimensionPixelSize(
3641 R.dimen.notification_badge_size);
3642 Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003643 Canvas canvas = new Canvas(bitmap);
Jorim Jaggid05aa3e2014-08-28 17:52:27 +02003644 badge.setBounds(0, 0, size, size);
Kenny Guy8a0101b2014-05-08 23:34:12 +01003645 badge.draw(canvas);
3646 return bitmap;
3647 }
3648
Selim Cinekc848c3a2016-01-13 15:27:30 -08003649 private void bindProfileBadge(RemoteViews contentView) {
Kenny Guy98193ea2014-07-24 19:54:37 +01003650 Bitmap profileBadge = getProfileBadge();
3651
Kenny Guy98193ea2014-07-24 19:54:37 +01003652 if (profileBadge != null) {
Selim Cinekc848c3a2016-01-13 15:27:30 -08003653 contentView.setImageViewBitmap(R.id.profile_badge, profileBadge);
3654 contentView.setViewVisibility(R.id.profile_badge, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003655 if (isColorized()) {
3656 contentView.setDrawableParameters(R.id.profile_badge, false, -1,
3657 getPrimaryTextColor(), PorterDuff.Mode.SRC_ATOP, -1);
3658 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003659 }
Kenny Guy98193ea2014-07-24 19:54:37 +01003660 }
3661
Christoph Studerfe718432014-09-01 18:21:18 +02003662 private void resetStandardTemplate(RemoteViews contentView) {
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003663 resetNotificationHeader(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003664 resetContentMargins(contentView);
Christoph Studerfe718432014-09-01 18:21:18 +02003665 contentView.setViewVisibility(R.id.right_icon, View.GONE);
Selim Cinek860b6da2015-12-16 19:02:19 -08003666 contentView.setViewVisibility(R.id.title, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003667 contentView.setTextViewText(R.id.title, null);
Selim Cinek41598732016-01-11 16:58:37 -08003668 contentView.setViewVisibility(R.id.text, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003669 contentView.setTextViewText(R.id.text, null);
Selim Cinek29603462015-11-17 19:04:39 -08003670 contentView.setViewVisibility(R.id.text_line_1, View.GONE);
Selim Cinek41598732016-01-11 16:58:37 -08003671 contentView.setTextViewText(R.id.text_line_1, null);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003672 }
3673
Selim Cinekeaa29ca2015-11-23 13:51:13 -08003674 /**
3675 * Resets the notification header to its original state
3676 */
3677 private void resetNotificationHeader(RemoteViews contentView) {
Adrian Roosc4337a32016-08-02 18:30:34 -07003678 // Small icon doesn't need to be reset, as it's always set. Resetting would prevent
3679 // re-using the drawable when the notification is updated.
Selim Cinek7b836392015-12-04 20:02:59 -08003680 contentView.setBoolean(R.id.notification_header, "setExpanded", false);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003681 contentView.setTextViewText(R.id.app_name_text, null);
Christoph Studerca1db712014-09-10 17:31:33 +02003682 contentView.setViewVisibility(R.id.chronometer, View.GONE);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003683 contentView.setViewVisibility(R.id.header_text, View.GONE);
Adrian Roos9dfb78f2016-06-30 15:43:44 -07003684 contentView.setTextViewText(R.id.header_text, null);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003685 contentView.setViewVisibility(R.id.header_text_divider, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003686 contentView.setViewVisibility(R.id.time_divider, View.GONE);
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003687 contentView.setViewVisibility(R.id.time, View.GONE);
Selim Cinekc848c3a2016-01-13 15:27:30 -08003688 contentView.setImageViewIcon(R.id.profile_badge, null);
3689 contentView.setViewVisibility(R.id.profile_badge, View.GONE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003690 }
3691
3692 private void resetContentMargins(RemoteViews contentView) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003693 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
3694 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003695 }
3696
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003697 private RemoteViews applyStandardTemplate(int resId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003698 return applyStandardTemplate(resId, mParams.reset().fillTextsFrom(this));
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003699 }
3700
3701 /**
3702 * @param hasProgress whether the progress bar should be shown and set
3703 */
3704 private RemoteViews applyStandardTemplate(int resId, boolean hasProgress) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003705 return applyStandardTemplate(resId, mParams.reset().hasProgress(hasProgress)
3706 .fillTextsFrom(this));
Adrian Roosc1a80b02016-04-05 14:54:55 -07003707 }
3708
Adrian Roos70d7aa32017-01-11 15:39:06 -08003709 private RemoteViews applyStandardTemplate(int resId, StandardTemplateParams p) {
Kenny Guy77320062014-08-27 21:37:15 +01003710 RemoteViews contentView = new BuilderRemoteViews(mContext.getApplicationInfo(), resId);
Dan Sandler539aad42014-08-04 00:43:39 -04003711
Christoph Studerfe718432014-09-01 18:21:18 +02003712 resetStandardTemplate(contentView);
3713
Julia Reynoldsd9228f12015-10-20 10:37:27 -04003714 final Bundle ex = mN.extras;
Selim Cinek7b9605b2017-01-19 17:36:00 -08003715 updateBackgroundColor(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08003716 bindNotificationHeader(contentView, p.ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003717 bindLargeIcon(contentView);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003718 boolean showProgress = handleProgressBar(p.hasProgress, contentView, ex);
3719 if (p.title != null) {
Selim Cinek860b6da2015-12-16 19:02:19 -08003720 contentView.setViewVisibility(R.id.title, View.VISIBLE);
Adrian Roos70d7aa32017-01-11 15:39:06 -08003721 contentView.setTextViewText(R.id.title, p.title);
Adrian Roos72171622017-01-27 10:32:06 -08003722 if (!p.ambient) {
3723 setTextViewColorPrimary(contentView, R.id.title);
3724 }
Selim Cinek954cc232016-05-20 13:29:23 -07003725 contentView.setViewLayoutWidth(R.id.title, showProgress
3726 ? ViewGroup.LayoutParams.WRAP_CONTENT
3727 : ViewGroup.LayoutParams.MATCH_PARENT);
Joe Onorato561d3852010-11-20 18:09:34 -08003728 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08003729 if (p.text != null) {
Selim Cinek41598732016-01-11 16:58:37 -08003730 int textId = showProgress ? com.android.internal.R.id.text_line_1
3731 : com.android.internal.R.id.text;
Adrian Roos70d7aa32017-01-11 15:39:06 -08003732 contentView.setTextViewText(textId, p.text);
Adrian Roos72171622017-01-27 10:32:06 -08003733 if (!p.ambient) {
3734 setTextViewColorSecondary(contentView, textId);
3735 }
Selim Cinek41598732016-01-11 16:58:37 -08003736 contentView.setViewVisibility(textId, View.VISIBLE);
Joe Onorato561d3852010-11-20 18:09:34 -08003737 }
Selim Cinekc848c3a2016-01-13 15:27:30 -08003738
Selim Cinek279fa862016-06-14 10:57:25 -07003739 setContentMinHeight(contentView, showProgress || mN.hasLargeIcon());
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003740
Selim Cinek29603462015-11-17 19:04:39 -08003741 return contentView;
3742 }
3743
Selim Cinek7b9605b2017-01-19 17:36:00 -08003744 private void setTextViewColorPrimary(RemoteViews contentView, int id) {
3745 ensureColors();
3746 contentView.setTextColor(id, mPrimaryTextColor);
3747 }
3748
3749 private int getPrimaryTextColor() {
3750 ensureColors();
3751 return mPrimaryTextColor;
3752 }
3753
3754 private int getActionBarColor() {
3755 ensureColors();
3756 return mActionBarColor;
3757 }
3758
3759 private void setTextViewColorSecondary(RemoteViews contentView, int id) {
3760 ensureColors();
3761 contentView.setTextColor(id, mSecondaryTextColor);
3762 }
3763
3764 private void ensureColors() {
3765 int backgroundColor = getBackgroundColor();
3766 if (mPrimaryTextColor == COLOR_INVALID
3767 || mSecondaryTextColor == COLOR_INVALID
3768 || mActionBarColor == COLOR_INVALID
3769 || mTextColorsAreForBackground != backgroundColor) {
3770 mTextColorsAreForBackground = backgroundColor;
3771 mPrimaryTextColor = NotificationColorUtil.resolvePrimaryColor(
3772 mContext, backgroundColor);
3773 mSecondaryTextColor = NotificationColorUtil.resolveSecondaryColor(
3774 mContext, backgroundColor);
Selim Cinek875ba9b2017-02-13 16:20:17 -08003775 mActionBarColor = NotificationColorUtil.resolveActionBarColor(mContext,
3776 backgroundColor);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003777 }
3778 }
3779
3780 private void updateBackgroundColor(RemoteViews contentView) {
3781 if (isColorized()) {
3782 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundColor",
3783 getBackgroundColor());
3784 } else {
3785 // Clear it!
3786 contentView.setInt(R.id.status_bar_latest_event_content, "setBackgroundResource",
3787 0);
3788 }
3789 }
3790
Selim Cinek860b6da2015-12-16 19:02:19 -08003791 /**
3792 * @param remoteView the remote view to update the minheight in
3793 * @param hasMinHeight does it have a mimHeight
3794 * @hide
3795 */
3796 void setContentMinHeight(RemoteViews remoteView, boolean hasMinHeight) {
3797 int minHeight = 0;
3798 if (hasMinHeight) {
3799 // we need to set the minHeight of the notification
3800 minHeight = mContext.getResources().getDimensionPixelSize(
3801 com.android.internal.R.dimen.notification_min_content_height);
3802 }
3803 remoteView.setInt(R.id.notification_main_column, "setMinimumHeight", minHeight);
3804 }
3805
Selim Cinek29603462015-11-17 19:04:39 -08003806 private boolean handleProgressBar(boolean hasProgress, RemoteViews contentView, Bundle ex) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003807 final int max = ex.getInt(EXTRA_PROGRESS_MAX, 0);
3808 final int progress = ex.getInt(EXTRA_PROGRESS, 0);
3809 final boolean ind = ex.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
3810 if (hasProgress && (max != 0 || ind)) {
Selim Cinek29603462015-11-17 19:04:39 -08003811 contentView.setViewVisibility(com.android.internal.R.id.progress, View.VISIBLE);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003812 contentView.setProgressBar(
Selim Cinek29603462015-11-17 19:04:39 -08003813 R.id.progress, max, progress, ind);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003814 contentView.setProgressBackgroundTintList(
3815 R.id.progress, ColorStateList.valueOf(mContext.getColor(
3816 R.color.notification_progress_background_color)));
Selim Cinek29603462015-11-17 19:04:39 -08003817 if (mN.color != COLOR_DEFAULT) {
Adrian Roos4ff3b122016-02-01 12:26:13 -08003818 ColorStateList colorStateList = ColorStateList.valueOf(resolveContrastColor());
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003819 contentView.setProgressTintList(R.id.progress, colorStateList);
3820 contentView.setProgressIndeterminateTintList(R.id.progress, colorStateList);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04003821 }
Selim Cinek29603462015-11-17 19:04:39 -08003822 return true;
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003823 } else {
3824 contentView.setViewVisibility(R.id.progress, View.GONE);
Selim Cinek29603462015-11-17 19:04:39 -08003825 return false;
Jeff Sharkey1c400132011-08-05 14:50:13 -07003826 }
Joe Onorato561d3852010-11-20 18:09:34 -08003827 }
3828
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003829 private void bindLargeIcon(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07003830 if (mN.mLargeIcon == null && mN.largeIcon != null) {
3831 mN.mLargeIcon = Icon.createWithBitmap(mN.largeIcon);
3832 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003833 if (mN.mLargeIcon != null) {
3834 contentView.setViewVisibility(R.id.right_icon, View.VISIBLE);
3835 contentView.setImageViewIcon(R.id.right_icon, mN.mLargeIcon);
3836 processLargeLegacyIcon(mN.mLargeIcon, contentView);
Adrian Roos2d5dbba2016-06-08 17:11:53 -07003837 int endMargin = R.dimen.notification_content_picture_margin;
3838 contentView.setViewLayoutMarginEndDimen(R.id.line1, endMargin);
3839 contentView.setViewLayoutMarginEndDimen(R.id.text, endMargin);
3840 contentView.setViewLayoutMarginEndDimen(R.id.progress, endMargin);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003841 }
3842 }
3843
Adrian Roos487374f2017-01-11 15:48:14 -08003844 private void bindNotificationHeader(RemoteViews contentView, boolean ambient) {
3845 bindSmallIcon(contentView, ambient);
3846 bindHeaderAppName(contentView, ambient);
3847 if (!ambient) {
3848 // Ambient view does not have these
3849 bindHeaderText(contentView);
3850 bindHeaderChronometerAndTime(contentView);
Adrian Roos487374f2017-01-11 15:48:14 -08003851 bindProfileBadge(contentView);
3852 }
Adrian Roosd83e9992017-03-16 15:17:57 -07003853 bindExpandButton(contentView);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003854 }
3855
3856 private void bindExpandButton(RemoteViews contentView) {
Selim Cinek99104832017-01-25 14:47:33 -08003857 int color = getPrimaryHighlightColor();
Selim Cinek7b9605b2017-01-19 17:36:00 -08003858 contentView.setDrawableParameters(R.id.expand_button, false, -1, color,
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003859 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08003860 contentView.setInt(R.id.notification_header, "setOriginalNotificationColor",
Selim Cinek7b9605b2017-01-19 17:36:00 -08003861 color);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003862 }
3863
Selim Cinek99104832017-01-25 14:47:33 -08003864 /**
3865 * @return the color that is used as the first primary highlight color. This is applied
3866 * in several places like the action buttons or the app name in the header.
3867 */
3868 private int getPrimaryHighlightColor() {
3869 return isColorized() ? getPrimaryTextColor() : resolveContrastColor();
3870 }
3871
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003872 private void bindHeaderChronometerAndTime(RemoteViews contentView) {
3873 if (showsTimeOrChronometer()) {
Selim Cinek29603462015-11-17 19:04:39 -08003874 contentView.setViewVisibility(R.id.time_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003875 setTextViewColorSecondary(contentView, R.id.time_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003876 if (mN.extras.getBoolean(EXTRA_SHOW_CHRONOMETER)) {
3877 contentView.setViewVisibility(R.id.chronometer, View.VISIBLE);
3878 contentView.setLong(R.id.chronometer, "setBase",
3879 mN.when + (SystemClock.elapsedRealtime() - System.currentTimeMillis()));
3880 contentView.setBoolean(R.id.chronometer, "setStarted", true);
Adrian Roos96b7e202016-05-17 13:50:38 -07003881 boolean countsDown = mN.extras.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN);
Selim Cinekc3b752e2016-04-20 16:13:59 -07003882 contentView.setChronometerCountDown(R.id.chronometer, countsDown);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003883 setTextViewColorSecondary(contentView, R.id.chronometer);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003884 } else {
3885 contentView.setViewVisibility(R.id.time, View.VISIBLE);
3886 contentView.setLong(R.id.time, "setTime", mN.when);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003887 setTextViewColorSecondary(contentView, R.id.time);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003888 }
Selim Cinekb85f36fd2016-04-20 18:46:36 -07003889 } else {
3890 // We still want a time to be set but gone, such that we can show and hide it
3891 // on demand in case it's a child notification without anything in the header
3892 contentView.setLong(R.id.time, "setTime", mN.when != 0 ? mN.when : mN.creationTime);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003893 }
3894 }
3895
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003896 private void bindHeaderText(RemoteViews contentView) {
3897 CharSequence headerText = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
3898 if (headerText == null && mStyle != null && mStyle.mSummaryTextSet
Selim Cinek03d0d652015-11-13 13:18:09 -05003899 && mStyle.hasSummaryInHeader()) {
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003900 headerText = mStyle.mSummaryText;
Selim Cinek03d0d652015-11-13 13:18:09 -05003901 }
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003902 if (headerText == null
3903 && mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
3904 && mN.extras.getCharSequence(EXTRA_INFO_TEXT) != null) {
3905 headerText = mN.extras.getCharSequence(EXTRA_INFO_TEXT);
3906 }
3907 if (headerText != null) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003908 // TODO: Remove the span entirely to only have the string with propper formating.
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003909 contentView.setTextViewText(R.id.header_text, processLegacyText(headerText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08003910 setTextViewColorSecondary(contentView, R.id.header_text);
Selim Cinek0f9dd1e2016-04-05 17:03:40 -07003911 contentView.setViewVisibility(R.id.header_text, View.VISIBLE);
3912 contentView.setViewVisibility(R.id.header_text_divider, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08003913 setTextViewColorSecondary(contentView, R.id.header_text_divider);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003914 }
3915 }
3916
Adrian Rooseba05822016-04-22 17:09:27 -07003917 /**
3918 * @hide
3919 */
3920 public String loadHeaderAppName() {
Dan Sandler732bd6c2016-04-12 14:20:32 -04003921 CharSequence name = null;
3922 final PackageManager pm = mContext.getPackageManager();
3923 if (mN.extras.containsKey(EXTRA_SUBSTITUTE_APP_NAME)) {
3924 // only system packages which lump together a bunch of unrelated stuff
3925 // may substitute a different name to make the purpose of the
3926 // notification more clear. the correct package label should always
3927 // be accessible via SystemUI.
3928 final String pkg = mContext.getPackageName();
3929 final String subName = mN.extras.getString(EXTRA_SUBSTITUTE_APP_NAME);
3930 if (PackageManager.PERMISSION_GRANTED == pm.checkPermission(
3931 android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME, pkg)) {
3932 name = subName;
3933 } else {
3934 Log.w(TAG, "warning: pkg "
3935 + pkg + " attempting to substitute app name '" + subName
3936 + "' without holding perm "
3937 + android.Manifest.permission.SUBSTITUTE_NOTIFICATION_APP_NAME);
3938 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003939 }
Dan Sandler732bd6c2016-04-12 14:20:32 -04003940 if (TextUtils.isEmpty(name)) {
3941 name = pm.getApplicationLabel(mContext.getApplicationInfo());
3942 }
3943 if (TextUtils.isEmpty(name)) {
3944 // still nothing?
3945 return null;
3946 }
3947
3948 return String.valueOf(name);
3949 }
Adrian Roos487374f2017-01-11 15:48:14 -08003950 private void bindHeaderAppName(RemoteViews contentView, boolean ambient) {
Dan Sandler732bd6c2016-04-12 14:20:32 -04003951 contentView.setTextViewText(R.id.app_name_text, loadHeaderAppName());
Adrian Roos72171622017-01-27 10:32:06 -08003952 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08003953 setTextViewColorPrimary(contentView, R.id.app_name_text);
3954 } else {
3955 contentView.setTextColor(R.id.app_name_text,
3956 ambient ? resolveAmbientColor() : resolveContrastColor());
3957 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003958 }
3959
Adrian Roos487374f2017-01-11 15:48:14 -08003960 private void bindSmallIcon(RemoteViews contentView, boolean ambient) {
Selim Cinek279fa862016-06-14 10:57:25 -07003961 if (mN.mSmallIcon == null && mN.icon != 0) {
3962 mN.mSmallIcon = Icon.createWithResource(mContext, mN.icon);
3963 }
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003964 contentView.setImageViewIcon(R.id.icon, mN.mSmallIcon);
Adrian Roos9b45a152016-06-28 13:32:29 -07003965 contentView.setDrawableParameters(R.id.icon, false /* targetBackground */,
3966 -1 /* alpha */, -1 /* colorFilter */, null /* mode */, mN.iconLevel);
Adrian Roos487374f2017-01-11 15:48:14 -08003967 processSmallIconColor(mN.mSmallIcon, contentView, ambient);
Selim Cinek65b2e7c2015-10-26 14:11:31 -07003968 }
3969
Jorim Jaggi445d3c02014-08-19 22:33:42 +02003970 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003971 * @return true if the built notification will show the time or the chronometer; false
3972 * otherwise
3973 */
3974 private boolean showsTimeOrChronometer() {
Selim Cinekc2c0b042016-05-18 17:13:46 -07003975 return mN.showsTime() || mN.showsChronometer();
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02003976 }
3977
Christoph Studerfe718432014-09-01 18:21:18 +02003978 private void resetStandardTemplateWithActions(RemoteViews big) {
Adrian Roos4c1fcc82016-03-31 14:39:39 -07003979 // actions_container is only reset when there are no actions to avoid focus issues with
3980 // remote inputs.
Christoph Studerfe718432014-09-01 18:21:18 +02003981 big.setViewVisibility(R.id.actions, View.GONE);
Christoph Studerfe718432014-09-01 18:21:18 +02003982 big.removeAllViews(R.id.actions);
Adrian Roose458aa82015-12-08 16:17:19 -08003983
3984 big.setViewVisibility(R.id.notification_material_reply_container, View.GONE);
3985 big.setTextViewText(R.id.notification_material_reply_text_1, null);
3986
3987 big.setViewVisibility(R.id.notification_material_reply_text_2, View.GONE);
3988 big.setTextViewText(R.id.notification_material_reply_text_2, null);
3989 big.setViewVisibility(R.id.notification_material_reply_text_3, View.GONE);
3990 big.setTextViewText(R.id.notification_material_reply_text_3, null);
Adrian Roosf852a422016-06-03 13:33:43 -07003991
3992 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target, 0);
Christoph Studerfe718432014-09-01 18:21:18 +02003993 }
3994
Daniel Sandler96fd7c12012-03-30 16:37:36 -04003995 private RemoteViews applyStandardTemplateWithActions(int layoutId) {
Adrian Roos70d7aa32017-01-11 15:39:06 -08003996 return applyStandardTemplateWithActions(layoutId, mParams.reset().fillTextsFrom(this));
Adrian Roos48d746a2016-04-12 14:57:28 -07003997 }
3998
Adrian Roos70d7aa32017-01-11 15:39:06 -08003999 private RemoteViews applyStandardTemplateWithActions(int layoutId,
4000 StandardTemplateParams p) {
4001 RemoteViews big = applyStandardTemplate(layoutId, p);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004002
Christoph Studerfe718432014-09-01 18:21:18 +02004003 resetStandardTemplateWithActions(big);
4004
Adrian Roose458aa82015-12-08 16:17:19 -08004005 boolean validRemoteInput = false;
4006
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004007 int N = mActions.size();
Adrian Roos487374f2017-01-11 15:48:14 -08004008 boolean emphazisedMode = mN.fullScreenIntent != null && !p.ambient;
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004009 big.setBoolean(R.id.actions, "setEmphasizedMode", emphazisedMode);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004010 if (N > 0) {
Adrian Roos7052de52016-03-03 15:53:34 -08004011 big.setViewVisibility(R.id.actions_container, View.VISIBLE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004012 big.setViewVisibility(R.id.actions, View.VISIBLE);
Adrian Roos487374f2017-01-11 15:48:14 -08004013 if (p.ambient) {
4014 big.setInt(R.id.actions, "setBackgroundColor", Color.TRANSPARENT);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004015 } else if (isColorized()) {
4016 big.setInt(R.id.actions, "setBackgroundColor", getActionBarColor());
4017 } else {
4018 big.setInt(R.id.actions, "setBackgroundColor", mContext.getColor(
4019 R.color.notification_action_list));
Adrian Roos487374f2017-01-11 15:48:14 -08004020 }
Adrian Roosf852a422016-06-03 13:33:43 -07004021 big.setViewLayoutMarginBottomDimen(R.id.notification_action_list_margin_target,
4022 R.dimen.notification_action_list_height);
Daniel Sandler8680bf82012-05-15 16:52:52 -04004023 if (N>MAX_ACTION_BUTTONS) N=MAX_ACTION_BUTTONS;
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004024 for (int i=0; i<N; i++) {
Adrian Roose458aa82015-12-08 16:17:19 -08004025 Action action = mActions.get(i);
4026 validRemoteInput |= hasValidRemoteInput(action);
4027
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004028 final RemoteViews button = generateActionButton(action, emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004029 i % 2 != 0, p.ambient);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004030 big.addView(R.id.actions, button);
4031 }
Adrian Roos4c1fcc82016-03-31 14:39:39 -07004032 } else {
4033 big.setViewVisibility(R.id.actions_container, View.GONE);
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004034 }
Adrian Roose458aa82015-12-08 16:17:19 -08004035
4036 CharSequence[] replyText = mN.extras.getCharSequenceArray(EXTRA_REMOTE_INPUT_HISTORY);
Adrian Roos487374f2017-01-11 15:48:14 -08004037 if (!p.ambient && validRemoteInput && replyText != null
Adrian Roose458aa82015-12-08 16:17:19 -08004038 && replyText.length > 0 && !TextUtils.isEmpty(replyText[0])) {
4039 big.setViewVisibility(R.id.notification_material_reply_container, View.VISIBLE);
4040 big.setTextViewText(R.id.notification_material_reply_text_1, replyText[0]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004041 setTextViewColorSecondary(big, R.id.notification_material_reply_text_1);
Adrian Roose458aa82015-12-08 16:17:19 -08004042
4043 if (replyText.length > 1 && !TextUtils.isEmpty(replyText[1])) {
4044 big.setViewVisibility(R.id.notification_material_reply_text_2, View.VISIBLE);
4045 big.setTextViewText(R.id.notification_material_reply_text_2, replyText[1]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004046 setTextViewColorSecondary(big, R.id.notification_material_reply_text_2);
Adrian Roose458aa82015-12-08 16:17:19 -08004047
4048 if (replyText.length > 2 && !TextUtils.isEmpty(replyText[2])) {
4049 big.setViewVisibility(
4050 R.id.notification_material_reply_text_3, View.VISIBLE);
4051 big.setTextViewText(R.id.notification_material_reply_text_3, replyText[2]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004052 setTextViewColorSecondary(big, R.id.notification_material_reply_text_3);
Adrian Roose458aa82015-12-08 16:17:19 -08004053 }
4054 }
4055 }
4056
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004057 return big;
4058 }
4059
Adrian Roose458aa82015-12-08 16:17:19 -08004060 private boolean hasValidRemoteInput(Action action) {
4061 if (TextUtils.isEmpty(action.title) || action.actionIntent == null) {
4062 // Weird actions
4063 return false;
4064 }
4065
4066 RemoteInput[] remoteInputs = action.getRemoteInputs();
4067 if (remoteInputs == null) {
4068 return false;
4069 }
4070
4071 for (RemoteInput r : remoteInputs) {
4072 CharSequence[] choices = r.getChoices();
4073 if (r.getAllowFreeFormInput() || (choices != null && choices.length != 0)) {
4074 return true;
4075 }
4076 }
4077 return false;
4078 }
4079
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004080 /**
4081 * Construct a RemoteViews for the final 1U notification layout. In order:
4082 * 1. Custom contentView from the caller
4083 * 2. Style's proposed content view
4084 * 3. Standard template view
4085 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004086 public RemoteViews createContentView() {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004087 return createContentView(false /* increasedheight */ );
4088 }
4089
4090 /**
4091 * Construct a RemoteViews for the smaller content view.
4092 *
4093 * @param increasedHeight true if this layout be created with an increased height. Some
4094 * styles may support showing more then just that basic 1U size
4095 * and the system may decide to render important notifications
4096 * slightly bigger even when collapsed.
4097 *
4098 * @hide
4099 */
4100 public RemoteViews createContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08004101 if (mN.contentView != null && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004102 return mN.contentView;
4103 } else if (mStyle != null) {
Selim Cinek7d1009b2017-01-25 15:28:28 -08004104 final RemoteViews styleView = mStyle.makeContentView(increasedHeight);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004105 if (styleView != null) {
4106 return styleView;
4107 }
Joe Onorato46439ce2010-11-19 13:56:21 -08004108 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004109 return applyStandardTemplate(getBaseLayoutResource());
Joe Onorato46439ce2010-11-19 13:56:21 -08004110 }
4111
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004112 /**
4113 * Construct a RemoteViews for the final big notification layout.
4114 */
Julia Reynolds3b848122016-02-26 10:45:32 -05004115 public RemoteViews createBigContentView() {
Selim Cinek850a8542015-11-11 11:48:36 -05004116 RemoteViews result = null;
Selim Cinek593610c2016-02-16 18:42:57 -08004117 if (mN.bigContentView != null
4118 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004119 return mN.bigContentView;
4120 } else if (mStyle != null) {
Selim Cinek850a8542015-11-11 11:48:36 -05004121 result = mStyle.makeBigContentView();
Selim Cinek90dcf6d2015-11-18 20:24:13 -08004122 hideLine1Text(result);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004123 } else if (mActions.size() != 0) {
4124 result = applyStandardTemplateWithActions(getBigBaseLayoutResource());
Selim Cinek850a8542015-11-11 11:48:36 -05004125 }
Selim Cinek6743c0b2017-01-18 18:24:01 -08004126 makeHeaderExpanded(result);
Selim Cinek850a8542015-11-11 11:48:36 -05004127 return result;
4128 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004129
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004130 /**
Selim Cinek414ad332017-02-24 19:06:12 -08004131 * Construct a RemoteViews for the final notification header only. This will not be
4132 * colorized.
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004133 *
4134 * @hide
4135 */
4136 public RemoteViews makeNotificationHeader() {
Selim Cinek414ad332017-02-24 19:06:12 -08004137 Boolean colorized = (Boolean) mN.extras.get(EXTRA_COLORIZED);
4138 mN.extras.putBoolean(EXTRA_COLORIZED, false);
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004139 RemoteViews header = new BuilderRemoteViews(mContext.getApplicationInfo(),
4140 R.layout.notification_template_header);
4141 resetNotificationHeader(header);
Adrian Roos487374f2017-01-11 15:48:14 -08004142 bindNotificationHeader(header, false /* ambient */);
Selim Cinek414ad332017-02-24 19:06:12 -08004143 if (colorized != null) {
4144 mN.extras.putBoolean(EXTRA_COLORIZED, colorized);
4145 } else {
4146 mN.extras.remove(EXTRA_COLORIZED);
4147 }
Selim Cinekeaa29ca2015-11-23 13:51:13 -08004148 return header;
4149 }
4150
Adrian Roos487374f2017-01-11 15:48:14 -08004151 /**
4152 * Construct a RemoteViews for the ambient version of the notification.
4153 *
4154 * @hide
4155 */
4156 public RemoteViews makeAmbientNotification() {
4157 RemoteViews ambient = applyStandardTemplateWithActions(
4158 R.layout.notification_template_material_ambient,
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004159 mParams.reset().ambient(true).fillTextsFrom(this).hasProgress(false));
Adrian Roos487374f2017-01-11 15:48:14 -08004160 return ambient;
4161 }
4162
Selim Cinek29603462015-11-17 19:04:39 -08004163 private void hideLine1Text(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004164 if (result != null) {
4165 result.setViewVisibility(R.id.text_line_1, View.GONE);
4166 }
Selim Cinek29603462015-11-17 19:04:39 -08004167 }
4168
Selim Cinek6743c0b2017-01-18 18:24:01 -08004169 /**
4170 * Adapt the Notification header if this view is used as an expanded view.
4171 *
4172 * @hide
4173 */
4174 public static void makeHeaderExpanded(RemoteViews result) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08004175 if (result != null) {
4176 result.setBoolean(R.id.notification_header, "setExpanded", true);
4177 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004178 }
4179
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004180 /**
4181 * Construct a RemoteViews for the final heads-up notification layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004182 *
4183 * @param increasedHeight true if this layout be created with an increased height. Some
4184 * styles may support showing more then just that basic 1U size
4185 * and the system may decide to render important notifications
4186 * slightly bigger even when collapsed.
4187 *
4188 * @hide
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004189 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004190 public RemoteViews createHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08004191 if (mN.headsUpContentView != null
4192 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynolds089e3e42015-11-18 09:59:57 -05004193 return mN.headsUpContentView;
4194 } else if (mStyle != null) {
Selim Cinek87ed69b2017-02-09 15:59:43 -08004195 final RemoteViews styleView = mStyle.makeHeadsUpContentView(increasedHeight);
4196 if (styleView != null) {
4197 return styleView;
4198 }
Julia Reynolds089e3e42015-11-18 09:59:57 -05004199 } else if (mActions.size() == 0) {
4200 return null;
4201 }
4202
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004203 return applyStandardTemplateWithActions(getBigBaseLayoutResource());
Chris Wren8fd39ec2014-02-27 17:43:26 -05004204 }
4205
Selim Cinek624c02db2015-12-14 21:00:02 -08004206 /**
Selim Cinek87ed69b2017-02-09 15:59:43 -08004207 * Construct a RemoteViews for the final heads-up notification layout.
4208 */
4209 public RemoteViews createHeadsUpContentView() {
4210 return createHeadsUpContentView(false /* useIncreasedHeight */);
4211 }
4212
4213 /**
Selim Cinek624c02db2015-12-14 21:00:02 -08004214 * Construct a RemoteViews for the display in public contexts like on the lockscreen.
4215 *
4216 * @hide
4217 */
4218 public RemoteViews makePublicContentView() {
4219 if (mN.publicVersion != null) {
4220 final Builder builder = recoverBuilder(mContext, mN.publicVersion);
Julia Reynolds3b848122016-02-26 10:45:32 -05004221 return builder.createContentView();
Selim Cinek624c02db2015-12-14 21:00:02 -08004222 }
4223 Bundle savedBundle = mN.extras;
4224 Style style = mStyle;
4225 mStyle = null;
4226 Icon largeIcon = mN.mLargeIcon;
4227 mN.mLargeIcon = null;
Selim Cinek279fa862016-06-14 10:57:25 -07004228 Bitmap largeIconLegacy = mN.largeIcon;
4229 mN.largeIcon = null;
Selim Cinek624c02db2015-12-14 21:00:02 -08004230 Bundle publicExtras = new Bundle();
4231 publicExtras.putBoolean(EXTRA_SHOW_WHEN,
4232 savedBundle.getBoolean(EXTRA_SHOW_WHEN));
4233 publicExtras.putBoolean(EXTRA_SHOW_CHRONOMETER,
4234 savedBundle.getBoolean(EXTRA_SHOW_CHRONOMETER));
Adrian Roos96b7e202016-05-17 13:50:38 -07004235 publicExtras.putBoolean(EXTRA_CHRONOMETER_COUNT_DOWN,
4236 savedBundle.getBoolean(EXTRA_CHRONOMETER_COUNT_DOWN));
Selim Cinek624c02db2015-12-14 21:00:02 -08004237 publicExtras.putCharSequence(EXTRA_TITLE,
4238 mContext.getString(R.string.notification_hidden_text));
4239 mN.extras = publicExtras;
4240 final RemoteViews publicView = applyStandardTemplate(getBaseLayoutResource());
4241 mN.extras = savedBundle;
4242 mN.mLargeIcon = largeIcon;
Selim Cinek279fa862016-06-14 10:57:25 -07004243 mN.largeIcon = largeIconLegacy;
Selim Cinek624c02db2015-12-14 21:00:02 -08004244 mStyle = style;
4245 return publicView;
4246 }
4247
Selim Cinek6743c0b2017-01-18 18:24:01 -08004248 /**
4249 * Construct a content view for the display when low - priority
4250 *
4251 * @param useRegularSubtext uses the normal subtext set if there is one available. Otherwise
4252 * a new subtext is created consisting of the content of the
4253 * notification.
4254 * @hide
4255 */
4256 public RemoteViews makeLowPriorityContentView(boolean useRegularSubtext) {
4257 int color = mN.color;
4258 mN.color = COLOR_DEFAULT;
4259 CharSequence summary = mN.extras.getCharSequence(EXTRA_SUB_TEXT);
4260 if (!useRegularSubtext || TextUtils.isEmpty(summary)) {
4261 CharSequence newSummary = createSummaryText();
4262 if (!TextUtils.isEmpty(newSummary)) {
4263 mN.extras.putCharSequence(EXTRA_SUB_TEXT, newSummary);
4264 }
4265 }
Selim Cinek875ba9b2017-02-13 16:20:17 -08004266
Selim Cinek6743c0b2017-01-18 18:24:01 -08004267 RemoteViews header = makeNotificationHeader();
Selim Cinek1b554392017-02-28 17:22:49 -08004268 header.setBoolean(R.id.notification_header, "setAcceptAllTouches", true);
Selim Cinek6743c0b2017-01-18 18:24:01 -08004269 if (summary != null) {
4270 mN.extras.putCharSequence(EXTRA_SUB_TEXT, summary);
4271 } else {
4272 mN.extras.remove(EXTRA_SUB_TEXT);
4273 }
4274 mN.color = color;
4275 return header;
4276 }
Selim Cinek624c02db2015-12-14 21:00:02 -08004277
Selim Cinek6743c0b2017-01-18 18:24:01 -08004278 private CharSequence createSummaryText() {
4279 CharSequence titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE);
4280 if (USE_ONLY_TITLE_IN_LOW_PRIORITY_SUMMARY) {
4281 return titleText;
4282 }
4283 SpannableStringBuilder summary = new SpannableStringBuilder();
4284 if (titleText == null) {
4285 titleText = mN.extras.getCharSequence(Notification.EXTRA_TITLE_BIG);
4286 }
4287 BidiFormatter bidi = BidiFormatter.getInstance();
4288 if (titleText != null) {
4289 summary.append(bidi.unicodeWrap(titleText));
4290 }
4291 CharSequence contentText = mN.extras.getCharSequence(Notification.EXTRA_TEXT);
4292 if (titleText != null && contentText != null) {
4293 summary.append(bidi.unicodeWrap(mContext.getText(
4294 R.string.notification_header_divider_symbol_with_spaces)));
4295 }
4296 if (contentText != null) {
4297 summary.append(bidi.unicodeWrap(contentText));
4298 }
4299 return summary;
4300 }
Chris Wren8fd39ec2014-02-27 17:43:26 -05004301
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004302 private RemoteViews generateActionButton(Action action, boolean emphazisedMode,
Adrian Roos487374f2017-01-11 15:48:14 -08004303 boolean oddAction, boolean ambient) {
Daniel Sandler8680bf82012-05-15 16:52:52 -04004304 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07004305 RemoteViews button = new BuilderRemoteViews(mContext.getApplicationInfo(),
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004306 emphazisedMode ? getEmphasizedActionLayoutResource()
4307 : tombstone ? getActionTombstoneLayoutResource()
4308 : getActionLayoutResource());
Daniel Sandler8680bf82012-05-15 16:52:52 -04004309 if (!tombstone) {
Daniel Sandlere5518842012-05-10 16:20:40 -04004310 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
Daniel Sandlere5518842012-05-10 16:20:40 -04004311 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004312 button.setContentDescription(R.id.action0, action.title);
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004313 if (action.mRemoteInputs != null) {
4314 button.setRemoteInputs(R.id.action0, action.mRemoteInputs);
4315 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004316 // TODO: handle emphasized mode / actions right
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004317 if (emphazisedMode) {
Selim Cinek981962e2016-07-20 20:41:58 -07004318 // change the background bgColor
4319 int bgColor = mContext.getColor(oddAction ? R.color.notification_action_list
4320 : R.color.notification_action_list_dark);
4321 button.setDrawableParameters(R.id.button_holder, true, -1, bgColor,
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004322 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinek981962e2016-07-20 20:41:58 -07004323 CharSequence title = action.title;
4324 ColorStateList[] outResultColor = null;
4325 if (isLegacy()) {
4326 title = clearColorSpans(title);
4327 } else {
4328 outResultColor = new ColorStateList[1];
4329 title = ensureColorSpanContrast(title, bgColor, outResultColor);
4330 }
4331 button.setTextViewText(R.id.action0, title);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004332 setTextViewColorPrimary(button, R.id.action0);
Selim Cinek981962e2016-07-20 20:41:58 -07004333 if (outResultColor != null && outResultColor[0] != null) {
4334 // We need to set the text color as well since changing a text to uppercase
4335 // clears its spans.
4336 button.setTextColor(R.id.action0, outResultColor[0]);
Selim Cinek7b9605b2017-01-19 17:36:00 -08004337 } else if (mN.color != COLOR_DEFAULT && !isColorized()) {
Selim Cinek981962e2016-07-20 20:41:58 -07004338 button.setTextColor(R.id.action0,resolveContrastColor());
4339 }
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004340 } else {
Selim Cinek981962e2016-07-20 20:41:58 -07004341 button.setTextViewText(R.id.action0, processLegacyText(action.title));
Adrian Roos72171622017-01-27 10:32:06 -08004342 if (isColorized() && !ambient) {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004343 setTextViewColorPrimary(button, R.id.action0);
4344 } else if (mN.color != COLOR_DEFAULT) {
Adrian Roos487374f2017-01-11 15:48:14 -08004345 button.setTextColor(R.id.action0,
4346 ambient ? resolveAmbientColor() : resolveContrastColor());
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004347 }
Adrian Roosfe84e1f2015-11-04 15:55:39 -08004348 }
Daniel Sandler96fd7c12012-03-30 16:37:36 -04004349 return button;
4350 }
4351
Joe Onoratocb109a02011-01-18 17:57:41 -08004352 /**
Selim Cinek981962e2016-07-20 20:41:58 -07004353 * Clears all color spans of a text
4354 * @param charSequence the input text
4355 * @return the same text but without color spans
4356 */
4357 private CharSequence clearColorSpans(CharSequence charSequence) {
4358 if (charSequence instanceof Spanned) {
4359 Spanned ss = (Spanned) charSequence;
4360 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4361 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4362 for (Object span : spans) {
4363 Object resultSpan = span;
4364 if (resultSpan instanceof CharacterStyle) {
4365 resultSpan = ((CharacterStyle) span).getUnderlying();
4366 }
4367 if (resultSpan instanceof TextAppearanceSpan) {
4368 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4369 if (originalSpan.getTextColor() != null) {
4370 resultSpan = new TextAppearanceSpan(
4371 originalSpan.getFamily(),
4372 originalSpan.getTextStyle(),
4373 originalSpan.getTextSize(),
4374 null,
4375 originalSpan.getLinkTextColor());
4376 }
4377 } else if (resultSpan instanceof ForegroundColorSpan
4378 || (resultSpan instanceof BackgroundColorSpan)) {
4379 continue;
4380 } else {
4381 resultSpan = span;
4382 }
4383 builder.setSpan(resultSpan, ss.getSpanStart(span), ss.getSpanEnd(span),
4384 ss.getSpanFlags(span));
4385 }
4386 return builder;
4387 }
4388 return charSequence;
4389 }
4390
4391 /**
4392 * Ensures contrast on color spans against a background color. also returns the color of the
4393 * text if a span was found that spans over the whole text.
4394 *
4395 * @param charSequence the charSequence on which the spans are
4396 * @param background the background color to ensure the contrast against
4397 * @param outResultColor an array in which a color will be returned as the first element if
4398 * there exists a full length color span.
4399 * @return the contrasted charSequence
4400 */
4401 private CharSequence ensureColorSpanContrast(CharSequence charSequence, int background,
4402 ColorStateList[] outResultColor) {
4403 if (charSequence instanceof Spanned) {
4404 Spanned ss = (Spanned) charSequence;
4405 Object[] spans = ss.getSpans(0, ss.length(), Object.class);
4406 SpannableStringBuilder builder = new SpannableStringBuilder(ss.toString());
4407 for (Object span : spans) {
4408 Object resultSpan = span;
4409 int spanStart = ss.getSpanStart(span);
4410 int spanEnd = ss.getSpanEnd(span);
4411 boolean fullLength = (spanEnd - spanStart) == charSequence.length();
4412 if (resultSpan instanceof CharacterStyle) {
4413 resultSpan = ((CharacterStyle) span).getUnderlying();
4414 }
4415 if (resultSpan instanceof TextAppearanceSpan) {
4416 TextAppearanceSpan originalSpan = (TextAppearanceSpan) resultSpan;
4417 ColorStateList textColor = originalSpan.getTextColor();
4418 if (textColor != null) {
4419 int[] colors = textColor.getColors();
4420 int[] newColors = new int[colors.length];
4421 for (int i = 0; i < newColors.length; i++) {
4422 newColors[i] = NotificationColorUtil.ensureLargeTextContrast(
4423 colors[i], background);
4424 }
4425 textColor = new ColorStateList(textColor.getStates().clone(),
4426 newColors);
4427 resultSpan = new TextAppearanceSpan(
4428 originalSpan.getFamily(),
4429 originalSpan.getTextStyle(),
4430 originalSpan.getTextSize(),
4431 textColor,
4432 originalSpan.getLinkTextColor());
4433 if (fullLength) {
4434 outResultColor[0] = new ColorStateList(
4435 textColor.getStates().clone(), newColors);
4436 }
4437 }
4438 } else if (resultSpan instanceof ForegroundColorSpan) {
4439 ForegroundColorSpan originalSpan = (ForegroundColorSpan) resultSpan;
4440 int foregroundColor = originalSpan.getForegroundColor();
4441 foregroundColor = NotificationColorUtil.ensureLargeTextContrast(
4442 foregroundColor, background);
4443 resultSpan = new ForegroundColorSpan(foregroundColor);
4444 if (fullLength) {
4445 outResultColor[0] = ColorStateList.valueOf(foregroundColor);
4446 }
4447 } else {
4448 resultSpan = span;
4449 }
4450
4451 builder.setSpan(resultSpan, spanStart, spanEnd, ss.getSpanFlags(span));
4452 }
4453 return builder;
4454 }
4455 return charSequence;
4456 }
4457
4458 /**
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004459 * @return Whether we are currently building a notification from a legacy (an app that
Alan Viverette3cb07a462014-06-06 14:19:53 -07004460 * doesn't create material notifications by itself) app.
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004461 */
4462 private boolean isLegacy() {
Selim Cinek7b9605b2017-01-19 17:36:00 -08004463 if (!mIsLegacyInitialized) {
4464 mIsLegacy = mContext.getApplicationInfo().targetSdkVersion
4465 < Build.VERSION_CODES.LOLLIPOP;
4466 mIsLegacyInitialized = true;
4467 }
4468 return mIsLegacy;
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004469 }
4470
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004471 private CharSequence processLegacyText(CharSequence charSequence) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08004472 return processLegacyText(charSequence, false /* ambient */);
4473 }
4474
4475 private CharSequence processLegacyText(CharSequence charSequence, boolean ambient) {
4476 boolean isAlreadyLightText = isLegacy() || textColorsNeedInversion();
4477 boolean wantLightText = ambient;
4478 if (isAlreadyLightText != wantLightText) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004479 return getColorUtil().invertCharSequenceColors(charSequence);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004480 } else {
4481 return charSequence;
4482 }
4483 }
4484
Dan Sandler26e81cf2014-05-06 10:01:27 -04004485 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004486 * Apply any necessariy colors to the small icon
Dan Sandler26e81cf2014-05-06 10:01:27 -04004487 */
Adrian Roos487374f2017-01-11 15:48:14 -08004488 private void processSmallIconColor(Icon smallIcon, RemoteViews contentView,
4489 boolean ambient) {
Selim Cinekea4bef72015-12-02 15:51:10 -08004490 boolean colorable = !isLegacy() || getColorUtil().isGrayscaleIcon(mContext, smallIcon);
Selim Cinek99104832017-01-25 14:47:33 -08004491 int color = ambient ? resolveAmbientColor() : getPrimaryHighlightColor();
Selim Cinekea4bef72015-12-02 15:51:10 -08004492 if (colorable) {
Adrian Roos487374f2017-01-11 15:48:14 -08004493 contentView.setDrawableParameters(R.id.icon, false, -1, color,
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004494 PorterDuff.Mode.SRC_ATOP, -1);
Selim Cinekea4bef72015-12-02 15:51:10 -08004495
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004496 }
Selim Cinekea4bef72015-12-02 15:51:10 -08004497 contentView.setInt(R.id.notification_header, "setOriginalIconColor",
Adrian Roos487374f2017-01-11 15:48:14 -08004498 colorable ? color : NotificationHeaderView.NO_COLOR);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004499 }
4500
Dan Sandler26e81cf2014-05-06 10:01:27 -04004501 /**
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004502 * Make the largeIcon dark if it's a fake smallIcon (that is,
Dan Sandler26e81cf2014-05-06 10:01:27 -04004503 * if it's grayscale).
4504 */
4505 // TODO: also check bounds, transparency, that sort of thing.
Dan Sandlerd63f9322015-05-06 15:18:49 -04004506 private void processLargeLegacyIcon(Icon largeIcon, RemoteViews contentView) {
4507 if (largeIcon != null && isLegacy()
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004508 && getColorUtil().isGrayscaleIcon(mContext, largeIcon)) {
Selim Cinek65b2e7c2015-10-26 14:11:31 -07004509 // resolve color will fall back to the default when legacy
Adrian Roos4ff3b122016-02-01 12:26:13 -08004510 contentView.setDrawableParameters(R.id.icon, false, -1, resolveContrastColor(),
Dan Sandler190d58d2014-05-15 09:33:39 -04004511 PorterDuff.Mode.SRC_ATOP, -1);
Jorim Jaggi92df1f22014-12-16 19:44:41 +01004512 }
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004513 }
4514
Julia Reynolds10ee1fc2015-11-09 11:04:55 -05004515 private void sanitizeColor() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004516 if (mN.color != COLOR_DEFAULT) {
4517 mN.color |= 0xFF000000; // no alpha for custom colors
Jorim Jaggi74419312014-06-10 20:57:21 +02004518 }
Jorim Jaggi74419312014-06-10 20:57:21 +02004519 }
4520
Adrian Roos4ff3b122016-02-01 12:26:13 -08004521 int resolveContrastColor() {
4522 if (mCachedContrastColorIsFor == mN.color && mCachedContrastColor != COLOR_INVALID) {
4523 return mCachedContrastColor;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004524 }
Adrian Roos4ff3b122016-02-01 12:26:13 -08004525 final int contrasted = NotificationColorUtil.resolveContrastColor(mContext, mN.color);
4526
4527 mCachedContrastColorIsFor = mN.color;
4528 return mCachedContrastColor = contrasted;
Dan Sandler26e81cf2014-05-06 10:01:27 -04004529 }
4530
Adrian Roos487374f2017-01-11 15:48:14 -08004531 int resolveAmbientColor() {
4532 if (mCachedAmbientColorIsFor == mN.color && mCachedAmbientColorIsFor != COLOR_INVALID) {
4533 return mCachedAmbientColor;
4534 }
4535 final int contrasted = NotificationColorUtil.resolveAmbientColor(mContext, mN.color);
4536
4537 mCachedAmbientColorIsFor = mN.color;
4538 return mCachedAmbientColor = contrasted;
4539 }
4540
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01004541 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004542 * Apply the unstyled operations and return a new {@link Notification} object.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004543 * @hide
Joe Onoratocb109a02011-01-18 17:57:41 -08004544 */
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004545 public Notification buildUnstyled() {
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004546 if (mActions.size() > 0) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004547 mN.actions = new Action[mActions.size()];
4548 mActions.toArray(mN.actions);
Daniel Sandlera0a938c2012-03-15 08:42:37 -04004549 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004550 if (!mPersonList.isEmpty()) {
4551 mN.extras.putStringArray(EXTRA_PEOPLE,
4552 mPersonList.toArray(new String[mPersonList.size()]));
Dan Sandler0bf2ed82013-12-21 23:33:41 -06004553 }
Selim Cinek247fa012016-02-18 09:50:48 -08004554 if (mN.bigContentView != null || mN.contentView != null
4555 || mN.headsUpContentView != null) {
4556 mN.extras.putBoolean(EXTRA_CONTAINS_CUSTOM_VIEW, true);
4557 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004558 return mN;
Joe Onorato46439ce2010-11-19 13:56:21 -08004559 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004560
Julia Reynolds3b848122016-02-26 10:45:32 -05004561 /**
4562 * Creates a Builder from an existing notification so further changes can be made.
4563 * @param context The context for your application / activity.
4564 * @param n The notification to create a Builder from.
4565 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004566 public static Notification.Builder recoverBuilder(Context context, Notification n) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02004567 // Re-create notification context so we can access app resources.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004568 ApplicationInfo applicationInfo = n.extras.getParcelable(
4569 EXTRA_BUILDER_APPLICATION_INFO);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004570 Context builderContext;
Julia Reynoldsda303542015-11-23 14:00:20 -05004571 if (applicationInfo != null) {
4572 try {
4573 builderContext = context.createApplicationContext(applicationInfo,
4574 Context.CONTEXT_RESTRICTED);
4575 } catch (NameNotFoundException e) {
4576 Log.e(TAG, "ApplicationInfo " + applicationInfo + " not found");
4577 builderContext = context; // try with our context
4578 }
4579 } else {
4580 builderContext = context; // try with given context
Christoph Studer4600f9b2014-07-22 22:44:43 +02004581 }
4582
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004583 return new Builder(builderContext, n);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004584 }
4585
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004586 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004587 * @deprecated Use {@link #build()} instead.
4588 */
4589 @Deprecated
4590 public Notification getNotification() {
4591 return build();
4592 }
4593
4594 /**
4595 * Combine all of the options that have been set and return a new {@link Notification}
4596 * object.
4597 */
4598 public Notification build() {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004599 // first, add any extras from the calling code
4600 if (mUserExtras != null) {
4601 mN.extras = getAllExtras();
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004602 }
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004603
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004604 mN.creationTime = System.currentTimeMillis();
4605
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004606 // lazy stuff from mContext; see comment in Builder(Context, Notification)
Julia Reynoldsda303542015-11-23 14:00:20 -05004607 Notification.addFieldsFromContext(mContext, mN);
Christoph Studer943aa672014-08-03 20:31:16 +02004608
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004609 buildUnstyled();
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004610
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004611 if (mStyle != null) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004612 mStyle.buildStyled(mN);
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004613 }
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004614
Adrian Roos5081c0d2016-02-26 16:04:19 -08004615 if (mContext.getApplicationInfo().targetSdkVersion < Build.VERSION_CODES.N
4616 && (mStyle == null || !mStyle.displayCustomViewInline())) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004617 if (mN.contentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004618 mN.contentView = createContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004619 mN.extras.putInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT,
4620 mN.contentView.getSequenceNumber());
4621 }
4622 if (mN.bigContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004623 mN.bigContentView = createBigContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004624 if (mN.bigContentView != null) {
4625 mN.extras.putInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT,
4626 mN.bigContentView.getSequenceNumber());
4627 }
4628 }
4629 if (mN.headsUpContentView == null) {
Julia Reynolds3b848122016-02-26 10:45:32 -05004630 mN.headsUpContentView = createHeadsUpContentView();
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004631 if (mN.headsUpContentView != null) {
4632 mN.extras.putInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT,
4633 mN.headsUpContentView.getSequenceNumber());
4634 }
4635 }
4636 }
4637
Julia Reynolds4c0c2022016-02-02 15:11:59 -05004638 if ((mN.defaults & DEFAULT_LIGHTS) != 0) {
4639 mN.flags |= FLAG_SHOW_LIGHTS;
4640 }
4641
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004642 return mN;
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004643 }
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004644
4645 /**
4646 * Apply this Builder to an existing {@link Notification} object.
4647 *
4648 * @hide
4649 */
4650 public Notification buildInto(Notification n) {
Daniel Sandler1a497d32013-04-18 14:52:45 -04004651 build().cloneInto(n, true);
Daniel Sandlerbe6e7e02013-02-01 17:49:11 -05004652 return n;
4653 }
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004654
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004655 /**
Adrian Roos184bfe022016-03-03 13:41:44 -08004656 * Removes RemoteViews that were created for compatibility from {@param n}, if they did not
4657 * change.
4658 *
4659 * @return {@param n}, if no stripping is needed, otherwise a stripped clone of {@param n}.
4660 *
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004661 * @hide
4662 */
Adrian Roos184bfe022016-03-03 13:41:44 -08004663 public static Notification maybeCloneStrippedForDelivery(Notification n) {
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004664 String templateClass = n.extras.getString(EXTRA_TEMPLATE);
Adrian Roos184bfe022016-03-03 13:41:44 -08004665
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004666 // Only strip views for known Styles because we won't know how to
4667 // re-create them otherwise.
Adrian Roos184bfe022016-03-03 13:41:44 -08004668 if (!TextUtils.isEmpty(templateClass)
4669 && getNotificationStyleClass(templateClass) == null) {
4670 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004671 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004672
4673 // Only strip unmodified BuilderRemoteViews.
4674 boolean stripContentView = n.contentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004675 n.extras.getInt(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004676 n.contentView.getSequenceNumber();
4677 boolean stripBigContentView = n.bigContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004678 n.extras.getInt(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004679 n.bigContentView.getSequenceNumber();
4680 boolean stripHeadsUpContentView = n.headsUpContentView instanceof BuilderRemoteViews &&
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004681 n.extras.getInt(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT, -1) ==
Adrian Roos184bfe022016-03-03 13:41:44 -08004682 n.headsUpContentView.getSequenceNumber();
4683
4684 // Nothing to do here, no need to clone.
4685 if (!stripContentView && !stripBigContentView && !stripHeadsUpContentView) {
4686 return n;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004687 }
Adrian Roos184bfe022016-03-03 13:41:44 -08004688
4689 Notification clone = n.clone();
4690 if (stripContentView) {
4691 clone.contentView = null;
4692 clone.extras.remove(EXTRA_REBUILD_CONTENT_VIEW_ACTION_COUNT);
4693 }
4694 if (stripBigContentView) {
4695 clone.bigContentView = null;
4696 clone.extras.remove(EXTRA_REBUILD_BIG_CONTENT_VIEW_ACTION_COUNT);
4697 }
4698 if (stripHeadsUpContentView) {
4699 clone.headsUpContentView = null;
4700 clone.extras.remove(EXTRA_REBUILD_HEADS_UP_CONTENT_VIEW_ACTION_COUNT);
4701 }
4702 return clone;
Julia Reynoldsd4ea7412016-02-17 14:00:56 -05004703 }
4704
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004705 private int getBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004706 return R.layout.notification_template_material_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004707 }
4708
4709 private int getBigBaseLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004710 return R.layout.notification_template_material_big_base;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004711 }
4712
4713 private int getBigPictureLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004714 return R.layout.notification_template_material_big_picture;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004715 }
4716
4717 private int getBigTextLayoutResource() {
Jorim Jaggi445d3c02014-08-19 22:33:42 +02004718 return R.layout.notification_template_material_big_text;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004719 }
4720
4721 private int getInboxLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004722 return R.layout.notification_template_material_inbox;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004723 }
4724
Adrian Roosc1a80b02016-04-05 14:54:55 -07004725 private int getMessagingLayoutResource() {
4726 return R.layout.notification_template_material_messaging;
4727 }
4728
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004729 private int getActionLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004730 return R.layout.notification_material_action;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004731 }
4732
Selim Cinek06e9e1f2016-07-08 17:14:16 -07004733 private int getEmphasizedActionLayoutResource() {
4734 return R.layout.notification_material_action_emphasized;
4735 }
4736
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004737 private int getActionTombstoneLayoutResource() {
Alan Viverette3cb07a462014-06-06 14:19:53 -07004738 return R.layout.notification_material_action_tombstone;
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01004739 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004740
4741 private int getBackgroundColor() {
4742 if (isColorized()) {
4743 return mN.color;
4744 } else {
4745 return COLOR_DEFAULT;
4746 }
4747 }
4748
4749 private boolean isColorized() {
4750 return mN.isColorized();
4751 }
Selim Cinek99104832017-01-25 14:47:33 -08004752
4753 private boolean textColorsNeedInversion() {
4754 if (mStyle == null || !MediaStyle.class.equals(mStyle.getClass())) {
4755 return false;
4756 }
4757 int targetSdkVersion = mContext.getApplicationInfo().targetSdkVersion;
4758 return targetSdkVersion > Build.VERSION_CODES.M
4759 && targetSdkVersion < Build.VERSION_CODES.O;
4760 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004761 }
4762
4763 /**
4764 * @return whether this notification is ongoing and can't be dismissed by the user.
4765 */
4766 private boolean isOngoing() {
4767 final int ongoingFlags = Notification.FLAG_FOREGROUND_SERVICE
4768 | Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;
4769 return (flags & ongoingFlags) != 0;
4770 }
4771
4772 /**
Selim Cinek99104832017-01-25 14:47:33 -08004773 * @return whether this notification has a media session attached
4774 * @hide
4775 */
4776 public boolean hasMediaSession() {
4777 return extras.getParcelable(Notification.EXTRA_MEDIA_SESSION) != null;
4778 }
4779
4780 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08004781 * @return the style class of this notification
4782 * @hide
4783 */
4784 public Class<? extends Notification.Style> getNotificationStyle() {
4785 String templateClass = extras.getString(Notification.EXTRA_TEMPLATE);
4786
4787 if (!TextUtils.isEmpty(templateClass)) {
4788 return Notification.getNotificationStyleClass(templateClass);
4789 }
4790 return null;
4791 }
4792
4793 /**
Selim Cinek99104832017-01-25 14:47:33 -08004794 * @return true if this notification is colorized. This also factors in whether the
Selim Cinek7b9605b2017-01-19 17:36:00 -08004795 * notification is ongoing.
4796 *
4797 * @hide
4798 */
4799 public boolean isColorized() {
Selim Cinek99104832017-01-25 14:47:33 -08004800 Class<? extends Style> style = getNotificationStyle();
4801 if (MediaStyle.class.equals(style)) {
4802 Boolean colorized = (Boolean) extras.get(EXTRA_COLORIZED);
4803 if ((colorized == null || colorized) && hasMediaSession()) {
4804 return true;
4805 }
4806 } else if (DecoratedMediaCustomViewStyle.class.equals(style)) {
4807 if (extras.getBoolean(EXTRA_COLORIZED) && hasMediaSession()) {
4808 return true;
4809 }
4810 }
Selim Cinek7b9605b2017-01-19 17:36:00 -08004811 return extras.getBoolean(EXTRA_COLORIZED) && isOngoing();
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004812 }
4813
Selim Cinek279fa862016-06-14 10:57:25 -07004814 private boolean hasLargeIcon() {
4815 return mLargeIcon != null || largeIcon != null;
4816 }
4817
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004818 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004819 * @return true if the notification will show the time; false otherwise
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004820 * @hide
4821 */
Selim Cinekc2c0b042016-05-18 17:13:46 -07004822 public boolean showsTime() {
Selim Cinekb85f36fd2016-04-20 18:46:36 -07004823 return when != 0 && extras.getBoolean(EXTRA_SHOW_WHEN);
4824 }
4825
4826 /**
Selim Cinekc2c0b042016-05-18 17:13:46 -07004827 * @return true if the notification will show a chronometer; false otherwise
4828 * @hide
4829 */
4830 public boolean showsChronometer() {
4831 return when != 0 && extras.getBoolean(EXTRA_SHOW_CHRONOMETER);
4832 }
4833
4834 /**
Julia Reynolds4a02afb2016-12-13 13:39:52 -05004835 * @hide
4836 */
4837 @SystemApi
4838 public static Class<? extends Style> getNotificationStyleClass(String templateClass) {
4839 Class<? extends Style>[] classes = new Class[] {
4840 BigTextStyle.class, BigPictureStyle.class, InboxStyle.class, MediaStyle.class,
4841 DecoratedCustomViewStyle.class, DecoratedMediaCustomViewStyle.class,
4842 MessagingStyle.class };
4843 for (Class<? extends Style> innerClass : classes) {
4844 if (templateClass.equals(innerClass.getName())) {
4845 return innerClass;
4846 }
4847 }
4848 return null;
4849 }
4850
4851 /**
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004852 * An object that can apply a rich notification style to a {@link Notification.Builder}
4853 * object.
4854 */
Griff Hazendfcb0802014-02-11 12:00:00 -08004855 public static abstract class Style {
Chris Wrend6297db2012-05-03 16:20:13 -04004856 private CharSequence mBigContentTitle;
Jorim Jaggi457a10d2014-09-08 16:18:23 +02004857
4858 /**
4859 * @hide
4860 */
4861 protected CharSequence mSummaryText = null;
4862
4863 /**
4864 * @hide
4865 */
4866 protected boolean mSummaryTextSet = false;
Chris Wrend6297db2012-05-03 16:20:13 -04004867
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004868 protected Builder mBuilder;
4869
Chris Wrend6297db2012-05-03 16:20:13 -04004870 /**
4871 * Overrides ContentTitle in the big form of the template.
4872 * This defaults to the value passed to setContentTitle().
4873 */
4874 protected void internalSetBigContentTitle(CharSequence title) {
4875 mBigContentTitle = title;
4876 }
4877
4878 /**
4879 * Set the first line of text after the detail section in the big form of the template.
4880 */
4881 protected void internalSetSummaryText(CharSequence cs) {
4882 mSummaryText = cs;
Daniel Sandler619738c2012-06-07 16:33:08 -04004883 mSummaryTextSet = true;
Chris Wrend6297db2012-05-03 16:20:13 -04004884 }
4885
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004886 public void setBuilder(Builder builder) {
4887 if (mBuilder != builder) {
4888 mBuilder = builder;
Daniel Sandlerc08dea22012-06-28 08:35:24 -07004889 if (mBuilder != null) {
4890 mBuilder.setStyle(this);
4891 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004892 }
4893 }
4894
Chris Wrend6297db2012-05-03 16:20:13 -04004895 protected void checkBuilder() {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004896 if (mBuilder == null) {
4897 throw new IllegalArgumentException("Style requires a valid Builder object");
4898 }
Chris Wrenfbd96ba2012-05-01 12:03:58 -04004899 }
Chris Wrend6297db2012-05-03 16:20:13 -04004900
4901 protected RemoteViews getStandardView(int layoutId) {
4902 checkBuilder();
4903
Christoph Studer4600f9b2014-07-22 22:44:43 +02004904 // Nasty.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004905 CharSequence oldBuilderContentTitle =
4906 mBuilder.getAllExtras().getCharSequence(EXTRA_TITLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004907 if (mBigContentTitle != null) {
4908 mBuilder.setContentTitle(mBigContentTitle);
4909 }
4910
Chris Wrend6297db2012-05-03 16:20:13 -04004911 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(layoutId);
4912
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004913 mBuilder.getAllExtras().putCharSequence(EXTRA_TITLE, oldBuilderContentTitle);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004914
Chris Wrend6297db2012-05-03 16:20:13 -04004915 if (mBigContentTitle != null && mBigContentTitle.equals("")) {
4916 contentView.setViewVisibility(R.id.line1, View.GONE);
Chris Wren67dc9a02012-05-16 01:03:20 -04004917 } else {
4918 contentView.setViewVisibility(R.id.line1, View.VISIBLE);
Chris Wrend6297db2012-05-03 16:20:13 -04004919 }
4920
Chris Wrend6297db2012-05-03 16:20:13 -04004921 return contentView;
4922 }
4923
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004924 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08004925 * Construct a Style-specific RemoteViews for the collapsed notification layout.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004926 * The default implementation has nothing additional to add.
Selim Cinek7d1009b2017-01-25 15:28:28 -08004927 *
4928 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004929 * @hide
4930 */
Selim Cinek7d1009b2017-01-25 15:28:28 -08004931 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004932 return null;
4933 }
4934
4935 /**
4936 * Construct a Style-specific RemoteViews for the final big notification layout.
4937 * @hide
4938 */
4939 public RemoteViews makeBigContentView() {
4940 return null;
4941 }
4942
4943 /**
4944 * Construct a Style-specific RemoteViews for the final HUN layout.
Selim Cinek87ed69b2017-02-09 15:59:43 -08004945 *
4946 * @param increasedHeight true if this layout be created with an increased height.
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004947 * @hide
4948 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08004949 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004950 return null;
4951 }
4952
4953 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004954 * Apply any style-specific extras to this notification before shipping it out.
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004955 * @hide
4956 */
4957 public void addExtras(Bundle extras) {
4958 if (mSummaryTextSet) {
4959 extras.putCharSequence(EXTRA_SUMMARY_TEXT, mSummaryText);
4960 }
4961 if (mBigContentTitle != null) {
4962 extras.putCharSequence(EXTRA_TITLE_BIG, mBigContentTitle);
4963 }
Chris Wren91ad5632013-06-05 15:05:57 -04004964 extras.putString(EXTRA_TEMPLATE, this.getClass().getName());
Daniel Sandlerf45564e2013-04-15 15:05:08 -04004965 }
4966
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004967 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004968 * Reconstruct the internal state of this Style object from extras.
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004969 * @hide
4970 */
Christoph Studer4600f9b2014-07-22 22:44:43 +02004971 protected void restoreFromExtras(Bundle extras) {
4972 if (extras.containsKey(EXTRA_SUMMARY_TEXT)) {
4973 mSummaryText = extras.getCharSequence(EXTRA_SUMMARY_TEXT);
4974 mSummaryTextSet = true;
4975 }
4976 if (extras.containsKey(EXTRA_TITLE_BIG)) {
4977 mBigContentTitle = extras.getCharSequence(EXTRA_TITLE_BIG);
4978 }
4979 }
4980
4981
4982 /**
4983 * @hide
4984 */
4985 public Notification buildStyled(Notification wip) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04004986 addExtras(wip.extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02004987 return wip;
4988 }
4989
Daniel Sandler0ec46202015-06-24 01:27:05 -04004990 /**
4991 * @hide
4992 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07004993 public void purgeResources() {}
4994
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04004995 /**
4996 * Calls {@link android.app.Notification.Builder#build()} on the Builder this Style is
4997 * attached to.
4998 *
4999 * @return the fully constructed Notification.
5000 */
5001 public Notification build() {
5002 checkBuilder();
5003 return mBuilder.build();
5004 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02005005
5006 /**
5007 * @hide
5008 * @return true if the style positions the progress bar on the second line; false if the
5009 * style hides the progress bar
5010 */
5011 protected boolean hasProgress() {
5012 return true;
5013 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005014
5015 /**
5016 * @hide
5017 * @return Whether we should put the summary be put into the notification header
5018 */
5019 public boolean hasSummaryInHeader() {
5020 return true;
5021 }
Selim Cinek593610c2016-02-16 18:42:57 -08005022
5023 /**
5024 * @hide
5025 * @return Whether custom content views are displayed inline in the style
5026 */
5027 public boolean displayCustomViewInline() {
5028 return false;
5029 }
Joe Onorato46439ce2010-11-19 13:56:21 -08005030 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005031
5032 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005033 * Helper class for generating large-format notifications that include a large image attachment.
Joe Malin8d40d042012-11-05 11:36:40 -08005034 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005035 * Here's how you'd set the <code>BigPictureStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005036 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005037 * Notification notif = new Notification.Builder(mContext)
5038 * .setContentTitle(&quot;New photo from &quot; + sender.toString())
5039 * .setContentText(subject)
5040 * .setSmallIcon(R.drawable.new_post)
5041 * .setLargeIcon(aBitmap)
5042 * .setStyle(new Notification.BigPictureStyle()
5043 * .bigPicture(aBigBitmap))
5044 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005045 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005046 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005047 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005048 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005049 public static class BigPictureStyle extends Style {
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005050 private Bitmap mPicture;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005051 private Icon mBigLargeIcon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005052 private boolean mBigLargeIconSet = false;
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005053
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005054 public BigPictureStyle() {
5055 }
5056
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005057 /**
5058 * @deprecated use {@code BigPictureStyle()}.
5059 */
5060 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005061 public BigPictureStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005062 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005063 }
5064
Chris Wrend6297db2012-05-03 16:20:13 -04005065 /**
5066 * Overrides ContentTitle in the big form of the template.
5067 * This defaults to the value passed to setContentTitle().
5068 */
5069 public BigPictureStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005070 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005071 return this;
5072 }
5073
5074 /**
5075 * Set the first line of text after the detail section in the big form of the template.
5076 */
5077 public BigPictureStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005078 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005079 return this;
5080 }
5081
Chris Wren0bd664d2012-08-01 13:56:56 -04005082 /**
5083 * Provide the bitmap to be used as the payload for the BigPicture notification.
5084 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005085 public BigPictureStyle bigPicture(Bitmap b) {
5086 mPicture = b;
5087 return this;
5088 }
5089
Chris Wren3745a3d2012-05-22 15:11:52 -04005090 /**
Chris Wren3745a3d2012-05-22 15:11:52 -04005091 * Override the large icon when the big notification is shown.
5092 */
5093 public BigPictureStyle bigLargeIcon(Bitmap b) {
Dan Sandlerd63f9322015-05-06 15:18:49 -04005094 return bigLargeIcon(b != null ? Icon.createWithBitmap(b) : null);
5095 }
5096
5097 /**
5098 * Override the large icon when the big notification is shown.
5099 */
5100 public BigPictureStyle bigLargeIcon(Icon icon) {
Chris Wren3745a3d2012-05-22 15:11:52 -04005101 mBigLargeIconSet = true;
Dan Sandlerd63f9322015-05-06 15:18:49 -04005102 mBigLargeIcon = icon;
Chris Wren3745a3d2012-05-22 15:11:52 -04005103 return this;
5104 }
5105
Riley Andrews0394a0c2015-11-03 23:36:52 -08005106 /** @hide */
5107 public static final int MIN_ASHMEM_BITMAP_SIZE = 128 * (1 << 10);
5108
Daniel Sandler0ec46202015-06-24 01:27:05 -04005109 /**
5110 * @hide
5111 */
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005112 @Override
5113 public void purgeResources() {
5114 super.purgeResources();
Riley Andrews8cee7c12015-11-01 23:36:04 -08005115 if (mPicture != null &&
5116 mPicture.isMutable() &&
Riley Andrews0394a0c2015-11-03 23:36:52 -08005117 mPicture.getAllocationByteCount() >= MIN_ASHMEM_BITMAP_SIZE) {
Jorim Jaggia0d58ae2015-06-03 11:48:13 -07005118 mPicture = mPicture.createAshmemBitmap();
5119 }
5120 if (mBigLargeIcon != null) {
5121 mBigLargeIcon.convertToAshmem();
5122 }
5123 }
Christoph Studer5c510ee2014-12-15 16:32:27 +01005124
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005125 /**
5126 * @hide
5127 */
5128 public RemoteViews makeBigContentView() {
5129 // Replace mN.mLargeIcon with mBigLargeIcon if mBigLargeIconSet
Christoph Studer5c510ee2014-12-15 16:32:27 +01005130 // This covers the following cases:
5131 // 1. mBigLargeIconSet -> mBigLargeIcon (null or non-null) applies, overrides
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005132 // mN.mLargeIcon
5133 // 2. !mBigLargeIconSet -> mN.mLargeIcon applies
Dan Sandlerd63f9322015-05-06 15:18:49 -04005134 Icon oldLargeIcon = null;
Selim Cineke99acb22016-08-04 12:55:48 -07005135 Bitmap largeIconLegacy = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005136 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005137 oldLargeIcon = mBuilder.mN.mLargeIcon;
5138 mBuilder.mN.mLargeIcon = mBigLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005139 // The legacy largeIcon might not allow us to clear the image, as it's taken in
5140 // replacement if the other one is null. Because we're restoring these legacy icons
5141 // for old listeners, this is in general non-null.
5142 largeIconLegacy = mBuilder.mN.largeIcon;
5143 mBuilder.mN.largeIcon = null;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005144 }
5145
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005146 RemoteViews contentView = getStandardView(mBuilder.getBigPictureLayoutResource());
Selim Cinek03d0d652015-11-13 13:18:09 -05005147 if (mSummaryTextSet) {
5148 contentView.setTextViewText(R.id.text, mBuilder.processLegacyText(mSummaryText));
Selim Cinek7b9605b2017-01-19 17:36:00 -08005149 mBuilder.setTextViewColorSecondary(contentView, R.id.text);
Selim Cinekc848c3a2016-01-13 15:27:30 -08005150 contentView.setViewVisibility(R.id.text, View.VISIBLE);
Selim Cinek03d0d652015-11-13 13:18:09 -05005151 }
Selim Cinek279fa862016-06-14 10:57:25 -07005152 mBuilder.setContentMinHeight(contentView, mBuilder.mN.hasLargeIcon());
Selim Cinek53e64a42015-11-16 10:40:56 -08005153
Christoph Studer5c510ee2014-12-15 16:32:27 +01005154 if (mBigLargeIconSet) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005155 mBuilder.mN.mLargeIcon = oldLargeIcon;
Selim Cineke99acb22016-08-04 12:55:48 -07005156 mBuilder.mN.largeIcon = largeIconLegacy;
Christoph Studer5c510ee2014-12-15 16:32:27 +01005157 }
5158
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005159 contentView.setImageViewBitmap(R.id.big_picture, mPicture);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005160 return contentView;
5161 }
5162
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005163 /**
5164 * @hide
5165 */
5166 public void addExtras(Bundle extras) {
5167 super.addExtras(extras);
5168
5169 if (mBigLargeIconSet) {
5170 extras.putParcelable(EXTRA_LARGE_ICON_BIG, mBigLargeIcon);
5171 }
5172 extras.putParcelable(EXTRA_PICTURE, mPicture);
5173 }
5174
Daniel Sandlercf1d39b2013-09-23 13:35:35 -04005175 /**
5176 * @hide
5177 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005178 @Override
Christoph Studer4600f9b2014-07-22 22:44:43 +02005179 protected void restoreFromExtras(Bundle extras) {
5180 super.restoreFromExtras(extras);
5181
5182 if (extras.containsKey(EXTRA_LARGE_ICON_BIG)) {
Christoph Studer5c510ee2014-12-15 16:32:27 +01005183 mBigLargeIconSet = true;
Christoph Studer4600f9b2014-07-22 22:44:43 +02005184 mBigLargeIcon = extras.getParcelable(EXTRA_LARGE_ICON_BIG);
Chris Wren3745a3d2012-05-22 15:11:52 -04005185 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02005186 mPicture = extras.getParcelable(EXTRA_PICTURE);
5187 }
Selim Cinek03d0d652015-11-13 13:18:09 -05005188
5189 /**
5190 * @hide
5191 */
5192 @Override
5193 public boolean hasSummaryInHeader() {
5194 return false;
5195 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005196 }
5197
5198 /**
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005199 * Helper class for generating large-format notifications that include a lot of text.
Joe Malin8d40d042012-11-05 11:36:40 -08005200 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005201 * Here's how you'd set the <code>BigTextStyle</code> on a notification:
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005202 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005203 * Notification notif = new Notification.Builder(mContext)
5204 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
5205 * .setContentText(subject)
5206 * .setSmallIcon(R.drawable.new_mail)
5207 * .setLargeIcon(aBitmap)
5208 * .setStyle(new Notification.BigTextStyle()
5209 * .bigText(aVeryLongString))
5210 * .build();
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005211 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005212 *
Daniel Sandler4dfbe832012-04-11 14:51:46 -04005213 * @see Notification#bigContentView
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005214 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005215 public static class BigTextStyle extends Style {
Jorim Jaggi457a10d2014-09-08 16:18:23 +02005216
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005217 private CharSequence mBigText;
5218
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005219 public BigTextStyle() {
5220 }
5221
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005222 /**
5223 * @deprecated use {@code BigTextStyle()}.
5224 */
5225 @Deprecated
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005226 public BigTextStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005227 setBuilder(builder);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005228 }
5229
Chris Wrend6297db2012-05-03 16:20:13 -04005230 /**
5231 * Overrides ContentTitle in the big form of the template.
5232 * This defaults to the value passed to setContentTitle().
5233 */
5234 public BigTextStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005235 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005236 return this;
5237 }
5238
5239 /**
5240 * Set the first line of text after the detail section in the big form of the template.
5241 */
5242 public BigTextStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005243 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005244 return this;
5245 }
5246
Chris Wren0bd664d2012-08-01 13:56:56 -04005247 /**
5248 * Provide the longer text to be displayed in the big form of the
5249 * template in place of the content text.
5250 */
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005251 public BigTextStyle bigText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005252 mBigText = safeCharSequence(cs);
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005253 return this;
5254 }
5255
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005256 /**
5257 * @hide
5258 */
5259 public void addExtras(Bundle extras) {
5260 super.addExtras(extras);
5261
Christoph Studer4600f9b2014-07-22 22:44:43 +02005262 extras.putCharSequence(EXTRA_BIG_TEXT, mBigText);
5263 }
5264
5265 /**
5266 * @hide
5267 */
5268 @Override
5269 protected void restoreFromExtras(Bundle extras) {
5270 super.restoreFromExtras(extras);
5271
5272 mBigText = extras.getCharSequence(EXTRA_BIG_TEXT);
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005273 }
5274
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005275 /**
Selim Cinek7d1009b2017-01-25 15:28:28 -08005276 * @param increasedHeight true if this layout be created with an increased height.
5277 *
5278 * @hide
5279 */
5280 @Override
5281 public RemoteViews makeContentView(boolean increasedHeight) {
5282 if (increasedHeight) {
5283 ArrayList<Action> actions = mBuilder.mActions;
5284 mBuilder.mActions = new ArrayList<>();
5285 RemoteViews remoteViews = makeBigContentView();
5286 mBuilder.mActions = actions;
5287 return remoteViews;
5288 }
5289 return super.makeContentView(increasedHeight);
5290 }
5291
5292 /**
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005293 * @hide
5294 */
Selim Cinek87ed69b2017-02-09 15:59:43 -08005295 @Override
5296 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
5297 if (increasedHeight && mBuilder.mActions.size() > 0) {
5298 return makeBigContentView();
5299 }
5300 return super.makeHeadsUpContentView(increasedHeight);
5301 }
5302
5303 /**
5304 * @hide
5305 */
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005306 public RemoteViews makeBigContentView() {
Christoph Studer4600f9b2014-07-22 22:44:43 +02005307
5308 // Nasty
Selim Cinek75998782016-04-26 10:39:17 -07005309 CharSequence text = mBuilder.getAllExtras().getCharSequence(EXTRA_TEXT);
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005310 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Daniel Sandler916ad912012-06-13 12:17:07 -04005311
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01005312 RemoteViews contentView = getStandardView(mBuilder.getBigTextLayoutResource());
Joe Malin8d40d042012-11-05 11:36:40 -08005313
Selim Cinek75998782016-04-26 10:39:17 -07005314 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, text);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005315
Selim Cinek3a2c4b92015-12-17 17:01:17 -08005316 CharSequence bigTextText = mBuilder.processLegacyText(mBigText);
Selim Cinek75998782016-04-26 10:39:17 -07005317 if (TextUtils.isEmpty(bigTextText)) {
5318 // In case the bigtext is null / empty fall back to the normal text to avoid a weird
5319 // experience
5320 bigTextText = mBuilder.processLegacyText(text);
5321 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07005322 applyBigTextContentView(mBuilder, contentView, bigTextText);
Selim Cinek4fb12d32015-11-19 18:10:48 -08005323
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005324 return contentView;
5325 }
5326
Adrian Roosb1f427c2016-05-26 12:27:15 -07005327 static void applyBigTextContentView(Builder builder,
5328 RemoteViews contentView, CharSequence bigTextText) {
5329 contentView.setTextViewText(R.id.big_text, bigTextText);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005330 builder.setTextViewColorSecondary(contentView, R.id.big_text);
Adrian Roosb1f427c2016-05-26 12:27:15 -07005331 contentView.setViewVisibility(R.id.big_text,
5332 TextUtils.isEmpty(bigTextText) ? View.GONE : View.VISIBLE);
Selim Cinek279fa862016-06-14 10:57:25 -07005333 contentView.setBoolean(R.id.big_text, "setHasImage", builder.mN.hasLargeIcon());
Adrian Roosb1f427c2016-05-26 12:27:15 -07005334 }
Daniel Sandlerf3b73432012-03-27 15:01:25 -04005335 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04005336
5337 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005338 * Helper class for generating large-format notifications that include multiple back-and-forth
5339 * messages of varying types between any number of people.
5340 *
5341 * <br>
5342 * If the platform does not provide large-format notifications, this method has no effect. The
5343 * user will always see the normal notification view.
5344 * <br>
5345 * This class is a "rebuilder": It attaches to a Builder object and modifies its behavior, like
5346 * so:
5347 * <pre class="prettyprint">
5348 *
5349 * Notification noti = new Notification.Builder()
5350 * .setContentTitle(&quot;2 new messages wtih &quot; + sender.toString())
5351 * .setContentText(subject)
5352 * .setSmallIcon(R.drawable.new_message)
5353 * .setLargeIcon(aBitmap)
5354 * .setStyle(new Notification.MessagingStyle(resources.getString(R.string.reply_name))
5355 * .addMessage(messages[0].getText(), messages[0].getTime(), messages[0].getSender())
5356 * .addMessage(messages[1].getText(), messages[1].getTime(), messages[1].getSender()))
5357 * .build();
5358 * </pre>
5359 */
5360 public static class MessagingStyle extends Style {
5361
5362 /**
5363 * The maximum number of messages that will be retained in the Notification itself (the
5364 * number displayed is up to the platform).
5365 */
5366 public static final int MAXIMUM_RETAINED_MESSAGES = 25;
5367
5368 CharSequence mUserDisplayName;
5369 CharSequence mConversationTitle;
Alex Hillsd9b04d92016-04-11 16:38:16 -04005370 List<Message> mMessages = new ArrayList<>();
Adrian Roos437cd562017-01-18 15:47:03 -08005371 List<Message> mHistoricMessages = new ArrayList<>();
Alex Hillsfc737de2016-03-23 17:33:02 -04005372
5373 MessagingStyle() {
5374 }
5375
5376 /**
Alex Hillsfd590442016-10-07 09:52:44 -04005377 * @param userDisplayName Required - the name to be displayed for any replies sent by the
5378 * user before the posting app reposts the notification with those messages after they've
5379 * been actually sent and in previous messages sent by the user added in
Alex Hillsfc737de2016-03-23 17:33:02 -04005380 * {@link #addMessage(Notification.MessagingStyle.Message)}
5381 */
Alex Hillsfd590442016-10-07 09:52:44 -04005382 public MessagingStyle(@NonNull CharSequence userDisplayName) {
Alex Hillsfc737de2016-03-23 17:33:02 -04005383 mUserDisplayName = userDisplayName;
5384 }
5385
5386 /**
5387 * Returns the name to be displayed for any replies sent by the user
5388 */
5389 public CharSequence getUserDisplayName() {
5390 return mUserDisplayName;
5391 }
5392
5393 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005394 * Sets the title to be displayed on this conversation. This should only be used for
5395 * group messaging and left unset for one-on-one conversations.
5396 * @param conversationTitle
5397 * @return this object for method chaining.
5398 */
5399 public MessagingStyle setConversationTitle(CharSequence conversationTitle) {
5400 mConversationTitle = conversationTitle;
5401 return this;
5402 }
5403
5404 /**
5405 * Return the title to be displayed on this conversation. Can be <code>null</code> and
5406 * should be for one-on-one conversations
5407 */
5408 public CharSequence getConversationTitle() {
5409 return mConversationTitle;
5410 }
5411
5412 /**
5413 * Adds a message for display by this notification. Convenience call for a simple
5414 * {@link Message} in {@link #addMessage(Notification.MessagingStyle.Message)}.
5415 * @param text A {@link CharSequence} to be displayed as the message content
5416 * @param timestamp Time at which the message arrived
5417 * @param sender A {@link CharSequence} to be used for displaying the name of the
5418 * sender. Should be <code>null</code> for messages by the current user, in which case
5419 * the platform will insert {@link #getUserDisplayName()}.
5420 * Should be unique amongst all individuals in the conversation, and should be
5421 * consistent during re-posts of the notification.
5422 *
5423 * @see Message#Message(CharSequence, long, CharSequence)
5424 *
5425 * @return this object for method chaining
5426 */
5427 public MessagingStyle addMessage(CharSequence text, long timestamp, CharSequence sender) {
Adrian Roos437cd562017-01-18 15:47:03 -08005428 return addMessage(new Message(text, timestamp, sender));
Alex Hillsfc737de2016-03-23 17:33:02 -04005429 }
5430
5431 /**
5432 * Adds a {@link Message} for display in this notification.
Adrian Roos437cd562017-01-18 15:47:03 -08005433 *
5434 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5435 * the newest last.
5436 *
Alex Hillsfc737de2016-03-23 17:33:02 -04005437 * @param message The {@link Message} to be displayed
5438 * @return this object for method chaining
5439 */
5440 public MessagingStyle addMessage(Message message) {
5441 mMessages.add(message);
5442 if (mMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5443 mMessages.remove(0);
5444 }
5445 return this;
5446 }
5447
5448 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005449 * Adds a {@link Message} for historic context in this notification.
5450 *
5451 * <p>Messages should be added as historic if they are not the main subject of the
5452 * notification but may give context to a conversation. The system may choose to present
5453 * them only when relevant, e.g. when replying to a message through a {@link RemoteInput}.
5454 *
5455 * <p>The messages should be added in chronologic order, i.e. the oldest first,
5456 * the newest last.
5457 *
5458 * @param message The historic {@link Message} to be added
5459 * @return this object for method chaining
5460 */
5461 public MessagingStyle addHistoricMessage(Message message) {
5462 mHistoricMessages.add(message);
5463 if (mHistoricMessages.size() > MAXIMUM_RETAINED_MESSAGES) {
5464 mHistoricMessages.remove(0);
5465 }
5466 return this;
5467 }
5468
5469 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005470 * Gets the list of {@code Message} objects that represent the notification
5471 */
5472 public List<Message> getMessages() {
5473 return mMessages;
5474 }
5475
5476 /**
Adrian Roos437cd562017-01-18 15:47:03 -08005477 * Gets the list of historic {@code Message}s in the notification.
5478 */
5479 public List<Message> getHistoricMessages() {
5480 return mHistoricMessages;
5481 }
5482
5483 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005484 * @hide
5485 */
5486 @Override
5487 public void addExtras(Bundle extras) {
5488 super.addExtras(extras);
5489 if (mUserDisplayName != null) {
5490 extras.putCharSequence(EXTRA_SELF_DISPLAY_NAME, mUserDisplayName);
5491 }
5492 if (mConversationTitle != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005493 extras.putCharSequence(EXTRA_CONVERSATION_TITLE, mConversationTitle);
Alex Hillsfc737de2016-03-23 17:33:02 -04005494 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005495 if (!mMessages.isEmpty()) { extras.putParcelableArray(EXTRA_MESSAGES,
5496 Message.getBundleArrayForMessages(mMessages));
Alex Hillsfc737de2016-03-23 17:33:02 -04005497 }
Adrian Roos437cd562017-01-18 15:47:03 -08005498 if (!mHistoricMessages.isEmpty()) { extras.putParcelableArray(EXTRA_HISTORIC_MESSAGES,
5499 Message.getBundleArrayForMessages(mHistoricMessages));
5500 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07005501
5502 fixTitleAndTextExtras(extras);
5503 }
5504
5505 private void fixTitleAndTextExtras(Bundle extras) {
5506 Message m = findLatestIncomingMessage();
5507 CharSequence text = (m == null) ? null : m.mText;
5508 CharSequence sender = m == null ? null
5509 : TextUtils.isEmpty(m.mSender) ? mUserDisplayName : m.mSender;
5510 CharSequence title;
5511 if (!TextUtils.isEmpty(mConversationTitle)) {
5512 if (!TextUtils.isEmpty(sender)) {
5513 BidiFormatter bidi = BidiFormatter.getInstance();
5514 title = mBuilder.mContext.getString(
5515 com.android.internal.R.string.notification_messaging_title_template,
5516 bidi.unicodeWrap(mConversationTitle), bidi.unicodeWrap(m.mSender));
5517 } else {
5518 title = mConversationTitle;
5519 }
5520 } else {
5521 title = sender;
5522 }
5523
5524 if (title != null) {
5525 extras.putCharSequence(EXTRA_TITLE, title);
5526 }
5527 if (text != null) {
5528 extras.putCharSequence(EXTRA_TEXT, text);
5529 }
Alex Hillsfc737de2016-03-23 17:33:02 -04005530 }
5531
5532 /**
5533 * @hide
5534 */
5535 @Override
5536 protected void restoreFromExtras(Bundle extras) {
5537 super.restoreFromExtras(extras);
5538
5539 mMessages.clear();
Adrian Roos437cd562017-01-18 15:47:03 -08005540 mHistoricMessages.clear();
Adrian Roos96b7e202016-05-17 13:50:38 -07005541 mUserDisplayName = extras.getCharSequence(EXTRA_SELF_DISPLAY_NAME);
5542 mConversationTitle = extras.getCharSequence(EXTRA_CONVERSATION_TITLE);
Adrian Roos437cd562017-01-18 15:47:03 -08005543 Parcelable[] messages = extras.getParcelableArray(EXTRA_MESSAGES);
5544 if (messages != null && messages instanceof Parcelable[]) {
5545 mMessages = Message.getMessagesFromBundleArray(messages);
5546 }
5547 Parcelable[] histMessages = extras.getParcelableArray(EXTRA_HISTORIC_MESSAGES);
5548 if (histMessages != null && histMessages instanceof Parcelable[]) {
5549 mHistoricMessages = Message.getMessagesFromBundleArray(histMessages);
Alex Hillsfc737de2016-03-23 17:33:02 -04005550 }
5551 }
5552
5553 /**
5554 * @hide
5555 */
Adrian Roosc1a80b02016-04-05 14:54:55 -07005556 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08005557 public RemoteViews makeContentView(boolean increasedHeight) {
5558 if (!increasedHeight) {
5559 Message m = findLatestIncomingMessage();
5560 CharSequence title = mConversationTitle != null
5561 ? mConversationTitle
5562 : (m == null) ? null : m.mSender;
5563 CharSequence text = (m == null)
5564 ? null
5565 : mConversationTitle != null ? makeMessageLine(m, mBuilder) : m.mText;
Alex Hillsfc737de2016-03-23 17:33:02 -04005566
Selim Cinek7d1009b2017-01-25 15:28:28 -08005567 return mBuilder.applyStandardTemplate(mBuilder.getBaseLayoutResource(),
5568 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
5569 } else {
5570 ArrayList<Action> actions = mBuilder.mActions;
5571 mBuilder.mActions = new ArrayList<>();
5572 RemoteViews remoteViews = makeBigContentView();
5573 mBuilder.mActions = actions;
5574 return remoteViews;
5575 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07005576 }
5577
5578 private Message findLatestIncomingMessage() {
5579 for (int i = mMessages.size() - 1; i >= 0; i--) {
5580 Message m = mMessages.get(i);
5581 // Incoming messages have a non-empty sender.
5582 if (!TextUtils.isEmpty(m.mSender)) {
5583 return m;
5584 }
5585 }
Adrian Roos33fbd2c2016-05-27 15:35:28 -07005586 if (!mMessages.isEmpty()) {
5587 // No incoming messages, fall back to outgoing message
5588 return mMessages.get(mMessages.size() - 1);
5589 }
Adrian Roosc1a80b02016-04-05 14:54:55 -07005590 return null;
5591 }
5592
5593 /**
5594 * @hide
5595 */
5596 @Override
5597 public RemoteViews makeBigContentView() {
5598 CharSequence title = !TextUtils.isEmpty(super.mBigContentTitle)
5599 ? super.mBigContentTitle
5600 : mConversationTitle;
5601 boolean hasTitle = !TextUtils.isEmpty(title);
5602
Adrian Roosfeafa052016-06-01 17:09:45 -07005603 if (mMessages.size() == 1) {
5604 // Special case for a single message: Use the big text style
5605 // so the collapsed and expanded versions match nicely.
5606 CharSequence bigTitle;
5607 CharSequence text;
5608 if (hasTitle) {
5609 bigTitle = title;
Selim Cinek7b9605b2017-01-19 17:36:00 -08005610 text = makeMessageLine(mMessages.get(0), mBuilder);
Adrian Roosfeafa052016-06-01 17:09:45 -07005611 } else {
5612 bigTitle = mMessages.get(0).mSender;
5613 text = mMessages.get(0).mText;
5614 }
Adrian Roosb1f427c2016-05-26 12:27:15 -07005615 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
5616 mBuilder.getBigTextLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08005617 mBuilder.mParams.reset().hasProgress(false).title(bigTitle).text(null));
Adrian Roosb1f427c2016-05-26 12:27:15 -07005618 BigTextStyle.applyBigTextContentView(mBuilder, contentView, text);
5619 return contentView;
5620 }
5621
Adrian Roos48d746a2016-04-12 14:57:28 -07005622 RemoteViews contentView = mBuilder.applyStandardTemplateWithActions(
Adrian Roosc1a80b02016-04-05 14:54:55 -07005623 mBuilder.getMessagingLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08005624 mBuilder.mParams.reset().hasProgress(false).title(title).text(null));
Adrian Roosc1a80b02016-04-05 14:54:55 -07005625
5626 int[] rowIds = {R.id.inbox_text0, R.id.inbox_text1, R.id.inbox_text2, R.id.inbox_text3,
5627 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
5628
5629 // Make sure all rows are gone in case we reuse a view.
5630 for (int rowId : rowIds) {
5631 contentView.setViewVisibility(rowId, View.GONE);
5632 }
5633
5634 int i=0;
Adrian Roos2d5dbba2016-06-08 17:11:53 -07005635 contentView.setViewLayoutMarginBottomDimen(R.id.line1,
5636 hasTitle ? R.dimen.notification_messaging_spacing : 0);
Adrian Roosc1a80b02016-04-05 14:54:55 -07005637 contentView.setInt(R.id.notification_messaging, "setNumIndentLines",
Selim Cinek279fa862016-06-14 10:57:25 -07005638 !mBuilder.mN.hasLargeIcon() ? 0 : (hasTitle ? 1 : 2));
Adrian Roosc1a80b02016-04-05 14:54:55 -07005639
Adrian Roosfeafa052016-06-01 17:09:45 -07005640 int contractedChildId = View.NO_ID;
5641 Message contractedMessage = findLatestIncomingMessage();
Adrian Roos437cd562017-01-18 15:47:03 -08005642 int firstHistoricMessage = Math.max(0, mHistoricMessages.size()
5643 - (rowIds.length - mMessages.size()));
5644 while (firstHistoricMessage + i < mHistoricMessages.size() && i < rowIds.length) {
5645 Message m = mHistoricMessages.get(firstHistoricMessage + i);
5646 int rowId = rowIds[i];
5647
Selim Cinek7b9605b2017-01-19 17:36:00 -08005648 contentView.setTextViewText(rowId, makeMessageLine(m, mBuilder));
Adrian Roos437cd562017-01-18 15:47:03 -08005649
5650 if (contractedMessage == m) {
5651 contractedChildId = rowId;
5652 }
5653
5654 i++;
5655 }
5656
Adrian Roosc1a80b02016-04-05 14:54:55 -07005657 int firstMessage = Math.max(0, mMessages.size() - rowIds.length);
5658 while (firstMessage + i < mMessages.size() && i < rowIds.length) {
5659 Message m = mMessages.get(firstMessage + i);
5660 int rowId = rowIds[i];
5661
5662 contentView.setViewVisibility(rowId, View.VISIBLE);
Selim Cinek7b9605b2017-01-19 17:36:00 -08005663 contentView.setTextViewText(rowId, makeMessageLine(m, mBuilder));
5664 mBuilder.setTextViewColorSecondary(contentView, rowId);
Adrian Roosc1a80b02016-04-05 14:54:55 -07005665
Adrian Roosfeafa052016-06-01 17:09:45 -07005666 if (contractedMessage == m) {
5667 contractedChildId = rowId;
5668 }
5669
Adrian Roosc1a80b02016-04-05 14:54:55 -07005670 i++;
5671 }
Adrian Roos437cd562017-01-18 15:47:03 -08005672 // Clear the remaining views for reapply. Ensures that historic message views can
5673 // reliably be identified as being GONE and having non-null text.
5674 while (i < rowIds.length) {
5675 int rowId = rowIds[i];
5676 contentView.setTextViewText(rowId, null);
5677 i++;
5678 }
5679
Adrian Roosfeafa052016-06-01 17:09:45 -07005680 // Record this here to allow transformation between the contracted and expanded views.
5681 contentView.setInt(R.id.notification_messaging, "setContractedChildId",
5682 contractedChildId);
Alex Hillsfc737de2016-03-23 17:33:02 -04005683 return contentView;
5684 }
5685
Selim Cinek7b9605b2017-01-19 17:36:00 -08005686 private CharSequence makeMessageLine(Message m, Builder builder) {
Adrian Roosc1a80b02016-04-05 14:54:55 -07005687 BidiFormatter bidi = BidiFormatter.getInstance();
5688 SpannableStringBuilder sb = new SpannableStringBuilder();
Selim Cinek7b9605b2017-01-19 17:36:00 -08005689 boolean colorize = builder.isColorized();
Adrian Roosc1a80b02016-04-05 14:54:55 -07005690 if (TextUtils.isEmpty(m.mSender)) {
5691 CharSequence replyName = mUserDisplayName == null ? "" : mUserDisplayName;
5692 sb.append(bidi.unicodeWrap(replyName),
Selim Cinek7b9605b2017-01-19 17:36:00 -08005693 makeFontColorSpan(colorize
5694 ? builder.getPrimaryTextColor()
5695 : mBuilder.resolveContrastColor()),
Adrian Roosc1a80b02016-04-05 14:54:55 -07005696 0 /* flags */);
5697 } else {
5698 sb.append(bidi.unicodeWrap(m.mSender),
Selim Cinek7b9605b2017-01-19 17:36:00 -08005699 makeFontColorSpan(colorize
5700 ? builder.getPrimaryTextColor()
5701 : Color.BLACK),
Adrian Roosc1a80b02016-04-05 14:54:55 -07005702 0 /* flags */);
5703 }
5704 CharSequence text = m.mText == null ? "" : m.mText;
5705 sb.append(" ").append(bidi.unicodeWrap(text));
5706 return sb;
5707 }
5708
Adrian Roosdedd1df2016-04-26 16:38:47 -07005709 /**
5710 * @hide
5711 */
5712 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08005713 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
5714 if (increasedHeight) {
5715 return makeBigContentView();
5716 }
Adrian Roosdedd1df2016-04-26 16:38:47 -07005717 Message m = findLatestIncomingMessage();
5718 CharSequence title = mConversationTitle != null
5719 ? mConversationTitle
5720 : (m == null) ? null : m.mSender;
5721 CharSequence text = (m == null)
5722 ? null
Selim Cinek7b9605b2017-01-19 17:36:00 -08005723 : mConversationTitle != null ? makeMessageLine(m, mBuilder) : m.mText;
Adrian Roosdedd1df2016-04-26 16:38:47 -07005724
5725 return mBuilder.applyStandardTemplateWithActions(mBuilder.getBigBaseLayoutResource(),
Adrian Roos70d7aa32017-01-11 15:39:06 -08005726 mBuilder.mParams.reset().hasProgress(false).title(title).text(text));
Adrian Roosdedd1df2016-04-26 16:38:47 -07005727 }
5728
Adrian Roosc1a80b02016-04-05 14:54:55 -07005729 private static TextAppearanceSpan makeFontColorSpan(int color) {
5730 return new TextAppearanceSpan(null, 0, 0,
5731 ColorStateList.valueOf(color), null);
5732 }
5733
Alex Hillsd9b04d92016-04-11 16:38:16 -04005734 public static final class Message {
5735
5736 static final String KEY_TEXT = "text";
5737 static final String KEY_TIMESTAMP = "time";
5738 static final String KEY_SENDER = "sender";
5739 static final String KEY_DATA_MIME_TYPE = "type";
5740 static final String KEY_DATA_URI= "uri";
Shane Brennan5a871862017-03-11 13:14:17 -08005741 static final String KEY_EXTRAS_BUNDLE = "extras";
Alex Hillsfc737de2016-03-23 17:33:02 -04005742
5743 private final CharSequence mText;
5744 private final long mTimestamp;
5745 private final CharSequence mSender;
5746
Shane Brennan5a871862017-03-11 13:14:17 -08005747 private Bundle mExtras = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04005748 private String mDataMimeType;
5749 private Uri mDataUri;
5750
5751 /**
5752 * Constructor
5753 * @param text A {@link CharSequence} to be displayed as the message content
5754 * @param timestamp Time at which the message arrived
5755 * @param sender A {@link CharSequence} to be used for displaying the name of the
5756 * sender. Should be <code>null</code> for messages by the current user, in which case
5757 * the platform will insert {@link MessagingStyle#getUserDisplayName()}.
5758 * Should be unique amongst all individuals in the conversation, and should be
5759 * consistent during re-posts of the notification.
5760 */
5761 public Message(CharSequence text, long timestamp, CharSequence sender){
5762 mText = text;
5763 mTimestamp = timestamp;
5764 mSender = sender;
5765 }
5766
5767 /**
5768 * Sets a binary blob of data and an associated MIME type for a message. In the case
5769 * where the platform doesn't support the MIME type, the original text provided in the
5770 * constructor will be used.
5771 * @param dataMimeType The MIME type of the content. See
5772 * <a href="{@docRoot}notifications/messaging.html"> for the list of supported MIME
5773 * types on Android and Android Wear.
5774 * @param dataUri The uri containing the content whose type is given by the MIME type.
5775 * <p class="note">
5776 * <ol>
5777 * <li>Notification Listeners including the System UI need permission to access the
5778 * data the Uri points to. The recommended ways to do this are:</li>
5779 * <li>Store the data in your own ContentProvider, making sure that other apps have
5780 * the correct permission to access your provider. The preferred mechanism for
5781 * providing access is to use per-URI permissions which are temporary and only
5782 * grant access to the receiving application. An easy way to create a
5783 * ContentProvider like this is to use the FileProvider helper class.</li>
5784 * <li>Use the system MediaStore. The MediaStore is primarily aimed at video, audio
5785 * and image MIME types, however beginning with Android 3.0 (API level 11) it can
5786 * also store non-media types (see MediaStore.Files for more info). Files can be
5787 * inserted into the MediaStore using scanFile() after which a content:// style
5788 * Uri suitable for sharing is passed to the provided onScanCompleted() callback.
5789 * Note that once added to the system MediaStore the content is accessible to any
5790 * app on the device.</li>
5791 * </ol>
5792 * @return this object for method chaining
5793 */
5794 public Message setData(String dataMimeType, Uri dataUri) {
5795 mDataMimeType = dataMimeType;
5796 mDataUri = dataUri;
5797 return this;
5798 }
5799
Alex Hillsfc737de2016-03-23 17:33:02 -04005800 /**
5801 * Get the text to be used for this message, or the fallback text if a type and content
5802 * Uri have been set
5803 */
5804 public CharSequence getText() {
5805 return mText;
5806 }
5807
5808 /**
5809 * Get the time at which this message arrived
5810 */
5811 public long getTimestamp() {
5812 return mTimestamp;
5813 }
5814
5815 /**
Shane Brennan5a871862017-03-11 13:14:17 -08005816 * Get the extras Bundle for this message.
5817 */
5818 public Bundle getExtras() {
5819 return mExtras;
5820 }
5821
5822 /**
Alex Hillsfc737de2016-03-23 17:33:02 -04005823 * Get the text used to display the contact's name in the messaging experience
5824 */
5825 public CharSequence getSender() {
5826 return mSender;
5827 }
5828
5829 /**
5830 * Get the MIME type of the data pointed to by the Uri
5831 */
5832 public String getDataMimeType() {
5833 return mDataMimeType;
5834 }
5835
5836 /**
5837 * Get the the Uri pointing to the content of the message. Can be null, in which case
5838 * {@see #getText()} is used.
5839 */
5840 public Uri getDataUri() {
5841 return mDataUri;
5842 }
5843
Alex Hillsd9b04d92016-04-11 16:38:16 -04005844 private Bundle toBundle() {
5845 Bundle bundle = new Bundle();
Alex Hillsfc737de2016-03-23 17:33:02 -04005846 if (mText != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005847 bundle.putCharSequence(KEY_TEXT, mText);
Alex Hillsfc737de2016-03-23 17:33:02 -04005848 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005849 bundle.putLong(KEY_TIMESTAMP, mTimestamp);
Alex Hillsfc737de2016-03-23 17:33:02 -04005850 if (mSender != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005851 bundle.putCharSequence(KEY_SENDER, mSender);
Alex Hillsfc737de2016-03-23 17:33:02 -04005852 }
5853 if (mDataMimeType != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005854 bundle.putString(KEY_DATA_MIME_TYPE, mDataMimeType);
Alex Hillsfc737de2016-03-23 17:33:02 -04005855 }
5856 if (mDataUri != null) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005857 bundle.putParcelable(KEY_DATA_URI, mDataUri);
Alex Hillsfc737de2016-03-23 17:33:02 -04005858 }
Shane Brennan5a871862017-03-11 13:14:17 -08005859 if (mExtras != null) {
5860 bundle.putBundle(KEY_EXTRAS_BUNDLE, mExtras);
5861 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005862 return bundle;
Alex Hillsfc737de2016-03-23 17:33:02 -04005863 }
5864
Alex Hillsd9b04d92016-04-11 16:38:16 -04005865 static Bundle[] getBundleArrayForMessages(List<Message> messages) {
5866 Bundle[] bundles = new Bundle[messages.size()];
5867 final int N = messages.size();
5868 for (int i = 0; i < N; i++) {
5869 bundles[i] = messages.get(i).toBundle();
5870 }
5871 return bundles;
5872 }
5873
Adrian Roosdedd1df2016-04-26 16:38:47 -07005874 static List<Message> getMessagesFromBundleArray(Parcelable[] bundles) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005875 List<Message> messages = new ArrayList<>(bundles.length);
5876 for (int i = 0; i < bundles.length; i++) {
Adrian Roosdedd1df2016-04-26 16:38:47 -07005877 if (bundles[i] instanceof Bundle) {
5878 Message message = getMessageFromBundle((Bundle)bundles[i]);
5879 if (message != null) {
5880 messages.add(message);
5881 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005882 }
5883 }
5884 return messages;
5885 }
5886
5887 static Message getMessageFromBundle(Bundle bundle) {
5888 try {
Adrian Roosfbddd2c2016-05-13 12:57:20 -07005889 if (!bundle.containsKey(KEY_TEXT) || !bundle.containsKey(KEY_TIMESTAMP)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005890 return null;
5891 } else {
5892 Message message = new Message(bundle.getCharSequence(KEY_TEXT),
5893 bundle.getLong(KEY_TIMESTAMP), bundle.getCharSequence(KEY_SENDER));
5894 if (bundle.containsKey(KEY_DATA_MIME_TYPE) &&
5895 bundle.containsKey(KEY_DATA_URI)) {
Alex Hillsd9b04d92016-04-11 16:38:16 -04005896 message.setData(bundle.getString(KEY_DATA_MIME_TYPE),
5897 (Uri) bundle.getParcelable(KEY_DATA_URI));
Alex Hillsfc737de2016-03-23 17:33:02 -04005898 }
Shane Brennan5a871862017-03-11 13:14:17 -08005899 if (bundle.containsKey(KEY_EXTRAS_BUNDLE)) {
5900 message.getExtras().putAll(bundle.getBundle(KEY_EXTRAS_BUNDLE));
5901 }
Alex Hillsd9b04d92016-04-11 16:38:16 -04005902 return message;
5903 }
5904 } catch (ClassCastException e) {
5905 return null;
5906 }
5907 }
Alex Hillsfc737de2016-03-23 17:33:02 -04005908 }
5909 }
5910
5911 /**
Daniel Sandler879c5e02012-04-17 16:46:51 -04005912 * Helper class for generating large-format notifications that include a list of (up to 5) strings.
Joe Malin8d40d042012-11-05 11:36:40 -08005913 *
Robert Ly91c5ce32014-06-08 15:37:00 -07005914 * Here's how you'd set the <code>InboxStyle</code> on a notification:
Daniel Sandler879c5e02012-04-17 16:46:51 -04005915 * <pre class="prettyprint">
Robert Ly91c5ce32014-06-08 15:37:00 -07005916 * Notification notif = new Notification.Builder(mContext)
5917 * .setContentTitle(&quot;5 New mails from &quot; + sender.toString())
5918 * .setContentText(subject)
5919 * .setSmallIcon(R.drawable.new_mail)
5920 * .setLargeIcon(aBitmap)
5921 * .setStyle(new Notification.InboxStyle()
5922 * .addLine(str1)
5923 * .addLine(str2)
5924 * .setContentTitle(&quot;&quot;)
5925 * .setSummaryText(&quot;+3 more&quot;))
5926 * .build();
Daniel Sandler879c5e02012-04-17 16:46:51 -04005927 * </pre>
Joe Malin8d40d042012-11-05 11:36:40 -08005928 *
Daniel Sandler879c5e02012-04-17 16:46:51 -04005929 * @see Notification#bigContentView
5930 */
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005931 public static class InboxStyle extends Style {
Daniel Sandler879c5e02012-04-17 16:46:51 -04005932 private ArrayList<CharSequence> mTexts = new ArrayList<CharSequence>(5);
5933
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005934 public InboxStyle() {
5935 }
5936
Adrian Roosf5faf9d2016-05-23 13:56:15 -07005937 /**
5938 * @deprecated use {@code InboxStyle()}.
5939 */
5940 @Deprecated
Daniel Sandler879c5e02012-04-17 16:46:51 -04005941 public InboxStyle(Builder builder) {
Chris Wrenfbd96ba2012-05-01 12:03:58 -04005942 setBuilder(builder);
Daniel Sandler879c5e02012-04-17 16:46:51 -04005943 }
5944
Chris Wrend6297db2012-05-03 16:20:13 -04005945 /**
5946 * Overrides ContentTitle in the big form of the template.
5947 * This defaults to the value passed to setContentTitle().
5948 */
5949 public InboxStyle setBigContentTitle(CharSequence title) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005950 internalSetBigContentTitle(safeCharSequence(title));
Chris Wrend6297db2012-05-03 16:20:13 -04005951 return this;
5952 }
5953
5954 /**
5955 * Set the first line of text after the detail section in the big form of the template.
5956 */
5957 public InboxStyle setSummaryText(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005958 internalSetSummaryText(safeCharSequence(cs));
Chris Wrend6297db2012-05-03 16:20:13 -04005959 return this;
5960 }
5961
Chris Wren0bd664d2012-08-01 13:56:56 -04005962 /**
5963 * Append a line to the digest section of the Inbox notification.
5964 */
Daniel Sandler879c5e02012-04-17 16:46:51 -04005965 public InboxStyle addLine(CharSequence cs) {
Daniel Sandlerdcbaf662013-04-26 16:23:09 -04005966 mTexts.add(safeCharSequence(cs));
Daniel Sandler879c5e02012-04-17 16:46:51 -04005967 return this;
5968 }
5969
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005970 /**
5971 * @hide
5972 */
5973 public void addExtras(Bundle extras) {
5974 super.addExtras(extras);
Christoph Studer4600f9b2014-07-22 22:44:43 +02005975
Daniel Sandlerf45564e2013-04-15 15:05:08 -04005976 CharSequence[] a = new CharSequence[mTexts.size()];
5977 extras.putCharSequenceArray(EXTRA_TEXT_LINES, mTexts.toArray(a));
5978 }
5979
Christoph Studer4600f9b2014-07-22 22:44:43 +02005980 /**
5981 * @hide
5982 */
5983 @Override
5984 protected void restoreFromExtras(Bundle extras) {
5985 super.restoreFromExtras(extras);
5986
5987 mTexts.clear();
5988 if (extras.containsKey(EXTRA_TEXT_LINES)) {
5989 Collections.addAll(mTexts, extras.getCharSequenceArray(EXTRA_TEXT_LINES));
5990 }
5991 }
5992
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005993 /**
5994 * @hide
5995 */
5996 public RemoteViews makeBigContentView() {
Selim Cinekc848c3a2016-01-13 15:27:30 -08005997 // Remove the content text so it disappears unless you have a summary
Christoph Studer4600f9b2014-07-22 22:44:43 +02005998 // Nasty
Julia Reynoldsd9228f12015-10-20 10:37:27 -04005999 CharSequence oldBuilderContentText = mBuilder.mN.extras.getCharSequence(EXTRA_TEXT);
6000 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, null);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006001
Jorim Jaggi39fa59f2014-02-25 15:38:45 +01006002 RemoteViews contentView = getStandardView(mBuilder.getInboxLayoutResource());
Daniel Sandler619738c2012-06-07 16:33:08 -04006003
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006004 mBuilder.getAllExtras().putCharSequence(EXTRA_TEXT, oldBuilderContentText);
Christoph Studer4600f9b2014-07-22 22:44:43 +02006005
Chris Wrend6297db2012-05-03 16:20:13 -04006006 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 -04006007 R.id.inbox_text4, R.id.inbox_text5, R.id.inbox_text6};
Chris Wrend6297db2012-05-03 16:20:13 -04006008
Chris Wren4ed80d52012-05-17 09:30:03 -04006009 // Make sure all rows are gone in case we reuse a view.
6010 for (int rowId : rowIds) {
6011 contentView.setViewVisibility(rowId, View.GONE);
6012 }
6013
Daniel Sandler879c5e02012-04-17 16:46:51 -04006014 int i=0;
Selim Cinek07c80172016-04-21 16:40:47 -07006015 int topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6016 R.dimen.notification_inbox_item_top_padding);
Selim Cinek247fa012016-02-18 09:50:48 -08006017 boolean first = true;
Selim Cinek07c80172016-04-21 16:40:47 -07006018 int onlyViewId = 0;
6019 int maxRows = rowIds.length;
6020 if (mBuilder.mActions.size() > 0) {
6021 maxRows--;
6022 }
6023 while (i < mTexts.size() && i < maxRows) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006024 CharSequence str = mTexts.get(i);
Selim Cinek07c80172016-04-21 16:40:47 -07006025 if (!TextUtils.isEmpty(str)) {
Daniel Sandler879c5e02012-04-17 16:46:51 -04006026 contentView.setViewVisibility(rowIds[i], View.VISIBLE);
Jorim Jaggi5c2d8462014-03-21 17:37:00 +01006027 contentView.setTextViewText(rowIds[i], mBuilder.processLegacyText(str));
Selim Cinek7b9605b2017-01-19 17:36:00 -08006028 mBuilder.setTextViewColorSecondary(contentView, rowIds[i]);
Selim Cinek07c80172016-04-21 16:40:47 -07006029 contentView.setViewPadding(rowIds[i], 0, topPadding, 0, 0);
Selim Cinek247fa012016-02-18 09:50:48 -08006030 handleInboxImageMargin(contentView, rowIds[i], first);
Selim Cinek07c80172016-04-21 16:40:47 -07006031 if (first) {
6032 onlyViewId = rowIds[i];
6033 } else {
6034 onlyViewId = 0;
6035 }
Selim Cinek247fa012016-02-18 09:50:48 -08006036 first = false;
Daniel Sandler879c5e02012-04-17 16:46:51 -04006037 }
6038 i++;
6039 }
Selim Cinek07c80172016-04-21 16:40:47 -07006040 if (onlyViewId != 0) {
6041 // We only have 1 entry, lets make it look like the normal Text of a Bigtext
6042 topPadding = mBuilder.mContext.getResources().getDimensionPixelSize(
6043 R.dimen.notification_text_margin_top);
6044 contentView.setViewPadding(onlyViewId, 0, topPadding, 0, 0);
6045 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006046
Daniel Sandler879c5e02012-04-17 16:46:51 -04006047 return contentView;
6048 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006049
Selim Cinek247fa012016-02-18 09:50:48 -08006050 private void handleInboxImageMargin(RemoteViews contentView, int id, boolean first) {
Selim Cinek1e0bf612015-11-20 15:57:26 -08006051 int endMargin = 0;
Selim Cinek247fa012016-02-18 09:50:48 -08006052 if (first) {
6053 final int max = mBuilder.mN.extras.getInt(EXTRA_PROGRESS_MAX, 0);
6054 final boolean ind = mBuilder.mN.extras.getBoolean(EXTRA_PROGRESS_INDETERMINATE);
6055 boolean hasProgress = max != 0 || ind;
Selim Cinek279fa862016-06-14 10:57:25 -07006056 if (mBuilder.mN.hasLargeIcon() && !hasProgress) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006057 endMargin = R.dimen.notification_content_picture_margin;
Selim Cinek247fa012016-02-18 09:50:48 -08006058 }
Selim Cinek1e0bf612015-11-20 15:57:26 -08006059 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006060 contentView.setViewLayoutMarginEndDimen(id, endMargin);
Selim Cinek1e0bf612015-11-20 15:57:26 -08006061 }
Daniel Sandler879c5e02012-04-17 16:46:51 -04006062 }
Dan Sandler842dd772014-05-15 09:36:47 -04006063
6064 /**
6065 * Notification style for media playback notifications.
6066 *
6067 * In the expanded form, {@link Notification#bigContentView}, up to 5
6068 * {@link Notification.Action}s specified with
Dan Sandler86647982015-05-13 23:41:13 -04006069 * {@link Notification.Builder#addAction(Action) addAction} will be
Dan Sandler842dd772014-05-15 09:36:47 -04006070 * shown as icon-only pushbuttons, suitable for transport controls. The Bitmap given to
6071 * {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap) setLargeIcon()} will be
6072 * treated as album artwork.
Selim Cinek99104832017-01-25 14:47:33 -08006073 * <p>
Dan Sandler842dd772014-05-15 09:36:47 -04006074 * Unlike the other styles provided here, MediaStyle can also modify the standard-size
6075 * {@link Notification#contentView}; by providing action indices to
Christoph Studerfde6f4d2014-12-12 13:23:26 +01006076 * {@link #setShowActionsInCompactView(int...)} you can promote up to 3 actions to be displayed
Dan Sandler842dd772014-05-15 09:36:47 -04006077 * in the standard view alongside the usual content.
Selim Cinek99104832017-01-25 14:47:33 -08006078 * <p>
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006079 * Notifications created with MediaStyle will have their category set to
6080 * {@link Notification#CATEGORY_TRANSPORT CATEGORY_TRANSPORT} unless you set a different
6081 * category using {@link Notification.Builder#setCategory(String) setCategory()}.
Selim Cinek99104832017-01-25 14:47:33 -08006082 * <p>
Jeff Browndba34ba2014-06-24 20:46:03 -07006083 * Finally, if you attach a {@link android.media.session.MediaSession.Token} using
6084 * {@link android.app.Notification.MediaStyle#setMediaSession(MediaSession.Token)},
Dan Sandler842dd772014-05-15 09:36:47 -04006085 * the System UI can identify this as a notification representing an active media session
6086 * and respond accordingly (by showing album artwork in the lockscreen, for example).
6087 *
Selim Cinek99104832017-01-25 14:47:33 -08006088 * <p>
6089 * Starting at {@link android.os.Build.VERSION_CODES#O Android O} any notification that has a
6090 * media session attached with {@link #setMediaSession(MediaSession.Token)} will be colorized.
6091 * You can opt-out of this behavior by using {@link Notification.Builder#setColorized(boolean)}.
6092 * <p>
6093 *
Dan Sandler842dd772014-05-15 09:36:47 -04006094 * To use this style with your Notification, feed it to
6095 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6096 * <pre class="prettyprint">
6097 * Notification noti = new Notification.Builder()
6098 * .setSmallIcon(R.drawable.ic_stat_player)
Christoph Studere935fe92014-11-24 14:18:06 +01006099 * .setContentTitle(&quot;Track title&quot;)
6100 * .setContentText(&quot;Artist - Album&quot;)
6101 * .setLargeIcon(albumArtBitmap))
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006102 * .setStyle(<b>new Notification.MediaStyle()</b>
6103 * .setMediaSession(mySession))
Dan Sandler842dd772014-05-15 09:36:47 -04006104 * .build();
6105 * </pre>
6106 *
6107 * @see Notification#bigContentView
Selim Cinek99104832017-01-25 14:47:33 -08006108 * @see Notification.Builder#setColorized(boolean)
Dan Sandler842dd772014-05-15 09:36:47 -04006109 */
6110 public static class MediaStyle extends Style {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006111 static final int MAX_MEDIA_BUTTONS_IN_COMPACT = 3;
Dan Sandler842dd772014-05-15 09:36:47 -04006112 static final int MAX_MEDIA_BUTTONS = 5;
6113
6114 private int[] mActionsToShowInCompact = null;
Jeff Browndba34ba2014-06-24 20:46:03 -07006115 private MediaSession.Token mToken;
Dan Sandler842dd772014-05-15 09:36:47 -04006116
6117 public MediaStyle() {
6118 }
6119
Adrian Roosf5faf9d2016-05-23 13:56:15 -07006120 /**
6121 * @deprecated use {@code MediaStyle()}.
6122 */
6123 @Deprecated
Dan Sandler842dd772014-05-15 09:36:47 -04006124 public MediaStyle(Builder builder) {
6125 setBuilder(builder);
6126 }
6127
6128 /**
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006129 * 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 -04006130 * notification view.
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006131 *
6132 * @param actions the indices of the actions to show in the compact notification view
Dan Sandler842dd772014-05-15 09:36:47 -04006133 */
6134 public MediaStyle setShowActionsInCompactView(int...actions) {
6135 mActionsToShowInCompact = actions;
6136 return this;
6137 }
6138
6139 /**
Jeff Browndba34ba2014-06-24 20:46:03 -07006140 * Attach a {@link android.media.session.MediaSession.Token} to this Notification
6141 * to provide additional playback information and control to the SystemUI.
Dan Sandler842dd772014-05-15 09:36:47 -04006142 */
Jeff Browndba34ba2014-06-24 20:46:03 -07006143 public MediaStyle setMediaSession(MediaSession.Token token) {
Dan Sandler842dd772014-05-15 09:36:47 -04006144 mToken = token;
6145 return this;
6146 }
6147
Christoph Studer4600f9b2014-07-22 22:44:43 +02006148 /**
6149 * @hide
6150 */
Dan Sandler842dd772014-05-15 09:36:47 -04006151 @Override
6152 public Notification buildStyled(Notification wip) {
Christoph Studer4600f9b2014-07-22 22:44:43 +02006153 super.buildStyled(wip);
Bryan Mawhinney6be8de32014-07-18 10:35:12 +01006154 if (wip.category == null) {
6155 wip.category = Notification.CATEGORY_TRANSPORT;
6156 }
Dan Sandler842dd772014-05-15 09:36:47 -04006157 return wip;
6158 }
6159
Christoph Studer4600f9b2014-07-22 22:44:43 +02006160 /**
6161 * @hide
6162 */
6163 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006164 public RemoteViews makeContentView(boolean increasedHeight) {
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006165 return makeMediaContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006166 }
6167
6168 /**
6169 * @hide
6170 */
6171 @Override
Julia Reynoldsd9228f12015-10-20 10:37:27 -04006172 public RemoteViews makeBigContentView() {
6173 return makeMediaBigContentView();
Christoph Studer4600f9b2014-07-22 22:44:43 +02006174 }
6175
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006176 /**
6177 * @hide
6178 */
6179 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006180 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006181 RemoteViews expanded = makeMediaBigContentView();
6182 return expanded != null ? expanded : makeMediaContentView();
6183 }
6184
Dan Sandler842dd772014-05-15 09:36:47 -04006185 /** @hide */
6186 @Override
6187 public void addExtras(Bundle extras) {
6188 super.addExtras(extras);
6189
6190 if (mToken != null) {
6191 extras.putParcelable(EXTRA_MEDIA_SESSION, mToken);
6192 }
Bryan Mawhinneye191f902014-07-22 12:50:09 +01006193 if (mActionsToShowInCompact != null) {
6194 extras.putIntArray(EXTRA_COMPACT_ACTIONS, mActionsToShowInCompact);
6195 }
Dan Sandler842dd772014-05-15 09:36:47 -04006196 }
6197
Christoph Studer4600f9b2014-07-22 22:44:43 +02006198 /**
6199 * @hide
6200 */
6201 @Override
6202 protected void restoreFromExtras(Bundle extras) {
6203 super.restoreFromExtras(extras);
6204
6205 if (extras.containsKey(EXTRA_MEDIA_SESSION)) {
6206 mToken = extras.getParcelable(EXTRA_MEDIA_SESSION);
6207 }
6208 if (extras.containsKey(EXTRA_COMPACT_ACTIONS)) {
6209 mActionsToShowInCompact = extras.getIntArray(EXTRA_COMPACT_ACTIONS);
6210 }
6211 }
6212
Selim Cinek5bf069a2015-11-10 19:14:27 -05006213 private RemoteViews generateMediaActionButton(Action action, int color) {
Dan Sandler842dd772014-05-15 09:36:47 -04006214 final boolean tombstone = (action.actionIntent == null);
Selim Cinekf33b1112015-07-15 17:45:11 -07006215 RemoteViews button = new BuilderRemoteViews(mBuilder.mContext.getApplicationInfo(),
Alan Viverette3cb07a462014-06-06 14:19:53 -07006216 R.layout.notification_material_media_action);
Dan Sandler68079d52015-07-22 10:45:30 -04006217 button.setImageViewIcon(R.id.action0, action.getIcon());
Selim Cinek5bf069a2015-11-10 19:14:27 -05006218 button.setDrawableParameters(R.id.action0, false, -1, color, PorterDuff.Mode.SRC_ATOP,
6219 -1);
Dan Sandler842dd772014-05-15 09:36:47 -04006220 if (!tombstone) {
6221 button.setOnClickPendingIntent(R.id.action0, action.actionIntent);
6222 }
6223 button.setContentDescription(R.id.action0, action.title);
6224 return button;
6225 }
6226
6227 private RemoteViews makeMediaContentView() {
6228 RemoteViews view = mBuilder.applyStandardTemplate(
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006229 R.layout.notification_template_material_media, false /* hasProgress */);
Dan Sandler842dd772014-05-15 09:36:47 -04006230
6231 final int numActions = mBuilder.mActions.size();
6232 final int N = mActionsToShowInCompact == null
6233 ? 0
6234 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
6235 if (N > 0) {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006236 view.removeAllViews(com.android.internal.R.id.media_actions);
Dan Sandler842dd772014-05-15 09:36:47 -04006237 for (int i = 0; i < N; i++) {
6238 if (i >= numActions) {
6239 throw new IllegalArgumentException(String.format(
6240 "setShowActionsInCompactView: action %d out of bounds (max %d)",
6241 i, numActions - 1));
6242 }
6243
6244 final Action action = mBuilder.mActions.get(mActionsToShowInCompact[i]);
Selim Cinek5bf069a2015-11-10 19:14:27 -05006245 final RemoteViews button = generateMediaActionButton(action,
Selim Cinek99104832017-01-25 14:47:33 -08006246 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006247 view.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006248 }
6249 }
Selim Cinekfdc738f2016-01-27 20:04:27 -08006250 handleImage(view);
6251 // handle the content margin
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006252 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006253 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006254 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinekfdc738f2016-01-27 20:04:27 -08006255 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006256 view.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Dan Sandler842dd772014-05-15 09:36:47 -04006257 return view;
6258 }
6259
Selim Cinek99104832017-01-25 14:47:33 -08006260 private int getPrimaryHighlightColor() {
6261 return mBuilder.getPrimaryHighlightColor();
6262 }
6263
Dan Sandler842dd772014-05-15 09:36:47 -04006264 private RemoteViews makeMediaBigContentView() {
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006265 final int actionCount = Math.min(mBuilder.mActions.size(), MAX_MEDIA_BUTTONS);
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006266 // Dont add an expanded view if there is no more content to be revealed
6267 int actionsInCompact = mActionsToShowInCompact == null
6268 ? 0
6269 : Math.min(mActionsToShowInCompact.length, MAX_MEDIA_BUTTONS_IN_COMPACT);
Selim Cinek279fa862016-06-14 10:57:25 -07006270 if (!mBuilder.mN.hasLargeIcon() && actionCount <= actionsInCompact) {
Selim Cinekcc10bfb2016-02-10 16:24:21 -08006271 return null;
6272 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006273 RemoteViews big = mBuilder.applyStandardTemplate(
6274 R.layout.notification_template_material_big_media,
6275 false);
Dan Sandler842dd772014-05-15 09:36:47 -04006276
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006277 if (actionCount > 0) {
6278 big.removeAllViews(com.android.internal.R.id.media_actions);
6279 for (int i = 0; i < actionCount; i++) {
Selim Cinek5bf069a2015-11-10 19:14:27 -05006280 final RemoteViews button = generateMediaActionButton(mBuilder.mActions.get(i),
Selim Cinek99104832017-01-25 14:47:33 -08006281 getPrimaryHighlightColor());
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006282 big.addView(com.android.internal.R.id.media_actions, button);
Dan Sandler842dd772014-05-15 09:36:47 -04006283 }
6284 }
Selim Cinek5bf069a2015-11-10 19:14:27 -05006285 handleImage(big);
Dan Sandler842dd772014-05-15 09:36:47 -04006286 return big;
6287 }
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006288
Selim Cinek5bf069a2015-11-10 19:14:27 -05006289 private void handleImage(RemoteViews contentView) {
Selim Cinek279fa862016-06-14 10:57:25 -07006290 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006291 contentView.setViewLayoutMarginEndDimen(R.id.line1, 0);
6292 contentView.setViewLayoutMarginEndDimen(R.id.text, 0);
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006293 }
6294 }
6295
Jorim Jaggi17ee3ec2014-08-29 03:47:31 +02006296 /**
6297 * @hide
6298 */
6299 @Override
6300 protected boolean hasProgress() {
6301 return false;
6302 }
Dan Sandler842dd772014-05-15 09:36:47 -04006303 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006304
Selim Cinek593610c2016-02-16 18:42:57 -08006305 /**
6306 * Notification style for custom views that are decorated by the system
6307 *
6308 * <p>Instead of providing a notification that is completely custom, a developer can set this
6309 * style and still obtain system decorations like the notification header with the expand
6310 * affordance and actions.
6311 *
6312 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6313 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6314 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6315 * corresponding custom views to display.
6316 *
6317 * To use this style with your Notification, feed it to
6318 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6319 * <pre class="prettyprint">
6320 * Notification noti = new Notification.Builder()
6321 * .setSmallIcon(R.drawable.ic_stat_player)
6322 * .setLargeIcon(albumArtBitmap))
6323 * .setCustomContentView(contentView);
6324 * .setStyle(<b>new Notification.DecoratedCustomViewStyle()</b>)
6325 * .build();
6326 * </pre>
6327 */
6328 public static class DecoratedCustomViewStyle extends Style {
6329
6330 public DecoratedCustomViewStyle() {
6331 }
6332
Selim Cinek593610c2016-02-16 18:42:57 -08006333 /**
6334 * @hide
6335 */
6336 public boolean displayCustomViewInline() {
6337 return true;
6338 }
6339
6340 /**
6341 * @hide
6342 */
6343 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006344 public RemoteViews makeContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006345 return makeStandardTemplateWithCustomContent(mBuilder.mN.contentView);
6346 }
6347
6348 /**
6349 * @hide
6350 */
6351 @Override
6352 public RemoteViews makeBigContentView() {
6353 return makeDecoratedBigContentView();
6354 }
6355
6356 /**
6357 * @hide
6358 */
6359 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006360 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek593610c2016-02-16 18:42:57 -08006361 return makeDecoratedHeadsUpContentView();
6362 }
6363
Selim Cinek593610c2016-02-16 18:42:57 -08006364 private RemoteViews makeDecoratedHeadsUpContentView() {
6365 RemoteViews headsUpContentView = mBuilder.mN.headsUpContentView == null
6366 ? mBuilder.mN.contentView
6367 : mBuilder.mN.headsUpContentView;
6368 if (mBuilder.mActions.size() == 0) {
6369 return makeStandardTemplateWithCustomContent(headsUpContentView);
6370 }
6371 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6372 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006373 buildIntoRemoteViewContent(remoteViews, headsUpContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006374 return remoteViews;
6375 }
6376
Selim Cinek593610c2016-02-16 18:42:57 -08006377 private RemoteViews makeStandardTemplateWithCustomContent(RemoteViews customContent) {
6378 RemoteViews remoteViews = mBuilder.applyStandardTemplate(
6379 mBuilder.getBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006380 buildIntoRemoteViewContent(remoteViews, customContent);
Selim Cinek593610c2016-02-16 18:42:57 -08006381 return remoteViews;
6382 }
6383
Selim Cinek593610c2016-02-16 18:42:57 -08006384 private RemoteViews makeDecoratedBigContentView() {
6385 RemoteViews bigContentView = mBuilder.mN.bigContentView == null
6386 ? mBuilder.mN.contentView
6387 : mBuilder.mN.bigContentView;
6388 if (mBuilder.mActions.size() == 0) {
6389 return makeStandardTemplateWithCustomContent(bigContentView);
6390 }
6391 RemoteViews remoteViews = mBuilder.applyStandardTemplateWithActions(
6392 mBuilder.getBigBaseLayoutResource());
Selim Cinek247fa012016-02-18 09:50:48 -08006393 buildIntoRemoteViewContent(remoteViews, bigContentView);
Selim Cinek593610c2016-02-16 18:42:57 -08006394 return remoteViews;
6395 }
Selim Cinek247fa012016-02-18 09:50:48 -08006396
6397 private void buildIntoRemoteViewContent(RemoteViews remoteViews,
6398 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08006399 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07006400 // Need to clone customContent before adding, because otherwise it can no longer be
6401 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08006402 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07006403 remoteViews.removeAllViews(R.id.notification_main_column);
6404 remoteViews.addView(R.id.notification_main_column, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08006405 }
Selim Cinek247fa012016-02-18 09:50:48 -08006406 // also update the end margin if there is an image
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006407 int endMargin = R.dimen.notification_content_margin_end;
Selim Cinek279fa862016-06-14 10:57:25 -07006408 if (mBuilder.mN.hasLargeIcon()) {
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006409 endMargin = R.dimen.notification_content_plus_picture_margin_end;
Selim Cinek247fa012016-02-18 09:50:48 -08006410 }
Adrian Roos2d5dbba2016-06-08 17:11:53 -07006411 remoteViews.setViewLayoutMarginEndDimen(R.id.notification_main_column, endMargin);
Selim Cinek247fa012016-02-18 09:50:48 -08006412 }
Selim Cinek593610c2016-02-16 18:42:57 -08006413 }
6414
Selim Cinek03eb3b72016-02-18 10:39:45 -08006415 /**
6416 * Notification style for media custom views that are decorated by the system
6417 *
6418 * <p>Instead of providing a media notification that is completely custom, a developer can set
6419 * this style and still obtain system decorations like the notification header with the expand
6420 * affordance and actions.
6421 *
6422 * <p>Use {@link android.app.Notification.Builder#setCustomContentView(RemoteViews)},
6423 * {@link android.app.Notification.Builder#setCustomBigContentView(RemoteViews)} and
6424 * {@link android.app.Notification.Builder#setCustomHeadsUpContentView(RemoteViews)} to set the
6425 * corresponding custom views to display.
Selim Cinek99104832017-01-25 14:47:33 -08006426 * <p>
6427 * Contrary to {@link MediaStyle} a developer has to opt-in to the colorizing of the
6428 * notification by using {@link Notification.Builder#setColorized(boolean)}.
6429 * <p>
Selim Cinek03eb3b72016-02-18 10:39:45 -08006430 * To use this style with your Notification, feed it to
6431 * {@link Notification.Builder#setStyle(android.app.Notification.Style)} like so:
6432 * <pre class="prettyprint">
6433 * Notification noti = new Notification.Builder()
6434 * .setSmallIcon(R.drawable.ic_stat_player)
6435 * .setLargeIcon(albumArtBitmap))
6436 * .setCustomContentView(contentView);
6437 * .setStyle(<b>new Notification.DecoratedMediaCustomViewStyle()</b>
6438 * .setMediaSession(mySession))
6439 * .build();
6440 * </pre>
6441 *
6442 * @see android.app.Notification.DecoratedCustomViewStyle
6443 * @see android.app.Notification.MediaStyle
6444 */
6445 public static class DecoratedMediaCustomViewStyle extends MediaStyle {
6446
6447 public DecoratedMediaCustomViewStyle() {
6448 }
6449
Selim Cinek03eb3b72016-02-18 10:39:45 -08006450 /**
6451 * @hide
6452 */
6453 public boolean displayCustomViewInline() {
6454 return true;
6455 }
6456
6457 /**
6458 * @hide
6459 */
6460 @Override
Selim Cinek7d1009b2017-01-25 15:28:28 -08006461 public RemoteViews makeContentView(boolean increasedHeight) {
6462 RemoteViews remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006463 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6464 mBuilder.mN.contentView);
6465 }
6466
6467 /**
6468 * @hide
6469 */
6470 @Override
6471 public RemoteViews makeBigContentView() {
6472 RemoteViews customRemoteView = mBuilder.mN.bigContentView != null
6473 ? mBuilder.mN.bigContentView
6474 : mBuilder.mN.contentView;
6475 return makeBigContentViewWithCustomContent(customRemoteView);
6476 }
6477
6478 private RemoteViews makeBigContentViewWithCustomContent(RemoteViews customRemoteView) {
6479 RemoteViews remoteViews = super.makeBigContentView();
6480 if (remoteViews != null) {
6481 return buildIntoRemoteView(remoteViews, R.id.notification_main_column,
6482 customRemoteView);
6483 } else if (customRemoteView != mBuilder.mN.contentView){
Selim Cinek7d1009b2017-01-25 15:28:28 -08006484 remoteViews = super.makeContentView(false /* increasedHeight */);
Selim Cinek03eb3b72016-02-18 10:39:45 -08006485 return buildIntoRemoteView(remoteViews, R.id.notification_content_container,
6486 customRemoteView);
6487 } else {
6488 return null;
6489 }
6490 }
6491
6492 /**
6493 * @hide
6494 */
6495 @Override
Selim Cinek87ed69b2017-02-09 15:59:43 -08006496 public RemoteViews makeHeadsUpContentView(boolean increasedHeight) {
Selim Cinek03eb3b72016-02-18 10:39:45 -08006497 RemoteViews customRemoteView = mBuilder.mN.headsUpContentView != null
6498 ? mBuilder.mN.headsUpContentView
6499 : mBuilder.mN.contentView;
6500 return makeBigContentViewWithCustomContent(customRemoteView);
6501 }
6502
6503 private RemoteViews buildIntoRemoteView(RemoteViews remoteViews, int id,
6504 RemoteViews customContent) {
Adrian Roos5081c0d2016-02-26 16:04:19 -08006505 if (customContent != null) {
Selim Cinekf91017e2016-03-14 12:25:09 -07006506 // Need to clone customContent before adding, because otherwise it can no longer be
6507 // parceled independently of remoteViews.
Adrian Roos5081c0d2016-02-26 16:04:19 -08006508 customContent = customContent.clone();
Selim Cinekf91017e2016-03-14 12:25:09 -07006509 remoteViews.removeAllViews(id);
6510 remoteViews.addView(id, customContent);
Adrian Roos5081c0d2016-02-26 16:04:19 -08006511 }
Selim Cinek03eb3b72016-02-18 10:39:45 -08006512 return remoteViews;
6513 }
6514 }
6515
Christoph Studer4600f9b2014-07-22 22:44:43 +02006516 // When adding a new Style subclass here, don't forget to update
6517 // Builder.getNotificationStyleClass.
6518
Griff Hazen61a9e862014-05-22 16:05:19 -07006519 /**
6520 * Extender interface for use with {@link Builder#extend}. Extenders may be used to add
6521 * metadata or change options on a notification builder.
6522 */
6523 public interface Extender {
6524 /**
6525 * Apply this extender to a notification builder.
6526 * @param builder the builder to be modified.
6527 * @return the build object for chaining.
6528 */
6529 public Builder extend(Builder builder);
6530 }
6531
6532 /**
6533 * Helper class to add wearable extensions to notifications.
6534 * <p class="note"> See
6535 * <a href="{@docRoot}wear/notifications/creating.html">Creating Notifications
6536 * for Android Wear</a> for more information on how to use this class.
6537 * <p>
6538 * To create a notification with wearable extensions:
6539 * <ol>
6540 * <li>Create a {@link android.app.Notification.Builder}, setting any desired
6541 * properties.
6542 * <li>Create a {@link android.app.Notification.WearableExtender}.
6543 * <li>Set wearable-specific properties using the
6544 * {@code add} and {@code set} methods of {@link android.app.Notification.WearableExtender}.
6545 * <li>Call {@link android.app.Notification.Builder#extend} to apply the extensions to a
6546 * notification.
6547 * <li>Post the notification to the notification system with the
6548 * {@code NotificationManager.notify(...)} methods.
6549 * </ol>
6550 *
6551 * <pre class="prettyprint">
6552 * Notification notif = new Notification.Builder(mContext)
6553 * .setContentTitle(&quot;New mail from &quot; + sender.toString())
6554 * .setContentText(subject)
6555 * .setSmallIcon(R.drawable.new_mail)
6556 * .extend(new Notification.WearableExtender()
6557 * .setContentIcon(R.drawable.new_mail))
6558 * .build();
6559 * NotificationManager notificationManger =
6560 * (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
6561 * notificationManger.notify(0, notif);</pre>
6562 *
6563 * <p>Wearable extensions can be accessed on an existing notification by using the
6564 * {@code WearableExtender(Notification)} constructor,
6565 * and then using the {@code get} methods to access values.
6566 *
6567 * <pre class="prettyprint">
6568 * Notification.WearableExtender wearableExtender = new Notification.WearableExtender(
6569 * notification);
Griff Hazen14f57992014-05-26 09:07:14 -07006570 * List&lt;Notification&gt; pages = wearableExtender.getPages();</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07006571 */
6572 public static final class WearableExtender implements Extender {
6573 /**
6574 * Sentinel value for an action index that is unset.
6575 */
6576 public static final int UNSET_ACTION_INDEX = -1;
6577
6578 /**
6579 * Size value for use with {@link #setCustomSizePreset} to show this notification with
6580 * default sizing.
6581 * <p>For custom display notifications created using {@link #setDisplayIntent},
Paul Soulosaa4f4bf2015-08-04 11:59:45 -07006582 * the default is {@link #SIZE_MEDIUM}. All other notifications size automatically based
Griff Hazen61a9e862014-05-22 16:05:19 -07006583 * on their content.
6584 */
6585 public static final int SIZE_DEFAULT = 0;
6586
6587 /**
6588 * Size value for use with {@link #setCustomSizePreset} to show this notification
6589 * with an extra small size.
6590 * <p>This value is only applicable for custom display notifications created using
6591 * {@link #setDisplayIntent}.
6592 */
6593 public static final int SIZE_XSMALL = 1;
6594
6595 /**
6596 * Size value for use with {@link #setCustomSizePreset} to show this notification
6597 * with a small size.
6598 * <p>This value is only applicable for custom display notifications created using
6599 * {@link #setDisplayIntent}.
6600 */
6601 public static final int SIZE_SMALL = 2;
6602
6603 /**
6604 * Size value for use with {@link #setCustomSizePreset} to show this notification
6605 * with a medium size.
6606 * <p>This value is only applicable for custom display notifications created using
6607 * {@link #setDisplayIntent}.
6608 */
6609 public static final int SIZE_MEDIUM = 3;
6610
6611 /**
6612 * Size value for use with {@link #setCustomSizePreset} to show this notification
6613 * with a large size.
6614 * <p>This value is only applicable for custom display notifications created using
6615 * {@link #setDisplayIntent}.
6616 */
6617 public static final int SIZE_LARGE = 4;
6618
Griff Hazend5f11f92014-05-27 15:40:09 -07006619 /**
6620 * Size value for use with {@link #setCustomSizePreset} to show this notification
6621 * full screen.
6622 * <p>This value is only applicable for custom display notifications created using
6623 * {@link #setDisplayIntent}.
6624 */
6625 public static final int SIZE_FULL_SCREEN = 5;
6626
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006627 /**
6628 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on for a
6629 * short amount of time when this notification is displayed on the screen. This
6630 * is the default value.
6631 */
6632 public static final int SCREEN_TIMEOUT_SHORT = 0;
6633
6634 /**
6635 * Sentinel value for use with {@link #setHintScreenTimeout} to keep the screen on
6636 * for a longer amount of time when this notification is displayed on the screen.
6637 */
6638 public static final int SCREEN_TIMEOUT_LONG = -1;
6639
Griff Hazen61a9e862014-05-22 16:05:19 -07006640 /** Notification extra which contains wearable extensions */
6641 private static final String EXTRA_WEARABLE_EXTENSIONS = "android.wearable.EXTENSIONS";
6642
Pete Gastaf6781d2014-10-07 15:17:05 -04006643 // Keys within EXTRA_WEARABLE_EXTENSIONS for wearable options.
Griff Hazen61a9e862014-05-22 16:05:19 -07006644 private static final String KEY_ACTIONS = "actions";
6645 private static final String KEY_FLAGS = "flags";
6646 private static final String KEY_DISPLAY_INTENT = "displayIntent";
6647 private static final String KEY_PAGES = "pages";
6648 private static final String KEY_BACKGROUND = "background";
6649 private static final String KEY_CONTENT_ICON = "contentIcon";
6650 private static final String KEY_CONTENT_ICON_GRAVITY = "contentIconGravity";
6651 private static final String KEY_CONTENT_ACTION_INDEX = "contentActionIndex";
6652 private static final String KEY_CUSTOM_SIZE_PRESET = "customSizePreset";
6653 private static final String KEY_CUSTOM_CONTENT_HEIGHT = "customContentHeight";
6654 private static final String KEY_GRAVITY = "gravity";
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006655 private static final String KEY_HINT_SCREEN_TIMEOUT = "hintScreenTimeout";
Nadia Benbernou948627e2016-04-14 14:41:08 -04006656 private static final String KEY_DISMISSAL_ID = "dismissalId";
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006657 private static final String KEY_BRIDGE_TAG = "bridgeTag";
Griff Hazen61a9e862014-05-22 16:05:19 -07006658
6659 // Flags bitwise-ored to mFlags
6660 private static final int FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE = 0x1;
6661 private static final int FLAG_HINT_HIDE_ICON = 1 << 1;
6662 private static final int FLAG_HINT_SHOW_BACKGROUND_ONLY = 1 << 2;
6663 private static final int FLAG_START_SCROLL_BOTTOM = 1 << 3;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006664 private static final int FLAG_HINT_AVOID_BACKGROUND_CLIPPING = 1 << 4;
Alex Hills4bcb06b2016-04-05 14:26:25 -04006665 private static final int FLAG_BIG_PICTURE_AMBIENT = 1 << 5;
Alex Hills9ab3a232016-04-05 14:54:56 -04006666 private static final int FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY = 1 << 6;
Griff Hazen61a9e862014-05-22 16:05:19 -07006667
6668 // Default value for flags integer
6669 private static final int DEFAULT_FLAGS = FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE;
6670
6671 private static final int DEFAULT_CONTENT_ICON_GRAVITY = Gravity.END;
6672 private static final int DEFAULT_GRAVITY = Gravity.BOTTOM;
6673
6674 private ArrayList<Action> mActions = new ArrayList<Action>();
6675 private int mFlags = DEFAULT_FLAGS;
6676 private PendingIntent mDisplayIntent;
6677 private ArrayList<Notification> mPages = new ArrayList<Notification>();
6678 private Bitmap mBackground;
6679 private int mContentIcon;
6680 private int mContentIconGravity = DEFAULT_CONTENT_ICON_GRAVITY;
6681 private int mContentActionIndex = UNSET_ACTION_INDEX;
6682 private int mCustomSizePreset = SIZE_DEFAULT;
6683 private int mCustomContentHeight;
6684 private int mGravity = DEFAULT_GRAVITY;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006685 private int mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04006686 private String mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006687 private String mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07006688
6689 /**
6690 * Create a {@link android.app.Notification.WearableExtender} with default
6691 * options.
6692 */
6693 public WearableExtender() {
6694 }
6695
6696 public WearableExtender(Notification notif) {
6697 Bundle wearableBundle = notif.extras.getBundle(EXTRA_WEARABLE_EXTENSIONS);
6698 if (wearableBundle != null) {
6699 List<Action> actions = wearableBundle.getParcelableArrayList(KEY_ACTIONS);
6700 if (actions != null) {
6701 mActions.addAll(actions);
6702 }
6703
6704 mFlags = wearableBundle.getInt(KEY_FLAGS, DEFAULT_FLAGS);
6705 mDisplayIntent = wearableBundle.getParcelable(KEY_DISPLAY_INTENT);
6706
6707 Notification[] pages = getNotificationArrayFromBundle(
6708 wearableBundle, KEY_PAGES);
6709 if (pages != null) {
6710 Collections.addAll(mPages, pages);
6711 }
6712
6713 mBackground = wearableBundle.getParcelable(KEY_BACKGROUND);
6714 mContentIcon = wearableBundle.getInt(KEY_CONTENT_ICON);
6715 mContentIconGravity = wearableBundle.getInt(KEY_CONTENT_ICON_GRAVITY,
6716 DEFAULT_CONTENT_ICON_GRAVITY);
6717 mContentActionIndex = wearableBundle.getInt(KEY_CONTENT_ACTION_INDEX,
6718 UNSET_ACTION_INDEX);
6719 mCustomSizePreset = wearableBundle.getInt(KEY_CUSTOM_SIZE_PRESET,
6720 SIZE_DEFAULT);
6721 mCustomContentHeight = wearableBundle.getInt(KEY_CUSTOM_CONTENT_HEIGHT);
6722 mGravity = wearableBundle.getInt(KEY_GRAVITY, DEFAULT_GRAVITY);
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006723 mHintScreenTimeout = wearableBundle.getInt(KEY_HINT_SCREEN_TIMEOUT);
Nadia Benbernou948627e2016-04-14 14:41:08 -04006724 mDismissalId = wearableBundle.getString(KEY_DISMISSAL_ID);
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006725 mBridgeTag = wearableBundle.getString(KEY_BRIDGE_TAG);
Griff Hazen61a9e862014-05-22 16:05:19 -07006726 }
6727 }
6728
6729 /**
6730 * Apply wearable extensions to a notification that is being built. This is typically
6731 * called by the {@link android.app.Notification.Builder#extend} method of
6732 * {@link android.app.Notification.Builder}.
6733 */
6734 @Override
6735 public Notification.Builder extend(Notification.Builder builder) {
6736 Bundle wearableBundle = new Bundle();
6737
6738 if (!mActions.isEmpty()) {
6739 wearableBundle.putParcelableArrayList(KEY_ACTIONS, mActions);
6740 }
6741 if (mFlags != DEFAULT_FLAGS) {
6742 wearableBundle.putInt(KEY_FLAGS, mFlags);
6743 }
6744 if (mDisplayIntent != null) {
6745 wearableBundle.putParcelable(KEY_DISPLAY_INTENT, mDisplayIntent);
6746 }
6747 if (!mPages.isEmpty()) {
6748 wearableBundle.putParcelableArray(KEY_PAGES, mPages.toArray(
6749 new Notification[mPages.size()]));
6750 }
6751 if (mBackground != null) {
6752 wearableBundle.putParcelable(KEY_BACKGROUND, mBackground);
6753 }
6754 if (mContentIcon != 0) {
6755 wearableBundle.putInt(KEY_CONTENT_ICON, mContentIcon);
6756 }
6757 if (mContentIconGravity != DEFAULT_CONTENT_ICON_GRAVITY) {
6758 wearableBundle.putInt(KEY_CONTENT_ICON_GRAVITY, mContentIconGravity);
6759 }
6760 if (mContentActionIndex != UNSET_ACTION_INDEX) {
6761 wearableBundle.putInt(KEY_CONTENT_ACTION_INDEX,
6762 mContentActionIndex);
6763 }
6764 if (mCustomSizePreset != SIZE_DEFAULT) {
6765 wearableBundle.putInt(KEY_CUSTOM_SIZE_PRESET, mCustomSizePreset);
6766 }
6767 if (mCustomContentHeight != 0) {
6768 wearableBundle.putInt(KEY_CUSTOM_CONTENT_HEIGHT, mCustomContentHeight);
6769 }
6770 if (mGravity != DEFAULT_GRAVITY) {
6771 wearableBundle.putInt(KEY_GRAVITY, mGravity);
6772 }
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006773 if (mHintScreenTimeout != 0) {
6774 wearableBundle.putInt(KEY_HINT_SCREEN_TIMEOUT, mHintScreenTimeout);
6775 }
Nadia Benbernou948627e2016-04-14 14:41:08 -04006776 if (mDismissalId != null) {
6777 wearableBundle.putString(KEY_DISMISSAL_ID, mDismissalId);
6778 }
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006779 if (mBridgeTag != null) {
6780 wearableBundle.putString(KEY_BRIDGE_TAG, mBridgeTag);
6781 }
Griff Hazen61a9e862014-05-22 16:05:19 -07006782
6783 builder.getExtras().putBundle(EXTRA_WEARABLE_EXTENSIONS, wearableBundle);
6784 return builder;
6785 }
6786
6787 @Override
6788 public WearableExtender clone() {
6789 WearableExtender that = new WearableExtender();
6790 that.mActions = new ArrayList<Action>(this.mActions);
6791 that.mFlags = this.mFlags;
6792 that.mDisplayIntent = this.mDisplayIntent;
6793 that.mPages = new ArrayList<Notification>(this.mPages);
6794 that.mBackground = this.mBackground;
6795 that.mContentIcon = this.mContentIcon;
6796 that.mContentIconGravity = this.mContentIconGravity;
6797 that.mContentActionIndex = this.mContentActionIndex;
6798 that.mCustomSizePreset = this.mCustomSizePreset;
6799 that.mCustomContentHeight = this.mCustomContentHeight;
6800 that.mGravity = this.mGravity;
Griff Hazen5f2edfc2014-09-29 16:28:44 -07006801 that.mHintScreenTimeout = this.mHintScreenTimeout;
Nadia Benbernou948627e2016-04-14 14:41:08 -04006802 that.mDismissalId = this.mDismissalId;
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04006803 that.mBridgeTag = this.mBridgeTag;
Griff Hazen61a9e862014-05-22 16:05:19 -07006804 return that;
6805 }
6806
6807 /**
6808 * Add a wearable action to this notification.
6809 *
6810 * <p>When wearable actions are added using this method, the set of actions that
6811 * show on a wearable device splits from devices that only show actions added
6812 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
6813 * of which actions display on different devices.
6814 *
6815 * @param action the action to add to this notification
6816 * @return this object for method chaining
6817 * @see android.app.Notification.Action
6818 */
6819 public WearableExtender addAction(Action action) {
6820 mActions.add(action);
6821 return this;
6822 }
6823
6824 /**
6825 * Adds wearable actions to this notification.
6826 *
6827 * <p>When wearable actions are added using this method, the set of actions that
6828 * show on a wearable device splits from devices that only show actions added
6829 * using {@link android.app.Notification.Builder#addAction}. This allows for customization
6830 * of which actions display on different devices.
6831 *
6832 * @param actions the actions to add to this notification
6833 * @return this object for method chaining
6834 * @see android.app.Notification.Action
6835 */
6836 public WearableExtender addActions(List<Action> actions) {
6837 mActions.addAll(actions);
6838 return this;
6839 }
6840
6841 /**
6842 * Clear all wearable actions present on this builder.
6843 * @return this object for method chaining.
6844 * @see #addAction
6845 */
6846 public WearableExtender clearActions() {
6847 mActions.clear();
6848 return this;
6849 }
6850
6851 /**
6852 * Get the wearable actions present on this notification.
6853 */
6854 public List<Action> getActions() {
6855 return mActions;
6856 }
6857
6858 /**
6859 * Set an intent to launch inside of an activity view when displaying
Griff Hazen14f57992014-05-26 09:07:14 -07006860 * this notification. The {@link PendingIntent} provided should be for an activity.
6861 *
6862 * <pre class="prettyprint">
6863 * Intent displayIntent = new Intent(context, MyDisplayActivity.class);
6864 * PendingIntent displayPendingIntent = PendingIntent.getActivity(context,
6865 * 0, displayIntent, PendingIntent.FLAG_UPDATE_CURRENT);
6866 * Notification notif = new Notification.Builder(context)
6867 * .extend(new Notification.WearableExtender()
6868 * .setDisplayIntent(displayPendingIntent)
6869 * .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
6870 * .build();</pre>
6871 *
6872 * <p>The activity to launch needs to allow embedding, must be exported, and
Griff Hazen831ca9d2014-06-17 00:38:38 -07006873 * should have an empty task affinity. It is also recommended to use the device
6874 * default light theme.
Griff Hazen14f57992014-05-26 09:07:14 -07006875 *
6876 * <p>Example AndroidManifest.xml entry:
6877 * <pre class="prettyprint">
6878 * &lt;activity android:name=&quot;com.example.MyDisplayActivity&quot;
6879 * android:exported=&quot;true&quot;
6880 * android:allowEmbedded=&quot;true&quot;
Griff Hazen831ca9d2014-06-17 00:38:38 -07006881 * android:taskAffinity=&quot;&quot;
6882 * android:theme=&quot;@android:style/Theme.DeviceDefault.Light&quot; /&gt;</pre>
Griff Hazen61a9e862014-05-22 16:05:19 -07006883 *
6884 * @param intent the {@link PendingIntent} for an activity
6885 * @return this object for method chaining
6886 * @see android.app.Notification.WearableExtender#getDisplayIntent
6887 */
6888 public WearableExtender setDisplayIntent(PendingIntent intent) {
6889 mDisplayIntent = intent;
6890 return this;
6891 }
6892
6893 /**
6894 * Get the intent to launch inside of an activity view when displaying this
6895 * notification. This {@code PendingIntent} should be for an activity.
6896 */
6897 public PendingIntent getDisplayIntent() {
6898 return mDisplayIntent;
6899 }
6900
6901 /**
6902 * Add an additional page of content to display with this notification. The current
6903 * notification forms the first page, and pages added using this function form
6904 * subsequent pages. This field can be used to separate a notification into multiple
6905 * sections.
6906 *
6907 * @param page the notification to add as another page
6908 * @return this object for method chaining
6909 * @see android.app.Notification.WearableExtender#getPages
6910 */
6911 public WearableExtender addPage(Notification page) {
6912 mPages.add(page);
6913 return this;
6914 }
6915
6916 /**
6917 * Add additional pages of content to display with this notification. The current
6918 * notification forms the first page, and pages added using this function form
6919 * subsequent pages. This field can be used to separate a notification into multiple
6920 * sections.
6921 *
6922 * @param pages a list of notifications
6923 * @return this object for method chaining
6924 * @see android.app.Notification.WearableExtender#getPages
6925 */
6926 public WearableExtender addPages(List<Notification> pages) {
6927 mPages.addAll(pages);
6928 return this;
6929 }
6930
6931 /**
6932 * Clear all additional pages present on this builder.
6933 * @return this object for method chaining.
6934 * @see #addPage
6935 */
6936 public WearableExtender clearPages() {
6937 mPages.clear();
6938 return this;
6939 }
6940
6941 /**
6942 * Get the array of additional pages of content for displaying this notification. The
6943 * current notification forms the first page, and elements within this array form
6944 * subsequent pages. This field can be used to separate a notification into multiple
6945 * sections.
6946 * @return the pages for this notification
6947 */
6948 public List<Notification> getPages() {
6949 return mPages;
6950 }
6951
6952 /**
6953 * Set a background image to be displayed behind the notification content.
6954 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
6955 * will work with any notification style.
6956 *
6957 * @param background the background bitmap
6958 * @return this object for method chaining
6959 * @see android.app.Notification.WearableExtender#getBackground
6960 */
6961 public WearableExtender setBackground(Bitmap background) {
6962 mBackground = background;
6963 return this;
6964 }
6965
6966 /**
6967 * Get a background image to be displayed behind the notification content.
6968 * Contrary to the {@link android.app.Notification.BigPictureStyle}, this background
6969 * will work with any notification style.
6970 *
6971 * @return the background image
6972 * @see android.app.Notification.WearableExtender#setBackground
6973 */
6974 public Bitmap getBackground() {
6975 return mBackground;
6976 }
6977
6978 /**
6979 * Set an icon that goes with the content of this notification.
6980 */
6981 public WearableExtender setContentIcon(int icon) {
6982 mContentIcon = icon;
6983 return this;
6984 }
6985
6986 /**
6987 * Get an icon that goes with the content of this notification.
6988 */
6989 public int getContentIcon() {
6990 return mContentIcon;
6991 }
6992
6993 /**
6994 * Set the gravity that the content icon should have within the notification display.
6995 * Supported values include {@link android.view.Gravity#START} and
6996 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
6997 * @see #setContentIcon
6998 */
6999 public WearableExtender setContentIconGravity(int contentIconGravity) {
7000 mContentIconGravity = contentIconGravity;
7001 return this;
7002 }
7003
7004 /**
7005 * Get the gravity that the content icon should have within the notification display.
7006 * Supported values include {@link android.view.Gravity#START} and
7007 * {@link android.view.Gravity#END}. The default value is {@link android.view.Gravity#END}.
7008 * @see #getContentIcon
7009 */
7010 public int getContentIconGravity() {
7011 return mContentIconGravity;
7012 }
7013
7014 /**
7015 * Set an action from this notification's actions to be clickable with the content of
Griff Hazen14f57992014-05-26 09:07:14 -07007016 * this notification. This action will no longer display separately from the
7017 * notification's content.
7018 *
Griff Hazenca48d352014-05-28 22:37:13 -07007019 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007020 * set, although the list of available actions comes from the main notification and not
7021 * from the child page's notification.
7022 *
7023 * @param actionIndex The index of the action to hoist onto the current notification page.
7024 * If wearable actions were added to the main notification, this index
7025 * will apply to that list, otherwise it will apply to the regular
7026 * actions list.
Griff Hazen61a9e862014-05-22 16:05:19 -07007027 */
7028 public WearableExtender setContentAction(int actionIndex) {
7029 mContentActionIndex = actionIndex;
7030 return this;
7031 }
7032
7033 /**
Griff Hazenca48d352014-05-28 22:37:13 -07007034 * Get the index of the notification action, if any, that was specified as being clickable
7035 * with the content of this notification. This action will no longer display separately
Griff Hazen14f57992014-05-26 09:07:14 -07007036 * from the notification's content.
Griff Hazen61a9e862014-05-22 16:05:19 -07007037 *
Griff Hazenca48d352014-05-28 22:37:13 -07007038 * <p>For notifications with multiple pages, child pages can also have content actions
Griff Hazen14f57992014-05-26 09:07:14 -07007039 * set, although the list of available actions comes from the main notification and not
7040 * from the child page's notification.
7041 *
7042 * <p>If wearable specific actions were added to the main notification, this index will
7043 * apply to that list, otherwise it will apply to the regular actions list.
Griff Hazenca48d352014-05-28 22:37:13 -07007044 *
7045 * @return the action index or {@link #UNSET_ACTION_INDEX} if no action was selected.
Griff Hazen61a9e862014-05-22 16:05:19 -07007046 */
7047 public int getContentAction() {
7048 return mContentActionIndex;
7049 }
7050
7051 /**
7052 * Set the gravity that this notification should have within the available viewport space.
7053 * Supported values include {@link android.view.Gravity#TOP},
7054 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7055 * The default value is {@link android.view.Gravity#BOTTOM}.
7056 */
7057 public WearableExtender setGravity(int gravity) {
7058 mGravity = gravity;
7059 return this;
7060 }
7061
7062 /**
7063 * Get the gravity that this notification should have within the available viewport space.
7064 * Supported values include {@link android.view.Gravity#TOP},
7065 * {@link android.view.Gravity#CENTER_VERTICAL} and {@link android.view.Gravity#BOTTOM}.
7066 * The default value is {@link android.view.Gravity#BOTTOM}.
7067 */
7068 public int getGravity() {
7069 return mGravity;
7070 }
7071
7072 /**
7073 * Set the custom size preset for the display of this notification out of the available
7074 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7075 * {@link #SIZE_LARGE}.
7076 * <p>Some custom size presets are only applicable for custom display notifications created
7077 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. Check the
7078 * documentation for the preset in question. See also
7079 * {@link #setCustomContentHeight} and {@link #getCustomSizePreset}.
7080 */
7081 public WearableExtender setCustomSizePreset(int sizePreset) {
7082 mCustomSizePreset = sizePreset;
7083 return this;
7084 }
7085
7086 /**
7087 * Get the custom size preset for the display of this notification out of the available
7088 * presets found in {@link android.app.Notification.WearableExtender}, e.g.
7089 * {@link #SIZE_LARGE}.
7090 * <p>Some custom size presets are only applicable for custom display notifications created
7091 * using {@link #setDisplayIntent}. Check the documentation for the preset in question.
7092 * See also {@link #setCustomContentHeight} and {@link #setCustomSizePreset}.
7093 */
7094 public int getCustomSizePreset() {
7095 return mCustomSizePreset;
7096 }
7097
7098 /**
7099 * Set the custom height in pixels for the display of this notification's content.
7100 * <p>This option is only available for custom display notifications created
7101 * using {@link android.app.Notification.WearableExtender#setDisplayIntent}. See also
7102 * {@link android.app.Notification.WearableExtender#setCustomSizePreset} and
7103 * {@link #getCustomContentHeight}.
7104 */
7105 public WearableExtender setCustomContentHeight(int height) {
7106 mCustomContentHeight = height;
7107 return this;
7108 }
7109
7110 /**
7111 * Get the custom height in pixels for the display of this notification's content.
7112 * <p>This option is only available for custom display notifications created
7113 * using {@link #setDisplayIntent}. See also {@link #setCustomSizePreset} and
7114 * {@link #setCustomContentHeight}.
7115 */
7116 public int getCustomContentHeight() {
7117 return mCustomContentHeight;
7118 }
7119
7120 /**
7121 * Set whether the scrolling position for the contents of this notification should start
7122 * at the bottom of the contents instead of the top when the contents are too long to
7123 * display within the screen. Default is false (start scroll at the top).
7124 */
7125 public WearableExtender setStartScrollBottom(boolean startScrollBottom) {
7126 setFlag(FLAG_START_SCROLL_BOTTOM, startScrollBottom);
7127 return this;
7128 }
7129
7130 /**
7131 * Get whether the scrolling position for the contents of this notification should start
7132 * at the bottom of the contents instead of the top when the contents are too long to
7133 * display within the screen. Default is false (start scroll at the top).
7134 */
7135 public boolean getStartScrollBottom() {
7136 return (mFlags & FLAG_START_SCROLL_BOTTOM) != 0;
7137 }
7138
7139 /**
7140 * Set whether the content intent is available when the wearable device is not connected
7141 * to a companion device. The user can still trigger this intent when the wearable device
7142 * is offline, but a visual hint will indicate that the content intent may not be available.
7143 * Defaults to true.
7144 */
7145 public WearableExtender setContentIntentAvailableOffline(
7146 boolean contentIntentAvailableOffline) {
7147 setFlag(FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE, contentIntentAvailableOffline);
7148 return this;
7149 }
7150
7151 /**
7152 * Get whether the content intent is available when the wearable device is not connected
7153 * to a companion device. The user can still trigger this intent when the wearable device
7154 * is offline, but a visual hint will indicate that the content intent may not be available.
7155 * Defaults to true.
7156 */
7157 public boolean getContentIntentAvailableOffline() {
7158 return (mFlags & FLAG_CONTENT_INTENT_AVAILABLE_OFFLINE) != 0;
7159 }
7160
7161 /**
7162 * Set a hint that this notification's icon should not be displayed.
7163 * @param hintHideIcon {@code true} to hide the icon, {@code false} otherwise.
7164 * @return this object for method chaining
7165 */
7166 public WearableExtender setHintHideIcon(boolean hintHideIcon) {
7167 setFlag(FLAG_HINT_HIDE_ICON, hintHideIcon);
7168 return this;
7169 }
7170
7171 /**
7172 * Get a hint that this notification's icon should not be displayed.
7173 * @return {@code true} if this icon should not be displayed, false otherwise.
7174 * The default value is {@code false} if this was never set.
7175 */
7176 public boolean getHintHideIcon() {
7177 return (mFlags & FLAG_HINT_HIDE_ICON) != 0;
7178 }
7179
7180 /**
7181 * Set a visual hint that only the background image of this notification should be
7182 * displayed, and other semantic content should be hidden. This hint is only applicable
7183 * to sub-pages added using {@link #addPage}.
7184 */
7185 public WearableExtender setHintShowBackgroundOnly(boolean hintShowBackgroundOnly) {
7186 setFlag(FLAG_HINT_SHOW_BACKGROUND_ONLY, hintShowBackgroundOnly);
7187 return this;
7188 }
7189
7190 /**
7191 * Get a visual hint that only the background image of this notification should be
7192 * displayed, and other semantic content should be hidden. This hint is only applicable
7193 * to sub-pages added using {@link android.app.Notification.WearableExtender#addPage}.
7194 */
7195 public boolean getHintShowBackgroundOnly() {
7196 return (mFlags & FLAG_HINT_SHOW_BACKGROUND_ONLY) != 0;
7197 }
7198
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007199 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007200 * Set a hint that this notification's background should not be clipped if possible,
7201 * and should instead be resized to fully display on the screen, retaining the aspect
7202 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007203 * @param hintAvoidBackgroundClipping {@code true} to avoid clipping if possible.
7204 * @return this object for method chaining
7205 */
7206 public WearableExtender setHintAvoidBackgroundClipping(
7207 boolean hintAvoidBackgroundClipping) {
7208 setFlag(FLAG_HINT_AVOID_BACKGROUND_CLIPPING, hintAvoidBackgroundClipping);
7209 return this;
7210 }
7211
7212 /**
Griff Hazen9c5be4e2014-11-17 17:47:50 -08007213 * Get a hint that this notification's background should not be clipped if possible,
7214 * and should instead be resized to fully display on the screen, retaining the aspect
7215 * ratio of the image. This can be useful for images like barcodes or qr codes.
Griff Hazen5f2edfc2014-09-29 16:28:44 -07007216 * @return {@code true} if it's ok if the background is clipped on the screen, false
7217 * otherwise. The default value is {@code false} if this was never set.
7218 */
7219 public boolean getHintAvoidBackgroundClipping() {
7220 return (mFlags & FLAG_HINT_AVOID_BACKGROUND_CLIPPING) != 0;
7221 }
7222
7223 /**
7224 * Set a hint that the screen should remain on for at least this duration when
7225 * this notification is displayed on the screen.
7226 * @param timeout The requested screen timeout in milliseconds. Can also be either
7227 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7228 * @return this object for method chaining
7229 */
7230 public WearableExtender setHintScreenTimeout(int timeout) {
7231 mHintScreenTimeout = timeout;
7232 return this;
7233 }
7234
7235 /**
7236 * Get the duration, in milliseconds, that the screen should remain on for
7237 * when this notification is displayed.
7238 * @return the duration in milliseconds if > 0, or either one of the sentinel values
7239 * {@link #SCREEN_TIMEOUT_SHORT} or {@link #SCREEN_TIMEOUT_LONG}.
7240 */
7241 public int getHintScreenTimeout() {
7242 return mHintScreenTimeout;
7243 }
7244
Alex Hills9ab3a232016-04-05 14:54:56 -04007245 /**
Alex Hills4bcb06b2016-04-05 14:26:25 -04007246 * Set a hint that this notification's {@link BigPictureStyle} (if present) should be
7247 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7248 * qr codes, as well as other simple black-and-white tickets.
7249 * @param hintAmbientBigPicture {@code true} to enable converstion and ambient.
7250 * @return this object for method chaining
7251 */
7252 public WearableExtender setHintAmbientBigPicture(boolean hintAmbientBigPicture) {
7253 setFlag(FLAG_BIG_PICTURE_AMBIENT, hintAmbientBigPicture);
7254 return this;
7255 }
7256
7257 /**
7258 * Get a hint that this notification's {@link BigPictureStyle} (if present) should be
7259 * converted to low-bit and displayed in ambient mode, especially useful for barcodes and
7260 * qr codes, as well as other simple black-and-white tickets.
7261 * @return {@code true} if it should be displayed in ambient, false otherwise
7262 * otherwise. The default value is {@code false} if this was never set.
7263 */
7264 public boolean getHintAmbientBigPicture() {
7265 return (mFlags & FLAG_BIG_PICTURE_AMBIENT) != 0;
7266 }
7267
7268 /**
Alex Hills9ab3a232016-04-05 14:54:56 -04007269 * Set a hint that this notification's content intent will launch an {@link Activity}
7270 * directly, telling the platform that it can generate the appropriate transitions.
7271 * @param hintContentIntentLaunchesActivity {@code true} if the content intent will launch
7272 * an activity and transitions should be generated, false otherwise.
7273 * @return this object for method chaining
7274 */
7275 public WearableExtender setHintContentIntentLaunchesActivity(
7276 boolean hintContentIntentLaunchesActivity) {
7277 setFlag(FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY, hintContentIntentLaunchesActivity);
7278 return this;
7279 }
7280
7281 /**
7282 * Get a hint that this notification's content intent will launch an {@link Activity}
7283 * directly, telling the platform that it can generate the appropriate transitions
7284 * @return {@code true} if the content intent will launch an activity and transitions should
7285 * be generated, false otherwise. The default value is {@code false} if this was never set.
7286 */
7287 public boolean getHintContentIntentLaunchesActivity() {
7288 return (mFlags & FLAG_HINT_CONTENT_INTENT_LAUNCHES_ACTIVITY) != 0;
7289 }
7290
Nadia Benbernou948627e2016-04-14 14:41:08 -04007291 /**
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007292 * Sets the dismissal id for this notification. If a notification is posted with a
7293 * dismissal id, then when that notification is canceled, notifications on other wearables
7294 * and the paired Android phone having that same dismissal id will also be canceled. See
Nadia Benbernou948627e2016-04-14 14:41:08 -04007295 * <a href="{@docRoot}wear/notifications/index.html">Adding Wearable Features to
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007296 * Notifications</a> for more information.
Nadia Benbernou948627e2016-04-14 14:41:08 -04007297 * @param dismissalId the dismissal id of the notification.
7298 * @return this object for method chaining
7299 */
7300 public WearableExtender setDismissalId(String dismissalId) {
7301 mDismissalId = dismissalId;
7302 return this;
7303 }
7304
7305 /**
7306 * Returns the dismissal id of the notification.
7307 * @return the dismissal id of the notification or null if it has not been set.
7308 */
7309 public String getDismissalId() {
7310 return mDismissalId;
7311 }
7312
Ariel Gertzenstein95ab5222016-09-02 17:00:16 -04007313 /**
7314 * Sets a bridge tag for this notification. A bridge tag can be set for notifications
7315 * posted from a phone to provide finer-grained control on what notifications are bridged
7316 * to wearables. See <a href="{@docRoot}wear/notifications/index.html">Adding Wearable
7317 * Features to Notifications</a> for more information.
7318 * @param bridgeTag the bridge tag of the notification.
7319 * @return this object for method chaining
7320 */
7321 public WearableExtender setBridgeTag(String bridgeTag) {
7322 mBridgeTag = bridgeTag;
7323 return this;
7324 }
7325
7326 /**
7327 * Returns the bridge tag of the notification.
7328 * @return the bridge tag or null if not present.
7329 */
7330 public String getBridgeTag() {
7331 return mBridgeTag;
7332 }
7333
Griff Hazen61a9e862014-05-22 16:05:19 -07007334 private void setFlag(int mask, boolean value) {
7335 if (value) {
7336 mFlags |= mask;
7337 } else {
7338 mFlags &= ~mask;
7339 }
7340 }
7341 }
7342
7343 /**
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007344 * <p>Helper class to add Android Auto extensions to notifications. To create a notification
7345 * with car extensions:
7346 *
7347 * <ol>
7348 * <li>Create an {@link Notification.Builder}, setting any desired
7349 * properties.
7350 * <li>Create a {@link CarExtender}.
7351 * <li>Set car-specific properties using the {@code add} and {@code set} methods of
7352 * {@link CarExtender}.
7353 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
7354 * to apply the extensions to a notification.
7355 * </ol>
7356 *
7357 * <pre class="prettyprint">
7358 * Notification notification = new Notification.Builder(context)
7359 * ...
7360 * .extend(new CarExtender()
7361 * .set*(...))
7362 * .build();
7363 * </pre>
7364 *
7365 * <p>Car extensions can be accessed on an existing notification by using the
7366 * {@code CarExtender(Notification)} constructor, and then using the {@code get} methods
7367 * to access values.
7368 */
7369 public static final class CarExtender implements Extender {
7370 private static final String TAG = "CarExtender";
7371
7372 private static final String EXTRA_CAR_EXTENDER = "android.car.EXTENSIONS";
7373 private static final String EXTRA_LARGE_ICON = "large_icon";
7374 private static final String EXTRA_CONVERSATION = "car_conversation";
7375 private static final String EXTRA_COLOR = "app_color";
7376
7377 private Bitmap mLargeIcon;
7378 private UnreadConversation mUnreadConversation;
7379 private int mColor = Notification.COLOR_DEFAULT;
7380
7381 /**
7382 * Create a {@link CarExtender} with default options.
7383 */
7384 public CarExtender() {
7385 }
7386
7387 /**
7388 * Create a {@link CarExtender} from the CarExtender options of an existing Notification.
7389 *
7390 * @param notif The notification from which to copy options.
7391 */
7392 public CarExtender(Notification notif) {
7393 Bundle carBundle = notif.extras == null ?
7394 null : notif.extras.getBundle(EXTRA_CAR_EXTENDER);
7395 if (carBundle != null) {
7396 mLargeIcon = carBundle.getParcelable(EXTRA_LARGE_ICON);
7397 mColor = carBundle.getInt(EXTRA_COLOR, Notification.COLOR_DEFAULT);
7398
7399 Bundle b = carBundle.getBundle(EXTRA_CONVERSATION);
7400 mUnreadConversation = UnreadConversation.getUnreadConversationFromBundle(b);
7401 }
7402 }
7403
7404 /**
7405 * Apply car extensions to a notification that is being built. This is typically called by
7406 * the {@link Notification.Builder#extend(Notification.Extender)}
7407 * method of {@link Notification.Builder}.
7408 */
7409 @Override
7410 public Notification.Builder extend(Notification.Builder builder) {
7411 Bundle carExtensions = new Bundle();
7412
7413 if (mLargeIcon != null) {
7414 carExtensions.putParcelable(EXTRA_LARGE_ICON, mLargeIcon);
7415 }
7416 if (mColor != Notification.COLOR_DEFAULT) {
7417 carExtensions.putInt(EXTRA_COLOR, mColor);
7418 }
7419
7420 if (mUnreadConversation != null) {
7421 Bundle b = mUnreadConversation.getBundleForUnreadConversation();
7422 carExtensions.putBundle(EXTRA_CONVERSATION, b);
7423 }
7424
7425 builder.getExtras().putBundle(EXTRA_CAR_EXTENDER, carExtensions);
7426 return builder;
7427 }
7428
7429 /**
7430 * Sets the accent color to use when Android Auto presents the notification.
7431 *
7432 * Android Auto uses the color set with {@link Notification.Builder#setColor(int)}
7433 * to accent the displayed notification. However, not all colors are acceptable in an
7434 * automotive setting. This method can be used to override the color provided in the
7435 * notification in such a situation.
7436 */
Tor Norbye80756e32015-03-02 09:39:27 -08007437 public CarExtender setColor(@ColorInt int color) {
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007438 mColor = color;
7439 return this;
7440 }
7441
7442 /**
7443 * Gets the accent color.
7444 *
Julia Reynoldsd9228f12015-10-20 10:37:27 -04007445 * @see #setColor
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007446 */
Tor Norbye80756e32015-03-02 09:39:27 -08007447 @ColorInt
Zhen Yu Song9d5528b2014-11-14 15:34:39 -08007448 public int getColor() {
7449 return mColor;
7450 }
7451
7452 /**
7453 * Sets the large icon of the car notification.
7454 *
7455 * If no large icon is set in the extender, Android Auto will display the icon
7456 * specified by {@link Notification.Builder#setLargeIcon(android.graphics.Bitmap)}
7457 *
7458 * @param largeIcon The large icon to use in the car notification.
7459 * @return This object for method chaining.
7460 */
7461 public CarExtender setLargeIcon(Bitmap largeIcon) {
7462 mLargeIcon = largeIcon;
7463 return this;
7464 }
7465
7466 /**
7467 * Gets the large icon used in this car notification, or null if no icon has been set.
7468 *
7469 * @return The large icon for the car notification.
7470 * @see CarExtender#setLargeIcon
7471 */
7472 public Bitmap getLargeIcon() {
7473 return mLargeIcon;
7474 }
7475
7476 /**
7477 * Sets the unread conversation in a message notification.
7478 *
7479 * @param unreadConversation The unread part of the conversation this notification conveys.
7480 * @return This object for method chaining.
7481 */
7482 public CarExtender setUnreadConversation(UnreadConversation unreadConversation) {
7483 mUnreadConversation = unreadConversation;
7484 return this;
7485 }
7486
7487 /**
7488 * Returns the unread conversation conveyed by this notification.
7489 * @see #setUnreadConversation(UnreadConversation)
7490 */
7491 public UnreadConversation getUnreadConversation() {
7492 return mUnreadConversation;
7493 }
7494
7495 /**
7496 * A class which holds the unread messages from a conversation.
7497 */
7498 public static class UnreadConversation {
7499 private static final String KEY_AUTHOR = "author";
7500 private static final String KEY_TEXT = "text";
7501 private static final String KEY_MESSAGES = "messages";
7502 private static final String KEY_REMOTE_INPUT = "remote_input";
7503 private static final String KEY_ON_REPLY = "on_reply";
7504 private static final String KEY_ON_READ = "on_read";
7505 private static final String KEY_PARTICIPANTS = "participants";
7506 private static final String KEY_TIMESTAMP = "timestamp";
7507
7508 private final String[] mMessages;
7509 private final RemoteInput mRemoteInput;
7510 private final PendingIntent mReplyPendingIntent;
7511 private final PendingIntent mReadPendingIntent;
7512 private final String[] mParticipants;
7513 private final long mLatestTimestamp;
7514
7515 UnreadConversation(String[] messages, RemoteInput remoteInput,
7516 PendingIntent replyPendingIntent, PendingIntent readPendingIntent,
7517 String[] participants, long latestTimestamp) {
7518 mMessages = messages;
7519 mRemoteInput = remoteInput;
7520 mReadPendingIntent = readPendingIntent;
7521 mReplyPendingIntent = replyPendingIntent;
7522 mParticipants = participants;
7523 mLatestTimestamp = latestTimestamp;
7524 }
7525
7526 /**
7527 * Gets the list of messages conveyed by this notification.
7528 */
7529 public String[] getMessages() {
7530 return mMessages;
7531 }
7532
7533 /**
7534 * Gets the remote input that will be used to convey the response to a message list, or
7535 * null if no such remote input exists.
7536 */
7537 public RemoteInput getRemoteInput() {
7538 return mRemoteInput;
7539 }
7540
7541 /**
7542 * Gets the pending intent that will be triggered when the user replies to this
7543 * notification.
7544 */
7545 public PendingIntent getReplyPendingIntent() {
7546 return mReplyPendingIntent;
7547 }
7548
7549 /**
7550 * Gets the pending intent that Android Auto will send after it reads aloud all messages
7551 * in this object's message list.
7552 */
7553 public PendingIntent getReadPendingIntent() {
7554 return mReadPendingIntent;
7555 }
7556
7557 /**
7558 * Gets the participants in the conversation.
7559 */
7560 public String[] getParticipants() {
7561 return mParticipants;
7562 }
7563
7564 /**
7565 * Gets the firs participant in the conversation.
7566 */
7567 public String getParticipant() {
7568 return mParticipants.length > 0 ? mParticipants[0] : null;
7569 }
7570
7571 /**
7572 * Gets the timestamp of the conversation.
7573 */
7574 public long getLatestTimestamp() {
7575 return mLatestTimestamp;
7576 }
7577
7578 Bundle getBundleForUnreadConversation() {
7579 Bundle b = new Bundle();
7580 String author = null;
7581 if (mParticipants != null && mParticipants.length > 1) {
7582 author = mParticipants[0];
7583 }
7584 Parcelable[] messages = new Parcelable[mMessages.length];
7585 for (int i = 0; i < messages.length; i++) {
7586 Bundle m = new Bundle();
7587 m.putString(KEY_TEXT, mMessages[i]);
7588 m.putString(KEY_AUTHOR, author);
7589 messages[i] = m;
7590 }
7591 b.putParcelableArray(KEY_MESSAGES, messages);
7592 if (mRemoteInput != null) {
7593 b.putParcelable(KEY_REMOTE_INPUT, mRemoteInput);
7594 }
7595 b.putParcelable(KEY_ON_REPLY, mReplyPendingIntent);
7596 b.putParcelable(KEY_ON_READ, mReadPendingIntent);
7597 b.putStringArray(KEY_PARTICIPANTS, mParticipants);
7598 b.putLong(KEY_TIMESTAMP, mLatestTimestamp);
7599 return b;
7600 }
7601
7602 static UnreadConversation getUnreadConversationFromBundle(Bundle b) {
7603 if (b == null) {
7604 return null;
7605 }
7606 Parcelable[] parcelableMessages = b.getParcelableArray(KEY_MESSAGES);
7607 String[] messages = null;
7608 if (parcelableMessages != null) {
7609 String[] tmp = new String[parcelableMessages.length];
7610 boolean success = true;
7611 for (int i = 0; i < tmp.length; i++) {
7612 if (!(parcelableMessages[i] instanceof Bundle)) {
7613 success = false;
7614 break;
7615 }
7616 tmp[i] = ((Bundle) parcelableMessages[i]).getString(KEY_TEXT);
7617 if (tmp[i] == null) {
7618 success = false;
7619 break;
7620 }
7621 }
7622 if (success) {
7623 messages = tmp;
7624 } else {
7625 return null;
7626 }
7627 }
7628
7629 PendingIntent onRead = b.getParcelable(KEY_ON_READ);
7630 PendingIntent onReply = b.getParcelable(KEY_ON_REPLY);
7631
7632 RemoteInput remoteInput = b.getParcelable(KEY_REMOTE_INPUT);
7633
7634 String[] participants = b.getStringArray(KEY_PARTICIPANTS);
7635 if (participants == null || participants.length != 1) {
7636 return null;
7637 }
7638
7639 return new UnreadConversation(messages,
7640 remoteInput,
7641 onReply,
7642 onRead,
7643 participants, b.getLong(KEY_TIMESTAMP));
7644 }
7645 };
7646
7647 /**
7648 * Builder class for {@link CarExtender.UnreadConversation} objects.
7649 */
7650 public static class Builder {
7651 private final List<String> mMessages = new ArrayList<String>();
7652 private final String mParticipant;
7653 private RemoteInput mRemoteInput;
7654 private PendingIntent mReadPendingIntent;
7655 private PendingIntent mReplyPendingIntent;
7656 private long mLatestTimestamp;
7657
7658 /**
7659 * Constructs a new builder for {@link CarExtender.UnreadConversation}.
7660 *
7661 * @param name The name of the other participant in the conversation.
7662 */
7663 public Builder(String name) {
7664 mParticipant = name;
7665 }
7666
7667 /**
7668 * Appends a new unread message to the list of messages for this conversation.
7669 *
7670 * The messages should be added from oldest to newest.
7671 *
7672 * @param message The text of the new unread message.
7673 * @return This object for method chaining.
7674 */
7675 public Builder addMessage(String message) {
7676 mMessages.add(message);
7677 return this;
7678 }
7679
7680 /**
7681 * Sets the pending intent and remote input which will convey the reply to this
7682 * notification.
7683 *
7684 * @param pendingIntent The pending intent which will be triggered on a reply.
7685 * @param remoteInput The remote input parcelable which will carry the reply.
7686 * @return This object for method chaining.
7687 *
7688 * @see CarExtender.UnreadConversation#getRemoteInput
7689 * @see CarExtender.UnreadConversation#getReplyPendingIntent
7690 */
7691 public Builder setReplyAction(
7692 PendingIntent pendingIntent, RemoteInput remoteInput) {
7693 mRemoteInput = remoteInput;
7694 mReplyPendingIntent = pendingIntent;
7695
7696 return this;
7697 }
7698
7699 /**
7700 * Sets the pending intent that will be sent once the messages in this notification
7701 * are read.
7702 *
7703 * @param pendingIntent The pending intent to use.
7704 * @return This object for method chaining.
7705 */
7706 public Builder setReadPendingIntent(PendingIntent pendingIntent) {
7707 mReadPendingIntent = pendingIntent;
7708 return this;
7709 }
7710
7711 /**
7712 * Sets the timestamp of the most recent message in an unread conversation.
7713 *
7714 * If a messaging notification has been posted by your application and has not
7715 * yet been cancelled, posting a later notification with the same id and tag
7716 * but without a newer timestamp may result in Android Auto not displaying a
7717 * heads up notification for the later notification.
7718 *
7719 * @param timestamp The timestamp of the most recent message in the conversation.
7720 * @return This object for method chaining.
7721 */
7722 public Builder setLatestTimestamp(long timestamp) {
7723 mLatestTimestamp = timestamp;
7724 return this;
7725 }
7726
7727 /**
7728 * Builds a new unread conversation object.
7729 *
7730 * @return The new unread conversation object.
7731 */
7732 public UnreadConversation build() {
7733 String[] messages = mMessages.toArray(new String[mMessages.size()]);
7734 String[] participants = { mParticipant };
7735 return new UnreadConversation(messages, mRemoteInput, mReplyPendingIntent,
7736 mReadPendingIntent, participants, mLatestTimestamp);
7737 }
7738 }
7739 }
7740
7741 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007742 * <p>Helper class to add Android TV extensions to notifications. To create a notification
7743 * with a TV extension:
7744 *
7745 * <ol>
7746 * <li>Create an {@link Notification.Builder}, setting any desired properties.
7747 * <li>Create a {@link TvExtender}.
7748 * <li>Set TV-specific properties using the {@code set} methods of
7749 * {@link TvExtender}.
7750 * <li>Call {@link Notification.Builder#extend(Notification.Extender)}
7751 * to apply the extension to a notification.
7752 * </ol>
7753 *
7754 * <pre class="prettyprint">
7755 * Notification notification = new Notification.Builder(context)
7756 * ...
7757 * .extend(new TvExtender()
7758 * .set*(...))
7759 * .build();
7760 * </pre>
7761 *
7762 * <p>TV extensions can be accessed on an existing notification by using the
7763 * {@code TvExtender(Notification)} constructor, and then using the {@code get} methods
7764 * to access values.
7765 *
7766 * @hide
7767 */
7768 @SystemApi
7769 public static final class TvExtender implements Extender {
7770 private static final String TAG = "TvExtender";
7771
7772 private static final String EXTRA_TV_EXTENDER = "android.tv.EXTENSIONS";
7773 private static final String EXTRA_FLAGS = "flags";
7774 private static final String EXTRA_CONTENT_INTENT = "content_intent";
7775 private static final String EXTRA_DELETE_INTENT = "delete_intent";
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007776 private static final String EXTRA_CHANNEL_ID = "channel_id";
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007777
7778 // Flags bitwise-ored to mFlags
7779 private static final int FLAG_AVAILABLE_ON_TV = 0x1;
7780
7781 private int mFlags;
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007782 private String mChannelId;
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007783 private PendingIntent mContentIntent;
7784 private PendingIntent mDeleteIntent;
7785
7786 /**
7787 * Create a {@link TvExtender} with default options.
7788 */
7789 public TvExtender() {
7790 mFlags = FLAG_AVAILABLE_ON_TV;
7791 }
7792
7793 /**
7794 * Create a {@link TvExtender} from the TvExtender options of an existing Notification.
7795 *
7796 * @param notif The notification from which to copy options.
7797 */
7798 public TvExtender(Notification notif) {
7799 Bundle bundle = notif.extras == null ?
7800 null : notif.extras.getBundle(EXTRA_TV_EXTENDER);
7801 if (bundle != null) {
7802 mFlags = bundle.getInt(EXTRA_FLAGS);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007803 mChannelId = bundle.getString(EXTRA_CHANNEL_ID);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007804 mContentIntent = bundle.getParcelable(EXTRA_CONTENT_INTENT);
7805 mDeleteIntent = bundle.getParcelable(EXTRA_DELETE_INTENT);
7806 }
7807 }
7808
7809 /**
7810 * Apply a TV extension to a notification that is being built. This is typically called by
7811 * the {@link Notification.Builder#extend(Notification.Extender)}
7812 * method of {@link Notification.Builder}.
7813 */
7814 @Override
7815 public Notification.Builder extend(Notification.Builder builder) {
7816 Bundle bundle = new Bundle();
7817
7818 bundle.putInt(EXTRA_FLAGS, mFlags);
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007819 bundle.putString(EXTRA_CHANNEL_ID, mChannelId);
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007820 if (mContentIntent != null) {
7821 bundle.putParcelable(EXTRA_CONTENT_INTENT, mContentIntent);
7822 }
7823
7824 if (mDeleteIntent != null) {
7825 bundle.putParcelable(EXTRA_DELETE_INTENT, mDeleteIntent);
7826 }
7827
7828 builder.getExtras().putBundle(EXTRA_TV_EXTENDER, bundle);
7829 return builder;
7830 }
7831
7832 /**
7833 * Returns true if this notification should be shown on TV. This method return true
7834 * if the notification was extended with a TvExtender.
7835 */
7836 public boolean isAvailableOnTv() {
7837 return (mFlags & FLAG_AVAILABLE_ON_TV) != 0;
7838 }
7839
7840 /**
Dmitri Plotnikovb8a04ee2017-01-20 10:39:22 -08007841 * Specifies the channel the notification should be delivered on when shown on TV.
7842 * It can be different from the channel that the notification is delivered to when
7843 * posting on a non-TV device.
7844 */
7845 public TvExtender setChannel(String channelId) {
7846 mChannelId = channelId;
7847 return this;
7848 }
7849
7850 /**
7851 * Returns the id of the channel this notification posts to on TV.
7852 */
7853 public String getChannel() {
7854 return mChannelId;
7855 }
7856
7857 /**
Dmitri Plotnikov9e9cfd92016-12-21 10:52:26 -08007858 * Supplies a {@link PendingIntent} to be sent when the notification is selected on TV.
7859 * If provided, it is used instead of the content intent specified
7860 * at the level of Notification.
7861 */
7862 public TvExtender setContentIntent(PendingIntent intent) {
7863 mContentIntent = intent;
7864 return this;
7865 }
7866
7867 /**
7868 * Returns the TV-specific content intent. If this method returns null, the
7869 * main content intent on the notification should be used.
7870 *
7871 * @see {@link Notification#contentIntent}
7872 */
7873 public PendingIntent getContentIntent() {
7874 return mContentIntent;
7875 }
7876
7877 /**
7878 * Supplies a {@link PendingIntent} to send when the notification is cleared explicitly
7879 * by the user on TV. If provided, it is used instead of the delete intent specified
7880 * at the level of Notification.
7881 */
7882 public TvExtender setDeleteIntent(PendingIntent intent) {
7883 mDeleteIntent = intent;
7884 return this;
7885 }
7886
7887 /**
7888 * Returns the TV-specific delete intent. If this method returns null, the
7889 * main delete intent on the notification should be used.
7890 *
7891 * @see {@link Notification#deleteIntent}
7892 */
7893 public PendingIntent getDeleteIntent() {
7894 return mDeleteIntent;
7895 }
7896 }
7897
7898 /**
Griff Hazen61a9e862014-05-22 16:05:19 -07007899 * Get an array of Notification objects from a parcelable array bundle field.
7900 * Update the bundle to have a typed array so fetches in the future don't need
7901 * to do an array copy.
7902 */
7903 private static Notification[] getNotificationArrayFromBundle(Bundle bundle, String key) {
7904 Parcelable[] array = bundle.getParcelableArray(key);
7905 if (array instanceof Notification[] || array == null) {
7906 return (Notification[]) array;
7907 }
7908 Notification[] typedArray = Arrays.copyOf(array, array.length,
7909 Notification[].class);
7910 bundle.putParcelableArray(key, typedArray);
7911 return typedArray;
7912 }
Christoph Studer4600f9b2014-07-22 22:44:43 +02007913
7914 private static class BuilderRemoteViews extends RemoteViews {
7915 public BuilderRemoteViews(Parcel parcel) {
7916 super(parcel);
7917 }
7918
Kenny Guy77320062014-08-27 21:37:15 +01007919 public BuilderRemoteViews(ApplicationInfo appInfo, int layoutId) {
7920 super(appInfo, layoutId);
Christoph Studer4600f9b2014-07-22 22:44:43 +02007921 }
7922
7923 @Override
7924 public BuilderRemoteViews clone() {
7925 Parcel p = Parcel.obtain();
7926 writeToParcel(p, 0);
7927 p.setDataPosition(0);
7928 BuilderRemoteViews brv = new BuilderRemoteViews(p);
7929 p.recycle();
7930 return brv;
7931 }
7932 }
Adrian Roos70d7aa32017-01-11 15:39:06 -08007933
7934 private static class StandardTemplateParams {
7935 boolean hasProgress = true;
7936 boolean ambient = false;
7937 CharSequence title;
7938 CharSequence text;
7939
7940 final StandardTemplateParams reset() {
7941 hasProgress = true;
7942 ambient = false;
7943 title = null;
7944 text = null;
7945 return this;
7946 }
7947
7948 final StandardTemplateParams hasProgress(boolean hasProgress) {
7949 this.hasProgress = hasProgress;
7950 return this;
7951 }
7952
7953 final StandardTemplateParams title(CharSequence title) {
7954 this.title = title;
7955 return this;
7956 }
7957
7958 final StandardTemplateParams text(CharSequence text) {
7959 this.text = text;
7960 return this;
7961 }
7962
7963 final StandardTemplateParams ambient(boolean ambient) {
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08007964 Preconditions.checkState(title == null && text == null, "must set ambient before text");
Adrian Roos70d7aa32017-01-11 15:39:06 -08007965 this.ambient = ambient;
7966 return this;
7967 }
7968
7969 final StandardTemplateParams fillTextsFrom(Builder b) {
7970 Bundle extras = b.mN.extras;
Adrian Roos0bc3f6a2017-03-06 11:54:05 -08007971 title = b.processLegacyText(extras.getCharSequence(EXTRA_TITLE), ambient);
7972 text = b.processLegacyText(extras.getCharSequence(EXTRA_TEXT), ambient);
Adrian Roos70d7aa32017-01-11 15:39:06 -08007973 return this;
7974 }
7975 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08007976}